Skip to content

Commit 637c3ec

Browse files
authored
Don't delete branch if other PRs with this branch are open (#18164)
fix #18149 Signed-off-by: a1012112796 <1012112796@qq.com>
1 parent 650a50a commit 637c3ec

File tree

5 files changed

+68
-1
lines changed

5 files changed

+68
-1
lines changed

models/pull_list.go

+10
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@ func GetUnmergedPullRequestsByHeadInfo(repoID int64, branch string) ([]*PullRequ
5959
Find(&prs)
6060
}
6161

62+
// HasUnmergedPullRequestsByHeadInfo checks if there are open and not merged pull request
63+
// by given head information (repo and branch)
64+
func HasUnmergedPullRequestsByHeadInfo(repoID int64, branch string) (bool, error) {
65+
return db.GetEngine(db.DefaultContext).
66+
Where("head_repo_id = ? AND head_branch = ? AND has_merged = ? AND issue.is_closed = ? AND flow = ?",
67+
repoID, branch, false, false, PullRequestFlowGithub).
68+
Join("INNER", "issue", "issue.id = pull_request.issue_id").
69+
Exist(&PullRequest{})
70+
}
71+
6272
// GetUnmergedPullRequestsByBaseInfo returns all pull requests that are open and has not been merged
6373
// by given base information (repo and branch).
6474
func GetUnmergedPullRequestsByBaseInfo(repoID int64, branch string) ([]*PullRequest, error) {

models/pull_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,18 @@ func TestGetUnmergedPullRequest(t *testing.T) {
107107
assert.True(t, IsErrPullRequestNotExist(err))
108108
}
109109

110+
func TestHasUnmergedPullRequestsByHeadInfo(t *testing.T) {
111+
assert.NoError(t, unittest.PrepareTestDatabase())
112+
113+
exist, err := HasUnmergedPullRequestsByHeadInfo(1, "branch2")
114+
assert.NoError(t, err)
115+
assert.Equal(t, true, exist)
116+
117+
exist, err = HasUnmergedPullRequestsByHeadInfo(1, "not_exist_branch")
118+
assert.NoError(t, err)
119+
assert.Equal(t, false, exist)
120+
}
121+
110122
func TestGetUnmergedPullRequestsByHeadInfo(t *testing.T) {
111123
assert.NoError(t, unittest.PrepareTestDatabase())
112124
prs, err := GetUnmergedPullRequestsByHeadInfo(1, "branch2")

routers/api/v1/repo/pull.go

+11
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 there are other PR's that use this branch as head branch.
877+
exist, err := models.HasUnmergedPullRequestsByHeadInfo(pr.HeadRepoID, pr.HeadBranch)
878+
if err != nil {
879+
ctx.ServerError("HasUnmergedPullRequestsByHeadInfo", err)
880+
return
881+
}
882+
if exist {
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

+13-1
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+
exist, err := models.HasUnmergedPullRequestsByHeadInfo(pull.HeadRepoID, pull.HeadBranch)
1611+
if err != nil {
1612+
ctx.ServerError("HasUnmergedPullRequestsByHeadInfo", err)
1613+
return
1614+
}
1615+
1616+
isPullBranchDeletable = !exist
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

+22
Original file line numberDiff line numberDiff line change
@@ -1051,6 +1051,17 @@ func MergePullRequest(ctx *context.Context) {
10511051
log.Trace("Pull request merged: %d", pr.ID)
10521052

10531053
if form.DeleteBranchAfterMerge {
1054+
// Don't cleanup when other pr use this branch as head branch
1055+
exist, err := models.HasUnmergedPullRequestsByHeadInfo(pr.HeadRepoID, pr.HeadBranch)
1056+
if err != nil {
1057+
ctx.ServerError("HasUnmergedPullRequestsByHeadInfo", err)
1058+
return
1059+
}
1060+
if exist {
1061+
ctx.Redirect(issue.Link())
1062+
return
1063+
}
1064+
10541065
var headRepo *git.Repository
10551066
if ctx.Repo != nil && ctx.Repo.Repository != nil && pr.HeadRepoID == ctx.Repo.Repository.ID && ctx.Repo.GitRepo != nil {
10561067
headRepo = ctx.Repo.GitRepo
@@ -1222,6 +1233,17 @@ func CleanUpPullRequest(ctx *context.Context) {
12221233
return
12231234
}
12241235

1236+
// Don't cleanup when there are other PR's that use this branch as head branch.
1237+
exist, err := models.HasUnmergedPullRequestsByHeadInfo(pr.HeadRepoID, pr.HeadBranch)
1238+
if err != nil {
1239+
ctx.ServerError("HasUnmergedPullRequestsByHeadInfo", err)
1240+
return
1241+
}
1242+
if exist {
1243+
ctx.NotFound("CleanUpPullRequest", nil)
1244+
return
1245+
}
1246+
12251247
if err := pr.LoadHeadRepo(); err != nil {
12261248
ctx.ServerError("LoadHeadRepo", err)
12271249
return

0 commit comments

Comments
 (0)