Skip to content

Commit b1d66f5

Browse files
MarkusAmshovewxiaoguang
authored andcommitted
Disallow merge when required checked are missing (go-gitea#29143)
fixes go-gitea#21892 This PR disallows merging a PR when not all commit status contexts configured in the branch protection are met. Previously, the PR was happy to merge when one commit status was successful and the other contexts weren't reported. Any feedback is welcome, first time Go :-) I'm also not sure if the changes in the template break something else Given the following branch protection: ![branch_protection](https://github.com/go-gitea/gitea/assets/2401875/f871b4e4-138b-435a-b496-f9ad432e3dec) This was shown before the change: ![before](https://github.com/go-gitea/gitea/assets/2401875/60424ff0-ee09-4fa0-856e-64e6e3fb0612) With the change, it is now shown as this: ![after](https://github.com/go-gitea/gitea/assets/2401875/4e464142-efb1-4889-8166-eb3be26c8f3d) --------- Co-authored-by: wxiaoguang <[email protected]> (cherry picked from commit a11ccc9)
1 parent e96e1be commit b1d66f5

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

routers/web/repo/pull.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,24 @@ func PrepareViewPullInfo(ctx *context.Context, issue *issues_model.Issue) *git.C
662662
}
663663

664664
if pb != nil && pb.EnableStatusCheck {
665+
666+
var missingRequiredChecks []string
667+
for _, requiredContext := range pb.StatusCheckContexts {
668+
contextFound := false
669+
matchesRequiredContext := createRequiredContextMatcher(requiredContext)
670+
for _, presentStatus := range commitStatuses {
671+
if matchesRequiredContext(presentStatus.Context) {
672+
contextFound = true
673+
break
674+
}
675+
}
676+
677+
if !contextFound {
678+
missingRequiredChecks = append(missingRequiredChecks, requiredContext)
679+
}
680+
}
681+
ctx.Data["MissingRequiredChecks"] = missingRequiredChecks
682+
665683
ctx.Data["is_context_required"] = func(context string) bool {
666684
for _, c := range pb.StatusCheckContexts {
667685
if c == context {
@@ -730,6 +748,18 @@ func PrepareViewPullInfo(ctx *context.Context, issue *issues_model.Issue) *git.C
730748
return compareInfo
731749
}
732750

751+
func createRequiredContextMatcher(requiredContext string) func(string) bool {
752+
if gp, err := glob.Compile(requiredContext); err == nil {
753+
return func(contextToCheck string) bool {
754+
return gp.Match(contextToCheck)
755+
}
756+
}
757+
758+
return func(contextToCheck string) bool {
759+
return requiredContext == contextToCheck
760+
}
761+
}
762+
733763
type pullCommitList struct {
734764
Commits []pull_service.CommitInfo `json:"commits"`
735765
LastReviewCommitSha string `json:"last_review_commit_sha"`

services/pull/commit_status.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ func MergeRequiredContextsCommitStatus(commitStatuses []*git_model.CommitStatus,
5252
}
5353
}
5454

55+
if matchedCount != len(requiredContexts) {
56+
return structs.CommitStatusPending
57+
}
58+
5559
if matchedCount == 0 {
5660
status := git_model.CalcCommitStatus(commitStatuses)
5761
if status != nil {

templates/repo/issue/view_content/pull.tmpl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
{{template "repo/pulls/status" (dict
2525
"CommitStatus" .LatestCommitStatus
2626
"CommitStatuses" .LatestCommitStatuses
27+
"MissingRequiredChecks" .MissingRequiredChecks
2728
"ShowHideChecks" true
2829
"is_context_required" .is_context_required
2930
)}}

templates/repo/pulls/status.tmpl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
Template Attributes:
33
* CommitStatus: summary of all commit status state
44
* CommitStatuses: all commit status elements
5+
* MissingRequiredChecks: commit check contexts that are required by branch protection but not present
56
* ShowHideChecks: whether use a button to show/hide the checks
67
* is_context_required: Used in pull request commit status check table
78
*/}}
89

910
{{if .CommitStatus}}
1011
<div class="commit-status-panel">
1112
<div class="ui top attached header commit-status-header">
12-
{{if eq .CommitStatus.State "pending"}}
13+
{{if or (eq .CommitStatus.State "pending") (.MissingRequiredChecks)}}
1314
{{ctx.Locale.Tr "repo.pulls.status_checking"}}
1415
{{else if eq .CommitStatus.State "success"}}
1516
{{ctx.Locale.Tr "repo.pulls.status_checks_success"}}
@@ -46,6 +47,13 @@ Template Attributes:
4647
</div>
4748
</div>
4849
{{end}}
50+
{{range .MissingRequiredChecks}}
51+
<div class="commit-status-item">
52+
{{svg "octicon-dot-fill" 18 "commit-status icon text yellow"}}
53+
<div class="status-context gt-ellipsis">{{.}}</div>
54+
<div class="ui label">{{ctx.Locale.Tr "repo.pulls.status_checks_requested"}}</div>
55+
</div>
56+
{{end}}
4957
</div>
5058
</div>
5159
{{end}}

0 commit comments

Comments
 (0)