Skip to content
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

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

Closed
wants to merge 11 commits into from

Conversation

tulzke
Copy link

@tulzke tulzke commented Dec 19, 2023

Added a change the destination branch for those pull requests whose destination branch was deleted as a result of merge.
PR's HEAD changes to HEAD of the merged PR.

Resolves #27062

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Dec 19, 2023
@pull-request-size pull-request-size bot added the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label Dec 19, 2023
@denyskon
Copy link
Member

Looks like a duplicate of #18478 to me, but with a slightly simpler implementation

@tulzke
Copy link
Author

tulzke commented Dec 27, 2023

@denyskon , thx for reply.
Yes, it looks like it.
But the PR you are referring to does not look active. He is almost two years old, and he is still not in master.
But me and our team really needs this feature.
What needs to be done so that we finally see this feature in the next minor version?

@denyskon
Copy link
Member

This implementation looks clean. I will try to test it out during the next few days :)
I think however that it counts as a new feature, so according to our policy it would not be released before Gitea 1.22

@denyskon denyskon added the giteabot/update-branch Hint for the bot that it should update a PR with the latest state on main label Dec 30, 2023
@GiteaBot GiteaBot removed the giteabot/update-branch Hint for the bot that it should update a PR with the latest state on main label Dec 30, 2023
Copy link
Member

@denyskon denyskon left a comment

Choose a reason for hiding this comment

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

According to my tests, this feature seems to work great, and the implementation is a lot simpler than in the linked PR 👍

routers/web/repo/pull.go Outdated Show resolved Hide resolved
routers/web/repo/pull.go Outdated Show resolved Hide resolved
@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Dec 30, 2023
tulzke and others added 3 commits January 2, 2024 21:35
Co-authored-by: Denys Konovalov <kontakt@denyskon.de>
Co-authored-by: Denys Konovalov <kontakt@denyskon.de>
@pull-request-size pull-request-size bot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Jan 2, 2024
@tulzke
Copy link
Author

tulzke commented Jan 2, 2024

Hello @denyskon. Thanks for the review.
I re-read my code and realized that assigning ctx.Repo.Repository to prTohead.Issue.Repo is not valid in all cases.
I added a condition to check that both issues have the same RepoId. If this is not the case, then Repo is loaded from the database.

I apologize for making changes after the review.

@GiteaBot GiteaBot added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Jan 3, 2024
@denyskon denyskon self-requested a review January 3, 2024 08:31
@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. labels Jan 3, 2024
@denyskon denyskon added the giteabot/update-branch Hint for the bot that it should update a PR with the latest state on main label Jan 3, 2024
@GiteaBot GiteaBot removed the giteabot/update-branch Hint for the bot that it should update a PR with the latest state on main label Jan 3, 2024
Comment on lines 1316 to 1345
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.

@delvh delvh changed the title Added PR redirection to a new branch if the target branch was deleted Redirect open PRs when the target branch is deleted through merging a PR Jan 3, 2024
@delvh
Copy link
Member

delvh commented Jan 3, 2024

Also, I'm currently wondering how edge cases will behave.
Do we run into any sort of security issue with this implementation, where someone can suddenly see something they are not supposed to see?
So, assume the following example:
We have

  • public repo A
  • fork B of A
  • private repo C

The owner of A has been given collaboration permissions on C.
A opens a PR targeting C and checks delete branch on merge.
Similarly, B opens a PR targeting the base branch of A.
As PR is merged.
So, is Bs PR now targeting C, a repo it doesn't even know of?
Am I missing something, or isn't this state possible, and it is a low priority security issue?

@denyskon
Copy link
Member

denyskon commented Jan 3, 2024

@delvh According to
grafik

A fork of a private repository remains private. So how should A in your case become public? 🤔

@denyskon denyskon removed the reviewed/wait-merge This pull request is part of the merge queue. It will be merged soon. label Jan 3, 2024
@kvaster
Copy link
Contributor

kvaster commented Jan 3, 2024

