diff --git a/changelog.d/fixed-merge-gate-cancelled-check.md b/changelog.d/fixed-merge-gate-cancelled-check.md new file mode 100644 index 0000000000..2daf019016 --- /dev/null +++ b/changelog.d/fixed-merge-gate-cancelled-check.md @@ -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). diff --git a/src/pkg/github/commit_ci.go b/src/pkg/github/commit_ci.go index 0e85d08a8a..84f89c615c 100644 --- a/src/pkg/github/commit_ci.go +++ b/src/pkg/github/commit_ci.go @@ -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) { @@ -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 @@ -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) @@ -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) diff --git a/src/pkg/github/commit_ci_test.go b/src/pkg/github/commit_ci_test.go index 1ffb3e4e7c..5f4ac0e99b 100644 --- a/src/pkg/github/commit_ci_test.go +++ b/src/pkg/github/commit_ci_test.go @@ -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{ @@ -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 diff --git a/src/pkg/github/merge_ci_gate.go b/src/pkg/github/merge_ci_gate.go index 6fb3055e12..05316bde4b 100644 --- a/src/pkg/github/merge_ci_gate.go +++ b/src/pkg/github/merge_ci_gate.go @@ -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 @@ -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,