Don't delete branch if other PRs with this branch are open (#18164)
fix #18149 Signed-off-by: a1012112796 <1012112796@qq.com>
This commit is contained in:
parent
650a50a7ba
commit
637c3ec5d8
|
@ -59,6 +59,16 @@ func GetUnmergedPullRequestsByHeadInfo(repoID int64, branch string) ([]*PullRequ
|
||||||
Find(&prs)
|
Find(&prs)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// HasUnmergedPullRequestsByHeadInfo checks if there are open and not merged pull request
|
||||||
|
// by given head information (repo and branch)
|
||||||
|
func HasUnmergedPullRequestsByHeadInfo(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
|
// GetUnmergedPullRequestsByBaseInfo returns all pull requests that are open and has not been merged
|
||||||
// by given base information (repo and branch).
|
// by given base information (repo and branch).
|
||||||
func GetUnmergedPullRequestsByBaseInfo(repoID int64, branch string) ([]*PullRequest, error) {
|
func GetUnmergedPullRequestsByBaseInfo(repoID int64, branch string) ([]*PullRequest, error) {
|
||||||
|
|
|
@ -107,6 +107,18 @@ func TestGetUnmergedPullRequest(t *testing.T) {
|
||||||
assert.True(t, IsErrPullRequestNotExist(err))
|
assert.True(t, IsErrPullRequestNotExist(err))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestHasUnmergedPullRequestsByHeadInfo(t *testing.T) {
|
||||||
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||||||
|
|
||||||
|
exist, err := HasUnmergedPullRequestsByHeadInfo(1, "branch2")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, true, exist)
|
||||||
|
|
||||||
|
exist, err = HasUnmergedPullRequestsByHeadInfo(1, "not_exist_branch")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, false, exist)
|
||||||
|
}
|
||||||
|
|
||||||
func TestGetUnmergedPullRequestsByHeadInfo(t *testing.T) {
|
func TestGetUnmergedPullRequestsByHeadInfo(t *testing.T) {
|
||||||
assert.NoError(t, unittest.PrepareTestDatabase())
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||||||
prs, err := GetUnmergedPullRequestsByHeadInfo(1, "branch2")
|
prs, err := GetUnmergedPullRequestsByHeadInfo(1, "branch2")
|
||||||
|
|
|
@ -873,6 +873,17 @@ func MergePullRequest(ctx *context.APIContext) {
|
||||||
log.Trace("Pull request merged: %d", pr.ID)
|
log.Trace("Pull request merged: %d", pr.ID)
|
||||||
|
|
||||||
if form.DeleteBranchAfterMerge {
|
if form.DeleteBranchAfterMerge {
|
||||||
|
// Don't cleanup when there are other PR's that use this branch as head branch.
|
||||||
|
exist, err := models.HasUnmergedPullRequestsByHeadInfo(pr.HeadRepoID, pr.HeadBranch)
|
||||||
|
if err != nil {
|
||||||
|
ctx.ServerError("HasUnmergedPullRequestsByHeadInfo", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if exist {
|
||||||
|
ctx.Status(http.StatusOK)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
var headRepo *git.Repository
|
var headRepo *git.Repository
|
||||||
if ctx.Repo != nil && ctx.Repo.Repository != nil && ctx.Repo.Repository.ID == pr.HeadRepoID && ctx.Repo.GitRepo != nil {
|
if ctx.Repo != nil && ctx.Repo.Repository != nil && ctx.Repo.Repository.ID == pr.HeadRepoID && ctx.Repo.GitRepo != nil {
|
||||||
headRepo = ctx.Repo.GitRepo
|
headRepo = ctx.Repo.GitRepo
|
||||||
|
|
|
@ -1600,11 +1600,23 @@ func ViewIssue(ctx *context.Context) {
|
||||||
} else {
|
} else {
|
||||||
ctx.Data["WontSignReason"] = "not_signed_in"
|
ctx.Data["WontSignReason"] = "not_signed_in"
|
||||||
}
|
}
|
||||||
ctx.Data["IsPullBranchDeletable"] = canDelete &&
|
|
||||||
|
isPullBranchDeletable := canDelete &&
|
||||||
pull.HeadRepo != nil &&
|
pull.HeadRepo != nil &&
|
||||||
git.IsBranchExist(ctx, pull.HeadRepo.RepoPath(), pull.HeadBranch) &&
|
git.IsBranchExist(ctx, pull.HeadRepo.RepoPath(), pull.HeadBranch) &&
|
||||||
(!pull.HasMerged || ctx.Data["HeadBranchCommitID"] == ctx.Data["PullHeadCommitID"])
|
(!pull.HasMerged || ctx.Data["HeadBranchCommitID"] == ctx.Data["PullHeadCommitID"])
|
||||||
|
|
||||||
|
if isPullBranchDeletable && pull.HasMerged {
|
||||||
|
exist, err := models.HasUnmergedPullRequestsByHeadInfo(pull.HeadRepoID, pull.HeadBranch)
|
||||||
|
if err != nil {
|
||||||
|
ctx.ServerError("HasUnmergedPullRequestsByHeadInfo", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
isPullBranchDeletable = !exist
|
||||||
|
}
|
||||||
|
ctx.Data["IsPullBranchDeletable"] = isPullBranchDeletable
|
||||||
|
|
||||||
stillCanManualMerge := func() bool {
|
stillCanManualMerge := func() bool {
|
||||||
if pull.HasMerged || issue.IsClosed || !ctx.IsSigned {
|
if pull.HasMerged || issue.IsClosed || !ctx.IsSigned {
|
||||||
return false
|
return false
|
||||||
|
|
|
@ -1051,6 +1051,17 @@ func MergePullRequest(ctx *context.Context) {
|
||||||
log.Trace("Pull request merged: %d", pr.ID)
|
log.Trace("Pull request merged: %d", pr.ID)
|
||||||
|
|
||||||
if form.DeleteBranchAfterMerge {
|
if form.DeleteBranchAfterMerge {
|
||||||
|
// Don't cleanup when other pr use this branch as head branch
|
||||||
|
exist, err := models.HasUnmergedPullRequestsByHeadInfo(pr.HeadRepoID, pr.HeadBranch)
|
||||||
|
if err != nil {
|
||||||
|
ctx.ServerError("HasUnmergedPullRequestsByHeadInfo", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if exist {
|
||||||
|
ctx.Redirect(issue.Link())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
var headRepo *git.Repository
|
var headRepo *git.Repository
|
||||||
if ctx.Repo != nil && ctx.Repo.Repository != nil && pr.HeadRepoID == ctx.Repo.Repository.ID && ctx.Repo.GitRepo != nil {
|
if ctx.Repo != nil && ctx.Repo.Repository != nil && pr.HeadRepoID == ctx.Repo.Repository.ID && ctx.Repo.GitRepo != nil {
|
||||||
headRepo = ctx.Repo.GitRepo
|
headRepo = ctx.Repo.GitRepo
|
||||||
|
@ -1222,6 +1233,17 @@ func CleanUpPullRequest(ctx *context.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Don't cleanup when there are other PR's that use this branch as head branch.
|
||||||
|
exist, err := models.HasUnmergedPullRequestsByHeadInfo(pr.HeadRepoID, pr.HeadBranch)
|
||||||
|
if err != nil {
|
||||||
|
ctx.ServerError("HasUnmergedPullRequestsByHeadInfo", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if exist {
|
||||||
|
ctx.NotFound("CleanUpPullRequest", nil)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if err := pr.LoadHeadRepo(); err != nil {
|
if err := pr.LoadHeadRepo(); err != nil {
|
||||||
ctx.ServerError("LoadHeadRepo", err)
|
ctx.ServerError("LoadHeadRepo", err)
|
||||||
return
|
return
|
||||||
|
|
Loading…
Reference in New Issue