11#!/usr/bin/env node
22import { execFileSync , spawnSync } from "node:child_process" ;
3- import { mkdirSync , writeFileSync } from "node:fs" ;
3+ import { mkdirSync , readFileSync , writeFileSync } from "node:fs" ;
44import path from "node:path" ;
5+ import { minimatch } from "minimatch" ;
6+ import { parse } from "yaml" ;
57import { booleanFlag , parseFlagArgs , stringFlag } from "./lib/arg-utils.mjs" ;
68import { isDirectRunUrl } from "./lib/direct-run.mjs" ;
79import { 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 ( ) ] ;
1518const CI_WORKFLOW_PATH = ".github/workflows/ci.yml" ;
1619const BUILD_ARTIFACTS_WORKFLOW = "Blacksmith Build Artifacts Testbox" ;
1720const ARTIFACT_FALLBACK_REQUIRED_WORKFLOWS = [
@@ -31,6 +34,7 @@ const HOSTED_GATE_CLOCK_SKEW_MS = 5 * 60 * 1_000;
3134const MAX_CI_REUSE_CANDIDATES = 5 ;
3235const CI_REUSE_RUN_LIST_LIMIT = 50 ;
3336const GIT_MAX_BUFFER_BYTES = 64 * 1024 * 1024 ;
37+ const PATH_GLOB_OPTIONS = { dot : true , nocomment : true , nonegate : true } ;
3438
3539export 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+
90144function 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+
764858function 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
0 commit comments