Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
72 commits
Select commit Hold shift + click to select a range
9f9f488
Make database supports glob branch name protected branch
lunny Aug 17, 2022
1dfa785
Fix bug
lunny Aug 17, 2022
bf27be6
Fix fmt
lunny Aug 17, 2022
963e438
Merge branch 'main' into lunny/glob_protected_branch_rule
lunny Aug 17, 2022
6f39f75
Merge branch 'main' into lunny/glob_protected_branch_rule
lunny Aug 18, 2022
588c5b7
Most UI changes
lunny Aug 18, 2022
997a01d
Merge branch 'main' into lunny/glob_protected_branch_rule
lunny Aug 18, 2022
9740a6d
merge main branch
lunny Sep 6, 2022
b12ac62
Fix test
lunny Sep 6, 2022
5e55875
Merge branch 'main' into lunny/glob_protected_branch_rule
lunny Sep 12, 2022
553869a
support protected branch rules
lunny Sep 12, 2022
aba5aad
support * not cross slash
lunny Sep 13, 2022
80d2396
Merge branch 'main' into lunny/glob_protected_branch_rule
lunny Oct 18, 2022
e894bdf
Fix lint
lunny Oct 18, 2022
957a20b
Fix matching algorithm
lunny Oct 19, 2022
d87e6bd
Merge branch 'main' into lunny/glob_protected_branch_rule
lunny Oct 19, 2022
30bbfd0
Fix import lint
lunny Oct 19, 2022
a82b8d1
Fix tests
lunny Oct 20, 2022
1837aae
Merge branch 'main' into lunny/glob_protected_branch_rule
lunny Oct 20, 2022
a5cc6d3
Rule Sort
lunny Oct 20, 2022
ad304ec
Fix tests
lunny Oct 22, 2022
3425685
Merge branch 'main' into lunny/glob_protected_branch_rule
lunny Oct 22, 2022
17a7982
Fix lint
lunny Oct 22, 2022
86b0029
Merge branch 'main' into lunny/glob_protected_branch_rule
lunny Oct 23, 2022
c0989ed
Fix doc
lunny Oct 23, 2022
562077e
Fix test
lunny Oct 23, 2022
fbb17f3
Merge branch 'main' into lunny/glob_protected_branch_rule
lunny Oct 23, 2022
220d385
Fix lint
lunny Oct 23, 2022
0c234ab
Merge branch 'main' into lunny/glob_protected_branch_rule
lunny Oct 23, 2022
31d6595
Fix lint
lunny Oct 23, 2022
0c1ef7f
Fix test
lunny Oct 24, 2022
b23a742
Merge branch 'main' into lunny/glob_protected_branch_rule
lunny Oct 24, 2022
b428771
merge main branch
lunny Oct 31, 2022
300e623
Merge branch 'main' into lunny/glob_protected_branch_rule
lunny Nov 4, 2022
3e0eefc
Merge branch 'main' into lunny/glob_protected_branch_rule
lunny Nov 5, 2022
398123b
Fix test
lunny Nov 5, 2022
94b5ae6
fix test
lunny Nov 10, 2022
e392b85
Merge branch 'main' into lunny/glob_protected_branch_rule
lunny Nov 10, 2022
4ec2dcb
Merge branch 'main' into lunny/glob_protected_branch_rule
lunny Dec 1, 2022
accc65c
Merge branch 'main' into lunny/glob_protected_branch_rule
lunny Dec 2, 2022
5b23369
Fix test
lunny Dec 3, 2022
fee37c3
Merge branch 'main' into lunny/glob_protected_branch_rule
lunny Dec 3, 2022
e50aad2
Merge main branch
lunny Dec 3, 2022
b0fa0ef
Fix lint
lunny Dec 3, 2022
71310ec
Merge branch 'main' into lunny/glob_protected_branch_rule
lunny Dec 3, 2022
d95f214
Fix test
lunny Dec 3, 2022
bdf75cc
Fix test
lunny Dec 3, 2022
7e43b21
Fix bug
lunny Dec 3, 2022
5e8aa8c
Merge branch 'main' into lunny/glob_protected_branch_rule
lunny Dec 3, 2022
11fcf57
Fix bug
lunny Dec 4, 2022
841cf18
Merge branch 'main' into lunny/glob_protected_branch_rule
lunny Dec 4, 2022
361baca
merge main branch
lunny Dec 4, 2022
6a55815
Some improvements
lunny Dec 5, 2022
40577d7
Add branch name back in API but mark it as deprecated
lunny Dec 5, 2022
39e28c5
Merge branch 'main' into lunny/glob_protected_branch_rule
lunny Dec 5, 2022
8fbe8ad
Fix lint
lunny Dec 6, 2022
16ca6c2
merge main branch
lunny Dec 10, 2022
9f18da1
fix lint
lunny Dec 11, 2022
ded5545
Merge branch 'main' into lunny/glob_protected_branch_rule
lunny Dec 11, 2022
e40338c
Merge branch 'main' into lunny/glob_protected_branch_rule
lunny Dec 11, 2022
8ab38d9
merge main branch
lunny Dec 13, 2022
389dce2
Merge branch 'lunny/glob_protected_branch_rule' of github.com:lunny/g…
lunny Dec 13, 2022
2eeb920
Merge branch 'main' into lunny/glob_protected_branch_rule
lunny Dec 14, 2022
9e3a06d
Update models/git/protected_branch.go
lunny Dec 21, 2022
73a589c
Merge branch 'main' into lunny/glob_protected_branch_rule
lunny Dec 30, 2022
e378263
Merge branch 'lunny/glob_protected_branch_rule' of github.com:lunny/g…
lunny Dec 30, 2022
59c0714
some improvement
lunny Dec 30, 2022
01c2057
Fix lint
lunny Dec 30, 2022
5d078d4
Merge branch 'main' into lunny/glob_protected_branch_rule
lunny Jan 1, 2023
f5c010b
merge main branch
lunny Jan 10, 2023
c1c1a8b
Merge branch 'lunny/glob_protected_branch_rule' of github.com:lunny/g…
lunny Jan 10, 2023
1bbc980
merge main branch
lunny Jan 16, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Make database supports glob branch name protected branch
  • Loading branch information
