Skip to content

Commit e70e375

Browse files
committed
Move all checks into DeleteBranch and PushUpdate
1 parent 7562393 commit e70e375

File tree

8 files changed

+26
-76
lines changed

8 files changed

+26
-76
lines changed

routers/api/v1/repo/branch.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,6 @@ func DeleteBranch(ctx *context.APIContext) {
133133

134134
branchName := ctx.PathParam("*")
135135

136-
if ctx.Repo.Repository.IsEmpty {
137-
ctx.Error(http.StatusForbidden, "", "Git Repository is empty.")
138-
return
139-
}
140-
141136
// check whether branches of this repository has been synced
142137
totalNumOfBranches, err := db.Count[git_model.Branch](ctx, git_model.FindBranchOptions{
143138
RepoID: ctx.Repo.Repository.ID,
@@ -155,12 +150,7 @@ func DeleteBranch(ctx *context.APIContext) {
155150
}
156151
}
157152

158-
if ctx.Repo.Repository.IsMirror {
159-
ctx.Error(http.StatusForbidden, "IsMirrored", fmt.Errorf("can not delete branch of an mirror repository"))
160-
return
161-
}
162-
163-
if err := repo_service.DeleteBranch(ctx, ctx.Doer, ctx.Repo.Repository, ctx.Repo.GitRepo, branchName); err != nil {
153+
if err := repo_service.DeleteBranch(ctx, ctx.Doer, ctx.Repo.Repository, ctx.Repo.GitRepo, branchName, nil); err != nil {
164154
switch {
165155
case git.IsErrBranchNotExist(err):
166156
ctx.NotFound(err)

routers/api/v1/repo/pull.go

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,17 +1046,6 @@ func MergePullRequest(ctx *context.APIContext) {
10461046
log.Trace("Pull request merged: %d", pr.ID)
10471047

10481048
if form.DeleteBranchAfterMerge {
1049-
// Don't cleanup when there are other PR's that use this branch as head branch.
1050-
exist, err := issues_model.HasUnmergedPullRequestsByHeadInfo(ctx, pr.HeadRepoID, pr.HeadBranch)
1051-
if err != nil {
1052-
ctx.ServerError("HasUnmergedPullRequestsByHeadInfo", err)
1053-
return
1054-
}
1055-
if exist {
1056-
ctx.Status(http.StatusOK)
1057-
return
1058-
}
1059-
10601049
var headRepo *git.Repository
10611050
if ctx.Repo != nil && ctx.Repo.Repository != nil && ctx.Repo.Repository.ID == pr.HeadRepoID && ctx.Repo.GitRepo != nil {
10621051
headRepo = ctx.Repo.GitRepo
@@ -1068,11 +1057,7 @@ func MergePullRequest(ctx *context.APIContext) {
10681057
}
10691058
defer headRepo.Close()
10701059
}
1071-
if err := pull_service.RetargetChildrenOnMerge(ctx, ctx.Doer, pr); err != nil {
1072-
ctx.Error(http.StatusInternalServerError, "RetargetChildrenOnMerge", err)
1073-
return
1074-
}
1075-
if err := repo_service.DeleteBranch(ctx, ctx.Doer, pr.HeadRepo, headRepo, pr.HeadBranch); err != nil {
1060+
if err := repo_service.DeleteBranch(ctx, ctx.Doer, pr.HeadRepo, headRepo, pr.HeadBranch, pr); err != nil {
10761061
switch {
10771062
case git.IsErrBranchNotExist(err):
10781063
ctx.NotFound(err)
@@ -1085,10 +1070,6 @@ func MergePullRequest(ctx *context.APIContext) {
10851070
}
10861071
return
10871072
}
1088-
if err := issues_model.AddDeletePRBranchComment(ctx, ctx.Doer, pr.BaseRepo, pr.Issue.ID, pr.HeadBranch); err != nil {
1089-
// Do not fail here as branch has already been deleted
1090-
log.Error("DeleteBranch: %v", err)
1091-
}
10921073
}
10931074

10941075
ctx.Status(http.StatusOK)

routers/web/repo/branch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func DeleteBranchPost(ctx *context.Context) {
9898
defer redirect(ctx)
9999
branchName := ctx.FormString("name")
100100

101-
if err := repo_service.DeleteBranch(ctx, ctx.Doer, ctx.Repo.Repository, ctx.Repo.GitRepo, branchName); err != nil {
101+
if err := repo_service.DeleteBranch(ctx, ctx.Doer, ctx.Repo.Repository, ctx.Repo.GitRepo, branchName, nil); err != nil {
102102
switch {
103103
case git.IsErrBranchNotExist(err):
104104
log.Debug("DeleteBranch: Can't delete non existing branch '%s'", branchName)

routers/web/repo/pull.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1493,12 +1493,7 @@ func CleanUpPullRequest(ctx *context.Context) {
14931493
func deleteBranch(ctx *context.Context, pr *issues_model.PullRequest, gitRepo *git.Repository) {
14941494
fullBranchName := pr.HeadRepo.FullName() + ":" + pr.HeadBranch
14951495

1496-
if err := pull_service.RetargetChildrenOnMerge(ctx, ctx.Doer, pr); err != nil {
1497-
ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName))
1498-
return
1499-
}
1500-
1501-
if err := repo_service.DeleteBranch(ctx, ctx.Doer, pr.HeadRepo, gitRepo, pr.HeadBranch); err != nil {
1496+
if err := repo_service.DeleteBranch(ctx, ctx.Doer, pr.HeadRepo, gitRepo, pr.HeadBranch, pr); err != nil {
15021497
switch {
15031498
case git.IsErrBranchNotExist(err):
15041499
ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName))
@@ -1513,11 +1508,6 @@ func deleteBranch(ctx *context.Context, pr *issues_model.PullRequest, gitRepo *g
15131508
return
15141509
}
15151510

1516-
if err := issues_model.AddDeletePRBranchComment(ctx, ctx.Doer, pr.BaseRepo, pr.IssueID, pr.HeadBranch); err != nil {
1517-
// Do not fail here as branch has already been deleted
1518-
log.Error("DeleteBranch: %v", err)
1519-
}
1520-
15211511
ctx.Flash.Success(ctx.Tr("repo.branch.deletion_success", fullBranchName))
15221512
}
15231513

services/automerge/automerge.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ func handlePullRequestAutoMerge(pullID int64, sha string) {
306306
}
307307

308308
if pr.Flow == issues_model.PullRequestFlowGithub && scheduledPRM.DeleteBranchAfterMerge {
309-
if err := repo_service.DeletePullRequestHeadBranch(ctx, pr, doer, headGitRepo); err != nil {
309+
if err := repo_service.DeleteBranch(ctx, doer, pr.HeadRepo, headGitRepo, pr.HeadBranch, pr); err != nil {
310310
log.Error("DeletePullRequestHeadBranch: %v", err)
311311
}
312312
}

services/pull/pull.go

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -606,17 +606,9 @@ func (errs errlist) Error() string {
606606
return ""
607607
}
608608

609-
// RetargetChildrenOnMerge retarget children pull requests on merge if possible
610-
func RetargetChildrenOnMerge(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest) error {
611-
if setting.Repository.PullRequest.RetargetChildrenOnMerge && pr.BaseRepoID == pr.HeadRepoID {
612-
return RetargetBranchPulls(ctx, doer, pr.HeadRepoID, pr.HeadBranch, pr.BaseBranch)
613-
}
614-
return nil
615-
}
616-
617609
// RetargetBranchPulls change target branch for all pull requests whose base branch is the branch
618610
// Both branch and targetBranch must be in the same repo (for security reasons)
619-
func RetargetBranchPulls(ctx context.Context, doer *user_model.User, repoID int64, branch, targetBranch string) error {
611+
func RetargetBranchPulls(ctx context.Context, doer *user_model.User, repoID int64, branch string, targetBranch string) error {
620612
prs, err := issues_model.GetUnmergedPullRequestsByBaseInfo(ctx, repoID, branch)
621613
if err != nil {
622614
return err
@@ -650,12 +642,14 @@ func CloseBranchPulls(ctx context.Context, doer *user_model.User, repoID int64,
650642
return err
651643
}
652644

653-
prs2, err := issues_model.GetUnmergedPullRequestsByBaseInfo(ctx, repoID, branch)
654-
if err != nil {
655-
return err
645+
if !setting.Repository.PullRequest.RetargetChildrenOnMerge {
646+
prs2, err := issues_model.GetUnmergedPullRequestsByBaseInfo(ctx, repoID, branch)
647+
if err != nil {
648+
return err
649+
}
650+
prs = append(prs, prs2...)
656651
}
657652

658-
prs = append(prs, prs2...)
659653
if err := issues_model.PullRequestList(prs).LoadAttributes(ctx); err != nil {
660654
return err
661655
}

services/repository/branch.go

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import (
3131
"code.gitea.io/gitea/modules/util"
3232
webhook_module "code.gitea.io/gitea/modules/webhook"
3333
notify_service "code.gitea.io/gitea/services/notify"
34-
pull_service "code.gitea.io/gitea/services/pull"
3534
files_service "code.gitea.io/gitea/services/repository/files"
3635

3736
"xorm.io/builder"
@@ -468,7 +467,7 @@ var (
468467
)
469468

470469
// DeleteBranch delete branch
471-
func DeleteBranch(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, gitRepo *git.Repository, branchName string) error {
470+
func DeleteBranch(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, gitRepo *git.Repository, branchName string, pr *issues_model.PullRequest) error {
472471
perm, err := access_model.GetUserRepoPermission(ctx, repo, doer)
473472
if err != nil {
474473
return err
@@ -515,6 +514,12 @@ func DeleteBranch(ctx context.Context, doer *user_model.User, repo *repo_model.R
515514
}
516515
}
517516

517+
if pr != nil {
518+
if err := issues_model.AddDeletePRBranchComment(ctx, doer, pr.BaseRepo, pr.Issue.ID, pr.HeadBranch); err != nil {
519+
return fmt.Errorf("DeleteBranch: %v", err)
520+
}
521+
}
522+
518523
return gitRepo.DeleteBranch(branchName, git.DeleteBranchOptions{
519524
Force: true,
520525
})
@@ -541,23 +546,6 @@ func DeleteBranch(ctx context.Context, doer *user_model.User, repo *repo_model.R
541546
return nil
542547
}
543548

544-
func DeletePullRequestHeadBranch(ctx context.Context, pr *issues_model.PullRequest, doer *user_model.User, headGitRepo *git.Repository) error {
545-
if err := pull_service.RetargetChildrenOnMerge(ctx, doer, pr); err != nil {
546-
return err
547-
}
548-
549-
if err := DeleteBranch(ctx, doer, pr.HeadRepo, headGitRepo, pr.HeadBranch); err != nil {
550-
return err
551-
}
552-
553-
if err := issues_model.AddDeletePRBranchComment(ctx, doer, pr.BaseRepo, pr.IssueID, pr.HeadBranch); err != nil {
554-
// Do not fail here as branch has already been deleted
555-
log.Error("AddDeletePRBranchComment: %v", err)
556-
}
557-
558-
return nil
559-
}
560-
561549
type BranchSyncOptions struct {
562550
RepoID int64
563551
}

services/repository/push.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,13 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error {
275275
}
276276
} else {
277277
notify_service.DeleteRef(ctx, pusher, repo, opts.RefFullName)
278+
279+
if setting.Repository.PullRequest.RetargetChildrenOnMerge {
280+
if err := pull_service.RetargetBranchPulls(ctx, pusher, repo.ID, branch, repo.DefaultBranch); err != nil {
281+
return err
282+
}
283+
}
284+
278285
if err = pull_service.CloseBranchPulls(ctx, pusher, repo.ID, branch); err != nil {
279286
// close all related pulls
280287
log.Error("close related pull request failed: %v", err)

0 commit comments

Comments
 (0)