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

sourceControl.ts

307 lines9.0 KB

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;
17​
18export interface ChangeRequestTerminology {
19 readonly shortLabel: string;
20 readonly singular: string;
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 }
111​
112function resolveChangeRequestPresentationForKind(
113 kind: SourceControlProviderKind,
114): ChangeRequestPresentation {
115 return resolveChangeRequestPresentation({ kind, name: "", baseUrl: "" });
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 };
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 };
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://");
148​
149function parseRemoteHost(remoteUrl: string): string | null {
150 const trimmed = remoteUrl.trim();
151 if (trimmed.length === 0) {
152 return null;
153 }
154​
155 const scpMatch = SCP_SSH_REMOTE_PATTERN.exec(trimmed);
156 if (scpMatch?.[1]) {
157 return scpMatch[1].toLowerCase();
158 }
159​
160 try {
161 return new URL(trimmed).host.toLowerCase();
162 } catch {
163 return null;
164 }
166​
167function parseHostName(host: string): string {
168 try {
169 return new URL(`https://${host}`).hostname.toLowerCase();
170 } catch {
171 return host.replace(/:\d+$/u, "").toLowerCase();
172 }
174​
175function toBaseUrl(host: string): string {
176 return `https://${host}`;
178​
179function hasDnsLabel(host: string, label: string): boolean {
180 return host.split(".").includes(label);
182​
183function isGitHubHost(host: string): boolean {
184 return host === "github.com" || hasDnsLabel(host, "github");
186​
187function isGitLabHost(host: string): boolean {
188 return host === "gitlab.com" || hasDnsLabel(host, "gitlab");
190​
191function isAzureDevOpsHost(host: string): boolean {
192 // `ssh.dev.azure.com` is the default Azure DevOps SSH clone host
193 // (git@ssh.dev.azure.com:v3/org/project/repo), so match any `*.dev.azure.com`
194 // subdomain, not just the bare `dev.azure.com`. Legacy hosts stay under
195 // `.visualstudio.com` (including `vs-ssh.visualstudio.com`).
196 return (
197 host === "dev.azure.com" ||
198 host.endsWith(".dev.azure.com") ||
199 host.endsWith(".visualstudio.com")
200 );
202​
203function isBitbucketHost(host: string): boolean {
204 return host === "bitbucket.org" || hasDnsLabel(host, "bitbucket");
206​
207export function detectSourceControlProviderFromRemoteUrl(
208 remoteUrl: string,
209): SourceControlProviderInfo | null {
210 const host = parseRemoteHost(remoteUrl);
211 if (!host) {
212 return null;
213 }
214 const hostname = parseHostName(host);
215​
216 if (
217 hostname === "codeberg.org" ||
218 hasDnsLabel(hostname, "forgejo") ||
219 hasDnsLabel(hostname, "gitea")
220 ) {
221 return {
222 kind: "forgejo",
223 name: "Forgejo",
224 baseUrl: /^https?:/iu.test(remoteUrl.trim())
225 ? new URL(remoteUrl.trim()).origin
226 : toBaseUrl(host),
227 };
228 }
229​
230 if (isGitHubHost(hostname)) {
231 return {
232 kind: "github",
233 name: hostname === "github.com" ? "GitHub" : "GitHub Self-Hosted",
234 baseUrl: toBaseUrl(host),
235 };
236 }
237​
238 if (isGitLabHost(hostname)) {
239 return {
240 kind: "gitlab",
241 name: hostname === "gitlab.com" ? "GitLab" : "GitLab Self-Hosted",
242 baseUrl: toBaseUrl(host),
243 };
244 }
245​
246 if (isAzureDevOpsHost(hostname)) {
247 return {
248 kind: "azure-devops",
249 name: "Azure DevOps",
250 baseUrl: toBaseUrl(host),
251 };
252 }
253​
254 if (isBitbucketHost(hostname)) {
255 return {
256 kind: "bitbucket",
257 name: hostname === "bitbucket.org" ? "Bitbucket" : "Bitbucket Self-Hosted",
258 baseUrl: toBaseUrl(host),
259 };
260 }
261​
262 return {
263 kind: "unknown",
264 name: host,
265 baseUrl: toBaseUrl(host),
266 };
268​
269/**
270 * The provider-native repository selector. `displayName` is the full path below the host, which
271 * is what nested GitLab groups need; owner/name is the two-segment fallback for identities
272 * recorded before that field existed.
273 *
274 * Azure DevOps is the exception: `az repos pr list --repository` takes a repository name, and
275 * takes the organisation and project from the checkout it detects — so the recorded
276 * `org/project/_git/repo` path is refused outright and the whole repository reads as
277 * unavailable. Its name is the last segment, which is what this hands over.
278 *
279 * One function because everything downstream is keyed by what it answers: the rows' own
280 * `repository`, the per-repository cursors, and the detail and diff reads a row leads to.
281 */
282export function sourceControlRepositorySelector(
283 identity:
284 | Pick<RepositoryIdentity, "provider" | "displayName" | "owner" | "name">
285 | null
286 | undefined,
287): string | null {
288 if (!identity) return null;
289 if (identity.provider === "azure-devops") {
290 const segments = (identity.displayName ?? "").split("/").filter((part) => part !== "_git");
291 return identity.name || segments.at(-1) || null;
292 }
293 if (identity.displayName) return identity.displayName;
294 return identity.owner && identity.name ? `${identity.owner}/${identity.name}` : null;
296​
297export function canonicalRepositoryKey(key: string): string {
298 return key
299 .replace(
300 /^(?:ssh\.dev\.azure\.com|vs-ssh\.visualstudio\.com)\/v3\/([^/]+)\/([^/]+)\/([^/]+)$/u,
301 "dev.azure.com/$1/$2/_git/$3",
302 )
303 .replace(
304 /^([^.]+)\.visualstudio\.com\/(?:defaultcollection\/)?([^/]+)\/_git\/([^/]+)$/u,
305 "dev.azure.com/$1/$2/_git/$3",
306 );