Published to npm (git-pr-brief@1.0.0). Fetches a PR/MR's raw diff over plain HTTPS (GitHub, GitLab, Rithub/Gitea, Bitbucket, or a raw .diff URL) and computes offline risk flags (lockfile, env, migration, CI config, large diff, missing tests, possible secrets) an agent can branch on via --json / --fail-on-flag. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
76 lines
2.4 KiB
JavaScript
76 lines
2.4 KiB
JavaScript
const FLAG_LABELS = {
|
|
LOCKFILE_CHANGED: "lockfile changed",
|
|
ENV_FILE_CHANGED: "env file changed",
|
|
MIGRATION_CHANGED: "db migration / schema file changed",
|
|
CI_CONFIG_CHANGED: "CI config changed",
|
|
LARGE_DIFF: "large diff",
|
|
MANY_FILES: "many files touched",
|
|
NO_TESTS_WITH_SOURCE: "source changed, no tests touched",
|
|
POSSIBLE_SECRET: "possible secret in an added line",
|
|
};
|
|
|
|
export function toJson({ label, diffUrl, parsed, flags }) {
|
|
return {
|
|
label,
|
|
diffUrl,
|
|
files: parsed.files.length,
|
|
additions: parsed.totals.additions,
|
|
deletions: parsed.totals.deletions,
|
|
binaryFiles: parsed.totals.binaryFiles,
|
|
changedFiles: parsed.files.map((f) => ({
|
|
path: f.path,
|
|
additions: f.additions,
|
|
deletions: f.deletions,
|
|
binary: f.binary,
|
|
newFile: f.newFile,
|
|
deletedFile: f.deletedFile,
|
|
renamedFrom: f.renamedFrom,
|
|
})),
|
|
risk: {
|
|
flagCount: flags.length,
|
|
flags: flags.map((f) => ({ code: f.code, label: FLAG_LABELS[f.code] ?? f.code, ...f })),
|
|
},
|
|
};
|
|
}
|
|
|
|
export function toHuman({ label, parsed, flags }) {
|
|
const lines = [];
|
|
lines.push(`git-pr-brief — ${label}`);
|
|
lines.push(
|
|
`${parsed.files.length} file(s) changed, +${parsed.totals.additions} -${parsed.totals.deletions}` +
|
|
(parsed.totals.binaryFiles ? `, ${parsed.totals.binaryFiles} binary` : "")
|
|
);
|
|
lines.push("");
|
|
|
|
if (flags.length === 0) {
|
|
lines.push("No risk flags — nothing unusual found by this pass.");
|
|
} else {
|
|
lines.push(`${flags.length} risk flag(s):`);
|
|
for (const flag of flags) {
|
|
const label2 = FLAG_LABELS[flag.code] ?? flag.code;
|
|
const extra = flag.files
|
|
? ` — ${flag.files.slice(0, 5).join(", ")}${flag.files.length > 5 ? ", ..." : ""}`
|
|
: flag.totalChangedLines
|
|
? ` — ${flag.totalChangedLines} lines`
|
|
: flag.fileCount
|
|
? ` — ${flag.fileCount} files`
|
|
: flag.count
|
|
? ` — ${flag.count} line(s)`
|
|
: "";
|
|
lines.push(` [${flag.code}] ${label2}${extra}`);
|
|
}
|
|
}
|
|
|
|
lines.push("");
|
|
lines.push("Changed files:");
|
|
for (const f of parsed.files.slice(0, 50)) {
|
|
const tag = f.binary ? "(binary)" : f.newFile ? "(new)" : f.deletedFile ? "(deleted)" : "";
|
|
lines.push(` +${f.additions} -${f.deletions} ${f.path} ${tag}`.trimEnd());
|
|
}
|
|
if (parsed.files.length > 50) {
|
|
lines.push(` ... and ${parsed.files.length - 50} more`);
|
|
}
|
|
|
|
return lines.join("\n");
|
|
}
|