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