Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions .github/workflows/ci-published-openapi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
name: "CI - Website: Check Published OpenAPI Documents"

# Fails a PR that changes a published OpenAPI document in
# website/public/openapi/, by hand or through a TypeSpec change. The
# `published-openapi-change` label allows a deliberate change; the diffs still
# print. See lib/core/DEVELOPMENT.md.

on:
pull_request:
types: [opened, synchronize, reopened, labeled, unlabeled]
paths:
- lib/core/**
- website/public/openapi/**
- website/src/specs/**
- website/src/scripts/prune-openapi-tags.ts
- website/src/scripts/check-published-openapi.ts
- website/package.json
# Holds the openapi3 emitter options, so a change rewrites every document
- website/tspconfig.yaml
# A TypeSpec compiler bump can change the generated documents
- pnpm-lock.yaml
- pnpm-workspace.yaml
- .github/workflows/ci-published-openapi.yml

defaults:
run:
working-directory: ./website

jobs:
check:
runs-on: ubuntu-latest

steps:
# The default pull_request checkout is the merge commit. Its first parent
# (HEAD^1) is the base branch tip the PR was merged against, so the check
# compares the merged result with that exact commit. fetch-depth 2 brings
# that parent along.
- name: Checkout code
uses: actions/checkout@v7
with:
fetch-depth: 2

- name: Install pnpm
uses: pnpm/action-setup@v6

- name: Install Node.js
uses: actions/setup-node@v7
with:
node-version-file: .nvmrc
cache: "pnpm"

- name: Install dependencies
working-directory: .
run: pnpm install --frozen-lockfile

# Checks the committed documents, regenerates them with
# `pnpm run typespec`, then checks again.
- name: Check published OpenAPI documents
run: pnpm run check:published-openapi --base HEAD^1
env:
PUBLISHED_OPENAPI_CHANGE_ALLOWED: ${{ contains(github.event.pull_request.labels.*.name, 'published-openapi-change') }}
1 change: 1 addition & 0 deletions lib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ The rules:
- **What goes into the hold branch:** `feat` and breaking (`!`) work for that release, and fixes to that unreleased work.
- **What goes straight to `main`:** `fix` PRs for code already released on `main`, Dependabot and security updates, and docs-only changes. Docs for unreleased work, like migration notes, go in the hold branch with that work.
- **Releases are always cut from `main`.**
- **Released OpenAPI documents stay fixed.** CI fails a PR, into `main` or a hold branch, that changes the website's OpenAPI document for any already released core version. The new version's document can change freely until its release. A deliberate edit needs the `published-openapi-change` label and a note in the PR saying why. See [core's development checklist](core/DEVELOPMENT.md#development-checklist).
- **Hold branches aren't for blocked work.** A PR waiting on a review, a decision or another PR stays open against its usual base. The hold branch is for next-release work clustered in planning. Work continues on it normally.
- **Keep it current with `main`.** Merge `main` into the hold branch at least weekly (**Update branch** on the staging PR does this). Rebasing works too.
- **Open the staging PR early.** Open the staging PR as a draft early on. CI and the website preview then run on all the release's changes together, and the PR updates as each PR lands. "This branch is out-of-date with the base branch" is normal. A real merge conflict stops CI and the preview, so resolve it right away.
Expand Down
3 changes: 2 additions & 1 deletion lib/core/DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Patch releases skip the hold branch: `fix(core)` PRs go straight to `main`. Befo
- [ ] An early PR into the hold branch adds the new version to the `Versions` enum in [`main.tsp`](lib/main.tsp)
- [ ] Every PR decorates what it changes with the matching [versioning decorators](https://typespec.io/docs/libraries/versioning/reference/decorators/#@TypeSpec.Versioning.removed)
- [ ] Every core PR runs `pnpm --filter website run typespec` and commits the regenerated files in [`website/public/openapi/`](../../website/public/openapi/) and [`website/public/schemas/yaml/`](../../website/public/schemas/yaml/)
- [ ] Published OpenAPI documents don't change. CI (`ci-published-openapi.yml`) fails a PR that changes any document whose version is at or below core's released version, whether by hand or through regeneration. If the change is deliberate, such as a description or example fix, add the `published-openapi-change` label and say why in the PR. Run it locally with `pnpm --filter website run check:published-openapi --base origin/<base>`.

### Release review checklist

Expand All @@ -47,7 +48,7 @@ Run this on the hold branch before merging the staging PR, and note the results
- [ ] Regenerated specs match the committed files: after `pnpm --filter website run typespec`, `git status` shows no changes
- [ ] Older versions are unchanged except for intended edits:
- [ ] `git fetch origin` and `git diff origin/main...origin/<hold-branch> -- website/public/openapi/` have been run
- [ ] The diff adds `openapi.<new-version>.yaml`, and older versions show only edits you meant to make, such as a description or example fix
- [ ] The diff adds `openapi.<new-version>.yaml`, and older versions show only edits you meant to make, such as a description or example fix. CI already fails on any other change to a released version unless the PR has the `published-openapi-change` label; check that each labelled PR says why.
- Comparing against `main` is what confirms the versioning decorators. Don't diff `lib/core/tsp-output/` against `website/public/openapi/`: the website build prunes unused tags (`website/src/scripts/prune-openapi-tags.ts`), so older versions always differ.
- [ ] The new version has been reviewed in Swagger with `cg preview`:
- [ ] From the repo root: `node lib/cli/dist/index.js preview website/public/openapi/openapi.<new-version>.yaml` (`pnpm install` builds the CLI), or `pnpm dlx @common-grants/cli preview <spec>`
Expand Down
1 change: 1 addition & 0 deletions website/.cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"Snohomish",
"subrecipient",
"TCCUs",
"textconv",
"Temelio",
"tiktok",
"timebox",
Expand Down
221 changes: 221 additions & 0 deletions website/__tests__/scripts/check-published-openapi.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
/**
* Tests for `src/scripts/check-published-openapi.ts` (#1392): which documents
* count as published, and when the check fails.
*/

