Skip to content

Redirect open PRs when the target branch is deleted through merging a PR #28539

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 11 commits into from
Closed
31 changes: 31 additions & 0 deletions routers/web/repo/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -1313,6 +1313,37 @@ func MergePullRequest(ctx *context.Context) {
return
}

pullRequestsToHead, err := issues_model.GetUnmergedPullRequestsByBaseInfo(ctx, pr.HeadRepoID, pr.HeadBranch)
if err != nil {
ctx.ServerError("GetUnmergedPullRequestsByBaseInfo", err)
return
}

for _, prToHead := range pullRequestsToHead {
if prToHead.BaseRepoID != pr.BaseRepoID {
continue
}

if err := prToHead.LoadIssue(ctx); err != nil {
ctx.ServerError(fmt.Sprintf("LoadIssueForPullRequest[%d]", prToHead.ID), err)
return
}

if prToHead.Issue.RepoID == pr.Issue.RepoID {
prToHead.Issue.Repo = pr.Issue.Repo
} else {
if err := prToHead.Issue.LoadRepo(ctx); err != nil {
ctx.ServerError(fmt.Sprintf("LoadRepoForIssue[%d]", prToHead.IssueID), err)
return
}
}

if err := pull_service.ChangeTargetBranch(ctx, prToHead, ctx.Doer, pr.BaseBranch); err != nil {
ctx.ServerError(fmt.Sprintf("ChangeTargetBranch[%d]", prToHead.ID), err)
return
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we please extract this code into a new subfunction?
Something like redirectOpenPulls or so?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's a good idea. This will allow you to use redirection in the API, as @kvaster noted.


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
Expand Down