Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
a1012112796 committed Jan 3, 2022
1 parent f7f627c commit 92466fe
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 12 deletions.
10 changes: 10 additions & 0 deletions models/pull_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
12 changes: 12 additions & 0 deletions models/pull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
6 changes: 3 additions & 3 deletions routers/api/v1/repo/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions routers/web/repo/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 6 additions & 6 deletions routers/web/repo/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 92466fe

Please sign in to comment.