import { describe, it, expect } from "vitest";
import {
baseRefFromArgv,
classifyChange,
compareSemver,
decideOutcome,
dedupeResults,
overrideFromInputs,
selectPublishedDocuments,
versionFromFilename,
OVERRIDE_LABEL,
type DocumentResult,
} from "@/scripts/check-published-openapi";

const dir = "website/public/openapi";
const files = (...versions: string[]) =>
versions.map((v) => `${dir}/openapi.${v}.yaml`);

describe("versionFromFilename", () => {
it("reads the version from a document filename", () => {
expect(versionFromFilename(`${dir}/openapi.0.5.0.yaml`)).toBe("0.5.0");
expect(versionFromFilename("openapi.10.2.31.yaml")).toBe("10.2.31");
});

it("ignores other files", () => {
expect(versionFromFilename(`${dir}/README.md`)).toBeNull();
expect(versionFromFilename("openapi.yaml")).toBeNull();
expect(versionFromFilename("openapi.0.5.yaml")).toBeNull();
});
});

describe("compareSemver", () => {
it("compares numerically, not as strings", () => {
expect(compareSemver("0.10.0", "0.9.0")).toBeGreaterThan(0);
expect(compareSemver("0.5.0", "0.6.0")).toBeLessThan(0);
expect(compareSemver("1.0.0", "0.99.99")).toBeGreaterThan(0);
expect(compareSemver("0.5.0", "0.5.0")).toBe(0);
});

it("ignores pre-release and build suffixes", () => {
expect(compareSemver("0.6.0-rc.1", "0.6.0")).toBe(0);
});

it("rejects non-versions", () => {
expect(() => compareSemver("latest", "0.5.0")).toThrow();
});
});

describe("selectPublishedDocuments", () => {
it("covers every version up to core's released version", () => {
const published = selectPublishedDocuments(
files("0.1.0", "0.2.0", "0.3.0", "0.4.0", "0.5.0"),
"0.5.0",
);
expect(published.map((d) => d.version)).toEqual([
"0.1.0",
"0.2.0",
"0.3.0",
"0.4.0",
"0.5.0",
]);
expect(published[0].path).toBe(`${dir}/openapi.0.1.0.yaml`);
});

it("leaves out a hold branch's next version until core's version moves", () => {
const onHold = files("0.1.0", "0.2.0", "0.3.0", "0.4.0", "0.5.0", "0.6.0");
expect(
selectPublishedDocuments(onHold, "0.5.0").map((d) => d.version),
).not.toContain("0.6.0");
expect(
selectPublishedDocuments(onHold, "0.6.0").map((d) => d.version),
).toContain("0.6.0");
});

it("sorts by version and skips unrelated files", () => {
const published = selectPublishedDocuments(
[...files("0.10.0", "0.2.0", "0.9.0"), `${dir}/notes.txt`],
"0.10.0",
);
expect(published.map((d) => d.version)).toEqual([
"0.2.0",
"0.9.0",
"0.10.0",
]);
});
});

describe("classifyChange", () => {
it("classifies unchanged, changed and deleted documents", () => {
expect(classifyChange("a\n", "a\n")).toBe("unchanged");
expect(classifyChange("a\n", "b\n")).toBe("changed");
expect(classifyChange("a\n", null)).toBe("deleted");
});
});

const result = (
overrides: Partial<DocumentResult> & Pick<DocumentResult, "path">,
): DocumentResult => ({
phase: "committed",
change: "changed",
diff: "--- a\n+++ b\n-old\n+new\n",
...overrides,
});

