diff --git a/models/pull_list.go b/models/pull_list.go index 7c5b83ae5f275..c4621afbac3a8 100644 --- a/models/pull_list.go +++ b/models/pull_list.go @@ -59,6 +59,16 @@ func GetUnmergedPullRequestsByHeadInfo(repoID int64, branch string) ([]*PullRequ Find(&prs) } +// CheckUnmergedPullRequestsByHeadInfo check if the pull requests that are open and has not been merged +// by given head information (repo and branch) exist. +func CheckUnmergedPullRequestsByHeadInfo(repoID int64, branch string) (bool, error) { + return db.GetEngine(db.DefaultContext). + Where("head_repo_id = ? AND head_branch = ? AND has_merged = ? AND issue.is_closed = ? AND flow = ?", + repoID, branch, false, false, PullRequestFlowGithub). + Join("INNER", "issue", "issue.id = pull_request.issue_id"). + Exist(&PullRequest{}) +} + // GetUnmergedPullRequestsByBaseInfo returns all pull requests that are open and has not been merged // by given base information (repo and branch). func GetUnmergedPullRequestsByBaseInfo(repoID int64, branch string) ([]*PullRequest, error) { diff --git a/models/pull_test.go b/models/pull_test.go index f5e9d486ff79e..ec4a172f4d05e 100644 --- a/models/pull_test.go +++ b/models/pull_test.go @@ -107,6 +107,18 @@ func TestGetUnmergedPullRequest(t *testing.T) { assert.True(t, IsErrPullRequestNotExist(err)) } +func TestCheckUnmergedPullRequestsByHeadInfo(t *testing.T) { + assert.NoError(t, unittest.PrepareTestDatabase()) + + exist, err := CheckUnmergedPullRequestsByHeadInfo(1, "branch2") + assert.NoError(t, err) + assert.Equal(t, true, exist) + + exist, err = CheckUnmergedPullRequestsByHeadInfo(1, "not_exist_branch") + assert.NoError(t, err) + assert.Equal(t, false, exist) +} + func TestGetUnmergedPullRequestsByHeadInfo(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) prs, err := GetUnmergedPullRequestsByHeadInfo(1, "branch2") diff --git a/routers/api/v1/repo/pull.go b/routers/api/v1/repo/pull.go index 28c9bb11c3d4a..2428f145db623 100644 --- a/routers/api/v1/repo/pull.go +++ b/routers/api/v1/repo/pull.go @@ -874,12 +874,12 @@ func MergePullRequest(ctx *context.APIContext) { if form.DeleteBranchAfterMerge { // Don't cleanup when other pr use this branch as head branch - prs, err := models.GetUnmergedPullRequestsByHeadInfo(pr.HeadRepoID, pr.HeadBranch) + exist, err := models.CheckUnmergedPullRequestsByHeadInfo(pr.HeadRepoID, pr.HeadBranch) if err != nil { - ctx.ServerError("GetUnmergedPullRequestsByHeadInfo", err) + ctx.ServerError("CheckUnmergedPullRequestsByHeadInfo", err) return } - if len(prs) > 0 { + if exist { ctx.Status(http.StatusOK) return } diff --git a/routers/web/repo/issue.go b/routers/web/repo/issue.go index f9f903e113cf3..2ff5982549e83 100644 --- a/routers/web/repo/issue.go +++ b/routers/web/repo/issue.go @@ -1607,13 +1607,13 @@ func ViewIssue(ctx *context.Context) { (!pull.HasMerged || ctx.Data["HeadBranchCommitID"] == ctx.Data["PullHeadCommitID"]) if isPullBranchDeletable && pull.HasMerged { - prs, err := models.GetUnmergedPullRequestsByHeadInfo(pull.HeadRepoID, pull.HeadBranch) + exist, err := models.CheckUnmergedPullRequestsByHeadInfo(pull.HeadRepoID, pull.HeadBranch) if err != nil { - ctx.ServerError("GetUnmergedPullRequestsByHeadInfo", err) + ctx.ServerError("CheckUnmergedPullRequestsByHeadInfo", err) return } - isPullBranchDeletable = len(prs) == 0 + isPullBranchDeletable = !exist } ctx.Data["IsPullBranchDeletable"] = isPullBranchDeletable diff --git a/routers/web/repo/pull.go b/routers/web/repo/pull.go index b16e0da4f6d94..33ec4ffbc80c3 100644 --- a/routers/web/repo/pull.go +++ b/routers/web/repo/pull.go @@ -1053,12 +1053,12 @@ func MergePullRequest(ctx *context.Context) { if form.DeleteBranchAfterMerge { // Don't cleanup when other pr use this branch as head branch - prs, err := models.GetUnmergedPullRequestsByHeadInfo(pr.HeadRepoID, pr.HeadBranch) + exist, err := models.CheckUnmergedPullRequestsByHeadInfo(pr.HeadRepoID, pr.HeadBranch) if err != nil { - ctx.ServerError("GetUnmergedPullRequestsByHeadInfo", err) + ctx.ServerError("CheckUnmergedPullRequestsByHeadInfo", err) return } - if len(prs) > 0 { + if exist { ctx.Redirect(issue.Link()) return } @@ -1273,12 +1273,12 @@ func CleanUpPullRequest(ctx *context.Context) { } // Don't cleanup when other pr use this branch as head branch - prs, err := models.GetUnmergedPullRequestsByHeadInfo(pr.HeadRepoID, pr.HeadBranch) + exist, err := models.CheckUnmergedPullRequestsByHeadInfo(pr.HeadRepoID, pr.HeadBranch) if err != nil { - ctx.ServerError("GetUnmergedPullRequestsByHeadInfo", err) + ctx.ServerError("CheckUnmergedPullRequestsByHeadInfo", err) return } - if len(prs) > 0 { + if exist { ctx.NotFound("CleanUpPullRequest", nil) return }