Skip to content

Commit f7f627c

Browse files
committed
Don't delete branch if other PRs with this branch are open
fix #18149 Signed-off-by: a1012112796 <[email protected]>
1 parent f499f23 commit f7f627c

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

routers/api/v1/repo/pull.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -873,6 +873,17 @@ func MergePullRequest(ctx *context.APIContext) {
873873
log.Trace("Pull request merged: %d", pr.ID)
874874

875875
if form.DeleteBranchAfterMerge {
876+
// Don't cleanup when other pr use this branch as head branch
877+
prs, err := models.GetUnmergedPullRequestsByHeadInfo(pr.HeadRepoID, pr.HeadBranch)
878+
if err != nil {
879+
ctx.ServerError("GetUnmergedPullRequestsByHeadInfo", err)
880+
return
881+
}
882+
if len(prs) > 0 {
883+
ctx.Status(http.StatusOK)
884+
return
885+
}
886+
876887
var headRepo *git.Repository
877888
if ctx.Repo != nil && ctx.Repo.Repository != nil && ctx.Repo.Repository.ID == pr.HeadRepoID && ctx.Repo.GitRepo != nil {
878889
headRepo = ctx.Repo.GitRepo

routers/web/repo/issue.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1600,11 +1600,23 @@ func ViewIssue(ctx *context.Context) {
16001600
} else {
16011601
ctx.Data["WontSignReason"] = "not_signed_in"
16021602
}
1603-
ctx.Data["IsPullBranchDeletable"] = canDelete &&
1603+
1604+
isPullBranchDeletable := canDelete &&
16041605
pull.HeadRepo != nil &&
16051606
git.IsBranchExist(ctx, pull.HeadRepo.RepoPath(), pull.HeadBranch) &&
16061607
(!pull.HasMerged || ctx.Data["HeadBranchCommitID"] == ctx.Data["PullHeadCommitID"])
16071608

1609+
if isPullBranchDeletable && pull.HasMerged {
1610+
prs, err := models.GetUnmergedPullRequestsByHeadInfo(pull.HeadRepoID, pull.HeadBranch)
1611+
if err != nil {
1612+
ctx.ServerError("GetUnmergedPullRequestsByHeadInfo", err)
1613+
return
1614+
}
1615+
1616+
isPullBranchDeletable = len(prs) == 0
1617+
}
1618+
ctx.Data["IsPullBranchDeletable"] = isPullBranchDeletable
1619+
16081620
stillCanManualMerge := func() bool {
16091621
if pull.HasMerged || issue.IsClosed || !ctx.IsSigned {
16101622
return false

routers/web/repo/pull.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,6 +1052,17 @@ func MergePullRequest(ctx *context.Context) {
10521052
log.Trace("Pull request merged: %d", pr.ID)
10531053

10541054
if form.DeleteBranchAfterMerge {
1055+
// Don't cleanup when other pr use this branch as head branch
1056+
prs, err := models.GetUnmergedPullRequestsByHeadInfo(pr.HeadRepoID, pr.HeadBranch)
1057+
if err != nil {
1058+
ctx.ServerError("GetUnmergedPullRequestsByHeadInfo", err)
1059+
return
1060+
}
1061+
if len(prs) > 0 {
1062+
ctx.Redirect(issue.Link())
1063+
return
1064+
}
1065+
10551066
var headRepo *git.Repository
10561067
if ctx.Repo != nil && ctx.Repo.Repository != nil && pr.HeadRepoID == ctx.Repo.Repository.ID && ctx.Repo.GitRepo != nil {
10571068
headRepo = ctx.Repo.GitRepo
@@ -1261,6 +1272,17 @@ func CleanUpPullRequest(ctx *context.Context) {
12611272
return
12621273
}
12631274

1275+
// Don't cleanup when other pr use this branch as head branch
1276+
prs, err := models.GetUnmergedPullRequestsByHeadInfo(pr.HeadRepoID, pr.HeadBranch)
1277+
if err != nil {
1278+
ctx.ServerError("GetUnmergedPullRequestsByHeadInfo", err)
1279+
return
1280+
}
1281+
if len(prs) > 0 {
1282+
ctx.NotFound("CleanUpPullRequest", nil)
1283+
return
1284+
}
1285+
12641286
if err := pr.LoadHeadRepo(); err != nil {
12651287
ctx.ServerError("LoadHeadRepo", err)
12661288
return

0 commit comments

Comments
 (0)