Open History to see the last commit
1import type {
2 RepositoryIdentity,
3 SourceControlProviderInfo,
4 SourceControlProviderKind,
5} from "@t3tools/contracts";
6
7export interface ChangeRequestPresentation {
8 readonly icon: "github" | "gitlab" | "forgejo" | "azure-devops" | "bitbucket" | "change-request";
9 readonly providerName: string;
10 readonly shortName: string;
11 readonly longName: string;
12 readonly pluralLongName: string;
13 readonly providerLongName: string;
14 readonly checkoutCommandExample?: string;
15 readonly urlExample: string;
16}
17
18export interface ChangeRequestTerminology {
19 readonly shortLabel: string;
20 readonly singular: string;
21}
22
23export const DEFAULT_CHANGE_REQUEST_TERMINOLOGY: ChangeRequestTerminology = {
24 shortLabel: "PR",
25 singular: "pull request",
26};
27
28const GITHUB_CHANGE_REQUEST_PRESENTATION: ChangeRequestPresentation = {
29 icon: "github",
30 providerName: "GitHub",
31 shortName: "PR",
32 longName: "pull request",
33 pluralLongName: "pull requests",
34 providerLongName: "GitHub pull request",
35 checkoutCommandExample: "gh pr checkout 123",
36 urlExample: "https://github.com/owner/repo/pull/42",
37};
38
39const GITHUB_ENTERPRISE_CHANGE_REQUEST_PRESENTATION: ChangeRequestPresentation = {
40 icon: "github",
41 providerName: "GitHub Enterprise",
42 shortName: "PR",
43 longName: "pull request",
44 pluralLongName: "pull requests",
45 providerLongName: "GitHub Enterprise pull request",
46 checkoutCommandExample: "gh pr checkout 123",
47 urlExample: "https://git.company.com/owner/repo/pull/42",
48};
49
50const GITLAB_CHANGE_REQUEST_PRESENTATION: ChangeRequestPresentation = {
51 icon: "gitlab",
52 providerName: "GitLab",
53 shortName: "MR",
54 longName: "merge request",
55 pluralLongName: "merge requests",
56 providerLongName: "GitLab merge request",
57 checkoutCommandExample: "glab mr checkout 123",
58 urlExample: "https://gitlab.com/group/project/-/merge_requests/42",
59};
60
61const FORGEJO_CHANGE_REQUEST_PRESENTATION: ChangeRequestPresentation = {
62 icon: "forgejo",
63 providerName: "Forgejo",
64 shortName: "PR",
65 longName: "pull request",
66 pluralLongName: "pull requests",
67 providerLongName: "Forgejo pull request",
68 checkoutCommandExample: "tea pr checkout 123",
69 urlExample: "https://codeberg.org/owner/repo/pulls/42",
70};
71
72const AZURE_DEVOPS_CHANGE_REQUEST_PRESENTATION: ChangeRequestPresentation = {
73 icon: "azure-devops",
74 providerName: "Azure DevOps",
75 shortName: "PR",
76 longName: "pull request",
77 pluralLongName: "pull requests",
78 providerLongName: "Azure DevOps pull request",
79 checkoutCommandExample: "az repos pr checkout --id 123",
80 urlExample: "https://dev.azure.com/org/project/_git/repo/pullrequest/42",
81};
82
83const BITBUCKET_CHANGE_REQUEST_PRESENTATION: ChangeRequestPresentation = {
84 icon: "bitbucket",
85 providerName: "Bitbucket",
86 shortName: "PR",
87 longName: "pull request",
88 pluralLongName: "pull requests",
89 providerLongName: "Bitbucket pull request",
90 urlExample: "https://bitbucket.org/workspace/repo/pull-requests/42",
91};
92
93const GENERIC_CHANGE_REQUEST_PRESENTATION: ChangeRequestPresentation = {
94 icon: "change-request",
95 providerName: "source control",
96 shortName: "change request",
97 longName: "change request",
98 pluralLongName: "change requests",
99 providerLongName: "change request",
100 urlExample: "#42",
101};
102
103export function resolveChangeRequestPresentation(
104 provider: SourceControlProviderInfo | null | undefined,
105): ChangeRequestPresentation {
106 switch (provider?.kind) {
107 case "github":
108 case undefined:
109 return GITHUB_CHANGE_REQUEST_PRESENTATION;
110 case "github-enterprise":
111 return GITHUB_ENTERPRISE_CHANGE_REQUEST_PRESENTATION;
112 case "gitlab":
113 return GITLAB_CHANGE_REQUEST_PRESENTATION;
114 case "forgejo":
115 return FORGEJO_CHANGE_REQUEST_PRESENTATION;
116 case "azure-devops":
117 return AZURE_DEVOPS_CHANGE_REQUEST_PRESENTATION;
118 case "bitbucket":
119 return BITBUCKET_CHANGE_REQUEST_PRESENTATION;
120 case "unknown":
121 return GENERIC_CHANGE_REQUEST_PRESENTATION;
122 }
123}
124
125function resolveChangeRequestPresentationForKind(
126 kind: SourceControlProviderKind,
127): ChangeRequestPresentation {
128 return resolveChangeRequestPresentation({ kind, name: "", baseUrl: "" });
129}
130
131export function getChangeRequestTerminology(
132 provider: SourceControlProviderInfo | null | undefined,
133): ChangeRequestTerminology {
134 if (!provider) {
135 return DEFAULT_CHANGE_REQUEST_TERMINOLOGY;
136 }
137
138 const presentation = resolveChangeRequestPresentation(provider);
139 return {
140 shortLabel: presentation.shortName,
141 singular: presentation.longName,
142 };
143}
144
145export function getChangeRequestTerminologyForKind(
146 kind: SourceControlProviderKind,
147): ChangeRequestTerminology {
148 const presentation = resolveChangeRequestPresentationForKind(kind);
149 return {
150 shortLabel: presentation.shortName,
151 singular: presentation.longName,
152 };
153}
154
155const SCP_SSH_REMOTE_PATTERN = /^[a-zA-Z0-9._-]+@([^:/]+):/;
156
157export function isSshRemoteUrl(remoteUrl: string): boolean {
158 const trimmed = remoteUrl.trim();
159 return SCP_SSH_REMOTE_PATTERN.test(trimmed) || trimmed.toLowerCase().startsWith("ssh://");
160}
161
162function parseRemoteHost(remoteUrl: string): string | null {
163 const trimmed = remoteUrl.trim();
164 if (trimmed.length === 0) {
165 return null;
166 }
167
168 const scpMatch = SCP_SSH_REMOTE_PATTERN.exec(trimmed);
169 if (scpMatch?.[1]) {
170 return scpMatch[1].toLowerCase();
171 }
172
173 try {
174 return new URL(trimmed).host.toLowerCase();
175 } catch {
176 return null;
177 }
178}
179
180function parseHostName(host: string): string {
181 try {
182 return new URL(`https://${host}`).hostname.toLowerCase();
183 } catch {
184 return host.replace(/:\d+$/u, "").toLowerCase();
185 }
186}
187
188function toBaseUrl(host: string): string {
189 return `https://${host}`;
190}
191
192function hasDnsLabel(host: string, label: string): boolean {
193 return host.split(".").includes(label);
194}
195
196/** Dotcom itself and the endpoints it serves elsewhere, such as `ssh.github.com` on port 443. */
197function isGitHubHost(host: string): boolean {
198 return host === "github.com" || host.endsWith(".github.com");
199}
200
201function isGitHubEnterpriseHost(host: string): boolean {
202 return !isGitHubHost(host) && (host.endsWith(".ghe.com") || hasDnsLabel(host, "github"));
203}
204
205function isGitLabHost(host: string): boolean {
206 return host === "gitlab.com" || hasDnsLabel(host, "gitlab");
207}
208
209function isAzureDevOpsHost(host: string): boolean {
210 // `ssh.dev.azure.com` is the default Azure DevOps SSH clone host
211 // (git@ssh.dev.azure.com:v3/org/project/repo), so match any `*.dev.azure.com`
212 // subdomain, not just the bare `dev.azure.com`. Legacy hosts stay under
213 // `.visualstudio.com` (including `vs-ssh.visualstudio.com`).
214 return (
215 host === "dev.azure.com" ||
216 host.endsWith(".dev.azure.com") ||
217 host.endsWith(".visualstudio.com")
218 );
219}
220
221function isBitbucketHost(host: string): boolean {
222 return host === "bitbucket.org" || hasDnsLabel(host, "bitbucket");
223}
224
225export function detectSourceControlProviderFromRemoteUrl(
226 remoteUrl: string,
227): SourceControlProviderInfo | null {
228 const host = parseRemoteHost(remoteUrl);
229 if (!host) {
230 return null;
231 }
232 const hostname = parseHostName(host);
233
234 if (
235 hostname === "codeberg.org" ||
236 hasDnsLabel(hostname, "forgejo") ||
237 hasDnsLabel(hostname, "gitea")
238 ) {
239 return {
240 kind: "forgejo",
241 name: "Forgejo",
242 baseUrl: /^https?:/iu.test(remoteUrl.trim())
243 ? new URL(remoteUrl.trim()).origin
244 : toBaseUrl(host),
245 };
246 }
247
248 if (isGitHubHost(hostname)) {
249 return {
250 kind: "github",
251 name: "GitHub",
252 baseUrl: toBaseUrl(host),
253 };
254 }
255
256 if (isGitHubEnterpriseHost(hostname)) {
257 return {
258 kind: "github-enterprise",
259 name: hostname,
260 baseUrl: toBaseUrl(host),
261 };
262 }
263
264 if (isGitLabHost(hostname)) {
265 return {
266 kind: "gitlab",
267 name: hostname === "gitlab.com" ? "GitLab" : "GitLab Self-Hosted",
268 baseUrl: toBaseUrl(host),
269 };
270 }
271
272 if (isAzureDevOpsHost(hostname)) {
273 return {
274 kind: "azure-devops",
275 name: "Azure DevOps",
276 baseUrl: toBaseUrl(host),
277 };
278 }
279
280 if (isBitbucketHost(hostname)) {
281 return {
282 kind: "bitbucket",
283 name: hostname === "bitbucket.org" ? "Bitbucket" : "Bitbucket Self-Hosted",
284 baseUrl: toBaseUrl(host),
285 };
286 }
287
288 return {
289 kind: "unknown",
290 name: host,
291 baseUrl: toBaseUrl(host),
292 };
293}
294
295/**
296 * The provider-native repository selector. `displayName` is the full path below the host, which
297 * is what nested GitLab groups need; owner/name is the two-segment fallback for identities
298 * recorded before that field existed.
299 *
300 * Azure DevOps is the exception: `az repos pr list --repository` takes a repository name, and
301 * takes the organisation and project from the checkout it detects — so the recorded
302 * `org/project/_git/repo` path is refused outright and the whole repository reads as
303 * unavailable. Its name is the last segment, which is what this hands over.
304 *
305 * One function because everything downstream is keyed by what it answers: the rows' own
306 * `repository`, the per-repository cursors, and the detail and diff reads a row leads to.
307 */
308export function sourceControlRepositorySelector(
309 identity:
310 | Pick<RepositoryIdentity, "provider" | "displayName" | "owner" | "name">
311 | null
312 | undefined,
313): string | null {
314 if (!identity) return null;
315 if (identity.provider === "azure-devops") {
316 const segments = (identity.displayName ?? "").split("/").filter((part) => part !== "_git");
317 return identity.name || segments.at(-1) || null;
318 }
319 if (identity.displayName) return identity.displayName;
320 return identity.owner && identity.name ? `${identity.owner}/${identity.name}` : null;
321}
322
323export function canonicalRepositoryKey(key: string): string {
324 return key
325 .replace(
326 /^(?:ssh\.dev\.azure\.com|vs-ssh\.visualstudio\.com)\/v3\/([^/]+)\/([^/]+)\/([^/]+)$/u,
327 "dev.azure.com/$1/$2/_git/$3",
328 )
329 .replace(
330 /^([^.]+)\.visualstudio\.com\/(?:defaultcollection\/)?([^/]+)\/_git\/([^/]+)$/u,
331 "dev.azure.com/$1/$2/_git/$3",
332 );
333}