Skip to content

Commit

Permalink
Don't delete branch if other PRs with this branch are open
Browse files Browse the repository at this point in the history
fix #18149

Signed-off-by: a1012112796 <[email protected]>
  • Loading branch information
a1012112796 committed Jan 3, 2022
1 parent f499f23 commit f7f627c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
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
prs, err := models.GetUnmergedPullRequestsByHeadInfo(pr.HeadRepoID, pr.HeadBranch)
if err != nil {
ctx.ServerError("GetUnmergedPullRequestsByHeadInfo", err)
return
}
if len(prs) > 0 {
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 {
prs, err := models.GetUnmergedPullRequestsByHeadInfo(pull.HeadRepoID, pull.HeadBranch)
if err != nil {
ctx.ServerError("GetUnmergedPullRequestsByHeadInfo", err)
return
}

isPullBranchDeletable = len(prs) == 0
}
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
prs, err := models.GetUnmergedPullRequestsByHeadInfo(pr.HeadRepoID, pr.HeadBranch)
if err != nil {
ctx.ServerError("GetUnmergedPullRequestsByHeadInfo", err)
return
}
if len(prs) > 0 {
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
prs, err := models.GetUnmergedPullRequestsByHeadInfo(pr.HeadRepoID, pr.HeadBranch)
if err != nil {
ctx.ServerError("GetUnmergedPullRequestsByHeadInfo", err)
return
}
if len(prs) > 0 {
ctx.NotFound("CleanUpPullRequest", nil)
return
}

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

0 comments on commit f7f627c

Please sign in to comment.