Skip to content

Commit 795d8cb

Browse files
committed
Try to keep branch PRs open when branch is deleted cause of it's PR merged and closed, fixes #18408
1 parent 59eb660 commit 795d8cb

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

routers/api/v1/repo/pull.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,13 @@ func MergePullRequest(ctx *context.APIContext) {
904904
}
905905
defer headRepo.Close()
906906
}
907+
if pr.BaseRepoID == pr.HeadRepoID {
908+
if err := pull_service.RebaseBranchPulls(ctx, ctx.Doer, pr.HeadRepoID, pr.HeadBranch, pr.BaseBranch); err != nil {
909+
log.Error("RebaseBranchPulls: %v", err)
910+
ctx.Error(http.StatusInternalServerError, "RebaseBranchPulls", err)
911+
return
912+
}
913+
}
907914
if err := repo_service.DeleteBranch(ctx, ctx.Doer, pr.HeadRepo, headRepo, pr.HeadBranch); err != nil {
908915
switch {
909916
case git.IsErrBranchNotExist(err):

routers/web/repo/pull.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1399,6 +1399,15 @@ func CleanUpPullRequest(ctx *context.Context) {
13991399

14001400
func deleteBranch(ctx *context.Context, pr *issues_model.PullRequest, gitRepo *git.Repository) {
14011401
fullBranchName := pr.HeadRepo.FullName() + ":" + pr.HeadBranch
1402+
1403+
if pr.BaseRepoID == pr.HeadRepoID {
1404+
if err := pull_service.RebaseBranchPulls(ctx, ctx.Doer, pr.HeadRepoID, pr.HeadBranch, pr.BaseBranch); err != nil {
1405+
log.Error("RebaseBranchPulls: %v", err)
1406+
ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName))
1407+
return
1408+
}
1409+
}
1410+
14021411
if err := repo_service.DeleteBranch(ctx, ctx.Doer, pr.HeadRepo, gitRepo, pr.HeadBranch); err != nil {
14031412
switch {
14041413
case git.IsErrBranchNotExist(err):

services/pull/pull.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,34 @@ func (errs errlist) Error() string {
497497
return ""
498498
}
499499

500+
// RebaseBranchPulls change target branch for all pull requests who's base branch is the branch
501+
func RebaseBranchPulls(ctx context.Context, doer *user_model.User, repoID int64, branch, targetBranch string) error {
502+
prs, err := models.GetUnmergedPullRequestsByBaseInfo(repoID, branch)
503+
if err != nil {
504+
return err
505+
}
506+
507+
if err := models.PullRequestList(prs).LoadAttributes(); err != nil {
508+
return err
509+
}
510+
511+
var errs errlist
512+
for _, pr := range prs {
513+
if err = pr.Issue.LoadAttributes(); err != nil {
514+
errs = append(errs, err)
515+
} else if err = ChangeTargetBranch(ctx, pr, doer, targetBranch); err != nil &&
516+
!models.IsErrIssueIsClosed(err) && !models.IsErrPullRequestHasMerged(err) &&
517+
!models.IsErrPullRequestAlreadyExists(err) {
518+
errs = append(errs, err)
519+
}
520+
}
521+
522+
if len(errs) > 0 {
523+
return errs
524+
}
525+
return nil
526+
}
527+
500528
// CloseBranchPulls close all the pull requests who's head branch is the branch
501529
func CloseBranchPulls(doer *user_model.User, repoID int64, branch string) error {
502530
prs, err := issues_model.GetUnmergedPullRequestsByHeadInfo(repoID, branch, false)

0 commit comments

Comments
 (0)