git-pr-brief/lib/risk.mjs
caelum0x 753fe98687 feat: git-pr-brief 1.0.0, cross-host agent-shaped PR/MR risk brief
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>
2026-11-30 21:04:45 +03:00

120 lines
3.9 KiB
JavaScript

// Heuristic, offline risk flags computed straight from the parsed diff.
// No AI call, no network beyond the one diff fetch already done — this is
// deliberately a fast, free, deterministic pass an agent can call on every
// PR/MR before deciding whether to look closer or auto-approve.
const LOCKFILES = [
/^package-lock\.json$/,
/^yarn\.lock$/,
/^pnpm-lock\.yaml$/,
/^Cargo\.lock$/,
/^Gemfile\.lock$/,
/^poetry\.lock$/,
/^composer\.lock$/,
/^go\.sum$/,
];
const MIGRATION_PATTERNS = [
/\/migrations?\//i,
/\/db\/migrate\//i,
/schema\.(sql|prisma)$/i,
/\.sql$/i,
];
const CI_PATTERNS = [
/^\.github\/workflows\//,
/^\.gitlab-ci\.ya?ml$/,
/^Jenkinsfile$/,
/^\.circleci\//,
/^\.drone\.ya?ml$/,
];
const ENV_PATTERNS = [/^\.env(\..+)?$/, /\/\.env(\..+)?$/];
const TEST_PATTERNS = [
/(^|\/)(test|tests|__tests__|spec|specs)\//i,
/\.(test|spec)\.[jt]sx?$/i,
/_test\.[a-z]+$/i,
/_spec\.[a-z]+$/i,
];
const SOURCE_EXT = /\.(js|jsx|ts|tsx|py|rb|go|rs|java|kt|swift|c|cc|cpp|cs|php)$/i;
// Secret-shaped line patterns. Deliberately broad and offline; false
// positives are expected and fine — this flags "look here", not "this is
// definitely a leak".
const SECRET_LINE_PATTERNS = [
/AKIA[0-9A-Z]{16}/, // AWS access key id
/-----BEGIN (RSA |EC |OPENSSH |DSA |)PRIVATE KEY-----/,
/\b(api[_-]?key|secret[_-]?key|access[_-]?token|client[_-]?secret)\s*[=:]\s*["']?[A-Za-z0-9_\-/+]{16,}/i,
/\bpassword\s*[=:]\s*["']?\S{6,}/i,
/ghp_[A-Za-z0-9]{36}/, // GitHub PAT
/sk-[A-Za-z0-9]{20,}/, // common LLM-provider secret key shape
];
function matchesAny(path, patterns) {
return patterns.some((p) => p.test(path));
}
export function computeRisk(diffText, { files, totals }, { largeDiffLines = 500, manyFilesCount = 30 } = {}) {
const flags = [];
const changedPaths = files.map((f) => f.path);
const lockfilesChanged = changedPaths.filter((p) => matchesAny(p, LOCKFILES));
if (lockfilesChanged.length) {
flags.push({ code: "LOCKFILE_CHANGED", files: lockfilesChanged });
}
const envChanged = changedPaths.filter((p) => matchesAny(p, ENV_PATTERNS));
if (envChanged.length) {
flags.push({ code: "ENV_FILE_CHANGED", files: envChanged });
}
const migrationsChanged = changedPaths.filter((p) => matchesAny(p, MIGRATION_PATTERNS));
if (migrationsChanged.length) {
flags.push({ code: "MIGRATION_CHANGED", files: migrationsChanged });
}
const ciChanged = changedPaths.filter((p) => matchesAny(p, CI_PATTERNS));
if (ciChanged.length) {
flags.push({ code: "CI_CONFIG_CHANGED", files: ciChanged });
}
const totalChangedLines = totals.additions + totals.deletions;
if (totalChangedLines > largeDiffLines) {
flags.push({ code: "LARGE_DIFF", totalChangedLines });
}
if (files.length > manyFilesCount) {
flags.push({ code: "MANY_FILES", fileCount: files.length });
}
const sourceChanged = changedPaths.some((p) => SOURCE_EXT.test(p) && !matchesAny(p, TEST_PATTERNS));
const testsChanged = changedPaths.some((p) => matchesAny(p, TEST_PATTERNS));
if (sourceChanged && !testsChanged) {
flags.push({ code: "NO_TESTS_WITH_SOURCE" });
}
const secretMatches = findPossibleSecrets(diffText);
if (secretMatches.length) {
flags.push({ code: "POSSIBLE_SECRET", samples: secretMatches.slice(0, 5), count: secretMatches.length });
}
return flags;
}
// Scans the raw diff text for added lines (not the whole file, just the +
// lines) matching secret-shaped patterns. Kept separate from computeRisk so
// callers that already have the raw text can pass it once.
export function findPossibleSecrets(diffText) {
const matches = [];
for (const rawLine of diffText.split("\n")) {
if (!rawLine.startsWith("+") || rawLine.startsWith("+++")) continue;
const line = rawLine.slice(1);
if (SECRET_LINE_PATTERNS.some((p) => p.test(line))) {
matches.push(line.trim().slice(0, 120));
}
}
return matches;
}