Skip to content

Fix bug to check whether user can update pull request branch or rebase branch#36465

Open
lunny wants to merge 16 commits intogo-gitea:mainfrom
lunny:lunny/fix_perm_check
Open

Fix bug to check whether user can update pull request branch or rebase branch#36465
lunny wants to merge 16 commits intogo-gitea:mainfrom
lunny:lunny/fix_perm_check

Conversation

@lunny
Copy link
Member

@lunny lunny commented Jan 27, 2026

When checking whether a user can update a pull request branch or perform an update via rebase, a maintainer should inherit the pull request author’s permissions if Allow maintainer edits is enabled.

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Jan 27, 2026
@github-actions github-actions bot added the modifies/go Pull requests that update Go code label Jan 27, 2026
@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 Jan 27, 2026
@lunny lunny marked this pull request as draft January 27, 2026 21:20
@lunny lunny changed the title Fix bug to check whether user can merge Fix bug to check whether user can update pull request branch or rebase branch Jan 28, 2026
@lunny lunny marked this pull request as ready for review January 28, 2026 03:59
Copy link
Contributor

@wxiaoguang wxiaoguang left a comment

Choose a reason for hiding this comment

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

The test is not complete, there will be regressions.

  1. You need to assert that "user can push" before the branch protection. Otherwise, any readers use also pass the tests.
  2. pushAllowed = pushAllowed || mergeAllowedMaintainer means "allow maintainer edit" can by pass the branch protection?
    • if yes, clearly test the behavior
    • if no, fix the bug
  3. The test code just duplicates with TestAPIPullUpdateByRebase2.
    • TestAPIPullUpdateByRebase2 can be deleted, and carefully test the function in unit test

@GiteaBot GiteaBot added lgtm/blocked A maintainer has reservations with the PR and thus it cannot be merged and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Jan 29, 2026
@lunny
Copy link
Member Author

lunny commented Jan 30, 2026

The test is not complete, there will be regressions.

  1. You need to assert that "user can push" before the branch protection. Otherwise, any readers use also pass the tests.

This has been checked and deeaece added some new test for that.

  1. pushAllowed = pushAllowed || mergeAllowedMaintainer means "allow maintainer edit" can by pass the branch protection?

    • if yes, clearly test the behavior
    • if no, fix the bug

deeaece updated the logic, now the user will inherit the pull request creator's permission when he is a maintainer and allowing maintainer edit is enabled.

  1. The test code just duplicates with TestAPIPullUpdateByRebase2.

    • TestAPIPullUpdateByRebase2 can be deleted, and carefully test the function in unit test

Removed.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR updates the pull request “update branch” permission logic so that, when Allow maintainer edits is enabled, a maintainer can update/rebase the PR branch by inheriting the PR author’s permissions (while still honoring repo config and branch protections).

Changes:

  • Refactors IsUserAllowedToUpdate to evaluate push/force-push permissions against head-branch protections and add “inherit poster permissions” behavior for maintainers.
  • Adds service-level unit tests for IsUserAllowedToUpdate (protected branches, repo config disabling rebase updates, read-only access, maintainer-edit inheritance).
  • Removes an integration test case that previously covered additional rebase-update scenarios.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
tests/integration/pull_update_test.go Removes a rebase-update integration test case.
services/pull/update.go Refactors PR update permission checks; adds maintainer-edit inheritance via poster permissions.
services/pull/update_test.go Introduces new unit tests for update permission logic and maintainer-edit behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +161 to +165
rebaseAllowed = rebaseAllowed && prBaseUnit.PullRequestsConfig().AllowRebaseUpdate

// 4. if the pull creator allows maintainer to edit, it means the write permissions of the head branch has been
// granted to the user with write permission of the base repository
if pull.AllowMaintainerEdit {
mergeAllowedMaintainer, err := isUserAllowedToMergeInRepoBranch(ctx, pull.BaseRepoID, pull.BaseBranch, baseRepoPerm, user)
// 3. if the pull creator allows maintainer to edit, we needs to check whether
// user is a maintainer and inherit pull request creator's permission
if pull.AllowMaintainerEdit && (!pushAllowed || !rebaseAllowed) {
Copy link

Copilot AI Feb 10, 2026

Choose a reason for hiding this comment

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

AllowRebaseUpdate is applied before the AllowMaintainerEdit inheritance logic, but later the inheritance block can set rebaseAllowed back to true (from the poster’s force-push permission), effectively bypassing the base repo’s AllowRebaseUpdate=false setting. Consider storing the base setting in a local variable and applying it after any inheritance, or AND-ing it into the inherited value as well.

Copilot uses AI. Check for mistakes.
// granted to the user with write permission of the base repository
if pull.AllowMaintainerEdit {
mergeAllowedMaintainer, err := isUserAllowedToMergeInRepoBranch(ctx, pull.BaseRepoID, pull.BaseBranch, baseRepoPerm, user)
// 3. if the pull creator allows maintainer to edit, we needs to check whether
Copy link

Copilot AI Feb 10, 2026

Choose a reason for hiding this comment

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

Typo/grammar: "we needs" should be "we need".

Suggested change
// 3. if the pull creator allows maintainer to edit, we needs to check whether
// 3. if the pull creator allows maintainer to edit, we need to check whether

Copilot uses AI. Check for mistakes.
Comment on lines +103 to +105
// isUserAllowedToPushOrForcePushInRepoBranch checks whether user is allowed to push or force push in the given repo and branch
// it will check both user permission and branch protection rules
func isUserAllowedToPushOrForcePushInRepoBranch(ctx context.Context, user *user_model.User, repo *repo_model.Repository, branch string) (pushAllowed, rebaseAllowed bool, err error) {
Copy link

Copilot AI Feb 10, 2026

Choose a reason for hiding this comment

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

The helper name/comment describe “push or force push”, but the second return value is named rebaseAllowed. Since this value is actually “force-push allowed” (used to decide rebase updates), consider renaming the return value (and related locals) to forcePushAllowed to avoid semantic confusion.

Copilot uses AI. Check for mistakes.
Comment on lines +130 to +138
func TestIsUserAllowedToUpdateMaintainerEditInheritsPosterPermissions(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())

pr := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 3})
pr.AllowMaintainerEdit = true
assert.NoError(t, pr.LoadHeadRepo(t.Context()))
assert.NoError(t, pr.LoadIssue(t.Context()))
assert.NoError(t, pr.Issue.LoadPoster(t.Context()))

Copy link

Copilot AI Feb 10, 2026

Choose a reason for hiding this comment

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

The maintainer-edit inheritance behavior isn’t currently tested together with AllowRebaseUpdate=false. Adding a test case where AllowMaintainerEdit=true, the user is a maintainer, the poster can force-push, but the base repo disables rebase updates would prevent regressions (and would catch cases where inheritance accidentally bypasses the config).

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport/v1.25 lgtm/blocked A maintainer has reservations with the PR and thus it cannot be merged modifies/go Pull requests that update Go code type/bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants