| 1 | import { existsSync, readFileSync, writeFileSync } from "node:fs"; |
| 2 | import { spawnSync } from "node:child_process"; |
| 3 | import { createHash } from "node:crypto"; |
| 4 | import { dirname, resolve } from "node:path"; |
| 5 | import { fileURLToPath } from "node:url"; |
| 6 | import { REPO_ROOT, deriveLatestPublishedRelease } from "./facts-lib.mjs"; |
| 7 | import { readDocTopics } from "./docs-sources-lib.mjs"; |
| 8 | |
| 9 | const here = dirname(fileURLToPath(import.meta.url)); |
| 10 | export const RELEASE_DOCS_PATH = resolve(here, "..", "data", "agent-docs.release.json"); |
| 11 | export const RELEASE_DOCS_INDEX_PATH = resolve(here, "..", "data", "agent-docs.release-index.json"); |
| 12 | export const RELEASE_DOCS_FORMAT = "codewhale.release-docs.v1"; |
| 13 | |
| 14 | function gitShow(tag, path) { |
| 15 | const result = spawnSync("git", ["-C", REPO_ROOT, "show", `${tag}:${path}`], { encoding: "utf8", maxBuffer: 64 * 1024 * 1024 }); |
| 16 | return result.status === 0 ? result.stdout : null; |
| 17 | } |
| 18 | |
| 19 | function tagResolves(tag) { |
| 20 | if (!/^v\d+\.\d+\.\d+$/.test(tag)) return false; |
| 21 | return spawnSync("git", ["-C", REPO_ROOT, "rev-parse", "--verify", "--quiet", `${tag}^{commit}`], { encoding: "utf8" }).status === 0; |
| 22 | } |
| 23 | |
| 24 | export { tagResolves }; |
| 25 | |
| 26 | export function deriveReleaseDocs(webDir, release) { |
| 27 | if (!release || !tagResolves(release.tag)) return null; |
| 28 | const topics = []; |
| 29 | const omitted = []; |
| 30 | for (const topic of readDocTopics(webDir)) { |
| 31 | const sources = []; |
| 32 | const missing = []; |
| 33 | for (const path of topic.repoSource) { |
| 34 | const content = gitShow(release.tag, path.split("#", 1)[0]); |
| 35 | if (content === null) missing.push(path); |
| 36 | else sources.push({ path, sha256: createHash("sha256").update(content).digest("hex"), content }); |
| 37 | } |
| 38 | if (sources.length === 0) omitted.push({ id: topic.id, slug: topic.slug, missing }); |
| 39 | else topics.push({ id: topic.id, slug: topic.slug, sources, missing }); |
| 40 | } |
| 41 | return { format: RELEASE_DOCS_FORMAT, tag: release.tag, version: release.version, topics, omitted }; |
| 42 | } |
| 43 | |
| 44 | export function serializeReleaseDocs(snapshot) { |
| 45 | return `${JSON.stringify(snapshot, null, 2)}\n`; |
| 46 | } |
| 47 | |
| 48 | export function releaseDocsIndex(snapshot) { |
| 49 | return { tag: snapshot.tag, slugs: snapshot.topics.map((topic) => topic.slug), omitted: snapshot.omitted.map((topic) => topic.slug) }; |
| 50 | } |
| 51 | |
| 52 | export function readCommittedReleaseDocsIndex() { |
| 53 | if (!existsSync(RELEASE_DOCS_INDEX_PATH)) return null; |
| 54 | try { return JSON.parse(readFileSync(RELEASE_DOCS_INDEX_PATH, "utf8")); } catch { return null; } |
| 55 | } |
| 56 | |
| 57 | export function readCommittedReleaseDocs() { |
| 58 | if (!existsSync(RELEASE_DOCS_PATH)) return null; |
| 59 | try { return JSON.parse(readFileSync(RELEASE_DOCS_PATH, "utf8")); } catch { return null; } |
| 60 | } |
| 61 | |
| 62 | export function refreshReleaseDocs(webDir, release) { |
| 63 | const committed = readCommittedReleaseDocs(); |
| 64 | const fresh = deriveReleaseDocs(webDir, release); |
| 65 | if (!fresh) { |
| 66 | const note = !release ? "no published release" : committed?.tag === release.tag |
| 67 | ? `tag ${release.tag} is not available in this checkout, keeping the committed snapshot` |
| 68 | : `tag ${release.tag} is not available in this checkout and the committed snapshot is for ${committed?.tag ?? "nothing"}`; |
| 69 | if (release && committed?.tag !== release.tag) throw new Error(`[release-docs] ${note}`); |
| 70 | console.warn(`[release-docs] ${note}`); |
| 71 | return committed; |
| 72 | } |
| 73 | const text = serializeReleaseDocs(fresh); |
| 74 | if (!existsSync(RELEASE_DOCS_PATH) || readFileSync(RELEASE_DOCS_PATH, "utf8") !== text) writeFileSync(RELEASE_DOCS_PATH, text); |
| 75 | const indexText = `${JSON.stringify(releaseDocsIndex(fresh), null, 2)}\n`; |
| 76 | if (!existsSync(RELEASE_DOCS_INDEX_PATH) || readFileSync(RELEASE_DOCS_INDEX_PATH, "utf8") !== indexText) writeFileSync(RELEASE_DOCS_INDEX_PATH, indexText); |
| 77 | return fresh; |
| 78 | } |
| 79 | |
| 80 | export function checkReleaseDocs(snapshot, release, registry, derived, index, { allowShallow = false } = {}) { |
| 81 | const problems = []; |
| 82 | if (!release) return ["the published release is unknown"]; |
| 83 | if (!snapshot || snapshot.format !== RELEASE_DOCS_FORMAT || !Array.isArray(snapshot.topics) || !Array.isArray(snapshot.omitted)) { |
| 84 | return ["data/agent-docs.release.json is missing or malformed; run npm run prebuild in a checkout that has the tag"]; |
| 85 | } |
| 86 | if (snapshot.tag !== release.tag) { |
| 87 | problems.push(`data/agent-docs.release.json is for ${snapshot.tag}, the published release is ${release.tag}; run npm run prebuild in a checkout that has the tag`); |
| 88 | } |
| 89 | if (snapshot.version !== release.version) problems.push(`snapshot version ${snapshot.version} differs from the published release ${release.version}`); |
| 90 | for (const topic of snapshot.topics) { |
| 91 | for (const source of topic.sources) { |
| 92 | if (createHash("sha256").update(source.content).digest("hex") !== source.sha256) problems.push(`${topic.slug}: ${source.path} does not match its recorded hash`); |
| 93 | } |
| 94 | } |
| 95 | const covered = new Set([...snapshot.topics, ...snapshot.omitted].map((topic) => topic.slug)); |
| 96 | for (const topic of registry) { |
| 97 | if (!covered.has(topic.slug)) problems.push(`docs topic ${topic.slug} is neither in the snapshot nor recorded as omitted`); |
| 98 | } |
| 99 | if (index !== undefined && JSON.stringify(index) !== JSON.stringify(releaseDocsIndex(snapshot))) { |
| 100 | problems.push("data/agent-docs.release-index.json does not match data/agent-docs.release.json; run npm run prebuild"); |
| 101 | } |
| 102 | if (!derived) { |
| 103 | if (!allowShallow) { |
| 104 | problems.push(`tag ${release.tag} is not in this checkout, so data/agent-docs.release.json could not be verified against the tagged documents; fetch tags or pass --allow-shallow / CODEWHALE_ALLOW_SHALLOW_CHECKOUT=1`); |
| 105 | } |
| 106 | } else if (problems.length === 0 && serializeReleaseDocs(derived) !== serializeReleaseDocs(snapshot)) { |
| 107 | problems.push(`data/agent-docs.release.json differs from the documents at ${release.tag}; run npm run prebuild`); |
| 108 | } |
| 109 | return problems; |
| 110 | } |
| 111 | |
| 112 | export function currentRelease() { |
| 113 | return deriveLatestPublishedRelease(); |
| 114 | } |
| 115 |