Skip to content

Commit

Permalink
Try to keep branch PRs open when branch is deleted cause of it's PR m…
Browse files Browse the repository at this point in the history
…erged and closed, fixes go-gitea#18408
  • Loading branch information
kvaster committed May 8, 2022
1 parent 290cc88 commit 91de000
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
7 changes: 7 additions & 0 deletions routers/api/v1/repo/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
9 changes: 9 additions & 0 deletions routers/web/repo/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
28 changes: 28 additions & 0 deletions services/pull/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 91de000

Please sign in to comment.