The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { ThreadLinkedPullRequest, ThreadPullRequestKey } from "@t3tools/contracts"; |
| 2 | |
| 3 | /** |
| 4 | * Frozen for migrations 050 and 064: the change request URL parsing and key normalization they |
| 5 | * shipped with. Migrations are history, so they must not follow the live source control hosts. |
| 6 | */ |
| 7 | |
| 8 | interface FrozenLink { |
| 9 | readonly host: string; |
| 10 | readonly repository: string; |
| 11 | readonly number: number; |
| 12 | readonly authority?: string; |
| 13 | } |
| 14 | |
| 15 | function canonicalRepositoryKey(key: string): string { |
| 16 | return key |
| 17 | .replace( |
| 18 | /^(?:ssh\.dev\.azure\.com|vs-ssh\.visualstudio\.com)\/v3\/([^/]+)\/([^/]+)\/([^/]+)$/u, |
| 19 | "dev.azure.com/$1/$2/_git/$3", |
| 20 | ) |
| 21 | .replace( |
| 22 | /^([^.]+)\.visualstudio\.com\/(?:defaultcollection\/)?([^/]+)\/_git\/([^/]+)$/u, |
| 23 | "dev.azure.com/$1/$2/_git/$3", |
| 24 | ); |
| 25 | } |
| 26 | |
| 27 | function isHostOf(hostname: string, apex: string, label: string): boolean { |
| 28 | return hostname === apex || hostname.endsWith(`.${apex}`) || hostname.split(".").includes(label); |
| 29 | } |
| 30 | |
| 31 | function claim(host: string, match: RegExpExecArray | null): FrozenLink | null { |
| 32 | const repository = match?.[1]; |
| 33 | const number = Number(match?.[2]); |
| 34 | return repository && Number.isSafeInteger(number) && number > 0 |
| 35 | ? { host, repository: repository.toLowerCase(), number } |
| 36 | : null; |
| 37 | } |
| 38 | |
| 39 | function parseChangeRequestUrl(targetUrl: string): FrozenLink | null { |
| 40 | let url: URL; |
| 41 | try { |
| 42 | url = new URL(targetUrl); |
| 43 | } catch { |
| 44 | return null; |
| 45 | } |
| 46 | if (url.protocol !== "https:" && url.protocol !== "http:") return null; |
| 47 | const host = url.hostname.toLowerCase(); |
| 48 | |
| 49 | if (host === "git.cafe" || host === "staging.git.cafe") { |
| 50 | const match = /^\/([^/]+\/[^/]+)\/pulls?\/(\d+)(?:\/|$)/u.exec(url.pathname); |
| 51 | if (match) return claim(host, match); |
| 52 | } else { |
| 53 | if (isHostOf(host, "github.com", "github")) { |
| 54 | const match = /^\/([^/]+\/[^/]+)\/pull\/(\d+)(?:\/|$)/u.exec(url.pathname); |
| 55 | if (match) return claim(host, match); |
| 56 | } |
| 57 | const forgejo = /^\/([^/]+(?:\/[^/]+)+)\/pulls\/(\d+)(?:\/|$)/u.exec(url.pathname); |
| 58 | if (forgejo) { |
| 59 | const link = claim(host, forgejo); |
| 60 | return link === null ? null : { ...link, authority: url.host.toLowerCase() }; |
| 61 | } |
| 62 | const gitlab = /^\/([^/]+(?:\/[^/]+)+)\/-\/merge_requests\/(\d+)(?:\/|$)/u.exec(url.pathname); |
| 63 | if (gitlab) return claim(host, gitlab); |
| 64 | if (isHostOf(host, "bitbucket.org", "bitbucket")) { |
| 65 | const match = /^\/([^/]+\/[^/]+)\/pull-requests\/(\d+)(?:\/|$)/u.exec(url.pathname); |
| 66 | if (match) return claim(host, match); |
| 67 | } |
| 68 | } |
| 69 | const azure = /^\/((?:[^/]+\/)*_git\/[^/]+)\/pullrequest\/(\d+)(?:\/|$)/u.exec(url.pathname); |
| 70 | const link = claim(host, azure); |
| 71 | return link === null || url.port === "" ? link : { ...link, authority: url.host.toLowerCase() }; |
| 72 | } |
| 73 | |
| 74 | function normalizeThreadPullRequestKey( |
| 75 | key: ThreadPullRequestKey & { readonly authority?: string }, |
| 76 | ): ThreadPullRequestKey { |
| 77 | const canonical = canonicalRepositoryKey( |
| 78 | `${(key.authority ?? key.host).trim().toLowerCase()}/${key.repository.trim().toLowerCase()}`, |
| 79 | ); |
| 80 | const separator = canonical.indexOf("/"); |
| 81 | return { |
| 82 | host: canonical.slice(0, separator), |
| 83 | repository: canonical.slice(separator + 1), |
| 84 | number: key.number, |
| 85 | }; |
| 86 | } |
| 87 | |
| 88 | export function legacyThreadPullRequestKey( |
| 89 | linked: Pick<ThreadLinkedPullRequest, "repository" | "number" | "url">, |
| 90 | ): ThreadPullRequestKey { |
| 91 | const parsed = parseChangeRequestUrl(linked.url); |
| 92 | if (parsed !== null && parsed.number === linked.number) { |
| 93 | const canonical = canonicalRepositoryKey(`${parsed.host}/${parsed.repository}`); |
| 94 | if (parsed.authority !== undefined || canonical.startsWith("dev.azure.com/")) { |
| 95 | return normalizeThreadPullRequestKey(parsed); |
| 96 | } |
| 97 | } |
| 98 | let host: string; |
| 99 | try { |
| 100 | host = new URL(linked.url).hostname; |
| 101 | } catch { |
| 102 | host = "unknown"; |
| 103 | } |
| 104 | return { |
| 105 | host: host.trim().toLowerCase() || "unknown", |
| 106 | repository: linked.repository.trim().toLowerCase(), |
| 107 | number: linked.number, |
| 108 | }; |
| 109 | } |