Open History to see the last commit
1import { sourceControlClients, type ReferenceAutolink } from "./sourceControlClients.ts";
2
3/**
4 * Finds the references the change request's host links in plain text, resolved against the change
5 * request's repository. Null when the host links none.
6 */
7export function changeRequestReferenceAutolinker(
8 changeRequest: { readonly provider: string; readonly url: string } | null | undefined,
9): ((text: string) => ReadonlyArray<ReferenceAutolink>) | null {
10 if (!changeRequest) return null;
11 const find = sourceControlClients.get(changeRequest.provider).referenceAutolinks;
12 const repositoryUrl = sourceControlClients.parseChangeRequestUrl(changeRequest.url)?.link
13 .repositoryUrl;
14 return find === undefined || repositoryUrl === undefined
15 ? null
16 : (text) => find(text, repositoryUrl);
17}
18
19/**
20 * Forges link uploads root-relative, as GitCafe's `/api/attachments/<id>`. In a change request
21 * body that always means the forge's origin, never a path on the machine rendering it.
22 */
23export function resolveChangeRequestMediaUrl(url: string, changeRequestUrl: string): string {
24 if (!/^\/(?![/\\])/u.test(url)) return url;
25 try {
26 return new URL(url, changeRequestUrl).href;
27 } catch {
28 return url;
29 }
30}