From 91de000b3c516dfc18c1d347645e887968869191 Mon Sep 17 00:00:00 2001 From: Viktor Kuzmin Date: Sun, 8 May 2022 23:46:00 +0300 Subject: [PATCH] Try to keep branch PRs open when branch is deleted cause of it's PR merged and closed, fixes #18408 --- routers/api/v1/repo/pull.go | 7 +++++++ routers/web/repo/pull.go | 9 +++++++++ services/pull/pull.go | 28 ++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/routers/api/v1/repo/pull.go b/routers/api/v1/repo/pull.go index f95bc6b16b16..70ad6863afb5 100644 --- a/routers/api/v1/repo/pull.go +++ b/routers/api/v1/repo/pull.go @@ -888,6 +888,13 @@ func MergePullRequest(ctx *context.APIContext) { } defer headRepo.Close() } + if pr.BaseRepoID == pr.HeadRepoID { + if err := pull_service.RebaseBranchPulls(ctx, ctx.Doer, pr.HeadRepoID, pr.HeadBranch, pr.BaseBranch); err != nil { + log.Error("RebaseBranchPulls: %v", err) + ctx.Error(http.StatusInternalServerError, "RebaseBranchPulls", err) + return + } + } if err := repo_service.DeleteBranch(ctx.Doer, pr.HeadRepo, headRepo, pr.HeadBranch); err != nil { switch { case git.IsErrBranchNotExist(err): diff --git a/routers/web/repo/pull.go b/routers/web/repo/pull.go index 27b61309a575..a3b7319aba3c 100644 --- a/routers/web/repo/pull.go +++ b/routers/web/repo/pull.go @@ -1318,6 +1318,15 @@ func CleanUpPullRequest(ctx *context.Context) { func deleteBranch(ctx *context.Context, pr *models.PullRequest, gitRepo *git.Repository) { fullBranchName := pr.HeadRepo.Owner.Name + "/" + pr.HeadBranch + + if pr.BaseRepoID == pr.HeadRepoID { + if err := pull_service.RebaseBranchPulls(ctx, ctx.Doer, pr.HeadRepoID, pr.HeadBranch, pr.BaseBranch); err != nil { + log.Error("RebaseBranchPulls: %v", err) + ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName)) + return + } + } + if err := repo_service.DeleteBranch(ctx.Doer, pr.HeadRepo, gitRepo, pr.HeadBranch); err != nil { switch { case git.IsErrBranchNotExist(err): diff --git a/services/pull/pull.go b/services/pull/pull.go index b94b6769a452..2c5274df7c0b 100644 --- a/services/pull/pull.go +++ b/services/pull/pull.go @@ -511,6 +511,34 @@ func (errs errlist) Error() string { return "" } +// RebaseBranchPulls change target branch for all pull requests who's base branch is the branch +func RebaseBranchPulls(ctx context.Context, doer *user_model.User, repoID int64, branch, targetBranch string) error { + prs, err := models.GetUnmergedPullRequestsByBaseInfo(repoID, branch) + if err != nil { + return err + } + + if err := models.PullRequestList(prs).LoadAttributes(); err != nil { + return err + } + + var errs errlist + for _, pr := range prs { + if err = pr.Issue.LoadAttributes(); err != nil { + errs = append(errs, err) + } else if err = ChangeTargetBranch(ctx, pr, doer, targetBranch); err != nil && + !models.IsErrIssueIsClosed(err) && !models.IsErrPullRequestHasMerged(err) && + !models.IsErrPullRequestAlreadyExists(err) { + errs = append(errs, err) + } + } + + if len(errs) > 0 { + return errs + } + return nil +} + // CloseBranchPulls close all the pull requests who's head branch is the branch func CloseBranchPulls(doer *user_model.User, repoID int64, branch string) error { prs, err := models.GetUnmergedPullRequestsByHeadInfo(repoID, branch)