// Plan2Code Web Console - the pull-request link for a branch, and the link to // the branch itself. // // One pure builder for the four host shapes in the git-dashboard spec // (overview.md -> Pull-request URL shapes). git-repo.mjs re-exports it for the // snapshot, and the board's base picker rebuilds the link in the browser with // the same code. No DOM access at module scope and no imports: the server, // git-repo.mjs, the page and `node --test` all import this file. // GitHub's compare path keeps the "/" in a branch name; each segment is // encoded on its own. const segments = (branch) => branch.split("/").map(encodeURIComponent).join("/"); const q = encodeURIComponent; const SHAPES = { github: (web, base, head) => `${web}/compare/${segments(base)}...${segments(head)}?quick_pull=1`, gitlab: (web, base, head) => `${web}/-/merge_requests/new?merge_request%5Bsource_branch%5D=${q(head)}&merge_request%5Btarget_branch%5D=${q(base)}`, azure: (web, base, head) => `${web}/pullrequestcreate?sourceRef=${q(head)}&targetRef=${q(base)}`, bitbucket: (web, base, head) => `${web}/pull-requests/new?source=${q(head)}&dest=${q(base)}`, }; /** * The "open a pull request" link for `head` into `base` on a remote, or null * when the host has no known shape (kind null or "none"), the remote has no * web address, or the result would not be an https: link. */ export function prUrl({ web, kind } = {}, { base, head } = {}) { const shape = SHAPES[kind]; if (!shape || !web || !base || !head) return null; return httpsOnly(shape(String(web).replace(/\/+$/, ""), base, head)); } const httpsOnly = (url) => { try { return new URL(url).protocol === "https:" ? url : null; } catch { return null; } }; // Where each host shows one branch's files. const BRANCH_SHAPES = { github: (web, branch) => `${web}/tree/${segments(branch)}`, gitlab: (web, branch) => `${web}/-/tree/${segments(branch)}`, azure: (web, branch) => `${web}?version=GB${q(branch)}`, bitbucket: (web, branch) => `${web}/branch/${segments(branch)}`, }; /** * The page for `branch` on a remote, under the same rules as prUrl(): null * for an unknown host kind, no web address, or a result that is not https:. */ export function branchUrl({ web, kind } = {}, branch) { const shape = BRANCH_SHAPES[kind]; if (!shape || !web || !branch) return null; return httpsOnly(shape(String(web).replace(/\/+$/, ""), branch)); }