返回 CodeWhale
compare-deployed-facts.mjs
根目录 / web / scripts / compare-deployed-facts.mjs
1 #!/usr/bin/env node
2 /**
3 * Compare the public deployment receipt with an exact local/source revision.
4 * No Cloudflare or GitHub credentials are required. By default this is a
5 * report-only command; --require-current turns drift/unavailability into a
6 * failing post-deploy gate.
7 */
8 import { execFileSync } from "node:child_process";
9 import { buildFacts, REPO_ROOT } from "./facts-lib.mjs";
10
11 function parseArgs(argv) {
12 const out = {
13 baseUrl: "https://codewhale.net",
14 expectedRevision: process.env.CODEWHALE_SOURCE_REVISION || process.env.GITHUB_SHA || null,
15 requireCurrent: false,
16 attempts: null,
17 retryDelayMs: 3_000,
18 };
19 for (let index = 0; index < argv.length; index += 1) {
20 const arg = argv[index];
21 if (arg === "--require-current") {
22 out.requireCurrent = true;
23 } else if (arg === "--base-url") {
24 out.baseUrl = argv[++index];
25 } else if (arg === "--expected-revision") {
26 out.expectedRevision = argv[++index];
27 } else if (arg === "--attempts") {
28 out.attempts = Number(argv[++index]);
29 } else if (arg === "--retry-delay-ms") {
30 out.retryDelayMs = Number(argv[++index]);
31 } else {
32 throw new Error(`unknown argument: ${arg}`);
33 }
34 }
35 out.attempts ??= out.requireCurrent ? 6 : 1;
36 return out;
37 }
38
39 function localRevision() {
40 try {
41 return execFileSync("git", ["rev-parse", "HEAD"], {
42 cwd: REPO_ROOT,
43 encoding: "utf8",
44 stdio: ["ignore", "pipe", "ignore"],
45 }).trim();
46 } catch {
47 return null;
48 }
49 }
50
51 function delay(ms) {
52 return new Promise((resolve) => setTimeout(resolve, ms));
53 }
54
55 async function fetchReceipt(url) {
56 try {
57 const response = await fetch(`${url}?receipt=${Date.now()}`, {
58 cache: "no-store",
59 headers: {
60 Accept: "application/json",
61 "User-Agent": "codewhale-deploy-facts-check",
62 },
63 });
64 if (!response.ok) {
65 return { error: `HTTP ${response.status}` };
66 }
67 return { receipt: await response.json() };
68 } catch (error) {
69 return { error: error instanceof Error ? error.message : String(error) };
70 }
71 }
72
73 function compare(expected, receipt) {
74 const differences = [];
75 const deployed = receipt?.deployed;
76 if (receipt?.schemaVersion !== 1 || !deployed || typeof deployed !== "object") {
77 return [{ field: "receipt", expected: "schemaVersion=1", deployed: null }];
78 }
79
80 const checks = [
81 ["sourceRevision", expected.sourceRevision, deployed.sourceRevision],
82 ["version", expected.version, deployed.version],
83 ["providerCount", expected.providerCount, deployed.providerCount],
84 ["toolCount", expected.toolCount, deployed.toolCount],
85 ["contributorCount", expected.contributorCount, deployed.contributorCount],
86 [
87 "latestPublishedRelease.tag",
88 expected.latestPublishedRelease?.tag ?? null,
89 receipt.latestPublishedRelease?.tag ?? null,
90 ],
91 ];
92 for (const [field, expectedValue, deployedValue] of checks) {
93 if (expectedValue !== deployedValue) {
94 differences.push({ field, expected: expectedValue, deployed: deployedValue });
95 }
96 }
97 return differences;
98 }
99
100 async function main() {
101 const args = parseArgs(process.argv.slice(2));
102 let baseUrl;
103 try {
104 baseUrl = new URL(args.baseUrl);
105 } catch {
106 throw new Error(`invalid --base-url: ${args.baseUrl}`);
107 }
108 if (!/^https?:$/.test(baseUrl.protocol)) {
109 throw new Error("--base-url must use http or https");
110 }
111
112 const expectedRevision = args.expectedRevision || localRevision();
113 if (!expectedRevision || !/^[0-9a-f]{40}$/i.test(expectedRevision)) {
114 throw new Error("expected revision must be an exact 40-character Git SHA");
115 }
116 if (!Number.isInteger(args.attempts) || args.attempts < 1 || args.attempts > 20) {
117 throw new Error("--attempts must be an integer from 1 to 20");
118 }
119 if (!Number.isFinite(args.retryDelayMs) || args.retryDelayMs < 0 || args.retryDelayMs > 30_000) {
120 throw new Error("--retry-delay-ms must be between 0 and 30000");
121 }
122
123 const facts = buildFacts();
124 const expected = {
125 sourceRevision: expectedRevision,
126 version: facts.version,
127 providerCount: facts.providers.length,
128 toolCount: facts.toolCount,
129 contributorCount: facts.contributorCount,
130 latestPublishedRelease: facts.latestPublishedRelease,
131 };
132 const endpoint = new URL("/api/facts", baseUrl).toString();
133 let last = { error: "not attempted" };
134 let differences = [];
135
136 for (let attempt = 1; attempt <= args.attempts; attempt += 1) {
137 last = await fetchReceipt(endpoint);
138 differences = last.receipt ? compare(expected, last.receipt) : [];
139 if (last.receipt && differences.length === 0) break;
140 if (attempt < args.attempts) await delay(args.retryDelayMs);
141 }
142
143 const status = last.receipt
144 ? differences.length === 0
145 ? "current"
146 : "drift"
147 : "unavailable";
148 const report = {
149 schemaVersion: 1,
150 checkedAt: new Date().toISOString(),
151 endpoint,
152 status,
153 expected,
154 deployed: last.receipt ?? null,
155 differences,
156 error: last.error ?? null,
157 deploymentAttempted: false,
158 };
159 console.log(JSON.stringify(report, null, 2));
160
161 if (args.requireCurrent && status !== "current") {
162 process.exitCode = 1;
163 }
164 }
165
166 main().catch((error) => {
167 console.error(`[compare-deployed-facts] ERROR: ${error.message}`);
168 process.exitCode = 1;
169 });
170
170 lines Plain Text