From 02c4a23964458b80b5a863de6b5a9f1f663c9818 Mon Sep 17 00:00:00 2001 From: YunYouJun Date: Thu, 12 Nov 2026 00:14:43 +0800 Subject: [PATCH] ci: scope integration checks and share workspace setup --- .../actions/ios-test-environment/action.yml | 7 +- .github/actions/setup-js/action.yml | 17 ++ .github/workflows/ci.yml | 66 ++------ docs/dev/ios-ci.md | 10 ++ docs/dev/support-delivery-automation.md | 2 +- scripts/ci-changes.mjs | 47 +++++- test/ciChanges.test.mjs | 154 ++++++++++++++---- 7 files changed, 201 insertions(+), 102 deletions(-) create mode 100644 .github/actions/setup-js/action.yml diff --git a/.github/actions/ios-test-environment/action.yml b/.github/actions/ios-test-environment/action.yml index c92740aaf..af7c1694f 100644 --- a/.github/actions/ios-test-environment/action.yml +++ b/.github/actions/ios-test-environment/action.yml @@ -10,14 +10,9 @@ outputs: runs: using: composite steps: - - uses: pnpm/action-setup@73e46992e05b19abd31f2f3c20e0e7370d762115 # v6 - - uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7 - with: - node-version-file: .nvmrc - cache: pnpm + - uses: ./.github/actions/setup-js - shell: bash run: | - pnpm install --frozen-lockfile pnpm run convert if ! command -v xcodegen >/dev/null 2>&1; then brew install xcodegen diff --git a/.github/actions/setup-js/action.yml b/.github/actions/setup-js/action.yml new file mode 100644 index 000000000..ad9459ece --- /dev/null +++ b/.github/actions/setup-js/action.yml @@ -0,0 +1,17 @@ +name: Prepare JavaScript workspace +description: Install repository-pinned Node.js, pnpm and frozen workspace dependencies +inputs: + registry-url: + description: Optional npm registry configuration for jobs that also invoke npm + default: '' +runs: + using: composite + steps: + - uses: pnpm/action-setup@73e46992e05b19abd31f2f3c20e0e7370d762115 # v6 + - uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7 + with: + node-version-file: .nvmrc + cache: pnpm + registry-url: ${{ inputs.registry-url }} + - shell: bash + run: pnpm install --frozen-lockfile diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c477c6b27..d3153e6a0 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -38,6 +38,7 @@ jobs: core_key: ${{ steps.pr_core.outputs.key }} core_run: ${{ steps.pr_core.outputs.run_id }} skip_native: ${{ steps.changes.outputs.skip_native }} + skip_artifact: ${{ steps.changes.outputs.skip_artifact }} ios_suites: ${{ steps.changes.outputs.ios_suites }} ios_max_parallel: ${{ steps.changes.outputs.ios_max_parallel }} ios_storekit_devices: ${{ steps.changes.outputs.ios_storekit_devices }} @@ -53,7 +54,7 @@ jobs: - name: Test isolated iOS infrastructure recovery run: node --test test/iosTestInfrastructure.test.mjs test/iosStoreKitInfrastructure.test.mjs - run: node --test test/iosCoreReuse.test.mjs - - name: Plan native CI coverage + - name: Plan CI coverage id: changes env: GH_TOKEN: ${{ github.token }} @@ -70,17 +71,7 @@ jobs: steps: - uses: actions/checkout@v7 - - name: Install pnpm - uses: pnpm/action-setup@v6 - - - name: Set node - uses: actions/setup-node@v7 - with: - node-version-file: .nvmrc - cache: pnpm - - - name: Install - run: pnpm install --frozen-lockfile + - uses: ./.github/actions/setup-js - name: Lint run: pnpm run lint @@ -90,17 +81,7 @@ jobs: steps: - uses: actions/checkout@v7 - - name: Install pnpm - uses: pnpm/action-setup@v6 - - - name: Set node - uses: actions/setup-node@v7 - with: - node-version-file: .nvmrc - cache: pnpm - - - name: Install - run: pnpm install --frozen-lockfile + - uses: ./.github/actions/setup-js - name: Convert CSV to JSON run: pnpm run convert @@ -114,12 +95,7 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v7 - - uses: pnpm/action-setup@v6 - - uses: actions/setup-node@v7 - with: - node-version-file: .nvmrc - cache: pnpm - - run: pnpm install --frozen-lockfile + - uses: ./.github/actions/setup-js - run: pnpm run build - name: Build documentation run: pnpm run docs:build @@ -129,27 +105,20 @@ jobs: node scripts/compose-product-site.mjs test: + name: test (ubuntu-latest) + needs: changes + if: ${{ !cancelled() }} permissions: contents: read actions: read - runs-on: ${{ matrix.os }} - - strategy: - matrix: - os: [ubuntu-latest] - fail-fast: false + runs-on: ubuntu-latest steps: - uses: actions/checkout@v7 - - uses: pnpm/action-setup@v6 - - name: Use repository Node.js version - uses: actions/setup-node@v7 + - uses: ./.github/actions/setup-js with: - node-version-file: .nvmrc registry-url: https://registry.npmjs.org/ - cache: pnpm - - run: pnpm install --frozen-lockfile - run: pnpm run convert - name: Verify managed AI services and trusted source contracts run: | @@ -167,10 +136,10 @@ jobs: run: node --test --test-reporter=dot test/webBuildInfo.test.mjs test/productSiteBuild.test.mjs test/supportAgentHttp.test.mjs test/cookGithubChanges.test.mjs test/cookDeliverySync.test.mjs test/supportLedger.test.mjs test/supportArtifacts.test.mjs test/edgeoneRelease.test.mjs test/cookSupportRunner.test.mjs - run: pnpm run test - name: Install pinned artifact SDK for runtime contract verification - if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository + if: (github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository) && (needs.changes.result != 'success' || needs.changes.outputs.skip_artifact != 'true') run: npm install --prefix "$RUNNER_TEMP/cook-artifact-contract" --ignore-scripts --no-audit --no-fund @actions/artifact@6.3.1 @azure/core-xml@1.6.0 - name: Verify finalized checkpoints with the real Actions artifact runtime - if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository + if: (github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository) && (needs.changes.result != 'success' || needs.changes.outputs.skip_artifact != 'true') uses: actions/github-script@v9 env: GH_TOKEN: ${{ github.token }} @@ -187,7 +156,7 @@ jobs: runs-on: ubuntu-latest permissions: {} steps: - - name: Require all Web jobs and every matrix entry to succeed + - name: Require all Web jobs to succeed env: RESULTS: ${{ toJSON(needs) }} run: node -e 'if (Object.values(JSON.parse(process.env.RESULTS)).some(job => job.result !== "success")) process.exit(1)' @@ -199,14 +168,7 @@ jobs: runs-on: ubuntu-24.04 steps: - uses: actions/checkout@v7 - - uses: pnpm/action-setup@v6 - - name: Set node - uses: actions/setup-node@v7 - with: - node-version-file: .nvmrc - cache: pnpm - - - run: pnpm install --frozen-lockfile + - uses: ./.github/actions/setup-js - run: pnpm run convert - name: Import Swift release signing keys run: gpg --batch --import .github/keys/swift-release-keys.asc diff --git a/docs/dev/ios-ci.md b/docs/dev/ios-ci.md index 878fd4183..94ce2930f 100644 --- a/docs/dev/ios-ci.md +++ b/docs/dev/ios-ci.md @@ -1,5 +1,15 @@ # iOS CI +## 2026-11-12 无关触发与重复配置精简 + +Node.js、pnpm、缓存和冻结依赖安装统一由 `.github/actions/setup-js` 定义,Web/Swift 任务和 iOS 环境共用;每个 runner 仍独立安装,Web build 继续从全新检出执行。去掉只有 Ubuntu 一项的 test 矩阵,保留 `test (ubuntu-latest)` 和现有汇总检查名称。 + +独立 Support 同步、账本、artifact 工具及已列出的测试不再触发模拟器/Swift;混入原生、共享协议、工程或未知输入仍按原计划执行。artifact 真实联网往返只对明确不相关的文档、浏览器 UI、原生 Swift/StoreKit/测试计划以及菜谱 CSV/JSON 免除;Support、artifact 脚本、依赖、CI 配置和未知文件仍执行。所有本地 artifact/账本单元测试始终运行。 + +两种免除都要求非空、完整的变更比较;push 还要求同分支精确前一提交的 CI 已成功,避免后续无关提交取消并掩盖前一项检查。比较失败、计划失败、手动运行均保留完整检查,fork PR 仍不访问需要权限的真实 artifact 运行时。原生核心/会员/full 的用例、StoreKit 断言、限时和结果复用条件不变。 + +[基线 PR CI](https://github.com/YunYouJun/cook/actions/runs/43047563706) 的 iPad 为 28.8 分钟、iPhone 17.1 分钟、StoreKit 15.7 分钟,Web 测试 1.4 分钟。这次主要减少无关修改的触发,不能据此宣称 iOS 功能改动自身会编译或执行得更快。 + ## 2026-11-08 原生检查启动开销 [PR #205 的成功 CI](https://github.com/YunYouJun/cook/actions/runs/42458399313) 中,iPhone core 任务耗时 18 分 04 秒,iPad core 耗时 22 分 35 秒,StoreKit 耗时 10 分 12 秒。日志显示原生构建同时编译 `arm64` 与 `x86_64`,但每台 runner 只使用宿主架构的模拟器;StoreKit 在服务已经运行时仍强制 `kickstart`,冷服务控制及等待可占数分钟。 diff --git a/docs/dev/support-delivery-automation.md b/docs/dev/support-delivery-automation.md index 6c56a9ce3..46d20345a 100644 --- a/docs/dev/support-delivery-automation.md +++ b/docs/dev/support-delivery-automation.md @@ -80,7 +80,7 @@ GitHub 中现有 `Production – cook` 部署记录来自 Vercel,不作为 Edg node scripts/cook-support-sync.mjs --allow-initialize ``` -GitHub 运行使用 Node action 获取官方 artifact 运行时;浏览器只在需要新的生产验收时安装,已验收版本的送达核对复用既有验收证明。CI 会用独立的合成加密数据执行真实 artifact 上传、下载与摘要验收,不访问 Support,不使用工单检查点命名空间。 +GitHub 运行使用 Node action 获取官方 artifact 运行时;浏览器只在需要新的生产验收时安装,已验收版本的送达核对复用既有验收证明。CI 在 Support/artifact 工具、依赖、工作流或未知输入改变时,用独立的合成加密数据执行真实 artifact 上传、下载与摘要验收,不访问 Support,不使用工单检查点命名空间。明确无关的产品或文档改动可免除该联网步骤;本地账本与 artifact 测试始终执行。分类不可用或前一条 push 的 CI 未通过时保留联网检查,手动 CI 也执行该检查。 环境变量使用 `COOK_SUPPORT_AGENT_CONFIG`、`COOK_SUPPORT_BINDINGS_FILE` 指向仓库外 0600 JSON,`COOK_SUPPORT_STATE_DIR` 指向 0700 私密目录;另设置加密密钥、只读 GitHub token、EdgeOne token 和绝对路径 `COOK_PLAYWRIGHT_MODULE`。只有显式 `--apply` 才执行工单命令。不要在命令或公开日志中写入凭据值。 diff --git a/scripts/ci-changes.mjs b/scripts/ci-changes.mjs index ecb97cdb3..f035441d8 100644 --- a/scripts/ci-changes.mjs +++ b/scripts/ci-changes.mjs @@ -63,6 +63,32 @@ function isWebOnly(path) { || webTestFiles.has(path) } +// These standalone Support tools never generate native resources or contracts. +// Keep an explicit list so new automation inputs still get full coverage. +const supportOnlyFiles = new Set([ + '.github/workflows/support-sync.yml', + 'scripts/cook-support-sync.mjs', + 'scripts/prepare-support-feedback.mjs', + 'scripts/support-artifact-worker.mjs', + 'scripts/test-support-artifact-runtime.mjs', + 'scripts/lib/cook-support-runner.mjs', + 'scripts/lib/support-agent-http.mjs', + 'scripts/lib/support-artifacts.mjs', + 'scripts/lib/support-ledger.mjs', + 'test/cookSupportRunner.test.mjs', + 'test/supportAgentHttp.test.mjs', + 'test/supportArtifacts.test.mjs', + 'test/supportLedger.test.mjs', +]) + +function needsNoArtifactRuntime(path) { + // The live contract uses synthetic ledger bytes, not App sources or recipes. + // Its scripts, dependencies, workflow and unknown inputs always run it. + return isDocumentation(path) || isWebOnly(path) + || /^apps\/ios\/.+\.(?:swift|storekit|xctestplan)$/.test(path) + || /^packages\/data\/.+\.(?:csv|json)$/.test(path) +} + function isMembershipInput(path) { return /^apps\/ios\/(?:Cook\/(?:Model\/(?:NativeMembershipStoreKit|Membership\w*)|Views\/Membership\w*)|CookCore\/(?:Sources\/CookCore|Tests\/CookCoreTests)\/Membership\w*|Cook(?:UI)?Tests\/Membership\w*)\.swift$/.test(path) || path.startsWith('apps/ios/StoreKitValidation/') @@ -125,15 +151,16 @@ export function detectNativeChanges(eventName, event, cwd = process.cwd()) { }) const paths = diff.split('\0').filter(Boolean) return { - skipNative: paths.length > 0 && paths.every(path => isDocumentation(path) || isWebOnly(path) || path === '.github/workflows/support-sync.yml'), - scope: paths.some(isMembershipInput) && paths.every(path => isMembershipInput(path) || isDocumentation(path) || isWebOnly(path)) ? 'membership' : 'core', + skipNative: paths.length > 0 && paths.every(path => isDocumentation(path) || isWebOnly(path) || supportOnlyFiles.has(path)), + skipArtifact: paths.length > 0 && paths.every(needsNoArtifactRuntime), + scope: paths.some(isMembershipInput) && paths.every(path => isMembershipInput(path) || isDocumentation(path) || isWebOnly(path) || supportOnlyFiles.has(path)) ? 'membership' : 'core', changedFiles: paths.length, comparison: `${base}${separator}${head}`, } } catch { // An unavailable base (including a force push/new branch) must never skip tests. - return { skipNative: false, changedFiles: null, comparison: 'unavailable' } + return { skipNative: false, skipArtifact: false, changedFiles: null, comparison: 'unavailable' } } } @@ -144,8 +171,8 @@ function previousPushPassed(event) { const branch = event.ref.slice('refs/heads/'.length) try { - // A Web/docs/Support push can cancel the only run that covers the previous - // native change. Carry its coverage forward only from a successful push. + // A new push can cancel the only run that covers an earlier native or + // artifact change. Carry coverage forward only from a successful push. const output = execFileSync('gh', [ 'api', `repos/${repo}/actions/workflows/ci.yml/runs`, @@ -180,14 +207,18 @@ if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.ur event = {} } const result = detectNativeChanges(process.env.GITHUB_EVENT_NAME, event) - if (result.skipNative && process.env.GITHUB_EVENT_NAME === 'push') { + if ((result.skipNative || result.skipArtifact) && process.env.GITHUB_EVENT_NAME === 'push') { result.previousPushPassed = previousPushPassed(event) - result.skipNative = result.previousPushPassed + if (!result.previousPushPassed) { + result.skipNative = false + result.skipArtifact = false + } } const ios = planIosTests(process.env.GITHUB_EVENT_NAME, event?.inputs?.ios_coverage, result.scope) if (process.env.GITHUB_OUTPUT) { appendFileSync(process.env.GITHUB_OUTPUT, [ `skip_native=${result.skipNative}`, + `skip_artifact=${result.skipArtifact}`, `ios_suites=${JSON.stringify(ios.suites)}`, `ios_max_parallel=${ios.maxParallel}`, `ios_storekit_devices=${JSON.stringify(ios.storekitDevices)}`, @@ -196,7 +227,7 @@ if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.ur } if (process.env.GITHUB_STEP_SUMMARY) { const reason = result.previousPushPassed === false ? ' Previous push did not have a verified successful CI run.' : '' - appendFileSync(process.env.GITHUB_STEP_SUMMARY, `Native CI: ${result.skipNative ? 'not required (only browser-owned inputs, documentation or the Support sync workflow changed)' : ios.suites.join(', ')}. StoreKit devices: ${ios.storekitDevices.join(', ')}.${reason} Changed paths: ${result.changedFiles ?? 'unknown'}. Comparison: ${result.comparison}.\n`) + appendFileSync(process.env.GITHUB_STEP_SUMMARY, `Native CI: ${result.skipNative ? 'not required (only browser-owned inputs, documentation or standalone Support tools changed)' : ios.suites.join(', ')}. StoreKit devices: ${ios.storekitDevices.join(', ')}. Live artifact contract: ${result.skipArtifact ? 'not required' : 'required'}.${reason} Changed paths: ${result.changedFiles ?? 'unknown'}. Comparison: ${result.comparison}.\n`) } console.log(JSON.stringify({ ...result, ios })) } diff --git a/test/ciChanges.test.mjs b/test/ciChanges.test.mjs index f600bae5b..5a8506399 100644 --- a/test/ciChanges.test.mjs +++ b/test/ciChanges.test.mjs @@ -7,6 +7,7 @@ import process from 'node:process' // eslint-disable-next-line test/no-import-node-test -- This bootstrap check runs before installing Vitest or workspace dependencies. import { test } from 'node:test' import { fileURLToPath } from 'node:url' +import { runInNewContext } from 'node:vm' import { detectNativeChanges, planIosTests } from '../scripts/ci-changes.mjs' function repository(run) { @@ -79,6 +80,59 @@ test('the standalone Support workflow and its docs can skip native jobs, but mix }) }) +test('standalone Support and artifact edits require live artifact checks without native jobs', () => { + repository(({ cwd, write, commit, base }) => { + for (const path of [ + 'scripts/cook-support-sync.mjs', + 'scripts/prepare-support-feedback.mjs', + 'scripts/support-artifact-worker.mjs', + 'scripts/test-support-artifact-runtime.mjs', + 'scripts/lib/cook-support-runner.mjs', + 'scripts/lib/support-agent-http.mjs', + 'scripts/lib/support-artifacts.mjs', + 'scripts/lib/support-ledger.mjs', + 'test/cookSupportRunner.test.mjs', + 'test/supportAgentHttp.test.mjs', + 'test/supportArtifacts.test.mjs', + 'test/supportLedger.test.mjs', + ]) write(path) + const support = commit() + const result = detectNativeChanges('pull_request', { pull_request: { base: { sha: base }, head: { sha: support } } }, cwd) + assert.equal(result.skipNative, true) + assert.equal(result.skipArtifact, false) + write('apps/ios/Cook/App.swift', 'changed') + const mixed = detectNativeChanges('push', { before: base, after: commit() }, cwd) + assert.equal(mixed.skipNative, false) + assert.equal(mixed.skipArtifact, false) + }) +}) + +test('App and recipe edits omit the synthetic artifact roundtrip while retaining native coverage', () => { + for (const path of ['apps/ios/Cook/App.swift', 'apps/ios/StoreKitValidation/Products.storekit', 'apps/ios/Tests.xctestplan', 'packages/data/recipes.csv', 'packages/data/recipe-amounts.json']) { + repository(({ cwd, write, commit, base }) => { + write(path, 'changed') + const result = detectNativeChanges('push', { before: base, after: commit() }, cwd) + assert.equal(result.skipNative, false, path) + assert.equal(result.skipArtifact, true, path) + }) + } +}) + +test('artifact tooling, installation inputs, unknown paths and moved tooling retain the live contract', () => { + for (const path of ['.github/workflows/ci.yml', '.github/workflows/support-sync.yml', '.github/actions/setup-js/action.yml', '.nvmrc', '.npmrc', 'package.json', 'pnpm-lock.yaml', 'pnpm-workspace.yaml', 'scripts/ci-changes.mjs', 'scripts/lib/new-artifact-input.mjs']) { + repository(({ cwd, write, commit, base }) => { + write(path) + assert.equal(detectNativeChanges('push', { before: base, after: commit() }, cwd).skipArtifact, false, path) + }) + } + repository(({ cwd, write, commit }) => { + write('scripts/support-artifact-worker.mjs') + const base = commit() + renameSync(join(cwd, 'scripts/support-artifact-worker.mjs'), join(cwd, 'docs/dev/worker.md')) + assert.equal(detectNativeChanges('push', { before: base, after: commit() }, cwd).skipArtifact, false) + }) +}) + test('moving source code into documentation still includes the removed source path', () => { repository(({ cwd, commit, base }) => { renameSync(join(cwd, 'apps/ios/Cook/App.swift'), join(cwd, 'docs/dev/moved.md')) @@ -187,51 +241,58 @@ test('the workflow entry point writes job outputs and defaults to native on inva writeFileSync(eventPath, JSON.stringify({ pull_request: { base: { sha: base }, head: { sha: after } } })) execFileSync(process.execPath, [entry], { cwd, env }) const corePlan = 'ios_suites=["core-regression"]\nios_max_parallel=2\nios_storekit_devices=["iphone"]\n' - assert.equal(readFileSync(outputPath, 'utf8'), `skip_native=true\n${corePlan}`) - assert.match(readFileSync(summaryPath, 'utf8'), /not required \(only browser-owned inputs, documentation or the Support sync workflow changed\)/) + assert.equal(readFileSync(outputPath, 'utf8'), `skip_native=true\nskip_artifact=true\n${corePlan}`) + assert.match(readFileSync(summaryPath, 'utf8'), /not required \(only browser-owned inputs, documentation or standalone Support tools changed\)/) for (const invalidEvent of ['{ invalid JSON', 'null']) { writeFileSync(eventPath, invalidEvent) execFileSync(process.execPath, [entry], { cwd, env }) } - assert.equal(readFileSync(outputPath, 'utf8'), `skip_native=true\n${corePlan}${`skip_native=false\n${corePlan}`.repeat(2)}`) + assert.equal(readFileSync(outputPath, 'utf8'), `skip_native=true\nskip_artifact=true\n${corePlan}${`skip_native=false\nskip_artifact=false\n${corePlan}`.repeat(2)}`) }) }) -test('a support-only push carries coverage only from a green push at the exact previous branch head', () => { - repository(({ cwd, write, commit, base }) => { - write('.github/workflows/support-sync.yml') - const after = commit() - const eventPath = join(cwd, 'event.json') - writeFileSync(eventPath, JSON.stringify({ before: base, after, ref: 'refs/heads/main' })) - const bin = join(cwd, 'bin') - mkdirSync(bin) - writeFileSync(join(bin, 'gh'), `#!/usr/bin/env node +test('push skips require a green CI run at the exact previous branch head', () => { + for (const [path, skipNative, skipArtifact] of [ + ['.github/workflows/support-sync.yml', true, false], + ['apps/ios/Cook/App.swift', false, true], + ['docs/dev/ci.md', true, true], + ]) { + repository(({ cwd, write, commit, base }) => { + write(path, 'changed') + const after = commit() + const eventPath = join(cwd, 'event.json') + writeFileSync(eventPath, JSON.stringify({ before: base, after, ref: 'refs/heads/main' })) + const bin = join(cwd, 'bin') + mkdirSync(bin) + writeFileSync(join(bin, 'gh'), `#!/usr/bin/env node if (process.env.FAKE_GH_OUTPUT === 'error') process.exit(1) process.stdout.write(process.env.FAKE_GH_OUTPUT) `, { mode: 0o755 }) - const entry = fileURLToPath(new URL('../scripts/ci-changes.mjs', import.meta.url)) - const passed = { head_sha: base, head_branch: 'main', event: 'push', conclusion: 'success' } - for (const [response, expected] of [ - [{ workflow_runs: [passed] }, true], - [{ workflow_runs: [] }, false], - ...[{ conclusion: 'cancelled' }, { conclusion: 'failure' }, { conclusion: null }, { head_sha: after }, { head_branch: 'dev' }, { event: 'pull_request' }] - .map(change => [{ workflow_runs: [{ ...passed, ...change }] }, false]), - ['error', false], - ]) { - const env = { - ...process.env, - PATH: `${bin}:${process.env.PATH}`, - GITHUB_REPOSITORY: 'example/cook', - GITHUB_OUTPUT: join(cwd, 'push-output.txt'), - GITHUB_STEP_SUMMARY: join(cwd, 'push-summary.md'), - GITHUB_EVENT_NAME: 'push', - GITHUB_EVENT_PATH: eventPath, - FAKE_GH_OUTPUT: response === 'error' ? response : JSON.stringify(response), + const entry = fileURLToPath(new URL('../scripts/ci-changes.mjs', import.meta.url)) + const passed = { head_sha: base, head_branch: 'main', event: 'push', conclusion: 'success' } + for (const [response, verified] of [ + [{ workflow_runs: [passed] }, true], + [{ workflow_runs: [] }, false], + ...[{ conclusion: 'cancelled' }, { conclusion: 'failure' }, { conclusion: null }, { head_sha: after }, { head_branch: 'dev' }, { event: 'pull_request' }] + .map(change => [{ workflow_runs: [{ ...passed, ...change }] }, false]), + ['error', false], + ]) { + const env = { + ...process.env, + PATH: `${bin}:${process.env.PATH}`, + GITHUB_REPOSITORY: 'example/cook', + GITHUB_OUTPUT: join(cwd, 'push-output.txt'), + GITHUB_STEP_SUMMARY: join(cwd, 'push-summary.md'), + GITHUB_EVENT_NAME: 'push', + GITHUB_EVENT_PATH: eventPath, + FAKE_GH_OUTPUT: response === 'error' ? response : JSON.stringify(response), + } + const result = JSON.parse(execFileSync(process.execPath, [entry], { cwd, env, encoding: 'utf8' })) + assert.equal(result.skipNative, verified && skipNative, path) + assert.equal(result.skipArtifact, verified && skipArtifact, path) } - const result = JSON.parse(execFileSync(process.execPath, [entry], { cwd, env, encoding: 'utf8' })) - assert.equal(result.skipNative, expected) - } - }) + }) + } }) test('empty diffs, missing objects, zero SHAs, malformed events and unsupported events require native jobs', () => { @@ -244,7 +305,30 @@ test('empty diffs, missing objects, zero SHAs, malformed events and unsupported ['push', null], ['workflow_dispatch', {}], ]) { - assert.equal(detectNativeChanges(name, event, cwd).skipNative, false) + const result = detectNativeChanges(name, event, cwd) + assert.equal(result.skipNative, false) + assert.equal(result.skipArtifact, false) } }) }) + +test('live artifact steps fail closed on missing or failed plans and preserve fork restrictions', () => { + const workflow = readFileSync(new URL('../.github/workflows/ci.yml', import.meta.url), 'utf8') + const testJob = workflow.split('\n test:\n')[1].split('\n web-acceptance:\n')[0] + assert.match(testJob, /needs: changes\n {4}if: \$\{\{ !cancelled\(\) \}\}/) + const conditions = [...testJob.matchAll(/ {8}if: (.+needs\.changes\.outputs\.skip_artifact.+)/g)].map(match => match[1]) + assert.equal(conditions.length, 2, 'installation and execution share the guarded decision') + for (const condition of conditions) { + for (const [result, skip, expected] of [['success', 'true', false], ['success', 'false', true], ['success', undefined, true], ['failure', 'true', true], ['skipped', undefined, true]]) { + for (const eventName of ['push', 'pull_request', 'workflow_dispatch']) { + const github = { event_name: eventName, repository: 'owner/cook', event: { pull_request: { head: { repo: { full_name: 'owner/cook' } } } } } + const needs = { changes: { result, outputs: { skip_artifact: skip } } } + assert.equal(runInNewContext(condition, { github, needs }), expected) + if (eventName === 'pull_request') { + github.event.pull_request.head.repo.full_name = 'fork/cook' + assert.equal(runInNewContext(condition, { github, needs }), false) + } + } + } + } +})