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>
22 lines
822 B
JavaScript
22 lines
822 B
JavaScript
// Fetches a raw unified diff over plain HTTPS. No auth for public PRs/MRs on
|
|
// any of the supported hosts; an optional bearer token can be supplied for
|
|
// private repos on hosts that accept one on the raw-diff endpoint (GitHub,
|
|
// Gitea/Rithub).
|
|
|
|
export async function fetchDiff(diffUrl, { token } = {}) {
|
|
const headers = { "user-agent": "git-pr-brief (https://www.npmjs.com/package/git-pr-brief)" };
|
|
if (token) {
|
|
headers.authorization = `Bearer ${token}`;
|
|
}
|
|
|
|
const res = await fetch(diffUrl, { headers, redirect: "follow" });
|
|
if (!res.ok) {
|
|
const hint =
|
|
res.status === 404
|
|
? " (private repo? pass --token, or the PR/MR number may be wrong)"
|
|
: "";
|
|
throw new Error(`Fetching diff failed: HTTP ${res.status} ${res.statusText} — ${diffUrl}${hint}`);
|
|
}
|
|
return res.text();
|
|
}
|