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

sourceControl.ts

311 lines9.1 KB

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