Skip to content

Commit 8fecb3d

Browse files
committed
fine tune
1 parent 7da099d commit 8fecb3d

File tree

2 files changed

+23
-22
lines changed

2 files changed

+23
-22
lines changed

services/pull/merge.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -547,10 +547,10 @@ var escapedSymbols = regexp.MustCompile(`([*[?! \\])`)
547547

548548
// IsUserAllowedToMerge check if user is allowed to merge PR with given permissions and branch protections
549549
func IsUserAllowedToMerge(ctx context.Context, pr *issues_model.PullRequest, p access_model.Permission, user *user_model.User) (bool, error) {
550-
return isUserAllowedToMergeRepoBranch(ctx, pr.BaseRepoID, pr.BaseBranch, p, user)
550+
return isUserAllowedToMergeInRepoBranch(ctx, pr.BaseRepoID, pr.BaseBranch, p, user)
551551
}
552552

553-
func isUserAllowedToMergeRepoBranch(ctx context.Context, repoID int64, branch string, p access_model.Permission, user *user_model.User) (bool, error) {
553+
func isUserAllowedToMergeInRepoBranch(ctx context.Context, repoID int64, branch string, p access_model.Permission, user *user_model.User) (bool, error) {
554554
if user == nil {
555555
return false, nil
556556
}

services/pull/update.go

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -121,27 +121,28 @@ func IsUserAllowedToUpdate(ctx context.Context, pull *issues_model.PullRequest,
121121
return false, false, err
122122
}
123123

124-
// 1. check base repository's AllowRebaseUpdate configuration, this should be
125-
// the setting of base repository not head repository
126-
prUnit, err := pull.BaseRepo.GetUnit(ctx, unit.TypePullRequests)
127-
if err != nil {
128-
if repo_model.IsErrUnitTypeNotExist(err) {
129-
return false, false, nil
130-
}
131-
log.Error("pr.BaseRepo.GetUnit(unit.TypePullRequests): %v", err)
132-
return false, false, err
124+
// 1. check base repository's AllowRebaseUpdate configuration
125+
// it is a config in base repo but controls the head (fork) repo's "Update" behavior
126+
prBaseUnit, err := pull.BaseRepo.GetUnit(ctx, unit.TypePullRequests)
127+
if repo_model.IsErrUnitTypeNotExist(err) {
128+
return false, false, nil // the PR unit is disabled in base repo
129+
} else if err != nil {
130+
return false, false, fmt.Errorf("get base repo unit: %v", err)
131+
} else {
132+
rebaseAllowed = prBaseUnit.PullRequestsConfig().AllowRebaseUpdate
133133
}
134-
rebaseAllowed = prUnit.PullRequestsConfig().AllowRebaseUpdate
135134

136135
// 2. check head branch protection whether rebase is allowed, if pb not found then rebase depends on the above setting
137-
pb, err := git_model.GetFirstMatchProtectedBranchRule(ctx, pull.HeadRepoID, pull.HeadBranch)
138-
if err != nil {
139-
return false, false, err
140-
}
141-
// If branch protected, disable rebase unless user is whitelisted to force push (which extends regular push)
142-
if pb != nil {
143-
pb.Repo = pull.HeadRepo
144-
rebaseAllowed = rebaseAllowed && pb.CanUserForcePush(ctx, user)
136+
{
137+
pb, err := git_model.GetFirstMatchProtectedBranchRule(ctx, pull.HeadRepoID, pull.HeadBranch)
138+
if err != nil {
139+
return false, false, err
140+
}
141+
// If branch protected, disable rebase unless user is whitelisted to force push (which extends regular push)
142+
if pb != nil {
143+
pb.Repo = pull.HeadRepo
144+
rebaseAllowed = rebaseAllowed && pb.CanUserForcePush(ctx, user)
145+
}
145146
}
146147

147148
// 3. check whether user has write access to head branch
@@ -150,15 +151,15 @@ func IsUserAllowedToUpdate(ctx context.Context, pull *issues_model.PullRequest,
150151
return false, false, err
151152
}
152153

153-
mergeAllowed, err = isUserAllowedToMergeRepoBranch(ctx, pull.HeadRepoID, pull.HeadBranch, headRepoPerm, user)
154+
mergeAllowed, err = isUserAllowedToMergeInRepoBranch(ctx, pull.HeadRepoID, pull.HeadBranch, headRepoPerm, user)
154155
if err != nil {
155156
return false, false, err
156157
}
157158

158159
// 4. if the pull creator allows maintainer to edit, it means the write permissions of the head branch has been
159160
// granted to the user with write permission of the base repository
160161
if pull.AllowMaintainerEdit {
161-
mergeAllowedMaintainer, err := isUserAllowedToMergeRepoBranch(ctx, pull.BaseRepoID, pull.BaseBranch, baseRepoPerm, user)
162+
mergeAllowedMaintainer, err := isUserAllowedToMergeInRepoBranch(ctx, pull.BaseRepoID, pull.BaseBranch, baseRepoPerm, user)
162163
if err != nil {
163164
return false, false, err
164165
}

0 commit comments

Comments
 (0)