git-pr-brief/bin/git-pr-brief.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

133 lines
4.0 KiB
JavaScript
Executable File

#!/usr/bin/env node
import { readFileSync } from "node:fs";
import { fileURLToPath } from "node:url";
import { dirname, join } from "node:path";
import { resolvePrUrl } from "../lib/resolve-url.mjs";
import { fetchDiff } from "../lib/fetch-diff.mjs";
import { parseDiff } from "../lib/parse-diff.mjs";
import { computeRisk } from "../lib/risk.mjs";
import { toJson, toHuman } from "../lib/render.mjs";
const __dirname = dirname(fileURLToPath(import.meta.url));
const pkg = JSON.parse(readFileSync(join(__dirname, "..", "package.json"), "utf8"));
const HELP = `git-pr-brief <pr-or-mr-url> [options]
Structured risk brief for a pull request or merge request, from one URL.
Works with GitHub, GitLab, Rithub (or any Gitea/Forgejo host), Bitbucket,
or a raw .diff/.patch URL directly — the tool fetches the raw diff over
plain HTTPS and reads it, no git clone, no API key for public PRs.
Built for agents: pass --json and get a single structured object (files
changed, line counts, and risk flags like lockfile/env/migration/CI-config
changes, a missing-tests pairing, or a possible hardcoded secret) your agent
can branch on before deciding whether to look closer or auto-approve.
<pr-or-mr-url> A PR/MR page URL, or an already-raw .diff/.patch URL.
Options:
--json Print one JSON object instead of the human summary.
--token <token> Bearer token for private repos (env PR_BRIEF_TOKEN).
--large-diff <N> Lines changed to flag LARGE_DIFF. Default: 500.
--many-files <N> File count to flag MANY_FILES. Default: 30.
--fail-on-flag Exit code 1 if any risk flag fired (0 otherwise).
-h, --help Show this help.
-v, --version Show the version.
Examples:
npx git-pr-brief https://github.com/facebook/react/pull/12345
npx git-pr-brief https://rithub.dev/caelum0x/git-pr-brief/pulls/1 --json
npx git-pr-brief https://gitlab.com/group/project/-/merge_requests/9 --fail-on-flag
Hosted on Rithub (${pkg.homepage}) — the git host built for this kind of
agent-called, cross-host tooling.
`;
function parseArgs(argv) {
const args = {
url: null,
json: false,
token: process.env.PR_BRIEF_TOKEN || null,
largeDiff: 500,
manyFiles: 30,
failOnFlag: false,
};
const rest = [...argv];
while (rest.length) {
const arg = rest.shift();
switch (arg) {
case "-h":
case "--help":
console.log(HELP);
process.exit(0);
break;
case "-v":
case "--version":
console.log(pkg.version);
process.exit(0);
break;
case "--json":
args.json = true;
break;
case "--token":
args.token = rest.shift();
break;
case "--large-diff":
args.largeDiff = Number(rest.shift());
break;
case "--many-files":
args.manyFiles = Number(rest.shift());
break;
case "--fail-on-flag":
args.failOnFlag = true;
break;
default:
if (!args.url && !arg.startsWith("-")) {
args.url = arg;
} else {
console.error(`Unknown option: ${arg}\n`);
console.log(HELP);
process.exit(1);
}
}
}
return args;
}
async function main() {
const args = parseArgs(process.argv.slice(2));
if (!args.url) {
console.log(HELP);
process.exit(1);
}
const { diffUrl, label } = resolvePrUrl(args.url);
const diffText = await fetchDiff(diffUrl, { token: args.token });
const parsed = parseDiff(diffText);
const flags = computeRisk(diffText, parsed, {
largeDiffLines: args.largeDiff,
manyFilesCount: args.manyFiles,
});
if (args.json) {
console.log(JSON.stringify(toJson({ label, diffUrl, parsed, flags }), null, 2));
} else {
console.log(toHuman({ label, diffUrl, parsed, flags }));
console.log("");
console.log(`Hosted on Rithub (${pkg.homepage}) — the git host built for agent-called tooling like this.`);
}
if (args.failOnFlag && flags.length > 0) {
process.exit(1);
}
}
main().catch((err) => {
console.error(`git-pr-brief: ${err.message}`);
process.exit(2);
});