drone/app/api/controller/repo/default_branch.go

126 lines
3.7 KiB
Go

// Copyright 2023 Harness, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package repo
import (
"context"
"fmt"
"time"
"github.com/harness/gitness/app/api/controller"
"github.com/harness/gitness/app/auth"
"github.com/harness/gitness/app/bootstrap"
repoevents "github.com/harness/gitness/app/events/repo"
"github.com/harness/gitness/app/paths"
"github.com/harness/gitness/audit"
"github.com/harness/gitness/contextutil"
"github.com/harness/gitness/git"
"github.com/harness/gitness/types"
"github.com/harness/gitness/types/enum"
"github.com/rs/zerolog/log"
)
type UpdateDefaultBranchInput struct {
Name string `json:"name"`
}
// TODO: handle the racing condition between update/delete default branch requests for a repo.
func (c *Controller) UpdateDefaultBranch(
ctx context.Context,
session *auth.Session,
repoRef string,
in *UpdateDefaultBranchInput,
) (*Repository, error) {
repo, err := c.getRepoCheckAccess(ctx, session, repoRef, enum.PermissionRepoEdit)
if err != nil {
return nil, err
}
repoClone := repo.Clone()
// the max time we give an update default branch to succeed
const timeout = 2 * time.Minute
// lock concurrent requests for updating the default branch of a repo
// requests will wait for previous ones to compelete before proceed
unlock, err := c.locker.LockDefaultBranch(
ctx,
repo.Repository.ID,
in.Name, // branch name only used for logging (lock is on repo)
timeout+30*time.Second, // add 30s to the lock to give enough time for updating default branch
)
if err != nil {
return nil, err
}
defer unlock()
writeParams, err := controller.CreateRPCInternalWriteParams(ctx, c.urlProvider, session, &repo.Repository)
if err != nil {
return nil, fmt.Errorf("failed to create RPC write params: %w", err)
}
// create new, time-restricted context to guarantee update completion, even if request is canceled.
// TODO: a proper error handling solution required.
ctx, cancel := context.WithTimeout(
contextutil.WithNewValues(context.Background(), ctx),
timeout,
)
defer cancel()
err = c.git.UpdateDefaultBranch(ctx, &git.UpdateDefaultBranchParams{
WriteParams: writeParams,
BranchName: in.Name,
})
if err != nil {
return nil, fmt.Errorf("failed to update the repo default branch: %w", err)
}
oldName := repo.Repository.DefaultBranch
repoBase, err := c.repoStore.UpdateOptLock(ctx, &repo.Repository, func(r *types.Repository) error {
r.DefaultBranch = in.Name
return nil
})
if err != nil {
return nil, fmt.Errorf("failed to update the repo default branch on db:%w", err)
}
repo = &Repository{
Repository: *repoBase,
IsPublic: repo.IsPublic,
}
err = c.auditService.Log(ctx,
session.Principal,
audit.NewResource(audit.ResourceTypeRepository, repo.Repository.Identifier),
audit.ActionUpdated,
paths.Space(repo.Repository.Path),
audit.WithOldObject(repoClone),
audit.WithNewObject(repo),
)
if err != nil {
log.Ctx(ctx).Warn().Msgf("failed to insert audit log for update default branch operation: %s", err)
}
c.eventReporter.DefaultBranchUpdated(ctx, &repoevents.DefaultBranchUpdatedPayload{
RepoID: repoBase.ID,
PrincipalID: bootstrap.NewSystemServiceSession().Principal.ID,
OldName: oldName,
NewName: repoBase.DefaultBranch,
})
return repo, nil
}