|
| 1 | +#!/usr/bin/env node |
| 2 | +// Guards against the "gittensory" branding silently creeping back into runtime source after the LoopOver |
| 3 | +// rebrand -- distinct from docs/prose residue (already owned by the ongoing rebrand-sweep PR series), this |
| 4 | +// targets the class of bug that actually broke things: a hardcoded metric name, MCP resource URI, or Qdrant |
| 5 | +// collection default left on the pre-rebrand string. That exact pattern caused live drift more than once |
| 6 | +// (e.g. #6786 -- ENRICHMENT_ANALYZERS_URI silently left as "gittensory://enrichment-analyzers" while its |
| 7 | +// sibling FINDING_TAXONOMY_URI was correctly renamed in the same PR). Scoped to executable code in `src/**` |
| 8 | +// and each workspace package's `bin/`, `lib/`, `src/`, `scripts/` dirs -- NOT `test/**` or `*.md`, where a |
| 9 | +// literal "gittensory" is usually an intentional, permanent historical reference (a Sentry ticket ID like |
| 10 | +// GITTENSORY-K/8, a stable comment-marker the bot must keep matching in already-posted PR bodies, a DB |
| 11 | +// `source` column value joined against historical rows) rather than drift; those files churn constantly and |
| 12 | +// would make this check pure noise if included. |
| 13 | +// |
| 14 | +// Baseline-diff, not a hard "zero gittensory" ban: scripts/branding-drift-baseline.json snapshots today's |
| 15 | +// known-legitimate per-file hit count (grandfathered, same shape as KNOWN_MIGRATION_DUPLICATES in |
| 16 | +// src/db/migration-collisions.ts). A file's count rising means new drift; falling means a cleanup landed |
| 17 | +// without updating the baseline. Either way the fix is the same: run `npm run branding-drift:update` and |
| 18 | +// commit the regenerated baseline -- mirrors this repo's existing generated-artifact convention (openapi.json, |
| 19 | +// cf-typegen, migrations) rather than inventing a new one. |
| 20 | +import { execFileSync } from "node:child_process"; |
| 21 | +import { readFileSync, writeFileSync } from "node:fs"; |
| 22 | +import { join } from "node:path"; |
| 23 | +import { fileURLToPath } from "node:url"; |
| 24 | + |
| 25 | +export const BASELINE_RELATIVE_PATH = "scripts/branding-drift-baseline.json"; |
| 26 | + |
| 27 | +// git pathspecs: executable code only. Each workspace package's bin/lib/src/scripts dirs mirror the |
| 28 | +// top-level src/** scope; docs/README/CHANGELOG/schema/terraform/css and every test dir are deliberately |
| 29 | +// excluded (see header comment). |
| 30 | +export const BRANDING_DRIFT_PATHSPECS = [ |
| 31 | + "src/**/*.ts", |
| 32 | + "src/**/*.tsx", |
| 33 | + "packages/*/bin/**", |
| 34 | + "packages/*/lib/**/*.js", |
| 35 | + "packages/*/lib/**/*.ts", |
| 36 | + "packages/*/src/**/*.ts", |
| 37 | + "packages/*/scripts/**/*.mjs", |
| 38 | + ":(exclude)**/*.test.ts", |
| 39 | + ":(exclude)**/*.test.tsx", |
| 40 | + ":(exclude)packages/*/test/**", |
| 41 | +]; |
| 42 | + |
| 43 | +function defaultExec(root, args) { |
| 44 | + try { |
| 45 | + return execFileSync("git", args, { cwd: root, encoding: "utf8" }); |
| 46 | + } catch (error) { |
| 47 | + // git grep exits 1 for "zero matches" -- not a real failure, just an empty result. |
| 48 | + if (error.status === 1) return ""; |
| 49 | + throw error; |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +/** Every tracked, non-excluded file with >=1 case-insensitive "gittensory" MATCHING LINE, and that line |
| 54 | + * count (line-granularity, not raw occurrence count -- sufficient to detect "something new appeared" without |
| 55 | + * the fragility of an exact substring-occurrence count). Uses `git grep`, so it only ever sees tracked files |
| 56 | + * exactly as CI would check them out -- no node_modules/dist/build noise to exclude by hand. */ |
| 57 | +export function scanBrandingHits({ root, exec = defaultExec }) { |
| 58 | + const output = exec(root, ["grep", "-ciI", "gittensory", "--", ...BRANDING_DRIFT_PATHSPECS]); |
| 59 | + const counts = {}; |
| 60 | + for (const line of output.split("\n")) { |
| 61 | + if (!line) continue; |
| 62 | + const separatorIndex = line.lastIndexOf(":"); |
| 63 | + const file = line.slice(0, separatorIndex); |
| 64 | + counts[file] = Number(line.slice(separatorIndex + 1)); |
| 65 | + } |
| 66 | + return counts; |
| 67 | +} |
| 68 | + |
| 69 | +/** Pure comparison: every failure is one of "increased" (new drift -- or a file that didn't exist in the |
| 70 | + * baseline at all, same failure shape) or "decreased" (a cleanup landed; still a failure so the baseline |
| 71 | + * never silently drifts stale, but a one-line fix -- regenerate). Sorted for stable, reviewable CI output. */ |
| 72 | +export function diffBrandingBaseline(baseline, current) { |
| 73 | + const failures = []; |
| 74 | + const files = new Set([...Object.keys(baseline), ...Object.keys(current)]); |
| 75 | + for (const file of [...files].sort()) { |
| 76 | + const before = baseline[file] ?? 0; |
| 77 | + const after = current[file] ?? 0; |
| 78 | + if (after > before) { |
| 79 | + failures.push( |
| 80 | + `${file}: "gittensory" mentions increased from ${before} to ${after} -- looks like new branding drift, not an intentional historical reference. If it genuinely belongs (e.g. a permanent Sentry ticket ID or a stable comment-marker already posted to live PRs), run \`npm run branding-drift:update\` and commit the regenerated baseline.`, |
| 81 | + ); |
| 82 | + } else if (after < before) { |
| 83 | + failures.push( |
| 84 | + `${file}: "gittensory" mentions decreased from ${before} to ${after} -- looks like a cleanup landed without regenerating the baseline. Run \`npm run branding-drift:update\` and commit the result.`, |
| 85 | + ); |
| 86 | + } |
| 87 | + } |
| 88 | + return failures; |
| 89 | +} |
| 90 | + |
| 91 | +function readBaseline(root) { |
| 92 | + return JSON.parse(readFileSync(join(root, BASELINE_RELATIVE_PATH), "utf8")); |
| 93 | +} |
| 94 | + |
| 95 | +function writeBaseline(root, counts) { |
| 96 | + const sorted = Object.fromEntries(Object.entries(counts).sort(([a], [b]) => a.localeCompare(b))); |
| 97 | + writeFileSync(join(root, BASELINE_RELATIVE_PATH), `${JSON.stringify(sorted, null, 2)}\n`); |
| 98 | +} |
| 99 | + |
| 100 | +function main() { |
| 101 | + const root = process.cwd(); |
| 102 | + const update = process.argv.includes("--update"); |
| 103 | + const current = scanBrandingHits({ root }); |
| 104 | + |
| 105 | + if (update) { |
| 106 | + writeBaseline(root, current); |
| 107 | + console.log(`Branding-drift baseline regenerated: ${Object.keys(current).length} file(s) with a "gittensory" reference.`); |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + const baseline = readBaseline(root); |
| 112 | + const failures = diffBrandingBaseline(baseline, current); |
| 113 | + |
| 114 | + if (failures.length > 0) { |
| 115 | + console.error(`Branding-drift check found ${failures.length} issue(s):`); |
| 116 | + for (const failure of failures) console.error(failure); |
| 117 | + process.exit(1); |
| 118 | + } |
| 119 | + |
| 120 | + console.log(`Branding-drift check ok: ${Object.keys(current).length} file(s) match the recorded baseline.`); |
| 121 | +} |
| 122 | + |
| 123 | +if (process.argv[1] === fileURLToPath(import.meta.url)) main(); |
0 commit comments