diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index b23c66e..f74949b 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -47,3 +47,9 @@ jobs: run: | pnpm run check pnpm run test:all +env: + LAPIS_CI_EVENT: ${{ github.event_name }} + LAPIS_CI_PR_AUTHOR: ${{ github.event.pull_request.user.login }} + LAPIS_CI_PR_AUTHOR_TYPE: ${{ github.event.pull_request.user.type }} + LAPIS_CI_PR_HEAD: ${{ github.event.pull_request.head.ref }} + LAPIS_DEPENDENCY_BOT_LOGIN: ${{ vars.LAPIS_DEPENDENCY_BOT_LOGIN }} diff --git a/.github/workflows/npm-publish.yml b/.github/workflows/npm-publish.yml index 290a02a..85e907c 100644 --- a/.github/workflows/npm-publish.yml +++ b/.github/workflows/npm-publish.yml @@ -127,9 +127,7 @@ jobs: - package-gate - publish-production if: >- - always() && - needs.package-gate.result == 'success' && - (needs.publish-production.result == 'success' || needs.publish-production.result == 'skipped') + always() && needs.package-gate.result == 'success' && (needs.publish-production.result == 'success' || needs.publish-production.result == 'skipped') runs-on: ubuntu-latest permissions: contents: read @@ -202,3 +200,16 @@ jobs: test "$(gh release view "$GITHUB_REF_NAME" --repo "$GITHUB_REPOSITORY" --json tagName --jq .tagName)" = "$GITHUB_REF_NAME" test "$(gh release view "$GITHUB_REF_NAME" --repo "$GITHUB_REPOSITORY" --json isDraft --jq .isDraft)" = "false" test "$(gh release view "$GITHUB_REF_NAME" --repo "$GITHUB_REPOSITORY" --json isPrerelease --jq .isPrerelease)" = "false" + notify-dependencies: + name: Notify dependency consumers + needs: + - verify-provenance + if: always() && needs.verify-provenance.result == 'success' + permissions: + contents: read + uses: lapismd/dependency-automation/.github/workflows/notify.yml@ea9d35b12b612467bbabf1c6271ffa41b5eda4ba + with: + automation-ref: ea9d35b12b612467bbabf1c6271ffa41b5eda4ba + source-sha: ${{ github.sha }} + secrets: + app-private-key: ${{ secrets.LAPIS_DEPENDENCY_APP_PRIVATE_KEY }} diff --git a/package.json b/package.json index 0b63021..ab7fa8a 100644 --- a/package.json +++ b/package.json @@ -63,15 +63,15 @@ "test:all": "pnpm run test:workspace && pnpm run test:architecture && pnpm run test:node", "test": "pnpm run build && pnpm run test:all", "publint": "pnpm run build && publint", - "spec:validate": "pnpm run build && node ./dist/cli.js validate", + "spec:validate": "pnpm run build && node scripts/dependency-automation/spec-policy.mjs --local validate", "spec:test": "pnpm run test", - "spec:build": "pnpm run build && node ./dist/cli.js build", + "spec:build": "pnpm run build && node scripts/dependency-automation/spec-policy.mjs --local build", "spec:serve": "pnpm run build && node ./dist/cli.js serve", - "spec:first": "pnpm run build && node ./dist/cli.js first", - "spec:check": "pnpm run build && node ./dist/cli.js check", + "spec:first": "pnpm run build && node scripts/dependency-automation/spec-policy.mjs --local first", + "spec:check": "pnpm run build && node scripts/dependency-automation/spec-policy.mjs --local check", "spec:index": "pnpm run build && node ./dist/cli.js index", "spec:search": "pnpm run build && node ./dist/cli.js search", - "spec:lint": "pnpm run build && node ./dist/cli.js validate --only markdownlint", + "spec:lint": "node scripts/dependency-automation/spec-policy.mjs --command spec:lint 'pnpm run build && node scripts/dependency-automation/spec-policy.mjs --local validate --only markdownlint'", "doctor": "pnpm run build && node ./dist/cli.js doctor", "workspace:check": "tsx ./packages/workspace-tools/src/cli.ts links check", "workspace:sync": "tsx ./packages/workspace-tools/src/cli.ts links sync", diff --git a/scripts/dependency-automation/spec-policy.mjs b/scripts/dependency-automation/spec-policy.mjs new file mode 100644 index 0000000..60a5b34 --- /dev/null +++ b/scripts/dependency-automation/spec-policy.mjs @@ -0,0 +1,132 @@ +import { readFileSync } from "node:fs"; +import { spawnSync } from "node:child_process"; +import { pathToFileURL } from "node:url"; +import { resolve } from "node:path"; + +export function botSpecificationExemption(env = process.env) { + if (env.CI !== "true") return false; + let name = env.LAPIS_CI_EVENT ?? env.GITHUB_EVENT_NAME; + let author = env.LAPIS_CI_PR_AUTHOR, + type = env.LAPIS_CI_PR_AUTHOR_TYPE, + head = env.LAPIS_CI_PR_HEAD; + if (env.GITHUB_EVENT_PATH) { + try { + const event = JSON.parse(readFileSync(env.GITHUB_EVENT_PATH, "utf8")); + author = event.pull_request?.user?.login; + type = event.pull_request?.user?.type; + head = event.pull_request?.head?.ref; + name = env.GITHUB_EVENT_NAME; + } catch (error) { + if (error.code !== "ENOENT") throw error; + } + } + if (name !== "pull_request" || type !== "Bot") return false; + const trusted = new Set([ + "dependabot[bot]", + "renovate[bot]", + "lapismd-dependency-bot[bot]", + ]); + if (env.LAPIS_DEPENDENCY_BOT_LOGIN) + trusted.add(env.LAPIS_DEPENDENCY_BOT_LOGIN); + return ( + trusted.has(author) || + (author === "github-actions[bot]" && head === "changeset-release/main") + ); +} + +export function skipSpecification(lane, env = process.env) { + const spec = + /^(?:spec:(?:check|validate|first|build|lint)|check:specification-docs)$/.test( + lane, + ); + if (!spec || !botSpecificationExemption(env)) return false; + console.log( + `Specification check ${lane} skipped for the trusted dependency-bot PR; functional checks remain enabled.`, + ); + return true; +} + +export function specificationDockerArguments(args, env = process.env) { + if (args[0] !== "create" || !args.includes("/input/worker.mjs")) return args; + const names = [ + "CI", + "LAPIS_CI_EVENT", + "LAPIS_CI_PR_AUTHOR", + "LAPIS_CI_PR_AUTHOR_TYPE", + "LAPIS_CI_PR_HEAD", + "LAPIS_DEPENDENCY_BOT_LOGIN", + ]; + const context = names + .filter((name) => env[name] !== undefined) + .flatMap((name) => ["--env", `${name}=${env[name]}`]); + return [args[0], ...context, ...args.slice(1)]; +} + +export async function runFunctionalCheckLanes({ + local = false, + root = process.cwd(), + loadConfig, + run = spawnSync, +} = {}) { + const config = loadConfig + ? await loadConfig(root) + : await ( + await import( + local + ? pathToFileURL(resolve(root, "dist/index.js")).href + : "@lapismd/spec-validator" + ) + ).loadResolvedConfig(root); + for (const lane of config.check?.lanes ?? []) { + const result = run(lane.command, lane.args ?? [], { + cwd: root, + stdio: "inherit", + env: process.env, + shell: process.platform === "win32", + }); + if (result.error) throw result.error; + if (result.status !== 0) return result.status ?? 1; + } + return 0; +} + +if ( + process.argv[1] && + import.meta.url === pathToFileURL(resolve(process.argv[1])).href +) { + const args = process.argv.slice(2); + if (args[0] === "--command") { + const [, lane, command, ...rest] = args; + if (!skipSpecification(lane)) { + const result = spawnSync( + "/bin/sh", + ["-c", command + ' "$@"', "specification", ...rest], + { stdio: "inherit", env: process.env }, + ); + if (result.error) throw result.error; + process.exitCode = result.status ?? 1; + } + } else { + const local = args[0] === "--local"; + if (local) args.shift(); + const operation = args.shift(); + if (!/^(check|validate|first|build|lint)$/.test(operation ?? "")) + throw Error("Expected a specification check operation"); + if (!skipSpecification(`spec:${operation}`)) { + const command = local ? process.execPath : "pnpm"; + const childArgs = local + ? ["dist/cli.js", operation, ...args] + : ["exec", "spec-validator", operation, ...args]; + const result = spawnSync(command, childArgs, { + stdio: "inherit", + env: process.env, + }); + if (result.error) throw result.error; + process.exitCode = result.status ?? 1; + } else if (operation === "check") { + // The validator's aggregate command may contain package tests and audits. + // Skip its specification phases while retaining every configured lane. + process.exitCode = await runFunctionalCheckLanes({ local }); + } + } +} diff --git a/spec/src/architecture.md b/spec/src/architecture.md index 3c62e98..da76b24 100644 --- a/spec/src/architecture.md +++ b/spec/src/architecture.md @@ -2,6 +2,12 @@ The repository is a pure Node package: pnpm owns dependency installation and repository tasks, TypeScript compiles `src` to the published `dist` output, and Node 22 or newer is the only runtime requirement for contributors and consumers. +Trusted dependency-bot pull requests use the shared PR-author specification +exemption for repository automation. Aggregate commands retain their functional +checks, and ordinary pull requests retain specification validation. Verified npm +publication may notify the shared dependency coordinator; notifications cannot +publish, merge, sign or deploy anything. Existing release gates remain required. + ## Public surface coverage | Surface | Public boundary | Requirement |