Skip to content

Commit 92466fe

Browse files
committed
simplify
1 parent f7f627c commit 92466fe

File tree

5 files changed

+34
-12
lines changed

5 files changed

+34
-12
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+
// CheckUnmergedPullRequestsByHeadInfo check if the pull requests that are open and has not been merged
63+
// by given head information (repo and branch) exist.
64+
func CheckUnmergedPullRequestsByHeadInfo(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 TestCheckUnmergedPullRequestsByHeadInfo(t *testing.T) {
111+
assert.NoError(t, unittest.PrepareTestDatabase())
112+
113+
exist, err := CheckUnmergedPullRequestsByHeadInfo(1, "branch2")
114+
assert.NoError(t, err)
115+
assert.Equal(t, true, exist)
116+
117+
exist, err = CheckUnmergedPullRequestsByHeadInfo(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

+3-3
Original file line numberDiff line numberDiff line change
@@ -874,12 +874,12 @@ func MergePullRequest(ctx *context.APIContext) {
874874

875875
if form.DeleteBranchAfterMerge {
876876
// Don't cleanup when other pr use this branch as head branch
877-
prs, err := models.GetUnmergedPullRequestsByHeadInfo(pr.HeadRepoID, pr.HeadBranch)
877+
exist, err := models.CheckUnmergedPullRequestsByHeadInfo(pr.HeadRepoID, pr.HeadBranch)
878878
if err != nil {
879-
ctx.ServerError("GetUnmergedPullRequestsByHeadInfo", err)
879+
ctx.ServerError("CheckUnmergedPullRequestsByHeadInfo", err)
880880
return
881881
}
882-
if len(prs) > 0 {
882+
if exist {
883883
ctx.Status(http.StatusOK)
884884
return
885885
}

routers/web/repo/issue.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -1607,13 +1607,13 @@ func ViewIssue(ctx *context.Context) {
16071607
(!pull.HasMerged || ctx.Data["HeadBranchCommitID"] == ctx.Data["PullHeadCommitID"])
16081608

16091609
if isPullBranchDeletable && pull.HasMerged {
1610-
prs, err := models.GetUnmergedPullRequestsByHeadInfo(pull.HeadRepoID, pull.HeadBranch)
1610+
exist, err := models.CheckUnmergedPullRequestsByHeadInfo(pull.HeadRepoID, pull.HeadBranch)
16111611
if err != nil {
1612-
ctx.ServerError("GetUnmergedPullRequestsByHeadInfo", err)
1612+
ctx.ServerError("CheckUnmergedPullRequestsByHeadInfo", err)
16131613
return
16141614
}
16151615

1616-
isPullBranchDeletable = len(prs) == 0
1616+
isPullBranchDeletable = !exist
16171617
}
16181618
ctx.Data["IsPullBranchDeletable"] = isPullBranchDeletable
16191619

routers/web/repo/pull.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -1053,12 +1053,12 @@ func MergePullRequest(ctx *context.Context) {
10531053

10541054
if form.DeleteBranchAfterMerge {
10551055
// Don't cleanup when other pr use this branch as head branch
1056-
prs, err := models.GetUnmergedPullRequestsByHeadInfo(pr.HeadRepoID, pr.HeadBranch)
1056+
exist, err := models.CheckUnmergedPullRequestsByHeadInfo(pr.HeadRepoID, pr.HeadBranch)
10571057
if err != nil {
1058-
ctx.ServerError("GetUnmergedPullRequestsByHeadInfo", err)
1058+
ctx.ServerError("CheckUnmergedPullRequestsByHeadInfo", err)
10591059
return
10601060
}
1061-
if len(prs) > 0 {
1061+
if exist {
10621062
ctx.Redirect(issue.Link())
10631063
return
10641064
}
@@ -1273,12 +1273,12 @@ func CleanUpPullRequest(ctx *context.Context) {
12731273
}
12741274

12751275
// Don't cleanup when other pr use this branch as head branch
1276-
prs, err := models.GetUnmergedPullRequestsByHeadInfo(pr.HeadRepoID, pr.HeadBranch)
1276+
exist, err := models.CheckUnmergedPullRequestsByHeadInfo(pr.HeadRepoID, pr.HeadBranch)
12771277
if err != nil {
1278-
ctx.ServerError("GetUnmergedPullRequestsByHeadInfo", err)
1278+
ctx.ServerError("CheckUnmergedPullRequestsByHeadInfo", err)
12791279
return
12801280
}
1281-
if len(prs) > 0 {
1281+
if exist {
12821282
ctx.NotFound("CleanUpPullRequest", nil)
12831283
return
12841284
}

0 commit comments

Comments
 (0)