From 637c3ec5d82f9d727724cc362aea37eeb2f64b40 Mon Sep 17 00:00:00 2001 From: a1012112796 <1012112796@qq.com> Date: Tue, 4 Jan 2022 03:45:58 +0800 Subject: Don't delete branch if other PRs with this branch are open (#18164) fix #18149 Signed-off-by: a1012112796 <1012112796@qq.com> --- routers/api/v1/repo/pull.go | 11 +++++++++++ routers/web/repo/issue.go | 14 +++++++++++++- routers/web/repo/pull.go | 22 ++++++++++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) (limited to 'routers') diff --git a/routers/api/v1/repo/pull.go b/routers/api/v1/repo/pull.go index 2b7280d39b..8297e35a17 100644 --- a/routers/api/v1/repo/pull.go +++ b/routers/api/v1/repo/pull.go @@ -873,6 +873,17 @@ func MergePullRequest(ctx *context.APIContext) { log.Trace("Pull request merged: %d", pr.ID) 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 if ctx.Repo != nil && ctx.Repo.Repository != nil && ctx.Repo.Repository.ID == pr.HeadRepoID && ctx.Repo.GitRepo != nil { headRepo = ctx.Repo.GitRepo diff --git a/routers/web/repo/issue.go b/routers/web/repo/issue.go index d55b6c96b6..ea16de3950 100644 --- a/routers/web/repo/issue.go +++ b/routers/web/repo/issue.go @@ -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.HasUnmergedPullRequestsByHeadInfo(pull.HeadRepoID, pull.HeadBranch) + if err != nil { + ctx.ServerError("HasUnmergedPullRequestsByHeadInfo", err) + return + } + + isPullBranchDeletable = !exist + } + ctx.Data["IsPullBranchDeletable"] = isPullBranchDeletable + stillCanManualMerge := func() bool { if pull.HasMerged || issue.IsClosed || !ctx.IsSigned { return false diff --git a/routers/web/repo/pull.go b/routers/web/repo/pull.go index 37826abfb4..b40eb1ea17 100644 --- a/routers/web/repo/pull.go +++ b/routers/web/repo/pull.go @@ -1051,6 +1051,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.HasUnmergedPullRequestsByHeadInfo(pr.HeadRepoID, pr.HeadBranch) + if err != nil { + ctx.ServerError("HasUnmergedPullRequestsByHeadInfo", err) + 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 @@ -1222,6 +1233,17 @@ func CleanUpPullRequest(ctx *context.Context) { 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 { ctx.ServerError("LoadHeadRepo", err) return -- cgit v1.2.3