lunny committed Aug 17, 2022
commit 9f9f4882f92fb8f015663dca5726937fa7830b70
423 changes: 5 additions & 418 deletions models/git/branches.go

Large diffs are not rendered by default.

436 changes: 436 additions & 0 deletions models/git/protected_branch.go

Large diffs are not rendered by default.

38 changes: 38 additions & 0 deletions models/git/protected_branch_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2022 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package git

import (
"context"

"code.gitea.io/gitea/models/db"
)

type ProtectedBranchRules []*ProtectedBranch

func (rules ProtectedBranchRules) GetFirstMatched(branchName string) *ProtectedBranch {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
func (rules ProtectedBranchRules) GetFirstMatched(branchName string) *ProtectedBranch {
// GetFirstMatched returns the first rule matching the branch name, or nil if the branch is not protected
func (rules ProtectedBranchRules) GetFirstMatched(branchName string) *ProtectedBranch {

for _, rule := range rules {
if rule.Match(branchName) {
return rule
}
}
return nil
}

// FindMatchedProtectedBranchRules load all repository's protected rules
func FindMatchedProtectedBranchRules(ctx context.Context, repoID int64) (ProtectedBranchRules, error) {
var rules []*ProtectedBranch
err := db.GetEngine(ctx).Where("repo_id = ?", repoID).Asc("created_unix").Find(&rules)
return rules, err
}

// GetFirstMatchProtectedBranchRule returns the first matched rules
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// GetFirstMatchProtectedBranchRule returns the first matched rules
// GetFirstMatchProtectedBranchRule returns the first matched rules or nil if none matches

func GetFirstMatchProtectedBranchRule(ctx context.Context, repoID int64, branchName string) (*ProtectedBranch, error) {
Copy link
Member

Choose a reason for hiding this comment

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

Hmm, should we perhaps omit the Match?
I think that's implied, and shorter names are better for readability…

rules, err := FindMatchedProtectedBranchRules(ctx, repoID)
if err != nil {
return nil, err
}
return rules.GetFirstMatched(branchName), nil
}
33 changes: 14 additions & 19 deletions models/issues/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,16 +150,16 @@ type PullRequest struct {
Issue *Issue `xorm:"-"`
Index int64

HeadRepoID int64 `xorm:"INDEX"`
HeadRepo *repo_model.Repository `xorm:"-"`
BaseRepoID int64 `xorm:"INDEX"`
BaseRepo *repo_model.Repository `xorm:"-"`
HeadBranch string
HeadCommitID string `xorm:"-"`
BaseBranch string
ProtectedBranch *git_model.ProtectedBranch `xorm:"-"`
MergeBase string `xorm:"VARCHAR(40)"`
AllowMaintainerEdit bool `xorm:"NOT NULL DEFAULT false"`
HeadRepoID int64 `xorm:"INDEX"`
HeadRepo *repo_model.Repository `xorm:"-"`
BaseRepoID int64 `xorm:"INDEX"`
BaseRepo *repo_model.Repository `xorm:"-"`
HeadBranch string
HeadCommitID string `xorm:"-"`
BaseBranch string
// ProtectedBranch *git_model.ProtectedBranch `xorm:"-"`
MergeBase string `xorm:"VARCHAR(40)"`
AllowMaintainerEdit bool `xorm:"NOT NULL DEFAULT false"`

HasMerged bool `xorm:"INDEX"`
MergedCommitID string `xorm:"VARCHAR(40)"`
Expand Down Expand Up @@ -305,13 +305,8 @@ func (pr *PullRequest) LoadIssueCtx(ctx context.Context) (err error) {
return err
}

// LoadProtectedBranch loads the protected branch of the base branch
func (pr *PullRequest) LoadProtectedBranch() (err error) {
return pr.LoadProtectedBranchCtx(db.DefaultContext)
}

// LoadProtectedBranchCtx loads the protected branch of the base branch
func (pr *PullRequest) LoadProtectedBranchCtx(ctx context.Context) (err error) {
// LoadProtectedBranchRules loads the protected branch of the base branch
/*func (pr *PullRequest) LoadProtectedBranchRules(ctx context.Context) (err error) {
if pr.ProtectedBranch == nil {
if pr.BaseRepo == nil {
if pr.BaseRepoID == 0 {
Expand All @@ -322,10 +317,10 @@ func (pr *PullRequest) LoadProtectedBranchCtx(ctx context.Context) (err error) {
return
}
}
pr.ProtectedBranch, err = git_model.GetProtectedBranchBy(ctx, pr.BaseRepo.ID, pr.BaseBranch)
pr.ProtectedBranch, err = git_model.GetFirstMatchProtectedBranchRule(ctx, pr.BaseRepo.ID, pr.BaseBranch)
}
return err
}
}*/

// ReviewCount represents a count of Reviews
type ReviewCount struct {
Expand Down
17 changes: 10 additions & 7 deletions models/issues/review.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,15 +264,17 @@ func IsOfficialReviewer(ctx context.Context, issue *Issue, reviewers ...*user_mo
if err != nil {
return false, err
}
if err = pr.LoadProtectedBranchCtx(ctx); err != nil {

rule, err := git_model.GetFirstMatchProtectedBranchRule(ctx, pr.BaseRepoID, pr.BaseBranch)
if err != nil {
return false, err
}
Comment on lines +267 to 270
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
rule, err := git_model.GetFirstMatchProtectedBranchRule(ctx, pr.BaseRepoID, pr.BaseBranch)
if err != nil {
return false, err
}
rule, err := git_model.GetFirstMatchProtectedBranchRule(ctx, pr.BaseRepoID, pr.BaseBranch)
if rule == nil || err != nil {
return false, err
}

if pr.ProtectedBranch == nil {
if rule == nil {
return false, nil
}
Comment on lines +271 to 273
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if rule == nil {
return false, nil
}


for _, reviewer := range reviewers {
official, err := git_model.IsUserOfficialReviewerCtx(ctx, pr.ProtectedBranch, reviewer)
official, err := git_model.IsUserOfficialReviewer(ctx, rule, reviewer)
if official || err != nil {
return official, err
}
Expand All @@ -287,18 +289,19 @@ func IsOfficialReviewerTeam(ctx context.Context, issue *Issue, team *organizatio
if err != nil {
return false, err
}
if err = pr.LoadProtectedBranchCtx(ctx); err != nil {
pb, err := git_model.GetFirstMatchProtectedBranchRule(ctx, pr.BaseRepoID, pr.BaseBranch)
if err != nil {
return false, err
}
Comment on lines +292 to 294
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if err != nil {
return false, err
}
if pb == nil || err != nil {
return false, err
}

if pr.ProtectedBranch == nil {
if pb == nil {
return false, nil
}
Comment on lines +295 to 297
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if pb == nil {
return false, nil
}


if !pr.ProtectedBranch.EnableApprovalsWhitelist {
if !pb.EnableApprovalsWhitelist {
return team.UnitAccessModeCtx(ctx, unit.TypeCode) >= perm.AccessModeWrite, nil
}

return base.Int64sContains(pr.ProtectedBranch.ApprovalsWhitelistTeamIDs, team.ID), nil
return base.Int64sContains(pb.ApprovalsWhitelistTeamIDs, team.ID), nil
}

// CreateReview creates a new review based on opts
Expand Down
5 changes: 3 additions & 2 deletions modules/context/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,10 @@ type CanCommitToBranchResults struct {
}

// CanCommitToBranch returns true if repository is editable and user has proper access level
// and branch is not protected for push
//
// and branch is not protected for push
func (r *Repository) CanCommitToBranch(ctx context.Context, doer *user_model.User) (CanCommitToBranchResults, error) {
protectedBranch, err := git_model.GetProtectedBranchBy(ctx, r.Repository.ID, r.BranchName)
protectedBranch, err := git_model.GetFirstMatchProtectedBranchRule(ctx, r.Repository.ID, r.BranchName)
if err != nil {
return CanCommitToBranchResults{}, err
}
Expand Down
16 changes: 9 additions & 7 deletions routers/api/v1/repo/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func GetBranch(ctx *context.APIContext) {
return
}

branchProtection, err := git_model.GetProtectedBranchBy(ctx, ctx.Repo.Repository.ID, branchName)
branchProtection, err := git_model.GetFirstMatchProtectedBranchRule(ctx, ctx.Repo.Repository.ID, branchName)
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetBranchProtection", err)
return
Expand Down Expand Up @@ -207,7 +207,7 @@ func CreateBranch(ctx *context.APIContext) {
return
}

branchProtection, err := git_model.GetProtectedBranchBy(ctx, ctx.Repo.Repository.ID, branch.Name)
branchProtection, err := git_model.GetFirstMatchProtectedBranchRule(ctx, ctx.Repo.Repository.ID, branch.Name)
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetBranchProtection", err)
return
Expand Down Expand Up @@ -260,6 +260,12 @@ func ListBranches(ctx *context.APIContext) {
return
}

rules, err := git_model.FindMatchedProtectedBranchRules(ctx, ctx.Repo.Repository.ID)
if err != nil {
ctx.Error(http.StatusInternalServerError, "FindMatchedProtectedBranchRules", err)
return
}

apiBranches := make([]*api.Branch, 0, len(branches))
for i := range branches {
c, err := branches[i].GetCommit()
Expand All @@ -272,11 +278,7 @@ func ListBranches(ctx *context.APIContext) {
ctx.Error(http.StatusInternalServerError, "GetCommit", err)
return
}
branchProtection, err := git_model.GetProtectedBranchBy(ctx, ctx.Repo.Repository.ID, branches[i].Name)
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetBranchProtection", err)
return
}
branchProtection := rules.GetFirstMatched(branches[i].Name)
apiBranch, err := convert.ToBranch(ctx.Repo.Repository, branches[i], c, branchProtection, ctx.Doer, ctx.Repo.IsAdmin())
if err != nil {
ctx.Error(http.StatusInternalServerError, "convert.ToBranch", err)
Expand Down
2 changes: 1 addition & 1 deletion routers/private/hook_pre_receive.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func preReceiveBranch(ctx *preReceiveContext, oldCommitID, newCommitID, refFullN
return
}

protectBranch, err := git_model.GetProtectedBranchBy(ctx, repo.ID, branchName)
protectBranch, err := git_model.GetFirstMatchProtectedBranchRule(ctx, repo.ID, branchName)
if err != nil {
log.Error("Unable to get protected branch: %s in %-v Error: %v", branchName, repo, err)
ctx.JSON(http.StatusInternalServerError, private.Response{
Expand Down
21 changes: 11 additions & 10 deletions routers/web/repo/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -1630,22 +1630,23 @@ func ViewIssue(ctx *context.Context) {
}
ctx.Data["DefaultSquashMergeMessage"] = defaultSquashMergeMessage

if err = pull.LoadProtectedBranch(); err != nil {
pb, err := git_model.GetFirstMatchProtectedBranchRule(ctx, pull.BaseRepoID, pull.BaseBranch)
if err != nil {
ctx.ServerError("LoadProtectedBranch", err)
return
}
ctx.Data["ShowMergeInstructions"] = true
if pull.ProtectedBranch != nil {
if pb != nil {
var showMergeInstructions bool
if ctx.Doer != nil {
showMergeInstructions = pull.ProtectedBranch.CanUserPush(ctx.Doer.ID)
}
ctx.Data["IsBlockedByApprovals"] = !issues_model.HasEnoughApprovals(ctx, pull.ProtectedBranch, pull)
ctx.Data["IsBlockedByRejection"] = issues_model.MergeBlockedByRejectedReview(ctx, pull.ProtectedBranch, pull)
ctx.Data["IsBlockedByOfficialReviewRequests"] = issues_model.MergeBlockedByOfficialReviewRequests(ctx, pull.ProtectedBranch, pull)
ctx.Data["IsBlockedByOutdatedBranch"] = issues_model.MergeBlockedByOutdatedBranch(pull.ProtectedBranch, pull)
ctx.Data["GrantedApprovals"] = issues_model.GetGrantedApprovalsCount(ctx, pull.ProtectedBranch, pull)
ctx.Data["RequireSigned"] = pull.ProtectedBranch.RequireSignedCommits
showMergeInstructions = pb.CanUserPush(ctx.Doer.ID)
}
ctx.Data["IsBlockedByApprovals"] = !issues_model.HasEnoughApprovals(ctx, pb, pull)
ctx.Data["IsBlockedByRejection"] = issues_model.MergeBlockedByRejectedReview(ctx, pb, pull)
ctx.Data["IsBlockedByOfficialReviewRequests"] = issues_model.MergeBlockedByOfficialReviewRequests(ctx, pb, pull)
ctx.Data["IsBlockedByOutdatedBranch"] = issues_model.MergeBlockedByOutdatedBranch(pb, pull)
ctx.Data["GrantedApprovals"] = issues_model.GetGrantedApprovalsCount(ctx, pb, pull)
ctx.Data["RequireSigned"] = pb.RequireSignedCommits
ctx.Data["ChangedProtectedFiles"] = pull.ChangedProtectedFiles
ctx.Data["IsBlockedByChangedProtectedFiles"] = len(pull.ChangedProtectedFiles) != 0
ctx.Data["ChangedProtectedFilesNum"] = len(pull.ChangedProtectedFiles)
Expand Down
20 changes: 11 additions & 9 deletions routers/web/repo/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,11 +414,12 @@ func PrepareViewPullInfo(ctx *context.Context, issue *issues_model.Issue) *git.C

setMergeTarget(ctx, pull)

if err := pull.LoadProtectedBranch(); err != nil {
pb, err := git_model.GetFirstMatchProtectedBranchRule(ctx, repo.ID, pull.BaseBranch)
if err != nil {
ctx.ServerError("LoadProtectedBranch", err)
return nil
}
ctx.Data["EnableStatusCheck"] = pull.ProtectedBranch != nil && pull.ProtectedBranch.EnableStatusCheck
ctx.Data["EnableStatusCheck"] = pb != nil && pb.EnableStatusCheck

var baseGitRepo *git.Repository
if pull.BaseRepoID == ctx.Repo.Repository.ID && ctx.Repo.GitRepo != nil {
Expand Down Expand Up @@ -544,16 +545,16 @@ func PrepareViewPullInfo(ctx *context.Context, issue *issues_model.Issue) *git.C
ctx.Data["LatestCommitStatus"] = git_model.CalcCommitStatus(commitStatuses)
}

if pull.ProtectedBranch != nil && pull.ProtectedBranch.EnableStatusCheck {
if pb != nil && pb.EnableStatusCheck {
ctx.Data["is_context_required"] = func(context string) bool {
for _, c := range pull.ProtectedBranch.StatusCheckContexts {
for _, c := range pb.StatusCheckContexts {
if c == context {
return true
}
}
return false
}
ctx.Data["RequiredStatusCheckState"] = pull_service.MergeRequiredContextsCommitStatus(commitStatuses, pull.ProtectedBranch.StatusCheckContexts)
ctx.Data["RequiredStatusCheckState"] = pull_service.MergeRequiredContextsCommitStatus(commitStatuses, pb.StatusCheckContexts)
}

ctx.Data["HeadBranchMovedOn"] = headBranchSha != sha
Expand Down Expand Up @@ -726,16 +727,17 @@ func ViewPullFiles(ctx *context.Context) {
return
}

if err = pull.LoadProtectedBranch(); err != nil {
pb, err := git_model.GetFirstMatchProtectedBranchRule(ctx, pull.BaseRepoID, pull.BaseBranch)
if err != nil {
ctx.ServerError("LoadProtectedBranch", err)
return
}

if pull.ProtectedBranch != nil {
glob := pull.ProtectedBranch.GetProtectedFilePatterns()
if pb != nil {
glob := pb.GetProtectedFilePatterns()
if len(glob) != 0 {
for _, file := range diff.Files {
file.IsProtected = pull.ProtectedBranch.IsProtectedFile(glob, file.Name)
file.IsProtected = pb.IsProtectedFile(glob, file.Name)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion services/asymkey/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ Loop:
return false, "", nil, &ErrWontSign{twofa}
}
case approved:
protectedBranch, err := git_model.GetProtectedBranchBy(ctx, repo.ID, pr.BaseBranch)
protectedBranch, err := git_model.GetFirstMatchProtectedBranchRule(ctx, repo.ID, pr.BaseBranch)
if err != nil {
return false, "", nil, err
}
Copy link
Member

Choose a reason for hiding this comment

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

I don't quite understand the logic one line below:
Why should we not sign when there is no branch protection?
Why not sign always when not manually disabled?

Expand Down
6 changes: 4 additions & 2 deletions services/pull/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
git_model "code.gitea.io/gitea/models/git"
issues_model "code.gitea.io/gitea/models/issues"
access_model "code.gitea.io/gitea/models/perm/access"
repo_model "code.gitea.io/gitea/models/repo"
Expand Down Expand Up @@ -127,11 +128,12 @@ func CheckPullMergable(stdCtx context.Context, doer *user_model.User, perm *acce

// isSignedIfRequired check if merge will be signed if required
func isSignedIfRequired(ctx context.Context, pr *issues_model.PullRequest, doer *user_model.User) (bool, error) {
if err := pr.LoadProtectedBranchCtx(ctx); err != nil {
pb, err := git_model.GetFirstMatchProtectedBranchRule(ctx, pr.BaseRepoID, pr.BaseBranch)
if err != nil {
return false, err
}

if pr.ProtectedBranch == nil || !pr.ProtectedBranch.RequireSignedCommits {
if pb == nil || !pb.RequireSignedCommits {
return true, nil
}

Expand Down
12 changes: 7 additions & 5 deletions services/pull/commit_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,11 @@ func IsCommitStatusContextSuccess(commitStatuses []*git_model.CommitStatus, requ

// IsPullCommitStatusPass returns if all required status checks PASS
func IsPullCommitStatusPass(ctx context.Context, pr *issues_model.PullRequest) (bool, error) {
if err := pr.LoadProtectedBranchCtx(ctx); err != nil {
pb, err := git_model.GetFirstMatchProtectedBranchRule(ctx, pr.BaseRepoID, pr.BaseBranch)
if err != nil {
return false, errors.Wrap(err, "GetLatestCommitStatus")
}
if pr.ProtectedBranch == nil || !pr.ProtectedBranch.EnableStatusCheck {
if pb == nil || !pb.EnableStatusCheck {
return true, nil
}

Expand Down Expand Up @@ -138,12 +139,13 @@ func GetPullRequestCommitStatusState(ctx context.Context, pr *issues_model.PullR
return "", errors.Wrap(err, "GetLatestCommitStatus")
}

if err := pr.LoadProtectedBranchCtx(ctx); err != nil {
pb, err := git_model.GetFirstMatchProtectedBranchRule(ctx, pr.BaseRepoID, pr.BaseBranch)
if err != nil {
return "", errors.Wrap(err, "LoadProtectedBranch")
}
var requiredContexts []string
if pr.ProtectedBranch != nil {
requiredContexts = pr.ProtectedBranch.StatusCheckContexts
if pb != nil {
requiredContexts = pb.StatusCheckContexts
}

return MergeRequiredContextsCommitStatus(commitStatuses, requiredContexts), nil
Expand Down
Loading