Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions changelog.d/fixed-merge-gate-cancelled-check.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Merge requests: a cancelled check (for example the 20-minute advisory mutation job timing out) no longer blocks the merge gate when the base branch's required checks cannot be read; GitHub still enforces the real required checks at merge time. When the required set is known to be empty (nothing enforced server-side), a cancelled non-advisory check still blocks locally, as do failed and timed-out checks (hivecommons/hive#17598).
26 changes: 25 additions & 1 deletion src/pkg/github/commit_ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,13 @@ type CommitCIOptions struct {
// must block locally even when UnknownRequiredChecksServerEnforced is set
// for metadata handling.
RequiredKnownEmpty bool
// NoServerBackstop says the local gate is the only gate for this head: the
// required set is known to be empty, so the merge endpoint enforces nothing.
// A cancelled non-ignorable check-run then blocks as check-cancelled instead
// of carrying no verdict. RequiredKnownEmpty implies it; callers that collapse
// a known-empty set into requiredKnown=false without RequiredKnownEmpty (to
// keep metadata contexts in play) set this one instead.
NoServerBackstop bool
}

func RequiredStatusCheckContexts(ctx context.Context, client *gh.Client, owner, repo, branch string, configSet map[string]bool, configKnown bool) (map[string]bool, bool) {
Expand Down Expand Up @@ -474,7 +481,11 @@ func HeadCommitPushedAt(ctx context.Context, client *gh.Client, owner, repo, sha
// real CI statuses/check-runs still block so the sweep does not race in-flight
// CI, while known metadata contexts are ignored. Without that option, or when
// RequiredKnownEmpty says the server enforces nothing, completed failures
// still block locally (the older fail-closed positive-evidence policy).
// still block locally (the older fail-closed positive-evidence policy),
// except a cancelled check run, which carries no verdict when the required
// set is unknown and the server enforces it; with no server backstop
// (RequiredKnownEmpty or NoServerBackstop) a cancelled non-ignorable check
// still blocks as check-cancelled.
// Later blockers are still walked after the first so that Evidence and
// MissingRequired are complete for the caller. A non-nil error means the
// evidence could not be gathered; Reason then names the failing API
Expand All @@ -493,6 +504,9 @@ func EvaluateCommitCI(ctx context.Context, client *gh.Client, owner, repo, sha s
// list: either the server will enforce it, or it is known to be empty.
ignoreMetaContexts := serverEnforcedUnknown || opts.RequiredKnownEmpty
blockCompletedFailures := !serverEnforcedUnknown || opts.RequiredKnownEmpty
// A cancelled run only carries no verdict when the merge endpoint still
// enforces the real required checks; with a known-empty set nothing does.
blockCancelled := opts.RequiredKnownEmpty || opts.NoServerBackstop
seen := make(map[string]bool)
requiredSuccess := make(map[string]bool)
ignoredMeta := make(map[string]bool)
Expand Down Expand Up @@ -657,6 +671,16 @@ func EvaluateCommitCI(ctx context.Context, client *gh.Client, owner, repo, sha s
requiredSuccess[name] = true
}
case "neutral", "skipped":
case "cancelled":
// A cancelled run (e.g. an advisory job that hit its timeout)
// carries no verdict of its own, like opaqueWorkflowRuns. With the
// required set known it is still a required failure, and with no
// server backstop it blocks locally like any other failure.
if requiredKnown {
block("required-check-failing:" + name)
} else if blockCancelled && !isIgnorableCICheck(name) {
block("check-cancelled")
}
default:
if requiredKnown {
block("required-check-failing:" + name)
Expand Down
97 changes: 97 additions & 0 deletions src/pkg/github/commit_ci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,21 @@ func TestEvaluateCommitCIFreshHeadExpectedChecks(t *testing.T) {
},
want: "check-failure",
},
{
name: "cancelled with known-empty required set blocks locally",
checks: []map[string]string{
{"name": "build", "status": "completed", "conclusion": "cancelled"},
{"name": "lint", "status": "completed", "conclusion": "success"},
},
opts: CommitCIOptions{
RequiredKnownEmpty: true,
RequireEvidence: true,
MinHeadAge: 3 * time.Minute,
HeadPushedAt: now.Add(-10 * time.Minute),
Now: func() time.Time { return now },
},
want: "check-cancelled",
},
{
name: "require evidence refuses empty actual-required repos with no statuses",
opts: CommitCIOptions{
Expand Down Expand Up @@ -429,6 +444,88 @@ func TestEvaluateCommitCIFailOnAnyFailedCheck(t *testing.T) {
}
}

// hivecommons/hive#17598: the merge-request gate calls EvaluateCommitCI
// without UnknownRequiredChecksServerEnforced, so completed failures block
// locally when the required set is unknown. A cancelled check run (an
// advisory job that hit its timeout) carries no verdict there; a real failure
// next to it still blocks.
func TestEvaluateCommitCICancelledCheckUnknownRequiredSet(t *testing.T) {
tests := []struct {
name string
checks []map[string]string
opts CommitCIOptions
wantGreen bool
want string
}{
{
name: "cancelled advisory check carries no verdict",
checks: []map[string]string{
{"name": "build", "status": "completed", "conclusion": "success"},
{"name": "mutation", "status": "completed", "conclusion": "cancelled"},
},
wantGreen: true,
},
{
name: "cancelled check blocks when nothing enforces required checks server-side",
checks: []map[string]string{
{"name": "build", "status": "completed", "conclusion": "success"},
{"name": "mutation", "status": "completed", "conclusion": "cancelled"},
},
opts: CommitCIOptions{NoServerBackstop: true},
want: "check-cancelled",
},
{
name: "failure still blocks next to a cancelled check",
checks: []map[string]string{
{"name": "build", "status": "completed", "conclusion": "success"},
{"name": "mutation", "status": "completed", "conclusion": "cancelled"},
{"name": "lint", "status": "completed", "conclusion": "failure"},
},
want: "check-failure",
},
{
name: "timed_out still blocks next to a cancelled check",
checks: []map[string]string{
{"name": "build", "status": "completed", "conclusion": "success"},
{"name": "mutation", "status": "completed", "conclusion": "cancelled"},
{"name": "lint", "status": "completed", "conclusion": "timed_out"},
},
want: "check-timed_out",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/repos/acme/widget/commits/sha/status":
json.NewEncoder(w).Encode(map[string]any{"state": "success", "total_count": 0})
case "/repos/acme/widget/commits/sha/check-runs":
json.NewEncoder(w).Encode(map[string]any{"total_count": len(tt.checks), "check_runs": tt.checks})
default:
t.Fatalf("unexpected request: %s %s", r.Method, r.URL.Path)
}
}))
defer api.Close()
client := gh.NewClient(nil)
base, err := url.Parse(api.URL + "/")
if err != nil {
t.Fatalf("ParseURL: %v", err)
}
client.BaseURL = base

opts := tt.opts
opts.RequireEvidence = true
st, err := EvaluateCommitCI(context.Background(), client, "acme", "widget", "sha", opts)
if err != nil {
t.Fatalf("EvaluateCommitCI returned error: %v", err)
}
if st.Green != tt.wantGreen || st.Reason != tt.want {
t.Fatalf("EvaluateCommitCI = (%v,%q), want (%v,%q)", st.Green, st.Reason, tt.wantGreen, tt.want)
}
})
}
}

