Skip to content

Commit 8c68f74

Browse files
authored
fix(ci): align hosted gates with workflow applicability (#120313)
1 parent c691f2e commit 8c68f74

6 files changed

Lines changed: 214 additions & 12 deletions

File tree

‎.github/workflows/ci-build-artifacts-testbox.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ on:
88
description: "Testbox session ID"
99
required: true
1010
pull_request:
11+
types: [opened, reopened, synchronize, ready_for_review]
1112
paths:
1213
- ".github/workflows/**"
1314

‎.github/workflows/ci-check-arm-testbox.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ on:
77
description: "Testbox session ID"
88
required: true
99
pull_request:
10+
types: [opened, reopened, synchronize, ready_for_review]
1011
paths:
1112
- ".github/workflows/**"
1213

‎scripts/verify-pr-hosted-gates.d.mts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export function parseWorkflowRunPage(raw: unknown): {
1212
workflowRuns: unknown;
1313
};
1414
export function workflowRunPageCount(totalCount: unknown): number;
15+
export function notApplicableScheduledHostedWorkflows(changedPaths: string[]): string[];
1516
export function collectHostedGateEvidence({
1617
sha,
1718
pr,
@@ -21,6 +22,7 @@ export function collectHostedGateEvidence({
2122
pullRequestHeadRepository,
2223
workflowRuns,
2324
ciGateJobs,
25+
notApplicableScheduledWorkflows,
2426
changelogOnly,
2527
nowMs,
2628
}: {
@@ -32,6 +34,7 @@ export function collectHostedGateEvidence({
3234
pullRequestHeadRepository?: string;
3335
workflowRuns: Array<Record<string, unknown>>;
3436
ciGateJobs?: Array<Record<string, unknown>>;
37+
notApplicableScheduledWorkflows?: string[];
3538
changelogOnly?: boolean | undefined;
3639
nowMs?: number | undefined;
3740
}): {
@@ -54,6 +57,7 @@ export function collectHostedGateEvidence({
5457
coveredBy: string;
5558
reason: string;
5659
}[];
60+
notApplicableWorkflows?: string[];
5761
};
5862
export function compareCommitPageCount(totalCommits: number): number;
5963
export function workflowRunQueryPaths(

‎scripts/verify-pr-hosted-gates.mjs‎

Lines changed: 112 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
#!/usr/bin/env node
22
import { execFileSync, spawnSync } from "node:child_process";
3-
import { mkdirSync, writeFileSync } from "node:fs";
3+
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
44
import path from "node:path";
5+
import { minimatch } from "minimatch";
6+
import { parse } from "yaml";
57
import { booleanFlag, parseFlagArgs, stringFlag } from "./lib/arg-utils.mjs";
68
import { isDirectRunUrl } from "./lib/direct-run.mjs";
79
import { execGhApiRead, plainGhEnv } from "./lib/plain-gh.mjs";
810

9-
export const SCHEDULED_HOSTED_WORKFLOWS = [
10-
"Blacksmith Testbox",
11-
"Blacksmith ARM Testbox",
12-
"Blacksmith Build Artifacts Testbox",
13-
"Workflow Sanity",
14-
];
11+
const SCHEDULED_HOSTED_WORKFLOW_PATHS = new Map([
12+
["Blacksmith Testbox", ".github/workflows/ci-check-testbox.yml"],
13+
["Blacksmith ARM Testbox", ".github/workflows/ci-check-arm-testbox.yml"],
14+
["Blacksmith Build Artifacts Testbox", ".github/workflows/ci-build-artifacts-testbox.yml"],
15+
["Workflow Sanity", ".github/workflows/workflow-sanity.yml"],
16+
]);
17+
export const SCHEDULED_HOSTED_WORKFLOWS = [...SCHEDULED_HOSTED_WORKFLOW_PATHS.keys()];
1518
const CI_WORKFLOW_PATH = ".github/workflows/ci.yml";
1619
const BUILD_ARTIFACTS_WORKFLOW = "Blacksmith Build Artifacts Testbox";
1720
const ARTIFACT_FALLBACK_REQUIRED_WORKFLOWS = [
@@ -31,6 +34,7 @@ const HOSTED_GATE_CLOCK_SKEW_MS = 5 * 60 * 1_000;
3134
const MAX_CI_REUSE_CANDIDATES = 5;
3235
const CI_REUSE_RUN_LIST_LIMIT = 50;
3336
const GIT_MAX_BUFFER_BYTES = 64 * 1024 * 1024;
37+
const PATH_GLOB_OPTIONS = { dot: true, nocomment: true, nonegate: true };
3438

3539
export function parseArgs(argv) {
3640
const args = {
@@ -87,6 +91,56 @@ export function parseArgs(argv) {
8791
return args;
8892
}
8993

94+
function matchesOrderedPathPatterns(changedPath, patterns) {
95+
let included = false;
96+
for (const declaredPattern of patterns ?? []) {
97+
const excluded = declaredPattern.startsWith("!");
98+
const pattern = excluded ? declaredPattern.slice(1) : declaredPattern;
99+
if (minimatch(changedPath, pattern, PATH_GLOB_OPTIONS)) {
100+
included = !excluded;
101+
}
102+
}
103+
return included;
104+
}
105+
106+
function pullRequestPathFilterApplies(pullRequest, changedPaths) {
107+
if (changedPaths.length === 0) {
108+
return false;
109+
}
110+
const includedPatterns = pullRequest.paths ?? [];
111+
if (includedPatterns.length > 0) {
112+
return changedPaths.some((changedPath) =>
113+
matchesOrderedPathPatterns(changedPath, includedPatterns),
114+
);
115+
}
116+
const ignoredPatterns = pullRequest["paths-ignore"] ?? [];
117+
if (ignoredPatterns.length > 0) {
118+
return changedPaths.some((changedPath) =>
119+
ignoredPatterns.every((pattern) => !minimatch(changedPath, pattern, PATH_GLOB_OPTIONS)),
120+
);
121+
}
122+
return true;
123+
}
124+
125+
export function notApplicableScheduledHostedWorkflows(changedPaths) {
126+
return [...SCHEDULED_HOSTED_WORKFLOW_PATHS]
127+
.filter(([expectedName, workflowPath]) => {
128+
const workflow = parse(readFileSync(workflowPath, "utf8"));
129+
if (workflow?.name !== expectedName) {
130+
throw new Error(`${workflowPath} must declare workflow name ${expectedName}.`);
131+
}
132+
if (!Object.hasOwn(workflow?.on ?? {}, "pull_request")) {
133+
throw new Error(`${workflowPath} must declare a pull_request trigger.`);
134+
}
135+
const pullRequest = workflow.on.pull_request ?? {};
136+
if (Object.hasOwn(pullRequest, "branches") || Object.hasOwn(pullRequest, "branches-ignore")) {
137+
throw new Error(`${workflowPath} pull_request branch filters are not supported.`);
138+
}
139+
return !pullRequestPathFilterApplies(pullRequest, changedPaths);
140+
})
141+
.map(([workflowName]) => workflowName);
142+
}
143+
90144
function formatObservedRuns(runs) {
91145
if (runs.length === 0) {
92146
return "none";
@@ -406,12 +460,21 @@ function runBelongsToPullRequest(
406460
);
407461
}
408462

409-
function canCoverQueuedBuildArtifacts(workflowRuns, sha, nowMs) {
463+
function canCoverQueuedBuildArtifacts(
464+
workflowRuns,
465+
sha,
466+
nowMs,
467+
notApplicableScheduledWorkflowNames,
468+
) {
410469
if (!hasSuccessfulRecentReleaseGate(workflowRuns, sha, nowMs)) {
411470
return false;
412471
}
413472
const supportingGatesPassed = ARTIFACT_FALLBACK_REQUIRED_WORKFLOWS.every((workflowName) => {
414-
const run = latestRun(matchingAuthoritativeRuns(workflowRuns, workflowName, sha, false));
473+
const matchingRuns = matchingAuthoritativeRuns(workflowRuns, workflowName, sha, false);
474+
if (matchingRuns.length === 0 && notApplicableScheduledWorkflowNames?.has(workflowName)) {
475+
return true;
476+
}
477+
const run = latestRun(matchingRuns);
415478
return isSuccessfulRecentRun(run, nowMs);
416479
});
417480
if (!supportingGatesPassed) {
@@ -465,12 +528,17 @@ export function collectHostedGateEvidence({
465528
ciGateJobs = [],
466529
loadCiReuseCandidates = () => [],
467530
execGit = runGit,
531+
notApplicableScheduledWorkflows,
468532
changelogOnly = false,
469533
nowMs = Date.now(),
470534
}) {
471535
if (!Array.isArray(workflowRuns)) {
472536
throw new Error("workflowRuns must be an array.");
473537
}
538+
const notApplicableScheduledWorkflowNames =
539+
notApplicableScheduledWorkflows === undefined
540+
? undefined
541+
: new Set(notApplicableScheduledWorkflows);
474542
const pullRequestCommitShaSet = new Set(pullRequestCommitShas);
475543

476544
const collectForSha = (
@@ -497,13 +565,23 @@ export function collectHostedGateEvidence({
497565
evidenceSha,
498566
allowManual,
499567
);
500-
if (matchingRuns.length === 0 && !requiredScheduledWorkflows.has(workflowName)) {
568+
if (
569+
matchingRuns.length === 0 &&
570+
!requiredScheduledWorkflows.has(workflowName) &&
571+
(notApplicableScheduledWorkflowNames === undefined ||
572+
notApplicableScheduledWorkflowNames.has(workflowName))
573+
) {
501574
continue;
502575
}
503576
if (
504577
allowManual &&
505578
workflowName === BUILD_ARTIFACTS_WORKFLOW &&
506-
canCoverQueuedBuildArtifacts(workflowRuns, evidenceSha, nowMs)
579+
canCoverQueuedBuildArtifacts(
580+
workflowRuns,
581+
evidenceSha,
582+
nowMs,
583+
notApplicableScheduledWorkflowNames,
584+
)
507585
) {
508586
fallbackCoveredWorkflows.push({
509587
name: workflowName,
@@ -562,6 +640,8 @@ export function collectHostedGateEvidence({
562640
const targetScheduledWorkflows = new Set(
563641
SCHEDULED_HOSTED_WORKFLOWS.filter(
564642
(workflowName) =>
643+
(notApplicableScheduledWorkflowNames !== undefined &&
644+
!notApplicableScheduledWorkflowNames.has(workflowName)) ||
565645
matchingAuthoritativeRuns(workflowRuns, workflowName, sha, false).length > 0,
566646
),
567647
);
@@ -632,6 +712,13 @@ export function collectHostedGateEvidence({
632712
if (selected.fallbackCoveredWorkflows.length > 0) {
633713
evidence.fallbackCoveredWorkflows = selected.fallbackCoveredWorkflows;
634714
}
715+
const notApplicableWorkflows = (notApplicableScheduledWorkflows ?? []).filter(
716+
(workflowName) =>
717+
matchingAuthoritativeRuns(workflowRuns, workflowName, sha, false).length === 0,
718+
);
719+
if (notApplicableWorkflows.length > 0) {
720+
evidence.notApplicableWorkflows = notApplicableWorkflows;
721+
}
635722
return evidence;
636723
}
637724

@@ -761,6 +848,13 @@ function loadPullRequestCommitShas(repo, { baseSha, headSha }) {
761848
return shas;
762849
}
763850

851+
function loadPullRequestChangedPaths(baseSha, headSha) {
852+
return runGit(["diff", "--name-only", `${baseSha}...${headSha}`])
853+
.trim()
854+
.split(/\r?\n/u)
855+
.filter(Boolean);
856+
}
857+
764858
function loadCiGateJobs(repo, workflowRuns, sha, nowMs = Date.now()) {
765859
// Only an in-progress exact-head CI run can benefit from gate proof.
766860
const candidates = workflowRuns.filter(
@@ -834,6 +928,7 @@ function main(argv = process.argv.slice(2)) {
834928
if (headSha !== args.sha) {
835929
throw new Error(`PR #${args.pr} head changed from ${args.sha} to ${headSha}.`);
836930
}
931+
const changedPaths = loadPullRequestChangedPaths(baseSha, headSha);
837932
const workflowRuns = loadWorkflowRuns(args.repo, args.sha, args.recentSha, headBranch);
838933
const evidence = collectHostedGateEvidence({
839934
sha: args.sha,
@@ -845,6 +940,7 @@ function main(argv = process.argv.slice(2)) {
845940
workflowRuns,
846941
ciGateJobs: loadCiGateJobs(args.repo, workflowRuns, args.sha),
847942
loadCiReuseCandidates: () => loadCiReuseCandidateRuns(args.repo, headBranch),
943+
notApplicableScheduledWorkflows: notApplicableScheduledHostedWorkflows(changedPaths),
848944
changelogOnly: args.changelogOnly,
849945
});
850946
const evidenceHeadSha = evidence.evidenceHeadSha ?? args.sha;
@@ -879,7 +975,11 @@ function main(argv = process.argv.slice(2)) {
879975
console.log(
880976
`Hosted gates passed for PR #${args.pr} at ${args.sha} using ${evidenceHeadSha}: ${manifest.workflows
881977
.map((workflow) => `${workflow.name}#${workflow.id}`)
882-
.join(", ")}`,
978+
.join(", ")}${
979+
manifest.notApplicableWorkflows?.length
980+
? `; not applicable: ${manifest.notApplicableWorkflows.join(", ")}`
981+
: ""
982+
}`,
883983
);
884984
}
885985

‎test/scripts/ci-workflow-guards.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,6 +1302,25 @@ NODE
13021302
});
13031303
});
13041304

1305+
it("keeps every path-filtered hosted gate runnable on landing-relevant events", () => {
1306+
const workflows = [
1307+
[".github/workflows/ci-check-testbox.yml", "check"],
1308+
[".github/workflows/ci-check-arm-testbox.yml", "check-arm"],
1309+
[".github/workflows/ci-build-artifacts-testbox.yml", "build-artifacts"],
1310+
] as const;
1311+
1312+
for (const [workflowPath, jobName] of workflows) {
1313+
const workflow = readWorkflow(workflowPath);
1314+
expect(workflow.on.pull_request).toEqual({
1315+
types: ["opened", "reopened", "synchronize", "ready_for_review"],
1316+
paths: [".github/workflows/**"],
1317+
});
1318+
expect(workflow.jobs[jobName].if).toBe(
1319+
"${{ github.event_name != 'pull_request' || !github.event.pull_request.draft }}",
1320+
);
1321+
}
1322+
});
1323+
13051324
it("pins every external GitHub Action reference to a full commit SHA", () => {
13061325
expect(findUnpinnedExternalActions()).toEqual([]);
13071326
});

‎test/scripts/verify-pr-hosted-gates.test.ts‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
collectHostedGateEvidence as collectHostedGateEvidenceRaw,
55
compareCommitPageCount,
66
HOSTED_GATE_MAX_AGE_HOURS,
7+
notApplicableScheduledHostedWorkflows,
78
parseArgs,
89
parseWorkflowRunPage,
910
SCHEDULED_HOSTED_WORKFLOWS,
@@ -173,6 +174,82 @@ function patchReuseOptions(
173174
}
174175

175176
describe("verify-pr-hosted-gates", () => {
177+
it("derives hosted-gate applicability from declared workflow path filters", () => {
178+
expect(notApplicableScheduledHostedWorkflows([".github/workflows/ci.yml"])).toEqual([]);
179+
expect(
180+
notApplicableScheduledHostedWorkflows(["test/e2e/qa-lab/runtime/script-evidence.ts"]),
181+
).toEqual([
182+
"Blacksmith Testbox",
183+
"Blacksmith ARM Testbox",
184+
"Blacksmith Build Artifacts Testbox",
185+
]);
186+
expect(notApplicableScheduledHostedWorkflows(["CHANGELOG.md"])).toEqual(
187+
SCHEDULED_HOSTED_WORKFLOWS,
188+
);
189+
});
190+
191+
it("requires an authoritative ARM run when declared workflow paths apply", () => {
192+
const workflowRuns = [
193+
successfulRun("CI", 1, "2026-06-17T10:47:00Z"),
194+
successfulRun("Blacksmith Testbox", 2, "2026-06-17T10:48:00Z"),
195+
successfulRun("Blacksmith Build Artifacts Testbox", 3, "2026-06-17T10:49:00Z"),
196+
successfulRun("Workflow Sanity", 4, "2026-06-17T10:50:00Z"),
197+
];
198+
199+
expect(() =>
200+
collectHostedGateEvidence({
201+
sha,
202+
workflowRuns,
203+
notApplicableScheduledWorkflows: [],
204+
}),
205+
).toThrow(`Missing successful recent Blacksmith ARM Testbox workflow for ${sha}`);
206+
});
207+
208+
it("records path-filtered hosted gates as not applicable for QA-only changes", () => {
209+
const evidence = collectHostedGateEvidence({
210+
sha,
211+
workflowRuns: [
212+
successfulRun("CI", 1, "2026-06-17T10:47:00Z"),
213+
successfulRun("Workflow Sanity", 2, "2026-06-17T10:48:00Z"),
214+
],
215+
notApplicableScheduledWorkflows: [
216+
"Blacksmith Testbox",
217+
"Blacksmith ARM Testbox",
218+
"Blacksmith Build Artifacts Testbox",
219+
],
220+
});
221+
222+
expect(evidence.workflows).toEqual([
223+
expect.objectContaining({ name: "CI", id: 1 }),
224+
expect.objectContaining({ name: "Workflow Sanity", id: 2 }),
225+
]);
226+
expect(evidence.notApplicableWorkflows).toEqual([
227+
"Blacksmith Testbox",
228+
"Blacksmith ARM Testbox",
229+
"Blacksmith Build Artifacts Testbox",
230+
]);
231+
});
232+
233+
it("does not require path-inapplicable ARM proof for queued artifact fallback", () => {
234+
const workflowRuns = queuedBuildArtifactFallbackRuns().filter(
235+
(run) => run.name !== "Blacksmith ARM Testbox",
236+
);
237+
const evidence = collectHostedGateEvidence({
238+
sha,
239+
workflowRuns,
240+
notApplicableScheduledWorkflows: ["Blacksmith ARM Testbox"],
241+
});
242+
243+
expect(evidence.fallbackCoveredWorkflows).toEqual([
244+
{
245+
name: BUILD_ARTIFACTS_WORKFLOW,
246+
coveredBy: "CI release gate",
247+
reason: "scheduled workflow is queued",
248+
},
249+
]);
250+
expect(evidence.notApplicableWorkflows).toEqual(["Blacksmith ARM Testbox"]);
251+
});
252+
176253
it("reuses successful recent CI from a patch-identical pre-rebase head", () => {
177254
const candidate = priorSuccessfulCiRun();
178255
const evidence = collectHostedGateEvidence({

0 commit comments

Comments
 (0)