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
143/**
144 * Rewrites the host of an SCP-like or `ssh://` remote. HTTPS remotes are left
145 * unchanged so API/browser host mapping can reuse the git URL without touching
146 * clone auth.
147 */
148export function rewriteGitRemoteUrlHost(remoteUrl: string, canonicalHostname: string): string {
149 const trimmed = remoteUrl.trim();
150 const hostname = canonicalHostname.trim().toLowerCase();
151 if (trimmed.length === 0 || hostname.length === 0) {
152 return remoteUrl;
153 }
154
155 const scpMatch = SCP_SSH_REMOTE_PATTERN.exec(trimmed);
156 if (scpMatch?.[1] !== undefined && scpMatch[2] !== undefined && scpMatch[3] !== undefined) {
157 return `${scpMatch[1]}@${hostname}:${scpMatch[3]}`;
158 }
159
160 if (trimmed.toLowerCase().startsWith("ssh://")) {
161 try {
162 const url = new URL(trimmed);
163 url.hostname = hostname;
164 return url.toString();
165 } catch {
166 return remoteUrl;
167 }
168 }
169
170 return remoteUrl;
171}
172
173function parseRemoteHost(remoteUrl: string): string | null {
174 const trimmed = remoteUrl.trim();
175 if (trimmed.length === 0) {
176 return null;
177 }
178
179 const scpMatch = SCP_SSH_REMOTE_PATTERN.exec(trimmed);
180 if (scpMatch?.[2]) {
181 return scpMatch[2].toLowerCase();
182 }
183
184 try {
185 return new URL(trimmed).host.toLowerCase();
186 } catch {
187 return null;
188 }
189}
190
191function parseHostName(host: string): string {
192 try {
193 return new URL(`https://${host}`).hostname.toLowerCase();
194 } catch {
195 return host.replace(/:\d+$/u, "").toLowerCase();
196 }
197}
198
199function toBaseUrl(host: string): string {
200 return `https://${host}`;
201}
202
203function hasDnsLabel(host: string, label: string): boolean {
204 return host.split(".").includes(label);
205}
206
207function isPublicOrDottedLabelHost(host: string, publicHost: string, label: string): boolean {
208 // Undotted SSH aliases (`github-personal`, `gitlab-work`) share a substring
209 // with the public host but are not enterprise installs. Require a real
210 // hostname (a dot) before treating a label match as self-hosted.
211 return host === publicHost || (host.includes(".") && hasDnsLabel(host, label));
212}
213
214function isGitHubHost(host: string): boolean {
215 return isPublicOrDottedLabelHost(host, "github.com", "github");
216}
217
218function isGitLabHost(host: string): boolean {
219 return isPublicOrDottedLabelHost(host, "gitlab.com", "gitlab");
220}
221
222function isAzureDevOpsHost(host: string): boolean {
223 // `ssh.dev.azure.com` is the default Azure DevOps SSH clone host
224 // (git@ssh.dev.azure.com:v3/org/project/repo), so match any `*.dev.azure.com`
225 // subdomain, not just the bare `dev.azure.com`. Legacy hosts stay under
226 // `.visualstudio.com` (including `vs-ssh.visualstudio.com`).
227 return (
228 host === "dev.azure.com" ||
229 host.endsWith(".dev.azure.com") ||
230 host.endsWith(".visualstudio.com")
231 );
232}
233
234function isBitbucketHost(host: string): boolean {
235 return isPublicOrDottedLabelHost(host, "bitbucket.org", "bitbucket");
236}
237
238export function detectSourceControlProviderFromRemoteUrl(
239 remoteUrl: string,
240): SourceControlProviderInfo | null {
241 const host = parseRemoteHost(remoteUrl);
242 if (!host) {
243 return null;
244 }
245 const hostname = parseHostName(host);
246
247 if (isGitHubHost(hostname)) {
248 return {
249 kind: "github",
250 name: hostname === "github.com" ? "GitHub" : "GitHub Self-Hosted",
251 baseUrl: toBaseUrl(host),
252 };
253 }
254
255 if (isGitLabHost(hostname)) {
256 return {
257 kind: "gitlab",
258 name: hostname === "gitlab.com" ? "GitLab" : "GitLab Self-Hosted",
259 baseUrl: toBaseUrl(host),
260 };
261 }
262
263 if (isAzureDevOpsHost(hostname)) {
264 return {
265 kind: "azure-devops",
266 name: "Azure DevOps",
267 baseUrl: toBaseUrl(host),
268 };
269 }
270
271 if (isBitbucketHost(hostname)) {
272 return {
273 kind: "bitbucket",
274 name: hostname === "bitbucket.org" ? "Bitbucket" : "Bitbucket Self-Hosted",
275 baseUrl: toBaseUrl(host),
276 };
277 }
278
279 return {
280 kind: "unknown",
281 name: host,
282 baseUrl: toBaseUrl(host),
283 };
284}