// hivecommons/hive#12457: the required-only gate walks past red optional
// checks, but reports them so the stage engine can keep the PR out of the
// merge-queue stage. Meta contexts (tide) are not red checks; a red REQUIRED
Expand Down
4 changes: 3 additions & 1 deletion src/pkg/github/merge_ci_gate.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ func (c *Client) verifyMergeRequestCI(ctx context.Context, repo string, number i
cfgSet, cfgKnown := c.configRequiredChecksForRepo(owner + "/" + name)
required, requiredKnown, fromConfig, _, _ := RequiredStatusCheckContextsDetailedWithSource(ctx, c.client, owner, name, baseBranch, cfgSet, cfgKnown)
actualRequired, actualKnown := required, requiredKnown && !fromConfig
if actualKnown && len(required) == 0 {
noServerBackstop := actualKnown && len(required) == 0
if noServerBackstop {
// GitHub reports both "branch not protected" and "protected but no
// required checks" as a known empty set. For the merge-request watcher
// that must not mean "ignore failing CI": absent required-check config
Expand Down Expand Up @@ -209,6 +210,7 @@ func (c *Client) verifyMergeRequestCI(ctx context.Context, repo string, number i
RequiredKnown: requiredKnown,
RequiredKnownFromConfig: fromConfig,
FailOnAnyFailedCheck: true,
NoServerBackstop: noServerBackstop,
ExpectedChecks: expected,
MinHeadAge: minHeadAge,
HeadPushedAt: minHeadAgeFrom,
Expand Down
Loading