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>
84 lines
2.8 KiB
JavaScript
84 lines
2.8 KiB
JavaScript
// Turns a PR / merge-request / pull-request web URL (or an already-raw diff
|
|
// URL) from any of the major git hosts into a raw unified-diff URL, plus a
|
|
// human-readable label for the thing being summarized.
|
|
//
|
|
// Supported today:
|
|
// GitHub https://github.com/{owner}/{repo}/pull/{n}
|
|
// Rithub (Gitea) https://rithub.dev/{owner}/{repo}/pulls/{n}
|
|
// (and any other Gitea/Forgejo host — same URL shape)
|
|
// GitLab https://gitlab.com/{group}/{project}/-/merge_requests/{n}
|
|
// (and self-hosted GitLab instances)
|
|
// Bitbucket https://bitbucket.org/{workspace}/{repo}/pull-requests/{n}
|
|
// Raw diff/patch any URL already ending in .diff or .patch, used as-is.
|
|
//
|
|
// Returns { diffUrl, label, host } or throws a descriptive Error.
|
|
|
|
export function resolvePrUrl(input) {
|
|
if (!input) {
|
|
throw new Error("No PR/MR URL given.");
|
|
}
|
|
|
|
if (/\.(diff|patch)(\?.*)?$/i.test(input)) {
|
|
return { diffUrl: input, label: input, host: "raw" };
|
|
}
|
|
|
|
let url;
|
|
try {
|
|
url = new URL(input);
|
|
} catch {
|
|
throw new Error(`Not a valid URL: ${input}`);
|
|
}
|
|
|
|
const host = url.hostname.toLowerCase();
|
|
const path = url.pathname.replace(/\/+$/, "");
|
|
|
|
// GitHub: /{owner}/{repo}/pull/{n}
|
|
const gh = path.match(/^\/([^/]+)\/([^/]+)\/pull\/(\d+)$/);
|
|
if (host === "github.com" && gh) {
|
|
const [, owner, repo, n] = gh;
|
|
return {
|
|
diffUrl: `https://github.com/${owner}/${repo}/pull/${n}.diff`,
|
|
label: `${owner}/${repo}#${n} (GitHub)`,
|
|
host: "github",
|
|
};
|
|
}
|
|
|
|
// GitLab (gitlab.com or self-hosted): /{...}/-/merge_requests/{n}
|
|
const gl = path.match(/^(\/.+)\/-\/merge_requests\/(\d+)$/);
|
|
if (gl) {
|
|
const [, projectPath, n] = gl;
|
|
return {
|
|
diffUrl: `https://${host}${projectPath}/-/merge_requests/${n}.diff`,
|
|
label: `${projectPath.replace(/^\//, "")}!${n} (${host})`,
|
|
host: "gitlab",
|
|
};
|
|
}
|
|
|
|
// Gitea / Forgejo / Rithub: /{owner}/{repo}/pulls/{n}
|
|
const gitea = path.match(/^\/([^/]+)\/([^/]+)\/pulls\/(\d+)$/);
|
|
if (gitea) {
|
|
const [, owner, repo, n] = gitea;
|
|
return {
|
|
diffUrl: `https://${host}/${owner}/${repo}/pulls/${n}.diff`,
|
|
label: `${owner}/${repo}#${n} (${host})`,
|
|
host: "gitea",
|
|
};
|
|
}
|
|
|
|
// Bitbucket: /{workspace}/{repo}/pull-requests/{n}
|
|
const bb = path.match(/^\/([^/]+)\/([^/]+)\/pull-requests\/(\d+)$/);
|
|
if (host === "bitbucket.org" && bb) {
|
|
const [, workspace, repo, n] = bb;
|
|
return {
|
|
diffUrl: `https://bitbucket.org/${workspace}/${repo}/pull-requests/${n}/diff`,
|
|
label: `${workspace}/${repo}#${n} (Bitbucket)`,
|
|
host: "bitbucket",
|
|
};
|
|
}
|
|
|
|
throw new Error(
|
|
`Don't recognize this as a GitHub/GitLab/Gitea(Rithub)/Bitbucket PR URL, ` +
|
|
`and it doesn't end in .diff or .patch either: ${input}`
|
|
);
|
|
}
|