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