From 066e6f059c3efb05f6ad67202246fed47728bb99 Mon Sep 17 00:00:00 2001 From: AsperforMias Date: Sun, 8 Nov 2026 07:01:38 -0800 Subject: [PATCH] ci: check the hub contract in component CI Private-repo PR CI now fails when a pnpm script or scripts/ file that a hub workflow calls is missing (server v1.0.5 removed smoke:backend:* and broke deploy CI), and runs ci.yml's Application-checks node:test suites (about 4s), including the production migration approval guard that would have stopped the 0034 incident. Both incidents were replayed against an assembled workspace and go red. Co-Authored-By: Claude Opus 5.5 --- .github/compose/check-hub-contract.mjs | 59 ++++++++++++++++++++++++++ .github/workflows/ci.yml | 3 ++ .github/workflows/component-ci.yml | 6 +++ docs/deployment.zh.md | 6 +-- docs/runbook.md | 6 ++- 5 files changed, 75 insertions(+), 5 deletions(-) create mode 100644 .github/compose/check-hub-contract.mjs diff --git a/.github/compose/check-hub-contract.mjs b/.github/compose/check-hub-contract.mjs new file mode 100644 index 00000000..869bd2fb --- /dev/null +++ b/.github/compose/check-hub-contract.mjs @@ -0,0 +1,59 @@ +#!/usr/bin/env node +// Hub contract: everything this repository's workflows call in the private +// components must exist in the assembled workspace. Run from the workspace root. +// +// node .github/compose/check-hub-contract.mjs # names and paths only +// node .github/compose/check-hub-contract.mjs --run-tests # also run ci.yml's node:test suites +// +// Component CI runs it on every private-repo PR, so removing or renaming a script +// that a deploy-branch workflow uses fails there, not after release (server v1.0.5). +// --run-tests brings ci.yml's dependency-free node:test suites to PR time, such as +// the production migration approval guard (the 0034 incident). +import { readdirSync, readFileSync, existsSync } from 'node:fs'; +import { execFileSync } from 'node:child_process'; +import { join } from 'node:path'; + +const workflowsDir = '.github/workflows'; +// Tests come from ci.yml's Application checks job (verify), which needs no services; +// these of its suites need a build or the workerd runtime that PR CI does not set up. +const testsNeedingBuild = new Set(['scripts/google-oauth-workerd.test.mjs', 'scripts/cost-monitor-watchdog.test.mjs', 'scripts/cost-monitor-verify.test.mjs', 'scripts/traffic-report-preview.test.mjs']); +const pnpmBuiltins = new Set(['install', 'i', 'add', 'exec', 'dlx', 'run', 'why', 'store', 'list', 'ls', 'audit']); + +// Commands only: comment lines often quote commands that no longer exist. +const commandText = file => readFileSync(`${workflowsDir}/${file}`, 'utf8').split('\n').filter(line => !/^\s*#/.test(line)).join('\n'); +// component-*.yml run in the private repos' own CI, which already fails on them. +const workflows = readdirSync(workflowsDir).filter(f => /\.ya?ml$/.test(f) && !f.startsWith('component-')).map(file => ({ file, text: commandText(file) })); +// A step starts at "- key:"; its commands resolve against its working-directory. +const steps = text => text.split(/\n(?=\s*- [\w-]+:)/).map(body => ({ body, dir: body.match(/^\s*working-directory:\s*['"]?([^'"\s]+)/m)?.[1] ?? '.' })); +const scriptsIn = dir => existsSync(join(dir, 'package.json')) ? JSON.parse(readFileSync(join(dir, 'package.json'), 'utf8')).scripts ?? {} : {}; +const problems = []; + +for (const { file, text } of workflows) { + for (const { body, dir } of steps(text)) { + if (dir.includes('${{')) continue; + const scripts = scriptsIn(dir); + for (const [, name] of body.matchAll(/(?:^|[ \t;&|(])pnpm[ \t]+(?:run[ \t]+)?([a-z][\w:.-]*)/gm)) { + if (!pnpmBuiltins.has(name) && !Object.hasOwn(scripts, name)) problems.push(`${file}: pnpm ${name} — no such script in ${join(dir, 'package.json')}`); + } + for (const [path] of body.matchAll(/(? w.file === 'ci.yml').text; + const verifyJob = ci.match(/\n verify:\n([\s\S]*?)(?=\n [a-z][\w-]*:\n|$)/)?.[1]; + if (!verifyJob) throw new Error('hub contract: ci.yml has no verify job'); + const tests = [...new Set([...verifyJob.matchAll(/node --test ([^\n;&|]+)/g)].flatMap(([, list]) => list.trim().split(/\s+/)))].filter(t => !testsNeedingBuild.has(t)); + console.log(`hub contract: running ${tests.length} ci.yml node:test suites`); + execFileSync(process.execPath, ['--test', ...tests], { stdio: 'inherit' }); +} diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d651befd..e2f1431c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -64,6 +64,9 @@ jobs: - name: Install locked dependencies run: pnpm install --frozen-lockfile + - name: Hub contract (every script these workflows call exists) + run: node .github/compose/check-hub-contract.mjs + - name: Verify release versions # Consistency of config/release-versions.json and the version constants in # the assembled workspace. The diff-based rule (a version change must update diff --git a/.github/workflows/component-ci.yml b/.github/workflows/component-ci.yml index d26031bf..d6599d4f 100644 --- a/.github/workflows/component-ci.yml +++ b/.github/workflows/component-ci.yml @@ -94,6 +94,12 @@ jobs: echo "::error::Dependencies differ from ghfind-server's pnpm-lock.yaml. Dependency changes need a matching lockfile update in FindAny/ghfind-server." exit 1 fi + - name: Hub contract (scripts the deploy workflows call, release contract tests) + working-directory: workspace + run: | + # The workspace is the latest hub release; releases before the check have no script. + if [ -f .github/compose/check-hub-contract.mjs ]; then node .github/compose/check-hub-contract.mjs --run-tests + else echo "::notice::hub ${{ steps.public.outputs.ref }} predates the hub contract check"; fi - name: Verify release versions against ghfind-server history if: inputs.component == 'ghfind-server' working-directory: self diff --git a/docs/deployment.zh.md b/docs/deployment.zh.md index 9735b73c..71467ad6 100644 --- a/docs/deployment.zh.md +++ b/docs/deployment.zh.md @@ -92,7 +92,7 @@ ghfind 是 GitHub 开发者与开源项目的评估平台,生产站点为 ' .github/` here. +still ran it. Fixed by hub v1.0.2.) Component CI enforces this: on every private-repo +PR, `.github/compose/check-hub-contract.mjs` fails if a `pnpm` script or `scripts/` +file that a hub workflow calls is missing. It also runs ci.yml's Application-checks +`node:test` suites, which include the production migration approval guard. ### compose