Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't delete branch if other PRs with this branch are open #18164

Merged
merged 4 commits into from
Jan 3, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
6543 marked this conversation as resolved.
Show resolved Hide resolved
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")
6543 marked this conversation as resolved.
Show resolved Hide resolved
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
11 changes: 11 additions & 0 deletions routers/api/v1/repo/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,17 @@ func MergePullRequest(ctx *context.APIContext) {
log.Trace("Pull request merged: %d", pr.ID)

if form.DeleteBranchAfterMerge {
// Don't cleanup when other pr use this branch as head branch
6543 marked this conversation as resolved.
Show resolved Hide resolved
exist, err := models.CheckUnmergedPullRequestsByHeadInfo(pr.HeadRepoID, pr.HeadBranch)
6543 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
ctx.ServerError("CheckUnmergedPullRequestsByHeadInfo", err)
6543 marked this conversation as resolved.
Show resolved Hide resolved
return
}
if exist {
ctx.Status(http.StatusOK)
return
}

var headRepo *git.Repository
if ctx.Repo != nil && ctx.Repo.Repository != nil && ctx.Repo.Repository.ID == pr.HeadRepoID && ctx.Repo.GitRepo != nil {
headRepo = ctx.Repo.GitRepo
Expand Down
14 changes: 13 additions & 1 deletion routers/web/repo/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -1600,11 +1600,23 @@ func ViewIssue(ctx *context.Context) {
} else {
ctx.Data["WontSignReason"] = "not_signed_in"
}
ctx.Data["IsPullBranchDeletable"] = canDelete &&

isPullBranchDeletable := canDelete &&
pull.HeadRepo != nil &&
git.IsBranchExist(ctx, pull.HeadRepo.RepoPath(), pull.HeadBranch) &&
(!pull.HasMerged || ctx.Data["HeadBranchCommitID"] == ctx.Data["PullHeadCommitID"])

if isPullBranchDeletable && pull.HasMerged {
exist, err := models.CheckUnmergedPullRequestsByHeadInfo(pull.HeadRepoID, pull.HeadBranch)
if err != nil {
ctx.ServerError("CheckUnmergedPullRequestsByHeadInfo", err)
6543 marked this conversation as resolved.
Show resolved Hide resolved
return
}

isPullBranchDeletable = !exist
}
ctx.Data["IsPullBranchDeletable"] = isPullBranchDeletable

stillCanManualMerge := func() bool {
if pull.HasMerged || issue.IsClosed || !ctx.IsSigned {
return false
Expand Down
22 changes: 22 additions & 0 deletions routers/web/repo/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -1052,6 +1052,17 @@ func MergePullRequest(ctx *context.Context) {
log.Trace("Pull request merged: %d", pr.ID)

if form.DeleteBranchAfterMerge {
// Don't cleanup when other pr use this branch as head branch
exist, err := models.CheckUnmergedPullRequestsByHeadInfo(pr.HeadRepoID, pr.HeadBranch)
if err != nil {
ctx.ServerError("CheckUnmergedPullRequestsByHeadInfo", err)
6543 marked this conversation as resolved.
Show resolved Hide resolved
return
}
if exist {
ctx.Redirect(issue.Link())
return
}

var headRepo *git.Repository
if ctx.Repo != nil && ctx.Repo.Repository != nil && pr.HeadRepoID == ctx.Repo.Repository.ID && ctx.Repo.GitRepo != nil {
headRepo = ctx.Repo.GitRepo
Expand Down Expand Up @@ -1261,6 +1272,17 @@ func CleanUpPullRequest(ctx *context.Context) {
return
}

// Don't cleanup when other pr use this branch as head branch
6543 marked this conversation as resolved.
Show resolved Hide resolved
exist, err := models.CheckUnmergedPullRequestsByHeadInfo(pr.HeadRepoID, pr.HeadBranch)
if err != nil {
ctx.ServerError("CheckUnmergedPullRequestsByHeadInfo", err)
6543 marked this conversation as resolved.
Show resolved Hide resolved
return
}
if exist {
ctx.NotFound("CleanUpPullRequest", nil)
return
}

if err := pr.LoadHeadRepo(); err != nil {
ctx.ServerError("LoadHeadRepo", err)
return
Expand Down