Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

sourceControl.ts

341 lines10.0 KB

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