Skip to content

Commit b6f348b

Browse files
committed
Add a switch to the GetUnmergedPullRequestsByHeadInfo func to control whether the status of PR must be open
1 parent 92ae573 commit b6f348b

File tree

3 files changed

+12
-10
lines changed

3 files changed

+12
-10
lines changed

models/issues/pull_list.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,15 @@ func listPullRequestStatement(baseRepoID int64, opts *PullRequestsOptions) (*xor
5252

5353
// GetUnmergedPullRequestsByHeadInfo returns all pull requests that are open and has not been merged
5454
// by given head information (repo and branch).
55-
func GetUnmergedPullRequestsByHeadInfo(repoID int64, branch string) ([]*PullRequest, error) {
55+
func GetUnmergedPullRequestsByHeadInfo(repoID int64, branch string, mustOpenedPR bool) ([]*PullRequest, error) {
5656
prs := make([]*PullRequest, 0, 2)
57-
return prs, db.GetEngine(db.DefaultContext).
58-
Where("head_repo_id = ? AND head_branch = ? AND has_merged = ? AND flow = ?",
59-
repoID, branch, false, PullRequestFlowGithub).
57+
sess := db.GetEngine(db.DefaultContext).
6058
Join("INNER", "issue", "issue.id = pull_request.issue_id").
61-
Find(&prs)
59+
Where("head_repo_id = ? AND head_branch = ? AND has_merged = ? AND flow = ?", repoID, branch, false, PullRequestFlowGithub)
60+
if mustOpenedPR {
61+
sess.Where("issue.is_closed = ?", false)
62+
}
63+
return prs, sess.Find(&prs)
6264
}
6365

6466
// CanMaintainerWriteToBranch check whether user is a maintainer and could write to the branch
@@ -71,7 +73,7 @@ func CanMaintainerWriteToBranch(p access_model.Permission, branch string, user *
7173
return false
7274
}
7375

74-
prs, err := GetUnmergedPullRequestsByHeadInfo(p.Units[0].RepoID, branch)
76+
prs, err := GetUnmergedPullRequestsByHeadInfo(p.Units[0].RepoID, branch, true)
7577
if err != nil {
7678
return false
7779
}

models/issues/pull_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func TestHasUnmergedPullRequestsByHeadInfo(t *testing.T) {
118118

119119
func TestGetUnmergedPullRequestsByHeadInfo(t *testing.T) {
120120
assert.NoError(t, unittest.PrepareTestDatabase())
121-
prs, err := issues_model.GetUnmergedPullRequestsByHeadInfo(1, "branch2")
121+
prs, err := issues_model.GetUnmergedPullRequestsByHeadInfo(1, "branch2", true)
122122
assert.NoError(t, err)
123123
assert.Len(t, prs, 1)
124124
for _, pr := range prs {

services/pull/pull.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ func AddTestPullRequestTask(doer *user_model.User, repoID int64, branch string,
257257
// If you don't let it run all the way then you will lose data
258258
// TODO: graceful: AddTestPullRequestTask needs to become a queue!
259259

260-
prs, err := issues_model.GetUnmergedPullRequestsByHeadInfo(repoID, branch)
260+
prs, err := issues_model.GetUnmergedPullRequestsByHeadInfo(repoID, branch, false)
261261
if err != nil {
262262
log.Error("Find pull requests [head_repo_id: %d, head_branch: %s]: %v", repoID, branch, err)
263263
return
@@ -500,7 +500,7 @@ func (errs errlist) Error() string {
500500

501501
// CloseBranchPulls close all the pull requests who's head branch is the branch
502502
func CloseBranchPulls(doer *user_model.User, repoID int64, branch string) error {
503-
prs, err := issues_model.GetUnmergedPullRequestsByHeadInfo(repoID, branch)
503+
prs, err := issues_model.GetUnmergedPullRequestsByHeadInfo(repoID, branch, true)
504504
if err != nil {
505505
return err
506506
}
@@ -536,7 +536,7 @@ func CloseRepoBranchesPulls(ctx context.Context, doer *user_model.User, repo *re
536536

537537
var errs errlist
538538
for _, branch := range branches {
539-
prs, err := issues_model.GetUnmergedPullRequestsByHeadInfo(repo.ID, branch.Name)
539+
prs, err := issues_model.GetUnmergedPullRequestsByHeadInfo(repo.ID, branch.Name, true)
540540
if err != nil {
541541
return err
542542
}

0 commit comments

Comments
 (0)