Open History to see the last commit
1import type { SourceControlProviderInfo, SourceControlProviderKind } from "@t3tools/contracts";
2
3export interface ChangeRequestPresentation {
4 readonly icon: "github" | "gitlab" | "azure-devops" | "bitbucket" | "change-request";
5 readonly providerName: string;
6 readonly shortName: string;
7 readonly longName: string;
8 readonly pluralLongName: string;
9 readonly providerLongName: string;
10 readonly checkoutCommandExample?: string;
11 readonly urlExample: string;
12}
13
14export interface ChangeRequestTerminology {
15 readonly shortLabel: string;
16 readonly singular: string;
17}
18
19export const DEFAULT_CHANGE_REQUEST_TERMINOLOGY: ChangeRequestTerminology = {
20 shortLabel: "PR",
21 singular: "pull request",
22};
23
24const GITHUB_CHANGE_REQUEST_PRESENTATION: ChangeRequestPresentation = {
25 icon: "github",
26 providerName: "GitHub",
27 shortName: "PR",
28 longName: "pull request",
29 pluralLongName: "pull requests",
30 providerLongName: "GitHub pull request",
31 checkoutCommandExample: "gh pr checkout 123",
32 urlExample: "https://github.com/owner/repo/pull/42",
33};
34
35const GITLAB_CHANGE_REQUEST_PRESENTATION: ChangeRequestPresentation = {
36 icon: "gitlab",
37 providerName: "GitLab",
38 shortName: "MR",
39 longName: "merge request",
40 pluralLongName: "merge requests",
41 providerLongName: "GitLab merge request",
42 checkoutCommandExample: "glab mr checkout 123",
43 urlExample: "https://gitlab.com/group/project/-/merge_requests/42",
44};
45
46const AZURE_DEVOPS_CHANGE_REQUEST_PRESENTATION: ChangeRequestPresentation = {
47 icon: "azure-devops",
48 providerName: "Azure DevOps",
49 shortName: "PR",
50 longName: "pull request",
51 pluralLongName: "pull requests",
52 providerLongName: "Azure DevOps pull request",
53 checkoutCommandExample: "az repos pr checkout --id 123",
54 urlExample: "https://dev.azure.com/org/project/_git/repo/pullrequest/42",
55};
56
57const BITBUCKET_CHANGE_REQUEST_PRESENTATION: ChangeRequestPresentation = {
58 icon: "bitbucket",
59 providerName: "Bitbucket",
60 shortName: "PR",
61 longName: "pull request",
62 pluralLongName: "pull requests",
63 providerLongName: "Bitbucket pull request",
64 urlExample: "https://bitbucket.org/workspace/repo/pull-requests/42",
65};
66
67const GENERIC_CHANGE_REQUEST_PRESENTATION: ChangeRequestPresentation = {
68 icon: "change-request",
69 providerName: "source control",
70 shortName: "change request",
71 longName: "change request",
72 pluralLongName: "change requests",
73 providerLongName: "change request",
74 urlExample: "#42",
75};
76
77export function resolveChangeRequestPresentation(
78 provider: SourceControlProviderInfo | null | undefined,
79): ChangeRequestPresentation {
80 switch (provider?.kind) {
81 case "github":
82 case undefined:
83 return GITHUB_CHANGE_REQUEST_PRESENTATION;
84 case "gitlab":
85 return GITLAB_CHANGE_REQUEST_PRESENTATION;
86 case "azure-devops":
87 return AZURE_DEVOPS_CHANGE_REQUEST_PRESENTATION;
88 case "bitbucket":
89 return BITBUCKET_CHANGE_REQUEST_PRESENTATION;
90 case "unknown":
91 return GENERIC_CHANGE_REQUEST_PRESENTATION;
92 }
93}
94
95export function resolveChangeRequestPresentationForKind(
96 kind: SourceControlProviderKind,
97): ChangeRequestPresentation {
98 return resolveChangeRequestPresentation({ kind, name: "", baseUrl: "" });
99}
100
101export function formatChangeRequestAction(
102 verb: "View" | "Create",
103 presentation: ChangeRequestPresentation,
104): string {
105 return `${verb} ${presentation.shortName}`;
106}
107
108export function formatCreateChangeRequestPhrase(presentation: ChangeRequestPresentation): string {
109 return `create ${presentation.shortName}`;
110}
111
112export function getChangeRequestTerminology(
113 provider: SourceControlProviderInfo | null | undefined,
114): ChangeRequestTerminology {
115 if (!provider) {
116 return DEFAULT_CHANGE_REQUEST_TERMINOLOGY;
117 }
118
119 const presentation = resolveChangeRequestPresentation(provider);
120 return {
121 shortLabel: presentation.shortName,
122 singular: presentation.longName,
123 };
124}
125
126export function getChangeRequestTerminologyForKind(
127 kind: SourceControlProviderKind,
128): ChangeRequestTerminology {
129 const presentation = resolveChangeRequestPresentationForKind(kind);
130 return {
131 shortLabel: presentation.shortName,
132 singular: presentation.longName,
133 };
134}
135
136const SCP_SSH_REMOTE_PATTERN = /^[a-zA-Z0-9._-]+@([^:/]+):/;
137
138export function isSshRemoteUrl(remoteUrl: string): boolean {
139 const trimmed = remoteUrl.trim();
140 return SCP_SSH_REMOTE_PATTERN.test(trimmed) || trimmed.toLowerCase().startsWith("ssh://");
141}
142
143function parseRemoteHost(remoteUrl: string): string | null {
144 const trimmed = remoteUrl.trim();
145 if (trimmed.length === 0) {
146 return null;
147 }
148
149 const scpMatch = SCP_SSH_REMOTE_PATTERN.exec(trimmed);
150 if (scpMatch?.[1]) {
151 return scpMatch[1].toLowerCase();
152 }
153
154 try {
155 return new URL(trimmed).host.toLowerCase();
156 } catch {
157 return null;
158 }
159}
160
161function parseHostName(host: string): string {
162 try {
163 return new URL(`https://${host}`).hostname.toLowerCase();
164 } catch {
165 return host.replace(/:\d+$/u, "").toLowerCase();
166 }
167}
168
169function toBaseUrl(host: string): string {
170 return `https://${host}`;
171}
172
173// Multi-account ~/.ssh/config setups follow the documented convention of
174// suffixing the real host in the alias, e.g. `Host github.com-work`. The alias
175// only exists for SSH, so HTTPS remotes keep their literal host.
176const SSH_ALIAS_HOSTNAME_PATTERN = /^(github\.com|gitlab\.com|bitbucket\.org)-[^./]+$/;
177
178export function canonicalizeSshRemoteHostname(hostname: string): string {
179 return SSH_ALIAS_HOSTNAME_PATTERN.exec(hostname)?.[1] ?? hostname;
180}
181
182function hasDnsLabel(host: string, label: string): boolean {
183 return host.split(".").includes(label);
184}
185
186function isGitHubHost(host: string): boolean {
187 return host === "github.com" || hasDnsLabel(host, "github");
188}
189
190function isGitLabHost(host: string): boolean {
191 return host === "gitlab.com" || hasDnsLabel(host, "gitlab");
192}
193
194function isAzureDevOpsHost(host: string): boolean {
195 // `ssh.dev.azure.com` is the default Azure DevOps SSH clone host
196 // (git@ssh.dev.azure.com:v3/org/project/repo), so match any `*.dev.azure.com`
197 // subdomain, not just the bare `dev.azure.com`. Legacy hosts stay under
198 // `.visualstudio.com` (including `vs-ssh.visualstudio.com`).
199 return (
200 host === "dev.azure.com" ||
201 host.endsWith(".dev.azure.com") ||
202 host.endsWith(".visualstudio.com")
203 );
204}
205
206function isBitbucketHost(host: string): boolean {
207 return host === "bitbucket.org" || hasDnsLabel(host, "bitbucket");
208}
209
210export function detectSourceControlProviderFromRemoteUrl(
211 remoteUrl: string,
212): SourceControlProviderInfo | null {
213 const parsedHost = parseRemoteHost(remoteUrl);
214 if (!parsedHost) {
215 return null;
216 }
217 const parsedHostname = parseHostName(parsedHost);
218 const hostname = isSshRemoteUrl(remoteUrl)
219 ? canonicalizeSshRemoteHostname(parsedHostname)
220 : parsedHostname;
221 // Stripping the alias suffix drops any port, but SSH ports never belong in
222 // the https base URL anyway; unaliased hosts keep their port as before.
223 const host = hostname === parsedHostname ? parsedHost : hostname;
224
225 if (isGitHubHost(hostname)) {
226 return {
227 kind: "github",
228 name: hostname === "github.com" ? "GitHub" : "GitHub Self-Hosted",
229 baseUrl: toBaseUrl(host),
230 };
231 }
232
233 if (isGitLabHost(hostname)) {
234 return {
235 kind: "gitlab",
236 name: hostname === "gitlab.com" ? "GitLab" : "GitLab Self-Hosted",
237 baseUrl: toBaseUrl(host),
238 };
239 }
240
241 if (isAzureDevOpsHost(hostname)) {
242 return {
243 kind: "azure-devops",
244 name: "Azure DevOps",
245 baseUrl: toBaseUrl(host),
246 };
247 }
248
249 if (isBitbucketHost(hostname)) {
250 return {
251 kind: "bitbucket",
252 name: hostname === "bitbucket.org" ? "Bitbucket" : "Bitbucket Self-Hosted",
253 baseUrl: toBaseUrl(host),
254 };
255 }
256
257 return {
258 kind: "unknown",
259 name: host,
260 baseUrl: toBaseUrl(host),
261 };
262}