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
173function hasDnsLabel(host: string, label: string): boolean {
174 return host.split(".").includes(label);
175}
176
177function isGitHubHost(host: string): boolean {
178 return host === "github.com" || hasDnsLabel(host, "github");
179}
180
181function isGitLabHost(host: string): boolean {
182 return host === "gitlab.com" || hasDnsLabel(host, "gitlab");
183}
184
185function isAzureDevOpsHost(host: string): boolean {
186 // `ssh.dev.azure.com` is the default Azure DevOps SSH clone host
187 // (git@ssh.dev.azure.com:v3/org/project/repo), so match any `*.dev.azure.com`
188 // subdomain, not just the bare `dev.azure.com`. Legacy hosts stay under
189 // `.visualstudio.com` (including `vs-ssh.visualstudio.com`).
190 return (
191 host === "dev.azure.com" ||
192 host.endsWith(".dev.azure.com") ||
193 host.endsWith(".visualstudio.com")
194 );
195}
196
197function isBitbucketHost(host: string): boolean {
198 return host === "bitbucket.org" || hasDnsLabel(host, "bitbucket");
199}
200
201export function detectSourceControlProviderFromRemoteUrl(
202 remoteUrl: string,
203): SourceControlProviderInfo | null {
204 const host = parseRemoteHost(remoteUrl);
205 if (!host) {
206 return null;
207 }
208 const hostname = parseHostName(host);
209
210 if (isGitHubHost(hostname)) {
211 return {
212 kind: "github",
213 name: hostname === "github.com" ? "GitHub" : "GitHub Self-Hosted",
214 baseUrl: toBaseUrl(host),
215 };
216 }
217
218 if (isGitLabHost(hostname)) {
219 return {
220 kind: "gitlab",
221 name: hostname === "gitlab.com" ? "GitLab" : "GitLab Self-Hosted",
222 baseUrl: toBaseUrl(host),
223 };
224 }
225
226 if (isAzureDevOpsHost(hostname)) {
227 return {
228 kind: "azure-devops",
229 name: "Azure DevOps",
230 baseUrl: toBaseUrl(host),
231 };
232 }
233
234 if (isBitbucketHost(hostname)) {
235 return {
236 kind: "bitbucket",
237 name: hostname === "bitbucket.org" ? "Bitbucket" : "Bitbucket Self-Hosted",
238 baseUrl: toBaseUrl(host),
239 };
240 }
241
242 return {
243 kind: "unknown",
244 name: host,
245 baseUrl: toBaseUrl(host),
246 };
247}