Skip to content

Commit 6ca0a8d

Browse files
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 e2eac75 commit 6ca0a8d

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

routers/web/repo/pull.go

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

637637
if pb != nil && pb.EnableStatusCheck {
638+
639+
var missingRequiredChecks []string
640+
for _, requiredContext := range pb.StatusCheckContexts {
641+
contextFound := false
642+
matchesRequiredContext := createRequiredContextMatcher(requiredContext)
643+
for _, presentStatus := range commitStatuses {
644+
if matchesRequiredContext(presentStatus.Context) {
645+
contextFound = true
646+
break
647+
}
648+
}
649+
650+
if !contextFound {
651+
missingRequiredChecks = append(missingRequiredChecks, requiredContext)
652+
}
653+
}
654+
ctx.Data["MissingRequiredChecks"] = missingRequiredChecks
655+
638656
ctx.Data["is_context_required"] = func(context string) bool {
639657
for _, c := range pb.StatusCheckContexts {
640658
if c == context {
@@ -703,6 +721,18 @@ func PrepareViewPullInfo(ctx *context.Context, issue *issues_model.Issue) *git.C
703721
return compareInfo
704722
}
705723

724+
func createRequiredContextMatcher(requiredContext string) func(string) bool {
725+
if gp, err := glob.Compile(requiredContext); err == nil {
726+
return func(contextToCheck string) bool {
727+
return gp.Match(contextToCheck)
728+
}
729+
}
730+
731+
return func(contextToCheck string) bool {
732+
return requiredContext == contextToCheck
733+
}
734+
}
735+
706736
type pullCommitList struct {
707737
Commits []pull_service.CommitInfo `json:"commits"`
708738
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
@@ -50,6 +50,10 @@ func MergeRequiredContextsCommitStatus(commitStatuses []*git_model.CommitStatus,
5050
}
5151
}
5252

53+
if matchedCount != len(requiredContexts) {
54+
return structs.CommitStatusPending
55+
}
56+
5357
if matchedCount == 0 {
5458
status := git_model.CalcCommitStatus(commitStatuses)
5559
if status != nil {

templates/repo/pulls/status.tmpl

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{{if $.LatestCommitStatus}}
22
{{if not $.Issue.PullRequest.HasMerged}}
33
<div class="ui top attached header">
4-
{{if eq .LatestCommitStatus.State "pending"}}
4+
{{if or (eq .LatestCommitStatus.State "pending") (.MissingRequiredChecks)}}
55
{{ctx.Locale.Tr "repo.pulls.status_checking"}}
66
{{else if eq .LatestCommitStatus.State "success"}}
77
{{ctx.Locale.Tr "repo.pulls.status_checks_success"}}
@@ -14,7 +14,7 @@
1414
{{else}}
1515
{{ctx.Locale.Tr "repo.pulls.status_checking"}}
1616
{{end}}
17-
</div>
17+
</div>
1818
{{end}}
1919

2020
{{range $.LatestCommitStatuses}}
@@ -31,4 +31,15 @@
3131
</div>
3232
</div>
3333
{{end}}
34+
{{range .MissingRequiredChecks}}
35+
<div class="ui attached segment pr-status">
36+
{{svg "octicon-dot-fill" 18 "commit-status icon text yellow"}}
37+
<div class="status-context">
38+
<span>{{.}}</span>
39+
<div class="ui status-details">
40+
<div class="ui label">{{ctx.Locale.Tr "repo.pulls.status_checks_requested"}}</div>
41+
</div>
42+
</div>
43+
</div>
44+
{{end}}
3445
{{end}}

0 commit comments

Comments
 (0)