|
| 1 | +// Drift guard: fails if the local CI-gate manifest (bin/ci-gates.js) falls out of |
| 2 | +// sync with .github/workflows/tests-pr.yml, or if the pinned tool versions in |
| 3 | +// dev.yml and tests-pr.yml disagree. Runs in CI (ci-gate-sync job) and locally |
| 4 | +// (pnpm check-ci-gates, also invoked by pre-ci). |
| 5 | +import {readFileSync} from 'node:fs' |
| 6 | +import {fileURLToPath} from 'node:url' |
| 7 | +import {dirname, join} from 'node:path' |
| 8 | + |
| 9 | +import {MANIFEST_JOB_IDS} from './ci-gates.js' |
| 10 | + |
| 11 | +const root = join(dirname(fileURLToPath(import.meta.url)), '..') |
| 12 | +const read = (rel) => readFileSync(join(root, rel), 'utf8') |
| 13 | + |
| 14 | +const problems = [] |
| 15 | + |
| 16 | +// 1. Workflow job ids must exactly match the manifest job ids. |
| 17 | +const workflow = read('.github/workflows/tests-pr.yml') |
| 18 | +const jobsSection = workflow.slice(workflow.search(/^jobs:/m)) |
| 19 | +const workflowJobIds = [...jobsSection.matchAll(/^ {2}([A-Za-z0-9_-]+):\s*$/gm)].map((match) => match[1]) |
| 20 | + |
| 21 | +const manifestSet = new Set(MANIFEST_JOB_IDS) |
| 22 | +const workflowSet = new Set(workflowJobIds) |
| 23 | +const missingFromManifest = workflowJobIds.filter((id) => !manifestSet.has(id)) |
| 24 | +const staleInManifest = MANIFEST_JOB_IDS.filter((id) => !workflowSet.has(id)) |
| 25 | + |
| 26 | +if (missingFromManifest.length > 0) { |
| 27 | + problems.push( |
| 28 | + `Workflow jobs not classified in bin/ci-gates.js: ${missingFromManifest.join(', ')}.\n` + |
| 29 | + ` Add each to CI_GATES as a 'pre-ci' gate (with a local command) or 'ci-only' (with a reason).`, |
| 30 | + ) |
| 31 | +} |
| 32 | +if (staleInManifest.length > 0) { |
| 33 | + problems.push( |
| 34 | + `bin/ci-gates.js lists jobs absent from tests-pr.yml: ${staleInManifest.join(', ')}.\n` + |
| 35 | + ` Remove them or fix the job id.`, |
| 36 | + ) |
| 37 | +} |
| 38 | + |
| 39 | +// 2. Pinned tool versions must agree between dev.yml and tests-pr.yml. |
| 40 | +const devYml = read('dev.yml') |
| 41 | +const pick = (source, regex, label) => { |
| 42 | + const match = source.match(regex) |
| 43 | + if (!match) problems.push(`Could not parse ${label}.`) |
| 44 | + return match ? match[1] : null |
| 45 | +} |
| 46 | + |
| 47 | +const ciNode = pick(workflow, /DEFAULT_NODE_VERSION:\s*'([^']+)'/, 'DEFAULT_NODE_VERSION in tests-pr.yml') |
| 48 | +const devNode = pick(devYml, /node:[\s\S]*?version:\s*([0-9][\w.-]*)/, 'node version in dev.yml') |
| 49 | +const ciPnpm = pick(workflow, /PNPM_VERSION:\s*'([^']+)'/, 'PNPM_VERSION in tests-pr.yml') |
| 50 | +const devPnpm = pick(devYml, /package_manager:\s*pnpm@([0-9][\w.-]*)/, 'pnpm version in dev.yml') |
| 51 | + |
| 52 | +if (ciNode && devNode && ciNode !== devNode) { |
| 53 | + problems.push(`Node version mismatch: dev.yml ${devNode} vs tests-pr.yml DEFAULT_NODE_VERSION ${ciNode}.`) |
| 54 | +} |
| 55 | +if (ciPnpm && devPnpm && ciPnpm !== devPnpm) { |
| 56 | + problems.push(`pnpm version mismatch: dev.yml ${devPnpm} vs tests-pr.yml PNPM_VERSION ${ciPnpm}.`) |
| 57 | +} |
| 58 | + |
| 59 | +if (problems.length > 0) { |
| 60 | + console.error('CI gate manifest is out of sync with the workflow:\n') |
| 61 | + for (const problem of problems) console.error(`- ${problem}`) |
| 62 | + process.exit(1) |
| 63 | +} |
| 64 | + |
| 65 | +console.log(`CI gate manifest in sync: ${workflowJobIds.length} workflow jobs classified; tool versions match (node ${ciNode}, pnpm ${ciPnpm}).`) |
0 commit comments