-
Notifications
You must be signed in to change notification settings - Fork 511
ci(cli): publish ts docs reference #6253
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
7ttp
wants to merge
5
commits into
develop
Choose a base branch
from
chore/publish-cli-reference
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
573b364
ci(cli): restore cli reference publishing
7ttp e52647d
test: classify publisher suite as e2e
7ttp f047e21
fix: open the docs pr when the branch is already ahead
7ttp da7b4c4
fix: scope docs pr reuse by owner and base
7ttp 3c77bb7
Merge branch 'develop' into chore/publish-cli-reference
7ttp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| import path from "node:path"; | ||
| import { describe, expect, it } from "vitest"; | ||
| import { stringify } from "yaml"; | ||
|
|
||
| const cliRoot = path.resolve(import.meta.dirname, ".."); | ||
|
|
||
| function runScript( | ||
| args: ReadonlyArray<string>, | ||
| stdin: string, | ||
| ): { exitCode: number; stdout: string; stderr: string } { | ||
| const { FORCE_COLOR: _forceColor, NO_COLOR: _noColor, ...environment } = process.env; | ||
| const result = Bun.spawnSync(["bun", "scripts/publish-docs-spec.ts", ...args], { | ||
| cwd: cliRoot, | ||
| env: environment, | ||
| stdin: Buffer.from(stdin), | ||
| }); | ||
| return { | ||
| exitCode: result.exitCode, | ||
| stdout: result.stdout.toString(), | ||
| stderr: result.stderr.toString(), | ||
| }; | ||
| } | ||
|
|
||
| function validSpec(version: string): string { | ||
| return stringify({ | ||
| clispec: "001", | ||
| info: { id: "cli", version, title: "Supabase CLI" }, | ||
| commands: Array.from({ length: 144 }, (_, index) => ({ id: `supabase-command-${index}` })), | ||
| }); | ||
| } | ||
|
|
||
| describe("publish-docs-spec.ts entrypoint", () => { | ||
| it("prints usage and fails without --version", () => { | ||
| const { exitCode, stderr } = runScript(["--dry-run"], validSpec("1.0.0")); | ||
| expect(exitCode).toBe(1); | ||
| expect(stderr).toContain("Usage:"); | ||
| }, 30_000); | ||
|
|
||
| it("refuses an empty spec", () => { | ||
| const { exitCode, stderr } = runScript(["--version", "1.0.0", "--dry-run"], ""); | ||
| expect(exitCode).toBe(1); | ||
| expect(stderr).toContain("Refusing to publish an empty spec"); | ||
| }, 30_000); | ||
|
|
||
| it("refuses a spec that is not valid clispec YAML", () => { | ||
| const { exitCode, stderr } = runScript( | ||
| ["--version", "1.0.0", "--dry-run"], | ||
| "clispec: [unclosed", | ||
| ); | ||
| expect(exitCode).toBe(1); | ||
| expect(stderr).toContain("Refusing to publish"); | ||
| }, 30_000); | ||
|
|
||
| it("refuses a truncated spec with too few commands", () => { | ||
| const truncated = stringify({ | ||
| clispec: "001", | ||
| info: { id: "cli", version: "1.0.0", title: "Supabase CLI" }, | ||
| commands: [{ id: "supabase-init" }], | ||
| }); | ||
| const { exitCode, stderr } = runScript(["--version", "1.0.0", "--dry-run"], truncated); | ||
| expect(exitCode).toBe(1); | ||
| expect(stderr).toContain("expected at least 144"); | ||
| }, 30_000); | ||
|
|
||
| it("refuses a spec whose version does not match --version", () => { | ||
| const { exitCode, stderr } = runScript(["--version", "1.0.0", "--dry-run"], validSpec("2.0.0")); | ||
| expect(exitCode).toBe(1); | ||
| expect(stderr).toContain("does not match --version 1.0.0"); | ||
| }, 30_000); | ||
|
|
||
| it("summarizes a valid spec in dry-run mode without publishing", () => { | ||
| const spec = validSpec("1.0.0"); | ||
| const { exitCode, stdout, stderr } = runScript(["--version", "1.0.0", "--dry-run"], spec); | ||
| expect(exitCode).toBe(0); | ||
| expect(stderr).toBe(""); | ||
| expect(stdout.trim()).toBe( | ||
| `Would publish ${spec.length} bytes to supabase/supabase:apps/docs/spec/cli_v1_commands.yaml on branch cli/ref-doc`, | ||
| ); | ||
| }, 30_000); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| /** | ||
| * Publishes the generated CLI reference to the docs site by opening a PR | ||
| * against supabase/supabase, replacing the Go `tools/bumpdoc` that was deleted | ||
| * in the monorepo merge (71b543255). The reference has not been republished | ||
| * since, so the published spec is frozen at 2.98.2. | ||
| * | ||
| * Reads the spec on stdin so it composes with the generator exactly the way the | ||
| * Go release job did: | ||
| * | ||
| * bun scripts/generate-docs-spec.ts <version> | bun scripts/publish-docs-spec.ts --version <version> | ||
| * | ||
| * Like `bumpdoc`, this is a no-op when the spec is already published and no PR | ||
| * is missing: it prints "already up to date" and exits 0. A branch whose spec | ||
| * is ahead of base still gets its PR ensured, so a run that pushed but failed | ||
| * to open the PR is repaired by the next release. Any real failure exits | ||
| * non-zero. | ||
| */ | ||
| import { $ } from "bun"; | ||
| import { mkdtemp, rm, writeFile } from "node:fs/promises"; | ||
| import { tmpdir } from "node:os"; | ||
| import path from "node:path"; | ||
| import process from "node:process"; | ||
| import { parseArgs } from "node:util"; | ||
| import { parse } from "yaml"; | ||
|
|
||
| const { values } = parseArgs({ | ||
| options: { | ||
| version: { type: "string" }, | ||
| repo: { type: "string", default: "supabase/supabase" }, | ||
| // Path of the spec inside the docs repo, as the Go job passed it. | ||
| "spec-path": { type: "string", default: "apps/docs/spec/cli_v1_commands.yaml" }, | ||
| branch: { type: "string", default: "cli/ref-doc" }, | ||
| base: { type: "string", default: "master" }, | ||
| "dry-run": { type: "boolean", default: false }, | ||
| }, | ||
| }); | ||
|
|
||
| const versionArgument = values.version; | ||
| if (!versionArgument) { | ||
| console.error( | ||
| "Usage: bun scripts/generate-docs-spec.ts <version> | bun scripts/publish-docs-spec.ts --version <version> [--repo <owner/repo>] [--spec-path <path>] [--branch <name>] [--base <branch>] [--dry-run]", | ||
| ); | ||
| process.exit(1); | ||
| } | ||
| const version = versionArgument.startsWith("v") ? versionArgument.slice(1) : versionArgument; | ||
|
|
||
| const repo = values.repo!; | ||
| const specPath = values["spec-path"]!; | ||
| const branch = values.branch!; | ||
| const base = values.base!; | ||
| const dryRun = values["dry-run"]!; | ||
|
|
||
| const spec = await Bun.stdin.text(); | ||
| if (spec.trim().length === 0) { | ||
| console.error("Refusing to publish an empty spec: nothing was piped in on stdin."); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| let parsed: { clispec?: unknown; info?: { version?: unknown }; commands?: unknown }; | ||
| try { | ||
| parsed = parse(spec); | ||
| } catch (error) { | ||
| console.error(`Refusing to publish: stdin is not valid YAML (${error}).`); | ||
| process.exit(1); | ||
| } | ||
| if (parsed?.clispec !== "001") { | ||
| console.error( | ||
| `Refusing to publish: expected clispec "001", got ${JSON.stringify(parsed?.clispec)}.`, | ||
| ); | ||
| process.exit(1); | ||
| } | ||
| if (parsed.info?.version !== version) { | ||
| console.error( | ||
| `Refusing to publish: the spec's version ${JSON.stringify(parsed.info?.version)} does not match --version ${version}.`, | ||
| ); | ||
| process.exit(1); | ||
| } | ||
| const commands = parsed.commands; | ||
| if (!Array.isArray(commands) || commands.length < 144) { | ||
| console.error( | ||
| `Refusing to publish: the spec has ${Array.isArray(commands) ? commands.length : 0} commands, expected at least 144.`, | ||
| ); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| if (dryRun) { | ||
| console.log(`Would publish ${spec.length} bytes to ${repo}:${specPath} on branch ${branch}`); | ||
| process.exit(0); | ||
| } | ||
|
|
||
| // `--depth 1` is enough: when `cli/ref-doc` already exists its tip is fetched | ||
| // and the new spec lands as one commit on top, so commits pushed onto an open | ||
| // PR (sidebar fixes) survive later releases; otherwise the branch starts from | ||
| // base. | ||
| const tmpDir = await mkdtemp(path.join(tmpdir(), "supabase-docs-")); | ||
| try { | ||
| await $`git clone --quiet --depth 1 --branch ${base} https://github.com/${repo}.git ${tmpDir}`; | ||
|
7ttp marked this conversation as resolved.
|
||
|
|
||
| const remoteBranch = await $`git -C ${tmpDir} fetch --quiet --depth 1 origin ${branch}` | ||
| .nothrow() | ||
| .quiet(); | ||
| if (remoteBranch.exitCode === 0) { | ||
| await $`git -C ${tmpDir} checkout --quiet -B ${branch} FETCH_HEAD`; | ||
| } else { | ||
| await $`git -C ${tmpDir} checkout --quiet -B ${branch}`; | ||
| } | ||
|
|
||
| const target = path.join(tmpDir, specPath); | ||
| await writeFile(target, spec); | ||
| const prettier = path.resolve(import.meta.dir, "../node_modules/.bin/prettier"); | ||
| await $`${prettier} --no-config --single-quote --print-width 100 --log-level warn --write ${target}`; | ||
|
|
||
| const message = "chore: update cli reference doc"; | ||
| await $`git -C ${tmpDir} add ${specPath}`; | ||
| const unchanged = await $`git -C ${tmpDir} diff --quiet --cached -- ${specPath}` | ||
| .nothrow() | ||
| .quiet(); | ||
| if (unchanged.exitCode === 0) { | ||
| console.log( | ||
| remoteBranch.exitCode === 0 | ||
| ? `${specPath} is already up to date on ${branch}` | ||
| : `${specPath} is already up to date in ${repo}`, | ||
| ); | ||
| } else { | ||
| await $`git -C ${tmpDir} -c ${"user.name=github-actions[bot]"} -c ${"user.email=41898282+github-actions[bot]@users.noreply.github.com"} commit --quiet -m ${message}`; | ||
| await $`git -C ${tmpDir} push --quiet origin ${branch}`; | ||
| console.log(`Pushed ${branch} to ${repo}`); | ||
| } | ||
|
|
||
| const unpublished = await $`git -C ${tmpDir} diff --quiet origin/${base} HEAD -- ${specPath}` | ||
| .nothrow() | ||
| .quiet(); | ||
| if (unpublished.exitCode === 1) { | ||
| const existing = | ||
| await $`gh pr list --repo ${repo} --head ${branch} --base ${base} --state open --json number,headRepositoryOwner` | ||
| .cwd(tmpDir) | ||
| .text(); | ||
| const openPulls: Array<{ headRepositoryOwner?: { login?: string } }> = JSON.parse(existing); | ||
| const ownPulls = openPulls.filter( | ||
| (pull) => | ||
| pull.headRepositoryOwner?.login?.toLowerCase() === repo.split("/")[0]?.toLowerCase(), | ||
| ); | ||
| if (ownPulls.length > 0) { | ||
| console.log(`Reusing the open PR for ${branch}`); | ||
| } else { | ||
| const body = [ | ||
| "Updates the CLI reference from the supabase/cli release workflow.", | ||
| "", | ||
| "Generated by `scripts/generate-docs-spec.ts` in supabase/cli — edit the", | ||
| "command tree or the content under `apps/cli/docs/` there rather than this", | ||
| "file.", | ||
| "", | ||
| "New commands also need an entry in `apps/docs/spec/common-cli-sections.json` —", | ||
| "without one a command's page is silently dropped from the docs site.", | ||
| "Sections fixes can be committed directly onto this branch — later releases", | ||
| "add commits on top instead of rewriting it.", | ||
| ].join("\n"); | ||
| await $`gh pr create --repo ${repo} --title ${message} --body ${body} --base ${base} --head ${branch}`.cwd( | ||
| tmpDir, | ||
| ); | ||
| console.log(`Opened a pull request against ${repo}`); | ||
| } | ||
| } | ||
| } finally { | ||
| await rm(tmpDir, { recursive: true, force: true }); | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.