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
5 changes: 5 additions & 0 deletions routers/api/v1/repo/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,11 @@ func MergePullRequest(ctx *context.APIContext) {
return
}

if err := pull_service.RedirectOpenPullsToBaseBranch(ctx, pr, ctx.Doer); err != nil {
ctx.ServerError("RedirectOpenPulls", err)
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
Expand Down
5 changes: 5 additions & 0 deletions routers/web/repo/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -1313,6 +1313,11 @@ func MergePullRequest(ctx *context.Context) {
return
}

if err := pull_service.RedirectOpenPullsToBaseBranch(ctx, pr, ctx.Doer); err != nil {
ctx.ServerError("RedirectOpenPulls", err)
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
Expand Down
33 changes: 33 additions & 0 deletions services/pull/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -953,3 +953,36 @@ func GetPullCommits(ctx *gitea_context.Context, issue *issues_model.Issue) ([]Co

return commits, lastReviewCommitID, nil
}

// RedirectOpenPullsToBaseBranch redirects open pull requests to base branch of specified PR
func RedirectOpenPullsToBaseBranch(ctx context.Context, basePR *issues_model.PullRequest, doer *user_model.User) error {
pullRequestsToHead, err := issues_model.GetUnmergedPullRequestsByBaseInfo(ctx, basePR.HeadRepoID, basePR.HeadBranch)
if err != nil {
return fmt.Errorf("failed to get unmerged pull requests: %w", err)
}

if err := issues_model.PullRequestList(pullRequestsToHead).LoadAttributes(ctx); err != nil {
return fmt.Errorf("failed to load attributes for pull requests: %w", err)
}

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

if prToHead.Issue.RepoID == basePR.Issue.RepoID {
prToHead.Issue.Repo = basePR.Issue.Repo
} else {
if err := prToHead.Issue.LoadRepo(ctx); err != nil {
return fmt.Errorf("failed to load repo for issue [%d]: %w", prToHead.IssueID, err)
}
}

if err := ChangeTargetBranch(ctx, prToHead, doer, basePR.BaseBranch); err != nil &&
!issues_model.IsErrPullRequestAlreadyExists(err) {
return fmt.Errorf("failed to change target branch for PR [%d]: %w", prToHead.ID, err)
}
}

return nil
}