Files
PaperClipAI/server/src/build-commit.ts
T
Devin FoleyandPaperclip 600e552d7b fix: attribute Sentry errors to the loaded source release (#13719)
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>
2026-09-20 08:08:33 -07:00

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;
}
}