I think that this PR is almost the same as mine (#18478), but it covers even less when mine:

This PR covers only web/repo/pull, but it do nothing with api - routers/api/v1/repo/pull and it should, I think.

And also both PRs do not cover following case:

Repo A.
Repo B is a fork of repo A
PR1 is a PR from B to A
PR2 is a PR from B to PR branch in B

What should happen in case PR1 is merged and deleted ?

  1. PR2 should be closed
  2. PR2 should be retarget to PR1's branch, but in repo B ?
  3. PR2 should be retarget to PR's target - i.e. to repo A ? But in this case we should also MOVE somehow PR2 to repo A...

@kvaster
Copy link
Contributor

kvaster commented Jan 3, 2024

So, I think, that we should:

  • cover all entry points for merging PR's
  • think of exact behaviour when base PR is targeting another repo

@kvaster
Copy link
Contributor

kvaster commented Jan 3, 2024

@denyskon , thx for reply. Yes, it looks like it. But the PR you are referring to does not look active. He is almost two years old, and he is still not in master. But me and our team really needs this feature. What needs to be done so that we finally see this feature in the next minor version?

The PR was complete from my point of view. Probably nobody cared about it and I had no time/wish to push this topic. I was just updating PR from time to time. Also I had no time (no experiance in go) to write unit tests for it.

@denyskon
Copy link
Member

denyskon commented Jan 3, 2024

@kvaster

To me, it doesn't matter which PR is merged in the end. As it already has two approvals, it would maybe happen faster here, but I'm also open to reviewing the other PR if needed.

I'd say we need the functionality for the API, which can easily be provided by the function that delvh suggested to add. As for the example you brought up, I don't think we should retarget these PRs as it would basically mean that PRs end up in a repo they maybe weren't intended to, without the author even being notified about it.

(cc @tulzke)

@kvaster
Copy link
Contributor

kvaster commented Jan 3, 2024

@denyskon, to me it always metters if someone is spending his time to help.

I already wrote my concerns about current status of this PR:

  1. Not all entry points for merging PR's are covered.
  2. Behaviour is unpredictable in case base PR is targeting another repo.
  3. pull_service.ChangeTargetBranch error is not checked and merge can fail even if it's not an error

@kvaster
Copy link
Contributor

kvaster commented Jan 3, 2024

This is not a competition. I really want this feature to be merged aswell. I' ve reopened my PR in case somebody will want to make a review.

#28686

@tulzke
Copy link
Author

tulzke commented Jan 4, 2024

  1. Behaviour is unpredictable in case base PR is targeting another repo.
    You are wrong, the behavior is quite predictable. If a redirect has not been made for the PR, then it will be closed as it is now.

I don't think redirecting PR to other repositories is a good idea.

  1. pull_service.ChangeTargetBranch error is not checked and merge can fail even if it's not an error

The error is being checked. Why did you decide that this was not the case?

@tulzke
Copy link
Author

tulzke commented Jan 4, 2024

@delvh in your case PR will be closed. The current behavior remained in place. If the PR has not been redirected to another branch, it will be closed.

@kvaster
Copy link
Contributor

kvaster commented Jan 4, 2024

@tulzke, seems I've missed - you're really checking if base repo is the same.

For error checking - ChangeTargetBranch emits also following errors which are not errors:

issues_model.IsErrIssueIsClosed(err)
models.IsErrPullRequestHasMerged(err)
issues_model.IsErrPullRequestAlreadyExists(err)

Also deleteBranch is called in reouter/web/repo/pull in two places:

MergePullRequest
CleanUpPullRequest

both should be covered

Also routers/api/v1/repo/pull have method MergePullRequest - this one also should be covered.

As I said before - I really don't know why we should go through this once again.

@github-actions github-actions bot added the modifies/api This PR adds API routes or modifies them label Jan 9, 2024
@tulzke
Copy link
Author

tulzke commented Jan 9, 2024

In the end, I don't care which PR is merged. I just want to see this feature in the next minor release.

I took the logic out into a separate function, and also added a call to the api.
Of the three voiced errors, it makes sense to check only one of them: issues_model.IsErrPullRequestAlreadyExists(err).

It doesn't make sense to check these two errors, because we only work with open PRS:

  • issues_model.IsErrIssueIsClosed(err)
  • models.IsErrPullRequestHasMerged(err)

@kvaster
Copy link
Contributor

kvaster commented Jan 9, 2024

@tulzke, my thoughts about issues_model.IsErrIssueIsClosed(err) and models.IsErrPullRequestHasMerged(err) were only about what should happen if someone will merge child PR concurrently.

Also what do you think about routers/api/v1/repo/pull and CleanUpPullRequest method ?

@tulzke
Copy link
Author

tulzke commented Jan 9, 2024

@kvaster

routers/api/v1/repo/pull

Yes, i added redirect to MergePullRequest.

CleanUpPullRequest

I think you're right.
I hadn't thought about this method before, because I always deleted the branch during the merge.
In this case, the implementation almost completely copies yours, except for the presence of a feature-flag.

@denyskon @delvh @a1012112796 Can we review @kvaster's proposed PR? It looks like everything is ready there.

@denyskon
Copy link
Member

Okay, I think we can agree on closing this in favor of #28686....

@denyskon denyskon closed this Jan 14, 2024
@GiteaBot GiteaBot removed this from the 1.22.0 milestone Jan 14, 2024
@tulzke
Copy link
Author

tulzke commented Jan 14, 2024

Okay, I think we can agree on closing this in favor of #28686....

Good news, thanks

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 11, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. modifies/api This PR adds API routes or modifies them size/M Denotes a PR that changes 30-99 lines, ignoring generated files. topic/pr Issues related to pull requests type/feature Completely new functionality. Can only be merged if feature freeze is not active.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Changing the destination branch of a pull request after deletion
7 participants