describe("dedupeResults", () => {
it("drops a regenerated diff that repeats the committed one", () => {
const path = `${dir}/openapi.0.3.0.yaml`;
const deduped = dedupeResults([
result({ path }),
result({ path, phase: "regenerated" }),
]);
expect(deduped).toHaveLength(1);
expect(deduped[0].phase).toBe("committed");
});

it("keeps a regenerated diff that differs from the committed one", () => {
const path = `${dir}/openapi.0.3.0.yaml`;
const deduped = dedupeResults([
result({ path }),
result({ path, phase: "regenerated", diff: "other" }),
]);
expect(deduped).toHaveLength(2);
});
});

describe("decideOutcome", () => {
const published = selectPublishedDocuments(files("0.4.0", "0.5.0"), "0.5.0");

it("passes when nothing changed", () => {
const outcome = decideOutcome({
baseRef: "origin/main",
published,
results: [],
overrideAllowed: false,
});
expect(outcome.exitCode).toBe(0);
expect(outcome.report).toContain("0.4.0, 0.5.0");
});

it("fails, names each changed document and shows its diff", () => {
const outcome = decideOutcome({
baseRef: "origin/main",
published,
results: [
result({ path: `${dir}/openapi.0.4.0.yaml`, phase: "regenerated" }),
result({ path: `${dir}/openapi.0.5.0.yaml`, change: "deleted" }),
],
overrideAllowed: false,
});
expect(outcome.exitCode).toBe(1);
expect(outcome.report).toContain(`\`${dir}/openapi.0.4.0.yaml\` changed`);
expect(outcome.report).toContain(`\`${dir}/openapi.0.5.0.yaml\` deleted`);
expect(outcome.report).toContain("+new");
expect(outcome.report).toContain("@returnTypeChangedFrom");
expect(outcome.report).toContain(OVERRIDE_LABEL);
});

it("passes with the override but still shows every diff", () => {
const outcome = decideOutcome({
baseRef: "origin/main",
published,
results: [result({ path: `${dir}/openapi.0.4.0.yaml` })],
overrideAllowed: true,
});
expect(outcome.exitCode).toBe(0);
expect(outcome.report).toContain(`allowed by the \`${OVERRIDE_LABEL}\``);
expect(outcome.report).toContain("+new");
});

it("truncates long diffs when asked, for the step summary", () => {
const long = Array.from({ length: 50 }, (_, i) => `+line ${i}`).join("\n");
const outcome = decideOutcome({
baseRef: "origin/main",
published,
results: [result({ path: `${dir}/openapi.0.4.0.yaml`, diff: long })],
overrideAllowed: false,
maxDiffLines: 10,
});
expect(outcome.report).toContain("+line 9");
expect(outcome.report).not.toContain("+line 10");
expect(outcome.report).toContain("40 more lines");
});

it("notes when the committed change also shows after regeneration", () => {
const path = `${dir}/openapi.0.4.0.yaml`;
const outcome = decideOutcome({
baseRef: "origin/main",
published,
results: [result({ path }), result({ path, phase: "regenerated" })],
overrideAllowed: false,
});
expect(outcome.report.match(/```diff/g)).toHaveLength(1);
expect(outcome.report).toContain("same diff after regeneration");
});
});

describe("inputs", () => {
it("reads the override from the flag or the environment", () => {
expect(overrideFromInputs(["--allow-changes"], {})).toBe(true);
expect(
overrideFromInputs([], { PUBLISHED_OPENAPI_CHANGE_ALLOWED: "true" }),
).toBe(true);
expect(
overrideFromInputs([], { PUBLISHED_OPENAPI_CHANGE_ALLOWED: "false" }),
).toBe(false);
expect(overrideFromInputs([], {})).toBe(false);
});

it("reads the base ref, defaulting to origin/main", () => {
expect(baseRefFromArgv([])).toBe("origin/main");
expect(baseRefFromArgv(["--base", "origin/CORE-v0.6.0"])).toBe(
"origin/CORE-v0.6.0",
);
expect(() => baseRefFromArgv(["--base"])).toThrow();
});
});
1 change: 1 addition & 0 deletions website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"check:a11y": "tsx src/scripts/a11y-scan.ts",
"check:quickstarts": "tsx src/scripts/check-quickstarts.ts",
"check:types": "tsc --noEmit",
"check:published-openapi": "tsx src/scripts/check-published-openapi.ts",
"checks": "pnpm check:lint && pnpm check:format && pnpm check:types && pnpm check:astro && pnpm check:spelling",
"ci": "pnpm run build && pnpm run checks && pnpm run test",
"audit:moderate": "pnpm audit --audit-level=moderate",
Expand Down
Loading
Loading