Skip to content

Commit 969122a

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]>
1 parent b5f9a0d commit 969122a

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
@@ -652,6 +652,24 @@ func PrepareViewPullInfo(ctx *context.Context, issue *issues_model.Issue) *git.C
652652
}
653653

654654
if pb != nil && pb.EnableStatusCheck {
655+
656+
var missingRequiredChecks []string
657+
for _, requiredContext := range pb.StatusCheckContexts {
658+
contextFound := false
659+
matchesRequiredContext := createRequiredContextMatcher(requiredContext)
660+
for _, presentStatus := range commitStatuses {
661+
if matchesRequiredContext(presentStatus.Context) {
662+
contextFound = true
663+
break
664+
}
665+
}
666+
667+
if !contextFound {
668+
missingRequiredChecks = append(missingRequiredChecks, requiredContext)
669+
}
670+
}
671+
ctx.Data["MissingRequiredChecks"] = missingRequiredChecks
672+
655673
ctx.Data["is_context_required"] = func(context string) bool {
656674
for _, c := range pb.StatusCheckContexts {
657675
if c == context {
@@ -720,6 +738,18 @@ func PrepareViewPullInfo(ctx *context.Context, issue *issues_model.Issue) *git.C
720738
return compareInfo
721739
}
722740

741+
func createRequiredContextMatcher(requiredContext string) func(string) bool {
742+
if gp, err := glob.Compile(requiredContext); err == nil {
743+
return func(contextToCheck string) bool {
744+
return gp.Match(contextToCheck)
745+
}
746+
}
747+
748+
return func(contextToCheck string) bool {
749+
return requiredContext == contextToCheck
750+
}
751+
}
752+
723753
type pullCommitList struct {
724754
Commits []pull_service.CommitInfo `json:"commits"`
725755
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
@@ -51,6 +51,10 @@ func MergeRequiredContextsCommitStatus(commitStatuses []*git_model.CommitStatus,
5151
}
5252
}
5353

54+
if matchedCount != len(requiredContexts) {
55+
return structs.CommitStatusPending
56+
}
57+
5458
if matchedCount == 0 {
5559
status := git_model.CalcCommitStatus(commitStatuses)
5660
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)