mirror of
https://github.com/go-gitea/gitea.git
synced 2026-12-04 15:54:57 +00:00
perf: reuse the doer's permission on pull request pages (#40532)
Pull request pages recomputed the doer's repository permission up to five times per request. Reuse `ctx.Repo.Permission` and cache lookups on other repositories, so forks are resolved once per request. | Merge box | Queries | MariaDB | +0.5 ms DB latency | |---|---|---|---| | Open PR | 40 → 32 | 57 → 56 ms | 125 → 118 ms | | Closed PR | 37 → 24 | 51 → 43 ms | 109 → 89 ms | | Fork PR | 52 → 39 | 78 → 69 ms | 159 → 146 ms | Assisted-by: Claude Code:claude-opus-5-5
This commit is contained in:
1 parent
489e3b9395
commit
5b212a1f36
14 files changed
+116
-201
No files matched your search
+5
-20
@@ -913,36 +913,21 @@ func MarkConversation(ctx context.Context, comment *Comment, doer *user_model.Us
|
||||
|
||||
// CanMarkConversation Add or remove Conversation mark for a code comment permission check
|
||||
// the PR writer , official reviewer and poster can do it
|
||||
func CanMarkConversation(ctx context.Context, issue *Issue, doer *user_model.User) (permResult bool, err error) {
|
||||
func CanMarkConversation(ctx context.Context, issue *Issue, doer *user_model.User, doerPerm access_model.Permission) (bool, error) {
|
||||
if doer == nil {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
if err = issue.LoadRepo(ctx); err != nil {
|
||||
if err := issue.LoadRepo(ctx); err != nil {
|
||||
return false, err
|
||||
}
|
||||
if issue.Repo.IsArchived {
|
||||
return false, nil
|
||||
}
|
||||
if doer.ID != issue.PosterID {
|
||||
p, err := access_model.GetDoerRepoPermission(ctx, issue.Repo, doer)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
permResult = p.CanAccess(perm.AccessModeWrite, unit.TypePullRequests)
|
||||
if !permResult {
|
||||
if permResult, err = IsOfficialReviewer(ctx, issue, doer); err != nil {
|
||||
return false, err
|
||||
}
|
||||
}
|
||||
|
||||
if !permResult {
|
||||
return false, nil
|
||||
}
|
||||
if doer.ID == issue.PosterID || doerPerm.CanWrite(unit.TypePullRequests) {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
return true, nil
|
||||
return IsOfficialReviewer(ctx, issue, doer)
|
||||
}
|
||||
|
||||
// DeleteReview delete a review and it's code comments
|
||||
|
||||
@@ -76,6 +76,8 @@ import (
|
||||
repo_model "gitea.dev/models/repo"
|
||||
"gitea.dev/models/unit"
|
||||
user_model "gitea.dev/models/user"
|
||||
"gitea.dev/modules/cache"
|
||||
"gitea.dev/modules/cachegroup"
|
||||
"gitea.dev/modules/httplib"
|
||||
"gitea.dev/modules/log"
|
||||
"gitea.dev/modules/setting"
|
||||
@@ -213,6 +215,9 @@ func repoAssignment() func(ctx *context.APIContext) {
|
||||
return
|
||||
}
|
||||
}
|
||||
if contextCache := cache.GetContextCache(ctx); contextCache != nil {
|
||||
contextCache.Put(cachegroup.RepoUserPermission, access_model.RepoUserPermissionCacheKey(repo.ID, ctx.Doer), ctx.Repo.Permission)
|
||||
}
|
||||
}
|
||||
|
||||
if !ctx.Repo.Permission.HasAnyUnitAccessOrPublicAccess() {
|
||||
|
||||
@@ -142,7 +142,7 @@ func ListPullRequests(ctx *context.APIContext) {
|
||||
return
|
||||
}
|
||||
|
||||
apiPrs, err := convert.ToAPIPullRequests(ctx, ctx.Repo.Repository, prs, ctx.Doer)
|
||||
apiPrs, err := convert.ToAPIPullRequests(ctx, ctx.Repo.Repository, ctx.Repo.Permission, prs, ctx.Doer)
|
||||
if err != nil {
|
||||
ctx.APIErrorInternal(err)
|
||||
return
|
||||
@@ -1185,15 +1185,12 @@ func UpdatePullRequest(ctx *context.APIContext) {
|
||||
return
|
||||
}
|
||||
|
||||
if err = pr.LoadBaseRepo(ctx); err != nil {
|
||||
ctx.APIErrorInternal(err)
|
||||
return
|
||||
}
|
||||
pr.BaseRepo = ctx.Repo.Repository
|
||||
// keep API back-compat: when no style is given, default to "merge" rather than the repo's DefaultUpdateStyle,
|
||||
// so existing API clients keep getting a merge update.
|
||||
rebase := repo_model.UpdateStyle(ctx.FormString("style", string(repo_model.UpdateStyleMerge))) == repo_model.UpdateStyleRebase
|
||||
|
||||
userUpdateStyles, err := pull_service.CheckUserAllowedToUpdate(ctx, pr, ctx.Doer)
|
||||
userUpdateStyles, err := pull_service.CheckUserAllowedToUpdate(ctx, pr, ctx.Doer, ctx.Repo.Permission)
|
||||
if err != nil {
|
||||
ctx.APIErrorInternal(err)
|
||||
return
|
||||
|
||||
@@ -357,7 +357,7 @@ func updatePullReviewCommentResolve(ctx *context.APIContext, isResolve bool) {
|
||||
return
|
||||
}
|
||||
|
||||
canMarkConv, err := issues_model.CanMarkConversation(ctx, comment.Issue, ctx.Doer)
|
||||
canMarkConv, err := issues_model.CanMarkConversation(ctx, comment.Issue, ctx.Doer, ctx.Repo.Permission)
|
||||
if err != nil {
|
||||
ctx.APIErrorInternal(err)
|
||||
return
|
||||
|
||||
@@ -5,7 +5,6 @@ package repo
|
||||
|
||||
import (
|
||||
git_model "gitea.dev/models/git"
|
||||
access_model "gitea.dev/models/perm/access"
|
||||
unit_model "gitea.dev/models/unit"
|
||||
"gitea.dev/modules/log"
|
||||
"gitea.dev/services/context"
|
||||
@@ -20,10 +19,6 @@ func prepareRecentlyPushedNewBranches(ctx *context.Context) {
|
||||
if ctx.Doer == nil {
|
||||
return
|
||||
}
|
||||
if err := ctx.Repo.Repository.GetBaseRepo(ctx); err != nil {
|
||||
log.Error("GetBaseRepo: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
opts := git_model.FindRecentlyPushedNewBranchesOptions{
|
||||
Repo: ctx.Repo.Repository,
|
||||
@@ -33,7 +28,7 @@ func prepareRecentlyPushedNewBranches(ctx *context.Context) {
|
||||
opts.BaseRepo = ctx.Repo.Repository.BaseRepo
|
||||
}
|
||||
|
||||
baseRepoPerm, err := access_model.GetDoerRepoPermission(ctx, opts.BaseRepo, ctx.Doer)
|
||||
baseRepoPerm, err := ctx.DoerRepoPermission(opts.BaseRepo)
|
||||
if err != nil {
|
||||
log.Error("GetDoerRepoPermission: %v", err)
|
||||
return
|
||||
|
||||
@@ -181,7 +181,7 @@ func (cpi *comparePageInfoType) parseCompareInfo(ctx *context.Context, comparePa
|
||||
isSameRepo := baseRepo.ID == headRepo.ID
|
||||
if !isSameRepo {
|
||||
// Assert ctx.Doer has permission to read headRepo's codes
|
||||
permHead, err := access_model.GetDoerRepoPermission(ctx, headRepo, ctx.Doer)
|
||||
permHead, err := ctx.DoerRepoPermission(headRepo)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -119,24 +119,11 @@ func roleDescriptor(ctx *context.Context, repo *repo_model.Repository, poster *u
|
||||
|
||||
// checkBlockedByIssues return canRead and notPermitted
|
||||
func checkBlockedByIssues(ctx *context.Context, blockers []*issues_model.DependencyInfo) (canRead, notPermitted []*issues_model.DependencyInfo) {
|
||||
repoPerms := make(map[int64]access_model.Permission)
|
||||
repoPerms[ctx.Repo.Repository.ID] = ctx.Repo.Permission
|
||||
for _, blocker := range blockers {
|
||||
// Get the permissions for this repository
|
||||
// If the repo ID exists in the map, return the exist permissions
|
||||
// else get the permission and add it to the map
|
||||
var perm access_model.Permission
|
||||
existPerm, ok := repoPerms[blocker.RepoID]
|
||||
if ok {
|
||||
perm = existPerm
|
||||
} else {
|
||||
var err error
|
||||
perm, err = access_model.GetDoerRepoPermission(ctx, &blocker.Repository, ctx.Doer)
|
||||
if err != nil {
|
||||
ctx.ServerErrorAuto(err)
|
||||
return nil, nil
|
||||
}
|
||||
repoPerms[blocker.RepoID] = perm
|
||||
perm, err := ctx.DoerRepoPermission(&blocker.Repository)
|
||||
if err != nil {
|
||||
ctx.ServerErrorAuto(err)
|
||||
return nil, nil
|
||||
}
|
||||
if perm.CanReadIssuesOrPulls(blocker.Issue.IsPull) {
|
||||
canRead = append(canRead, blocker)
|
||||
@@ -835,10 +822,8 @@ func (prInfo *pullRequestViewInfo) prepareMergeBox(ctx *context.Context, issue *
|
||||
pull_service.StartPullRequestCheckOnView(ctx, pull)
|
||||
|
||||
if ctx.IsSigned {
|
||||
if err := pull.LoadHeadRepo(ctx); err != nil {
|
||||
log.Error("LoadHeadRepo: %v", err)
|
||||
} else if pull.HeadRepo != nil && issue.IsClosed {
|
||||
perm, err := access_model.GetDoerRepoPermission(ctx, pull.HeadRepo, ctx.Doer)
|
||||
if pull.HeadRepo != nil && issue.IsClosed {
|
||||
perm, err := ctx.DoerRepoPermission(pull.HeadRepo)
|
||||
if err != nil {
|
||||
ctx.ServerErrorAuto(err)
|
||||
return
|
||||
@@ -865,24 +850,17 @@ func (prInfo *pullRequestViewInfo) prepareMergeBox(ctx *context.Context, issue *
|
||||
}
|
||||
}
|
||||
|
||||
if err := pull.LoadBaseRepo(ctx); err != nil {
|
||||
log.Error("LoadBaseRepo: %v", err)
|
||||
}
|
||||
perm, err := access_model.GetDoerRepoPermission(ctx, pull.BaseRepo, ctx.Doer)
|
||||
var err error
|
||||
data.hasPermToMerge, err = pull_service.IsUserAllowedToMerge(ctx, pull, ctx.Repo.Permission, ctx.Doer)
|
||||
if err != nil {
|
||||
ctx.ServerErrorAuto(err)
|
||||
return
|
||||
}
|
||||
data.hasPermToMerge, err = pull_service.IsUserAllowedToMerge(ctx, pull, perm, ctx.Doer)
|
||||
if err != nil {
|
||||
ctx.ServerErrorAuto(err)
|
||||
return
|
||||
}
|
||||
if !data.hasPermToMerge && perm.CanWrite(unit.TypeCode) {
|
||||
if !data.hasPermToMerge && ctx.Repo.Permission.CanWrite(unit.TypeCode) {
|
||||
data.mergeBlockers = append(data.mergeBlockers, ctx.Locale.Tr("repo.pulls.no_merge_access"))
|
||||
}
|
||||
|
||||
if ctx.Data["CanMarkConversation"], err = issues_model.CanMarkConversation(ctx, issue, ctx.Doer); err != nil {
|
||||
if ctx.Data["CanMarkConversation"], err = issues_model.CanMarkConversation(ctx, issue, ctx.Doer, ctx.Repo.Permission); err != nil {
|
||||
ctx.ServerErrorAuto(err)
|
||||
return
|
||||
}
|
||||
@@ -971,7 +949,7 @@ func (prInfo *pullRequestViewInfo) prepareMergeBox(ctx *context.Context, issue *
|
||||
func (prInfo *pullRequestViewInfo) preparePullUpdateActions(ctx *context.Context) {
|
||||
pull := prInfo.issue.PullRequest
|
||||
data := prInfo.MergeBoxData
|
||||
userUpdateStyles, err := pull_service.CheckUserAllowedToUpdate(ctx, pull, ctx.Doer)
|
||||
userUpdateStyles, err := pull_service.CheckUserAllowedToUpdate(ctx, pull, ctx.Doer, ctx.Repo.Permission)
|
||||
if err != nil {
|
||||
ctx.ServerErrorAuto(err)
|
||||
return
|
||||
|
||||
+11
-30
@@ -844,7 +844,7 @@ func viewPullFiles(ctx *context.Context, beforeCommitID, afterCommitID string) {
|
||||
ctx.Data["DiffNotAvailable"] = diffShortStat.NumFiles == 0
|
||||
ctx.Data["ShowDiffSummaryInToolbar"] = diffShortStat.NumFiles != 0
|
||||
|
||||
if ctx.Data["CanMarkConversation"], err = issues_model.CanMarkConversation(ctx, issue, ctx.Doer); err != nil {
|
||||
if ctx.Data["CanMarkConversation"], err = issues_model.CanMarkConversation(ctx, issue, ctx.Doer, ctx.Repo.Permission); err != nil {
|
||||
ctx.ServerErrorAuto(err)
|
||||
return
|
||||
}
|
||||
@@ -896,28 +896,18 @@ func viewPullFiles(ctx *context.Context, beforeCommitID, afterCommitID string) {
|
||||
ctx.Data["CanBlockUser"] = func(blocker, blockee *user_model.User) bool {
|
||||
return user_service.CanBlockUser(ctx, ctx.Doer, blocker, blockee)
|
||||
}
|
||||
if isShowAllCommits && pull.Flow == issues_model.PullRequestFlowGithub {
|
||||
if err := pull.LoadHeadRepo(ctx); err != nil {
|
||||
if isShowAllCommits && pull.Flow == issues_model.PullRequestFlowGithub && pull.HeadRepo != nil && !pull.HasMerged && ctx.Doer != nil {
|
||||
headPerm, err := ctx.DoerRepoPermission(pull.HeadRepo)
|
||||
if err != nil {
|
||||
ctx.ServerErrorAuto(err)
|
||||
return
|
||||
}
|
||||
|
||||
if pull.HeadRepo != nil {
|
||||
if !pull.HasMerged && ctx.Doer != nil {
|
||||
headPerm, err := access_model.GetDoerRepoPermission(ctx, pull.HeadRepo, ctx.Doer)
|
||||
if err != nil {
|
||||
ctx.ServerErrorAuto(err)
|
||||
return
|
||||
}
|
||||
|
||||
if issues_model.CanMaintainerWriteToBranch(ctx, headPerm, pull.HeadBranch, ctx.Doer) {
|
||||
ctx.Data["CanEditFile"] = true
|
||||
ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.edit_this_file")
|
||||
ctx.Data["HeadRepoLink"] = pull.HeadRepo.Link()
|
||||
ctx.Data["HeadBranchName"] = pull.HeadBranch
|
||||
ctx.Data["BackToLink"] = setting.AppSubURL + ctx.Req.URL.RequestURI()
|
||||
}
|
||||
}
|
||||
if issues_model.CanMaintainerWriteToBranch(ctx, headPerm, pull.HeadBranch, ctx.Doer) {
|
||||
ctx.Data["CanEditFile"] = true
|
||||
ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.edit_this_file")
|
||||
ctx.Data["HeadRepoLink"] = pull.HeadRepo.Link()
|
||||
ctx.Data["HeadBranchName"] = pull.HeadBranch
|
||||
ctx.Data["BackToLink"] = setting.AppSubURL + ctx.Req.URL.RequestURI()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -953,16 +943,7 @@ func UpdatePullRequest(ctx *context.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if err := issue.PullRequest.LoadBaseRepo(ctx); err != nil {
|
||||
ctx.ServerErrorAuto(err)
|
||||
return
|
||||
}
|
||||
if err := issue.PullRequest.LoadHeadRepo(ctx); err != nil {
|
||||
ctx.ServerErrorAuto(err)
|
||||
return
|
||||
}
|
||||
|
||||
userUpdateStyles, err := pull_service.CheckUserAllowedToUpdate(ctx, issue.PullRequest, ctx.Doer)
|
||||
userUpdateStyles, err := pull_service.CheckUserAllowedToUpdate(ctx, issue.PullRequest, ctx.Doer, ctx.Repo.Permission)
|
||||
if err != nil {
|
||||
ctx.ServerErrorAuto(err)
|
||||
return
|
||||
|
||||
@@ -143,7 +143,7 @@ func UpdateResolveConversation(ctx *context.Context) {
|
||||
}
|
||||
|
||||
var permResult bool
|
||||
if permResult, err = issues_model.CanMarkConversation(ctx, comment.Issue, ctx.Doer); err != nil {
|
||||
if permResult, err = issues_model.CanMarkConversation(ctx, comment.Issue, ctx.Doer, ctx.Repo.Permission); err != nil {
|
||||
ctx.ServerErrorAuto(err)
|
||||
return
|
||||
}
|
||||
@@ -195,7 +195,7 @@ func renderConversation(ctx *context.Context, comment *issues_model.Comment, ori
|
||||
upload.AddUploadContext(ctx, "comment")
|
||||
|
||||
ctx.Data["comments"] = comments
|
||||
if ctx.Data["CanMarkConversation"], err = issues_model.CanMarkConversation(ctx, comment.Issue, ctx.Doer); err != nil {
|
||||
if ctx.Data["CanMarkConversation"], err = issues_model.CanMarkConversation(ctx, comment.Issue, ctx.Doer, ctx.Repo.Permission); err != nil {
|
||||
ctx.ServerErrorAuto(err)
|
||||
return
|
||||
}
|
||||
|
||||
+16
-14
@@ -53,6 +53,14 @@ func (prc *PullRequestContext) SameRepo() bool {
|
||||
return prc.baseRepo != nil && prc.headRepo != nil && prc.baseRepo.ID == prc.headRepo.ID
|
||||
}
|
||||
|
||||
func (ctx *Context) DoerRepoPermission(repo *repo_model.Repository) (access_model.Permission, error) {
|
||||
return cache.GetWithContextCache(ctx, cachegroup.RepoUserPermission, access_model.RepoUserPermissionCacheKey(repo.ID, ctx.Doer),
|
||||
func(stdCtx context.Context, _key string) (access_model.Permission, error) {
|
||||
return access_model.GetDoerRepoPermission(stdCtx, repo, ctx.Doer)
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
func (prc *PullRequestContext) CanCreateNewPull() bool {
|
||||
if prc.canCreateNewPull != nil {
|
||||
return *prc.canCreateNewPull
|
||||
@@ -60,21 +68,15 @@ func (prc *PullRequestContext) CanCreateNewPull() bool {
|
||||
ctx := prc.ctx
|
||||
can := ctx.IsSigned && prc.baseRepo.CanContentChange()
|
||||
if can {
|
||||
basePerm, headPerm := ctx.Repo.Permission, ctx.Repo.Permission
|
||||
var err error
|
||||
if prc.baseRepo.ID != ctx.Repo.Repository.ID {
|
||||
basePerm, err = access_model.GetDoerRepoPermission(ctx, prc.baseRepo, ctx.Doer)
|
||||
if err != nil {
|
||||
log.Error("CanCreateNewPull: %v", err)
|
||||
return false
|
||||
}
|
||||
basePerm, err := ctx.DoerRepoPermission(prc.baseRepo)
|
||||
if err != nil {
|
||||
log.Error("CanCreateNewPull: %v", err)
|
||||
return false
|
||||
}
|
||||
if prc.headRepo.ID != ctx.Repo.Repository.ID {
|
||||
headPerm, err = access_model.GetDoerRepoPermission(ctx, prc.headRepo, ctx.Doer)
|
||||
if err != nil {
|
||||
log.Error("CanCreateNewPull: %v", err)
|
||||
return false
|
||||
}
|
||||
headPerm, err := ctx.DoerRepoPermission(prc.headRepo)
|
||||
if err != nil {
|
||||
log.Error("CanCreateNewPull: %v", err)
|
||||
return false
|
||||
}
|
||||
restriction := prc.baseRepo.MustGetUnit(ctx, unit_model.TypePullRequests).PullRequestsConfig().CreationRestriction
|
||||
can = issue.CheckCreationRestriction(basePerm, unit_model.TypePullRequests, restriction) == nil &&
|
||||
|
||||
+25
-31
@@ -35,6 +35,19 @@ func toAPIAutoMerge(ctx context.Context, autoMerge *pull_model.AutoMerge) *api.P
|
||||
}
|
||||
}
|
||||
|
||||
func doerRepoPermission(ctx context.Context, repo *repo_model.Repository, doer *user_model.User) access_model.Permission {
|
||||
repoPerm, err := cache.GetWithContextCache(ctx, cachegroup.RepoUserPermission, access_model.RepoUserPermissionCacheKey(repo.ID, doer),
|
||||
func(ctx context.Context, _key string) (access_model.Permission, error) {
|
||||
return access_model.GetDoerRepoPermission(ctx, repo, doer)
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
log.Error("GetDoerRepoPermission[%d]: %v", repo.ID, err)
|
||||
repoPerm.AccessMode = perm.AccessModeNone
|
||||
}
|
||||
return repoPerm
|
||||
}
|
||||
|
||||
// ToAPIPullRequest hides a head repository the doer cannot see
|
||||
func ToAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *user_model.User) *api.PullRequest {
|
||||
return toAPIPullRequest(ctx, pr, doer, false)
|
||||
@@ -77,15 +90,7 @@ func toAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *u
|
||||
return nil
|
||||
}
|
||||
|
||||
repoUserPerm, err := cache.GetWithContextCache(ctx, cachegroup.RepoUserPermission, access_model.RepoUserPermissionCacheKey(pr.BaseRepoID, doer),
|
||||
func(ctx context.Context, _ string) (access_model.Permission, error) {
|
||||
return access_model.GetDoerRepoPermission(ctx, pr.BaseRepo, doer)
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
log.Error("GetDoerRepoPermission[%d]: %v", pr.BaseRepoID, err)
|
||||
repoUserPerm.AccessMode = perm.AccessModeNone
|
||||
}
|
||||
repoUserPerm := doerRepoPermission(ctx, pr.BaseRepo, doer)
|
||||
|
||||
apiPullRequest := &api.PullRequest{
|
||||
ID: pr.ID,
|
||||
@@ -198,26 +203,26 @@ func toAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *u
|
||||
}
|
||||
|
||||
if pr.HeadRepo != nil && pr.Flow == issues_model.PullRequestFlowGithub {
|
||||
p, err := access_model.GetDoerRepoPermission(ctx, pr.HeadRepo, doer)
|
||||
if err != nil {
|
||||
log.Error("GetDoerRepoPermission[%d]: %v", pr.HeadRepoID, err)
|
||||
p.AccessMode = perm.AccessModeNone
|
||||
var headRepoPerm access_model.Permission
|
||||
if pr.IsFromFork() {
|
||||
headRepoPerm = doerRepoPermission(ctx, pr.HeadRepo, doer)
|
||||
}
|
||||
|
||||
// a hidden head repository is read from the base repository's PR ref
|
||||
headGitRepo := gitRepo
|
||||
exist = false
|
||||
if isPayload || pr.HeadRepoID == pr.BaseRepoID || p.HasAnyUnitAccessOrPublicAccess() {
|
||||
if isPayload || pr.IsSameRepo() || headRepoPerm.HasAnyUnitAccessOrPublicAccess() {
|
||||
apiPullRequest.Head.RepoID = pr.HeadRepo.ID
|
||||
apiPullRequest.Head.Repository = ToRepo(ctx, pr.HeadRepo, p)
|
||||
|
||||
if pr.HeadRepoID != pr.BaseRepoID { // not request-cached, a lingering cat-file would block deleting the fork on Windows
|
||||
if pr.IsFromFork() { // not request-cached, a lingering cat-file would block deleting the fork on Windows
|
||||
apiPullRequest.Head.Repository = ToRepo(ctx, pr.HeadRepo, headRepoPerm)
|
||||
headGitRepo, err = git.OpenRepository(ctx, pr.HeadRepo)
|
||||
if err != nil {
|
||||
log.Error("OpenRepository[%s]: %v", pr.HeadRepo.FullName(), err)
|
||||
return nil
|
||||
}
|
||||
defer headGitRepo.Close()
|
||||
} else {
|
||||
apiPullRequest.Head.Repository = apiPullRequest.Base.Repository
|
||||
}
|
||||
|
||||
exist, err = git_model.IsBranchExist(ctx, pr.HeadRepoID, pr.HeadBranch)
|
||||
@@ -296,7 +301,7 @@ func toAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *u
|
||||
return apiPullRequest
|
||||
}
|
||||
|
||||
func ToAPIPullRequests(ctx context.Context, baseRepo *repo_model.Repository, prs issues_model.PullRequestList, doer *user_model.User) ([]*api.PullRequest, error) {
|
||||
func ToAPIPullRequests(ctx context.Context, baseRepo *repo_model.Repository, baseRepoPerm access_model.Permission, prs issues_model.PullRequestList, doer *user_model.User) ([]*api.PullRequest, error) {
|
||||
for _, pr := range prs {
|
||||
pr.BaseRepo = baseRepo
|
||||
if pr.BaseRepoID == pr.HeadRepoID {
|
||||
@@ -358,12 +363,6 @@ func ToAPIPullRequests(ctx context.Context, baseRepo *repo_model.Repository, prs
|
||||
}
|
||||
defer closer.Close()
|
||||
|
||||
baseRepoPerm, err := access_model.GetDoerRepoPermission(ctx, baseRepo, doer)
|
||||
if err != nil {
|
||||
log.Error("GetDoerRepoPermission[%d]: %v", baseRepo.ID, err)
|
||||
baseRepoPerm.AccessMode = perm.AccessModeNone
|
||||
}
|
||||
|
||||
apiRepo := ToRepo(ctx, baseRepo, baseRepoPerm)
|
||||
|
||||
autoMerges, err := pull_model.GetScheduledMergeByPullIDs(ctx, container.FilterSlice(prs, func(pr *issues_model.PullRequest) (int64, bool) {
|
||||
@@ -458,13 +457,8 @@ func ToAPIPullRequests(ctx context.Context, baseRepo *repo_model.Repository, prs
|
||||
|
||||
// pull request head branch, both repository and branch could not exist
|
||||
if pr.HeadRepo != nil && pr.HeadRepoID != pr.BaseRepoID {
|
||||
p, err := access_model.GetDoerRepoPermission(ctx, pr.HeadRepo, doer)
|
||||
if err != nil {
|
||||
log.Error("GetDoerRepoPermission[%d]: %v", pr.HeadRepoID, err)
|
||||
p.AccessMode = perm.AccessModeNone
|
||||
}
|
||||
if p.HasAnyUnitAccessOrPublicAccess() {
|
||||
apiPullRequest.Head.Repository = ToRepo(ctx, pr.HeadRepo, p)
|
||||
if headRepoPerm := doerRepoPermission(ctx, pr.HeadRepo, doer); headRepoPerm.HasAnyUnitAccessOrPublicAccess() {
|
||||
apiPullRequest.Head.Repository = ToRepo(ctx, pr.HeadRepo, headRepoPerm)
|
||||
}
|
||||
}
|
||||
if apiPullRequest.Head.Repository != nil {
|
||||
|
||||
@@ -47,7 +47,7 @@ func TestPullRequest_APIFormat(t *testing.T) {
|
||||
assert.Nil(t, apiPullRequest.Head.Repository)
|
||||
assert.EqualValues(t, -1, apiPullRequest.Head.RepoID)
|
||||
|
||||
apiPullRequests, err := ToAPIPullRequests(t.Context(), pr.BaseRepo, []*issues_model.PullRequest{pr}, nil)
|
||||
apiPullRequests, err := ToAPIPullRequests(t.Context(), pr.BaseRepo, access_model.PermissionNoAccess(), []*issues_model.PullRequest{pr}, nil)
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, apiPullRequests, 1)
|
||||
assert.NotNil(t, apiPullRequests[0])
|
||||
@@ -64,7 +64,7 @@ func TestPullRequest_APIFormat(t *testing.T) {
|
||||
CommitMessage: "the body",
|
||||
}
|
||||
assert.Equal(t, expected, ToAPIPullRequest(t.Context(), pr, nil).AutoMerge)
|
||||
apiPullRequests, err = ToAPIPullRequests(t.Context(), pr.BaseRepo, []*issues_model.PullRequest{pr}, nil)
|
||||
apiPullRequests, err = ToAPIPullRequests(t.Context(), pr.BaseRepo, access_model.PermissionNoAccess(), []*issues_model.PullRequest{pr}, nil)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expected, apiPullRequests[0].AutoMerge)
|
||||
}
|
||||
+16
-33
@@ -103,38 +103,17 @@ func Update(pr *issues_model.PullRequest, doer *user_model.User, message string,
|
||||
|
||||
// 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, forcePushAllowed bool, err error) {
|
||||
if user == nil {
|
||||
return false, false, nil
|
||||
func isUserAllowedToPushOrForcePushInRepoBranch(ctx context.Context, user *user_model.User, repoPerm access_model.Permission, pb *git_model.ProtectedBranch) (pushAllowed, forcePushAllowed bool) {
|
||||
if pb == nil {
|
||||
return repoPerm.CanWrite(unit.TypeCode), repoPerm.CanWrite(unit.TypeCode)
|
||||
} else {
|
||||
return pb.CanUserPush(ctx, user, repoPerm), pb.CanUserForcePush(ctx, user, repoPerm)
|
||||
}
|
||||
|
||||
// 1. check user push permission on the given repository
|
||||
repoPerm, err := access_model.GetDoerRepoPermission(ctx, repo, user)
|
||||
if err != nil {
|
||||
if repo_model.IsErrUnitTypeNotExist(err) {
|
||||
return false, false, nil
|
||||
}
|
||||
return false, false, err
|
||||
}
|
||||
pushAllowed = repoPerm.CanWrite(unit.TypeCode)
|
||||
forcePushAllowed = pushAllowed
|
||||
|
||||
// 2. check branch protection whether user can push or force push
|
||||
pb, err := git_model.GetFirstMatchProtectedBranchRule(ctx, repo.ID, branch)
|
||||
if err != nil {
|
||||
return false, false, err
|
||||
}
|
||||
if pb != nil { // override previous results if there is a branch protection rule
|
||||
pb.Repo = repo
|
||||
pushAllowed = pb.CanUserPush(ctx, user, repoPerm)
|
||||
forcePushAllowed = pb.CanUserForcePush(ctx, user, repoPerm)
|
||||
}
|
||||
return pushAllowed, forcePushAllowed, nil
|
||||
}
|
||||
|
||||
// CheckUserAllowedToUpdate check if user is allowed to update PR with given permissions and branch protections
|
||||
// update PR means send new commits to PR head branch from base branch
|
||||
func CheckUserAllowedToUpdate(ctx context.Context, pull *issues_model.PullRequest, user *user_model.User) (ret struct {
|
||||
func CheckUserAllowedToUpdate(ctx context.Context, pull *issues_model.PullRequest, user *user_model.User, baseRepoPerm access_model.Permission) (ret struct {
|
||||
MergeAllowed, RebaseAllowed bool
|
||||
DefaultUpdateStyle repo_model.UpdateStyle
|
||||
}, err error,
|
||||
@@ -166,18 +145,21 @@ func CheckUserAllowedToUpdate(ctx context.Context, pull *issues_model.PullReques
|
||||
}
|
||||
|
||||
// 3. check user push permission on head repository
|
||||
ret.MergeAllowed, ret.RebaseAllowed, err = isUserAllowedToPushOrForcePushInRepoBranch(ctx, user, pull.HeadRepo, pull.HeadBranch)
|
||||
headRepoPerm := baseRepoPerm
|
||||
if pull.IsFromFork() {
|
||||
if headRepoPerm, err = access_model.GetDoerRepoPermission(ctx, pull.HeadRepo, user); err != nil {
|
||||
return ret, err
|
||||
}
|
||||
}
|
||||
headBranchRule, err := git_model.GetFirstMatchProtectedBranchRule(ctx, pull.HeadRepoID, pull.HeadBranch)
|
||||
if err != nil {
|
||||
return ret, err
|
||||
}
|
||||
ret.MergeAllowed, ret.RebaseAllowed = isUserAllowedToPushOrForcePushInRepoBranch(ctx, user, headRepoPerm, headBranchRule)
|
||||
|
||||
// 4. if the pull creator allows maintainer to edit, we need to check whether
|
||||
// user is a maintainer (has permission to merge into base branch) and inherit pull request poster's permission
|
||||
if pull.AllowMaintainerEdit && (!ret.MergeAllowed || !ret.RebaseAllowed) {
|
||||
baseRepoPerm, err := access_model.GetDoerRepoPermission(ctx, pull.BaseRepo, user)
|
||||
if err != nil {
|
||||
return ret, err
|
||||
}
|
||||
userAllowedToMergePR, err := isUserAllowedToMergeInRepoBranch(ctx, pull.BaseRepoID, pull.BaseBranch, baseRepoPerm, user)
|
||||
if err != nil {
|
||||
return ret, err
|
||||
@@ -191,10 +173,11 @@ func CheckUserAllowedToUpdate(ctx context.Context, pull *issues_model.PullReques
|
||||
if err := pull.Issue.LoadPoster(ctx); err != nil {
|
||||
return ret, err
|
||||
}
|
||||
posterPushAllowed, posterRebaseAllowed, err := isUserAllowedToPushOrForcePushInRepoBranch(ctx, pull.Issue.Poster, pull.HeadRepo, pull.HeadBranch)
|
||||
posterHeadRepoPerm, err := access_model.GetDoerRepoPermission(ctx, pull.HeadRepo, pull.Issue.Poster)
|
||||
if err != nil {
|
||||
return ret, err
|
||||
}
|
||||
posterPushAllowed, posterRebaseAllowed := isUserAllowedToPushOrForcePushInRepoBranch(ctx, pull.Issue.Poster, posterHeadRepoPerm, headBranchRule)
|
||||
if !ret.MergeAllowed {
|
||||
ret.MergeAllowed = posterPushAllowed
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
package pull
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"gitea.dev/models/db"
|
||||
@@ -35,9 +34,14 @@ func TestIsUserAllowedToUpdate(t *testing.T) {
|
||||
setRepoAllowMergeUpdate := func(t *testing.T, repoID int64, allow bool) {
|
||||
updatePRConfig(t, repoID, func(c *repo_model.PullRequestsConfig) { c.AllowMergeUpdate = allow })
|
||||
}
|
||||
checkUserAllowedToUpdate := func(ctx context.Context, pull *issues_model.PullRequest, user *user_model.User) (bool, bool, repo_model.UpdateStyle, error) {
|
||||
ret, err := CheckUserAllowedToUpdate(ctx, pull, user)
|
||||
return ret.MergeAllowed, ret.RebaseAllowed, ret.DefaultUpdateStyle, err
|
||||
checkUserAllowedToUpdate := func(t *testing.T, pull *issues_model.PullRequest, user *user_model.User) (bool, bool, repo_model.UpdateStyle) {
|
||||
t.Helper()
|
||||
require.NoError(t, pull.LoadBaseRepo(t.Context()))
|
||||
baseRepoPerm, err := access_model.GetDoerRepoPermission(t.Context(), pull.BaseRepo, user)
|
||||
require.NoError(t, err)
|
||||
ret, err := CheckUserAllowedToUpdate(t.Context(), pull, user, baseRepoPerm)
|
||||
require.NoError(t, err)
|
||||
return ret.MergeAllowed, ret.RebaseAllowed, ret.DefaultUpdateStyle
|
||||
}
|
||||
|
||||
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
||||
@@ -54,8 +58,7 @@ func TestIsUserAllowedToUpdate(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
defer db.DeleteByBean(t.Context(), protectedBranch)
|
||||
|
||||
pushAllowed, rebaseAllowed, defaultMergeStyle, err := checkUserAllowedToUpdate(t.Context(), pr2, user2)
|
||||
assert.NoError(t, err)
|
||||
pushAllowed, rebaseAllowed, defaultMergeStyle := checkUserAllowedToUpdate(t, pr2, user2)
|
||||
assert.False(t, pushAllowed)
|
||||
assert.False(t, rebaseAllowed)
|
||||
assert.Equal(t, repo_model.UpdateStyleMerge, defaultMergeStyle)
|
||||
@@ -64,8 +67,7 @@ func TestIsUserAllowedToUpdate(t *testing.T) {
|
||||
t.Run("DisallowRebaseWhenConfigDisabled", func(t *testing.T) {
|
||||
pr2 := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 2})
|
||||
setRepoAllowRebaseUpdate(t, pr2.BaseRepoID, false)
|
||||
pushAllowed, rebaseAllowed, _, err := checkUserAllowedToUpdate(t.Context(), pr2, user2)
|
||||
assert.NoError(t, err)
|
||||
pushAllowed, rebaseAllowed, _ := checkUserAllowedToUpdate(t, pr2, user2)
|
||||
assert.True(t, pushAllowed)
|
||||
assert.False(t, rebaseAllowed)
|
||||
})
|
||||
@@ -74,8 +76,7 @@ func TestIsUserAllowedToUpdate(t *testing.T) {
|
||||
pr2 := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 2})
|
||||
setRepoAllowRebaseUpdate(t, pr2.BaseRepoID, true)
|
||||
setRepoAllowMergeUpdate(t, pr2.BaseRepoID, false)
|
||||
pushAllowed, rebaseAllowed, _, err := checkUserAllowedToUpdate(t.Context(), pr2, user2)
|
||||
assert.NoError(t, err)
|
||||
pushAllowed, rebaseAllowed, _ := checkUserAllowedToUpdate(t, pr2, user2)
|
||||
assert.False(t, pushAllowed)
|
||||
assert.True(t, rebaseAllowed)
|
||||
setRepoAllowMergeUpdate(t, pr2.BaseRepoID, true)
|
||||
@@ -96,8 +97,7 @@ func TestIsUserAllowedToUpdate(t *testing.T) {
|
||||
require.NoError(t, pr2.LoadHeadRepo(t.Context()))
|
||||
assert.NoError(t, access_model.RecalculateUserAccess(t.Context(), pr2.HeadRepo, user4))
|
||||
|
||||
pushAllowed, rebaseAllowed, _, err := checkUserAllowedToUpdate(t.Context(), pr2, user4)
|
||||
assert.NoError(t, err)
|
||||
pushAllowed, rebaseAllowed, _ := checkUserAllowedToUpdate(t, pr2, user4)
|
||||
assert.False(t, pushAllowed)
|
||||
assert.False(t, rebaseAllowed)
|
||||
})
|
||||
@@ -114,8 +114,7 @@ func TestIsUserAllowedToUpdate(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
defer db.DeleteByBean(t.Context(), protectedBranch)
|
||||
|
||||
pushAllowed, rebaseAllowed, _, err := checkUserAllowedToUpdate(t.Context(), pr2, user2)
|
||||
assert.NoError(t, err)
|
||||
pushAllowed, rebaseAllowed, _ := checkUserAllowedToUpdate(t, pr2, user2)
|
||||
assert.True(t, pushAllowed)
|
||||
assert.False(t, rebaseAllowed)
|
||||
})
|
||||
@@ -137,22 +136,19 @@ func TestIsUserAllowedToUpdate(t *testing.T) {
|
||||
require.NoError(t, pr3.LoadHeadRepo(t.Context()))
|
||||
pr3.HeadRepo.IsPrivate = true
|
||||
|
||||
pushAllowed, _, _, err := checkUserAllowedToUpdate(t.Context(), pr3, pr3Poster)
|
||||
assert.NoError(t, err)
|
||||
pushAllowed, _, _ := checkUserAllowedToUpdate(t, pr3, pr3Poster)
|
||||
assert.True(t, pushAllowed)
|
||||
|
||||
publicOnlyPoster := *pr3Poster
|
||||
publicOnlyPoster.ExtDoerData = user_model.NewTokenExtDoerData(user_model.CredentialAccessToken, 1, "public-only,write:repository")
|
||||
pushAllowed, _, _, err = checkUserAllowedToUpdate(t.Context(), pr3, &publicOnlyPoster)
|
||||
assert.NoError(t, err)
|
||||
pushAllowed, _, _ = checkUserAllowedToUpdate(t, pr3, &publicOnlyPoster)
|
||||
assert.False(t, pushAllowed)
|
||||
})
|
||||
|
||||
t.Run("MaintainerEditRespectsPosterPermissions", func(t *testing.T) {
|
||||
pr3 := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 3})
|
||||
pr3.AllowMaintainerEdit = true
|
||||
pushAllowed, rebaseAllowed, _, err := checkUserAllowedToUpdate(t.Context(), pr3, pr3Poster)
|
||||
assert.NoError(t, err)
|
||||
pushAllowed, rebaseAllowed, _ := checkUserAllowedToUpdate(t, pr3, pr3Poster)
|
||||
assert.False(t, pushAllowed)
|
||||
assert.False(t, rebaseAllowed)
|
||||
})
|
||||
@@ -192,8 +188,7 @@ func TestIsUserAllowedToUpdate(t *testing.T) {
|
||||
setRepoAllowRebaseUpdate(t, pr3.BaseRepoID, false)
|
||||
}
|
||||
|
||||
pushAllowed, rebaseAllowed, _, err := checkUserAllowedToUpdate(t.Context(), pr3, pr3Poster)
|
||||
assert.NoError(t, err)
|
||||
pushAllowed, rebaseAllowed, _ := checkUserAllowedToUpdate(t, pr3, pr3Poster)
|
||||
assert.True(t, pushAllowed)
|
||||
if testCase.disableRebase {
|
||||
assert.False(t, rebaseAllowed)
|
||||
|
||||
Reference in new issue
Block a user