Skip to content

Commit 7523449

Browse files
committed
ci: regenerate from the committed spec so the drift check is hermetic
1 parent 05ad3ad commit 7523449

3 files changed

Lines changed: 42 additions & 18 deletions

File tree

.github/workflows/ci.yml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,16 @@ jobs:
1515
- uses: oven-sh/setup-bun@v2
1616
- run: bun install --frozen-lockfile
1717

18-
- name: Verify OpenAPI spec is in sync with live API
18+
# Hermetic: regenerates from the committed spec, so this verifies that the committed
19+
# generated types still reproduce from it, with no network dependency. Refreshing the
20+
# spec itself is the release workflow's job.
21+
- name: Verify generated types match the committed OpenAPI spec
22+
env:
23+
ROXYAPI_SPEC_FILE: specs/openapi.json
1924
run: |
2025
bun run generate
2126
git diff --exit-code -- specs/openapi.json packages/ui/src/types/ \
22-
|| { echo "::error::OpenAPI spec or generated types are stale. Run 'bun run generate' locally and commit."; exit 1; }
27+
|| { echo "::error::Generated types are stale. Run 'bun run generate' locally and commit."; exit 1; }
2328
2429
- run: bun run check
2530
- name: No stray backticks inside css`` templates

lefthook.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,15 @@ pre-commit:
5151
pre-push:
5252
parallel: false
5353
commands:
54+
# Hermetic: regenerates from the committed spec, so this verifies the committed types still
55+
# reproduce from it, with no network dependency. Refreshing the spec is the release job.
5456
spec-drift:
57+
env:
58+
ROXYAPI_SPEC_FILE: specs/openapi.json
5559
run: |
5660
bun run generate
5761
git diff --exit-code -- specs/openapi.json packages/ui/src/types/ \
58-
|| { echo "specs/openapi.json or generated types drifted from live spec. Run 'bun run generate' and commit."; exit 1; }
62+
|| { echo "Generated types drifted from the committed spec. Run 'bun run generate' and commit."; exit 1; }
5963
priority: 1
6064
typecheck:
6165
run: bun run typecheck

scripts/generate.ts

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,38 @@ const SPEC_URL =
1111
process.env.ROXY_OPENAPI_URL ?? 'https://roxyapi.com/api/v2/openapi.json';
1212
const SPEC_PATH = 'specs/openapi.json';
1313

14-
console.log(`Fetching OpenAPI spec from ${SPEC_URL}`);
14+
/**
15+
* Path to read the spec from instead of fetching it.
16+
*
17+
* @remarks
18+
* Keeps generation offline and byte-reproducible, which is what the codegen drift check in CI
19+
* relies on. Distinct from `ROXY_OPENAPI_URL`, which only points the fetch at a different server.
20+
*/
21+
const SPEC_FILE = process.env.ROXYAPI_SPEC_FILE;
1522

1623
let spec: unknown;
17-
try {
18-
const res = await fetch(SPEC_URL, {
19-
headers: { 'Cache-Control': 'no-cache' },
20-
});
21-
if (!res.ok) throw new Error(`HTTP ${res.status} ${res.statusText}`);
22-
spec = await res.json();
23-
} catch (err) {
24-
if (existsSync(SPEC_PATH)) {
25-
console.warn(
26-
`! Live spec fetch failed (${err instanceof Error ? err.message : String(err)}). Using cached ${SPEC_PATH}.`,
27-
);
28-
spec = JSON.parse(await Bun.file(SPEC_PATH).text());
29-
} else {
30-
throw err;
24+
if (SPEC_FILE) {
25+
console.log(
26+
`Reading OpenAPI spec from ${SPEC_FILE} (offline, ROXYAPI_SPEC_FILE)`,
27+
);
28+
spec = JSON.parse(await Bun.file(SPEC_FILE).text());
29+
} else {
30+
console.log(`Fetching OpenAPI spec from ${SPEC_URL}`);
31+
try {
32+
const res = await fetch(SPEC_URL, {
33+
headers: { 'Cache-Control': 'no-cache' },
34+
});
35+
if (!res.ok) throw new Error(`HTTP ${res.status} ${res.statusText}`);
36+
spec = await res.json();
37+
} catch (err) {
38+
if (existsSync(SPEC_PATH)) {
39+
console.warn(
40+
`! Live spec fetch failed (${err instanceof Error ? err.message : String(err)}). Using cached ${SPEC_PATH}.`,
41+
);
42+
spec = JSON.parse(await Bun.file(SPEC_PATH).text());
43+
} else {
44+
throw err;
45+
}
3146
}
3247
}
3348

0 commit comments

Comments
 (0)