Merge all deleteBranch as one function and also fix bug when delete branch don't close related PRs (#16067)
* Fix bug when delete branch don't close related PRs * Merge all deletebranch as one method * Add missed branch.go * fix comment Co-authored-by: Lauris BH <lauris@nix.lv>tokarchuk/v1.17
parent
3607f79d78
commit
0909695204
@ -0,0 +1,72 @@ |
||||
// Copyright 2021 The Gitea Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package repository |
||||
|
||||
import ( |
||||
"errors" |
||||
|
||||
"code.gitea.io/gitea/models" |
||||
"code.gitea.io/gitea/modules/git" |
||||
"code.gitea.io/gitea/modules/log" |
||||
repo_module "code.gitea.io/gitea/modules/repository" |
||||
pull_service "code.gitea.io/gitea/services/pull" |
||||
) |
||||
|
||||
// enmuerates all branch related errors
|
||||
var ( |
||||
ErrBranchIsDefault = errors.New("branch is default") |
||||
ErrBranchIsProtected = errors.New("branch is protected") |
||||
) |
||||
|
||||
// DeleteBranch delete branch
|
||||
func DeleteBranch(doer *models.User, repo *models.Repository, gitRepo *git.Repository, branchName string) error { |
||||
if branchName == repo.DefaultBranch { |
||||
return ErrBranchIsDefault |
||||
} |
||||
|
||||
isProtected, err := repo.IsProtectedBranch(branchName, doer) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
if isProtected { |
||||
return ErrBranchIsProtected |
||||
} |
||||
|
||||
commit, err := gitRepo.GetBranchCommit(branchName) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
if err := gitRepo.DeleteBranch(branchName, git.DeleteBranchOptions{ |
||||
Force: true, |
||||
}); err != nil { |
||||
return err |
||||
} |
||||
|
||||
if err := pull_service.CloseBranchPulls(doer, repo.ID, branchName); err != nil { |
||||
return err |
||||
} |
||||
|
||||
// Don't return error below this
|
||||
if err := PushUpdate( |
||||
&repo_module.PushUpdateOptions{ |
||||
RefFullName: git.BranchPrefix + branchName, |
||||
OldCommitID: commit.ID.String(), |
||||
NewCommitID: git.EmptySHA, |
||||
PusherID: doer.ID, |
||||
PusherName: doer.Name, |
||||
RepoUserName: repo.OwnerName, |
||||
RepoName: repo.Name, |
||||
}); err != nil { |
||||
log.Error("Update: %v", err) |
||||
} |
||||
|
||||
if err := repo.AddDeletedBranch(branchName, commit.ID.String(), doer.ID); err != nil { |
||||
log.Warn("AddDeletedBranch: %v", err) |
||||
} |
||||
|
||||
return nil |
||||
} |
Loading…
Reference in new issue