|
1 | 1 | /** |
2 | | - * Fetch GitHub's public GraphQL schema and write it to disk, the GraphQL |
3 | | - * sibling of trim-openapi.ts. The query-validation unit test |
4 | | - * (test/sections/graphql-queries.test.ts) loads the schema from disk - never |
5 | | - * the network - so test runs stay hermetic and fast; this script is the ONLY |
6 | | - * thing that touches the network. The output is a FETCHED, gitignored |
7 | | - * artifact (a multi-MB generated blob kept out of history): local devs run |
8 | | - * this once, and CI restores it from actions/cache or re-fetches on a miss. |
| 2 | + * Fetches GitHub's public GraphQL schema to disk, the GraphQL sibling of trim-openapi.ts. This script is the ONLY |
| 3 | + * thing that touches the network; the output is a fetched, gitignored artifact. |
| 4 | + * test/sections/graphql-queries.test.ts -> loads it from disk |
| 5 | + * local dev -> runs this once |
| 6 | + * CI -> restores it from cache, re-fetches on a miss |
| 7 | + * UPSTREAM_REF -> PINNED to a github/docs commit, so two runs months apart fetch byte-identical text |
9 | 8 | * |
10 | | - * Run: `bun .github/scripts/fetch-graphql-schema.ts` (writes the schema in |
11 | | - * place). Re-run to adopt a newer upstream ref. |
12 | | - * |
13 | | - * The upstream ref is PINNED to a github/docs commit SHA (the docs pipeline |
14 | | - * publishes the schema at src/graphql/data/fpt/schema.docs.graphql and |
15 | | - * updates it continuously on main), so two runs months apart fetch |
16 | | - * byte-identical schema text - the same reproducibility contract as |
17 | | - * trim-openapi's UPSTREAM_REF. |
| 9 | + * Run: `bun .github/scripts/fetch-graphql-schema.ts`; re-run after bumping UPSTREAM_REF. |
18 | 10 | */ |
19 | 11 |
|
20 | 12 | import { mkdirSync, renameSync, writeFileSync } from "node:fs"; |
21 | 13 | import { dirname, join } from "node:path"; |
22 | 14 | import { buildSchema } from "graphql"; |
23 | 15 | import { fetchTextWithRetry } from "./lib/fetch-retry.js"; |
24 | 16 |
|
25 | | -/** |
26 | | - * The github/docs commit the schema is fetched at. Bump this (and re-run) to |
27 | | - * adopt upstream schema changes; pinning to a SHA keeps the output |
28 | | - * reproducible. |
29 | | - */ |
30 | 17 | const UPSTREAM_REF = "01f2174e1ab5d15d4946cfe96ef7dfb5c9a8b889"; |
31 | 18 |
|
32 | | -/** The free-tier (github.com) schema, the flavor the action targets. */ |
| 19 | +/** fpt is the free-tier (github.com) flavor, the one the action targets. */ |
33 | 20 | const SCHEMA_URL = |
34 | 21 | `https://raw.githubusercontent.com/github/docs/${UPSTREAM_REF}` + |
35 | 22 | "/src/graphql/data/fpt/schema.docs.graphql"; |
36 | 23 |
|
37 | 24 | const OUT_PATH = join(import.meta.dir, "..", "..", "test", "e2e", "graphql", "schema.docs.graphql"); |
38 | 25 |
|
39 | | -/** Abandon a fetch attempt if the (large) schema has not arrived in this long. */ |
40 | 26 | const FETCH_TIMEOUT_MS = 60_000; |
41 | 27 |
|
42 | 28 | async function main(): Promise<number> { |
43 | 29 | console.log(`fetching ${SCHEMA_URL}`); |
44 | | - // Per-attempt timeout plus bounded retry (lib/fetch-retry.ts): a hung |
45 | | - // connection or a transient blip - even mid-download - fails loudly with |
46 | | - // advice instead of the script stalling forever, or one blip failing the |
47 | | - // whole CI gate. |
48 | 30 | const fetched = await fetchTextWithRetry("GraphQL schema", SCHEMA_URL, FETCH_TIMEOUT_MS); |
49 | 31 | if (!fetched.ok) { |
50 | 32 | throw new Error( |
51 | 33 | `failed to fetch the GraphQL schema: ${fetched.status} ${fetched.statusText} for ${SCHEMA_URL}. Check UPSTREAM_REF and the schema path`, |
52 | 34 | ); |
53 | 35 | } |
54 | 36 | const text = fetched.text; |
55 | | - // Integrity at generation, the assertRefFree analog: a truncated download |
56 | | - // or a moved upstream file must fail HERE, not as an opaque parse error in |
57 | | - // the disk-only consumer. |
| 37 | + // A truncated download or a moved upstream file must fail HERE, not as an opaque parse error in the disk-only consumer. |
58 | 38 | try { |
59 | 39 | buildSchema(text); |
60 | 40 | } catch (error) { |
61 | 41 | throw new Error( |
62 | 42 | `the fetched GraphQL schema from ${SCHEMA_URL} failed to parse: ${error instanceof Error ? error.message : String(error)}. The download may be truncated (re-run), or the upstream file changed shape (check UPSTREAM_REF)`, |
63 | 43 | ); |
64 | 44 | } |
65 | | - // Atomic write: serialize to a temp file, then rename over the target, so |
66 | | - // an aborted run leaves the previously written schema intact. The directory |
67 | | - // holds no tracked file, so a fresh checkout must create it first. |
| 45 | + // Temp file then rename, so an aborted run leaves the previously written schema intact. The directory holds no |
| 46 | + // tracked file, so a fresh checkout must create it first. |
68 | 47 | mkdirSync(dirname(OUT_PATH), { recursive: true }); |
69 | 48 | const tmpPath = `${OUT_PATH}.tmp`; |
70 | 49 | writeFileSync(tmpPath, text); |
|
0 commit comments