mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-10-07 16:11:46 +02:00
Attribute optional server and browser Sentry events to their source build. Use validated build commits for Docker and source/npm artifacts, preserve explicit server release overrides, and keep cached browser bundles tied to the commit they loaded. Verify 127 focused tests, server/UI typechecks, Docker and source build stamps, all 53 CI checks, and Greptile 5/5 with no unresolved comments. Co-Authored-By: Paperclip <noreply@paperclip.ing>
44 lines
1.5 KiB
TypeScript
44 lines
1.5 KiB
TypeScript
import { readFileSync } from "node:fs";
|
|
import { fileURLToPath } from "node:url";
|
|
|
|
type ReadTextFile = (path: string) => string;
|
|
|
|
const FULL_SHA_RE = /^[0-9a-f]{40}$/i;
|
|
const DEFAULT_BUILD_COMMIT_PATH = fileURLToPath(
|
|
new URL("../../.paperclip-build-commit", import.meta.url),
|
|
);
|
|
const DEFAULT_BUILD_INFO_PATH = fileURLToPath(new URL("./build-info.json", import.meta.url));
|
|
|
|
export function parseBuildCommit(value: string | null | undefined): string | null {
|
|
const commit = value?.trim() ?? "";
|
|
return FULL_SHA_RE.test(commit) ? commit.toLowerCase() : null;
|
|
}
|
|
|
|
export function readBuildCommit(
|
|
opts: {
|
|
environmentCommit?: string | null;
|
|
buildCommitPath?: string;
|
|
buildInfoPath?: string;
|
|
readTextFile?: ReadTextFile;
|
|
} = {},
|
|
): string | null {
|
|
const environmentCommit = parseBuildCommit(
|
|
opts.environmentCommit === undefined
|
|
? process.env.PAPERCLIP_BUILD_COMMIT
|
|
: opts.environmentCommit,
|
|
);
|
|
if (environmentCommit) return environmentCommit;
|
|
|
|
const readTextFile = opts.readTextFile ?? ((path: string) => readFileSync(path, "utf8"));
|
|
try {
|
|
const markerCommit = parseBuildCommit(readTextFile(opts.buildCommitPath ?? DEFAULT_BUILD_COMMIT_PATH));
|
|
if (markerCommit) return markerCommit;
|
|
} catch { /* The deployment marker is absent in npm packages. */ }
|
|
try {
|
|
const stamp = JSON.parse(readTextFile(opts.buildInfoPath ?? DEFAULT_BUILD_INFO_PATH));
|
|
return typeof stamp?.commit === "string" ? parseBuildCommit(stamp.commit) : null;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|