diff --git a/docs/architecture.md b/docs/architecture.md index a4090f27f..244093d6e 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -993,7 +993,8 @@ The two Forge inputs are updated separately: hourly and on an `api-schemas-updated` `repository_dispatch`. It pins the latest `main` commit that changed `openapi.json`, regenerates with the pinned forge build, writes a `minor` changeset, and opens or refreshes the - `automation/update-forge` PR. The existing branch and concurrency group are + `automation/update-forge` PR. A revision that leaves the generated output + unchanged is skipped. The existing branch and concurrency group are retained so the renamed workflow continues managing existing update PRs. Both PRs rewrite `_generated/`; whichever merges second is rebased or diff --git a/scripts/README.md b/scripts/README.md index 3eeafec62..6cbd7ab4e 100644 --- a/scripts/README.md +++ b/scripts/README.md @@ -17,7 +17,9 @@ repository dispatch. It selects the latest commit on the `main` branch of [cloudflare/api-schemas](https://github.com/cloudflare/api-schemas) that changed `openapi.json`. It regenerates commands with the existing Forge package pin and adds a minor changeset. Generation and `pnpm check` must pass before it -pushes. +pushes. If a revision leaves the generated output unchanged, it skips the +update, so the pin stays on the older revision until a schema change affects +cf. The OpenAPI workflow retains the former `automation/update-forge` branch and `update-forge` concurrency group so it can continue maintaining existing PRs. diff --git a/scripts/update-openapi.test.ts b/scripts/update-openapi.test.ts index 043bfad73..cbf226ce2 100644 --- a/scripts/update-openapi.test.ts +++ b/scripts/update-openapi.test.ts @@ -12,6 +12,7 @@ import type { TestContext } from "node:test"; const originalToken = process.env.GH_TOKEN; process.env.GH_TOKEN = "cf-write-token"; const { + changesGeneratedOutput, createOrUpdatePullRequest, extractOpenApiVersion, formatError, @@ -165,6 +166,25 @@ await test("a missing API schema repository is reported", async (context) => { assert.deepEqual(requests, ["/repos/cloudflare/api-schemas/commits"]); }); +await test("an update that only moves the pin and adds its changeset is a no-op", () => { + const bookkeeping = [ + "packages/cli/package.json", + `.changeset/update-openapi-${version.slice(0, 12)}.md`, + ]; + assert.equal(changesGeneratedOutput(bookkeeping, version), false); + assert.equal(changesGeneratedOutput([], version), false); + for (const changed of [ + "packages/cli/src/commands/_generated/index.ts", + `.changeset/update-openapi-${"c".repeat(12)}.md`, + "pnpm-lock.yaml", + ]) { + assert.equal( + changesGeneratedOutput([...bookkeeping, changed], version), + true + ); + } +}); + await test("the renamed updater recognizes old and new bot commits while protecting human edits", async (context) => { const bot = "41898282+github-actions[bot]@users.noreply.github.com"; const sha = "a".repeat(40); diff --git a/scripts/update-openapi.ts b/scripts/update-openapi.ts index d3029ad5b..1e9c04955 100644 --- a/scripts/update-openapi.ts +++ b/scripts/update-openapi.ts @@ -508,13 +508,26 @@ async function assertCleanWorktree(): Promise { } } +function changesetPath(version: string): string { + return `.changeset/update-openapi-${version.slice(0, 12)}.md`; +} + +/** + * Whether an update changes more than its own bookkeeping: the package.json + * pin and the changeset. A schema revision that leaves the generated output + * unchanged would otherwise ship an empty minor release. + */ +export function changesGeneratedOutput( + changedPaths: string[], + version: string +): boolean { + const bookkeeping = new Set([CLI_PACKAGE_JSON, changesetPath(version)]); + return changedPaths.some((path) => !bookkeeping.has(path)); +} + function writeChangeset(version: string): void { - const changesetPath = join( - REPO_ROOT, - `.changeset/update-openapi-${version.slice(0, 12)}.md` - ); writeFileSync( - changesetPath, + join(REPO_ROOT, changesetPath(version)), `--- "cf": minor --- @@ -723,6 +736,25 @@ async function main(): Promise { logStep("Regenerating the command surface"); await run("pnpm", ["generate"], { env: forgeEnvironment }); + // The worktree was clean, so this lists every file the update touched. + const changedPaths = ( + await output("git", [ + "ls-files", + "--modified", + "--others", + "--exclude-standard", + ]) + ) + .split("\n") + .filter(Boolean); + if (!changesGeneratedOutput(changedPaths, version)) { + // Leave the pin on the older revision until a schema change affects cf. + console.log( + `${latest} does not change the generated output; skipping the update.` + ); + return; + } + logStep("Validating the generated update"); await run("git", ["diff", "--check"], { env: forgeEnvironment }); await run("pnpm", ["check"], { env: forgeEnvironment });