Skip to content

Conversation

SpectralDuck
Copy link

If setting RETARGET_CHILDREN_ON_MERGE is true, we should retarget a PR that targets a branch we're merging to the target branch of the branch we're merging.

Previously, RETARGET_CHILDREN_ON_MERGE incorrectly retargeted all following PR's in a chain to the defaultBranch when merging a PR in the middle of a chain.

Fixes #35138


I've moved the RETARGET_CHILDREN_ON_MERGE behavior from pull/AdjustPullsCausedByBranchDeleted() to branch/DeleteBranch(), because we need to know which PR is being closed to retarget the child branch(es). In pull/pull.go we only know that a branch is being deleted, but not why.
I'm not very happy with the dependency, though. So if there's a cleaner option, I'd be happy to move this behavior back to pull/pull.go.

Previously, RETARGET_CHILDREN_ON_MERGE incorrectly retargeted all following PR's to the defaultBranch when merging a PR in the middle of a chain.

Fixes go-gitea#35138
@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Sep 11, 2025
@github-actions github-actions bot added the modifies/go Pull requests that update Go code label Sep 11, 2025
// AdjustPullsCausedByBranchDeleted close all the pull requests with the deleted branch as head
// Retarget all PR's with the deleted branch as target to the default branch
// We don't handle setting.Repository.PullRequest.RetargetChildrenOnMerge here,
// because we would need the PR that was merged to determine which branch to redirect to
Copy link
Member

Choose a reason for hiding this comment

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

If these pull requests are not handled, they will because a dead pull request because head branch deleted.

Copy link
Author

Choose a reason for hiding this comment

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

Hi @lunny, I think my comment is a bit unclear.

What I mean to say is: once we're here, we're not going to give PRs any special treatment due to RetargetChildrenOnMerge. Any branch that still has the toDelete branch as head, will be redirected to the default branch.

My comment was only meant to explain why we're not giving those PRs special treatment here.

I think it's best if I remove the last two lines of this comment. The reason I felt the need to explain here, was because this was the place where we used to handle RetargetChildrenOnMerge. Now that we handle it in the PR-merge code-path, there's no reason for anyone to expect us to handle it on generic branch close.

In summary: I think the code's behavior is what we want (retarget PRs to the default branch when the head branch is deleted), and this comment (line 673 and 674) is confusing and should be deleted. Do you agree?

PS Sorry for the double-post. I was logged in to the wrong account :(

}

if err := db.WithTx(ctx, func(ctx context.Context) error {
if pr != nil && setting.Repository.PullRequest.RetargetChildrenOnMerge {
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Author

Choose a reason for hiding this comment

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. modifies/go Pull requests that update Go code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

RETARGET_CHILDREN_ON_MERGE retargets branches to the main branch instead of the target of the parent branch
4 participants