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

sourceControl.ts

312 lines9.2 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​
182/** `*.ghe.com` is GitHub Enterprise Cloud with data residency, one subdomain per enterprise. */
183function isGitHubHost(host: string): boolean {
184 return host === "github.com" || host.endsWith(".ghe.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 isGitCafeHost(host: string): boolean {
204 return host === "git.cafe" || host === "staging.git.cafe";
206​
207function isBitbucketHost(host: string): boolean {
208 return host === "bitbucket.org" || hasDnsLabel(host, "bitbucket");
210​
211export function detectSourceControlProviderFromRemoteUrl(
212 remoteUrl: string,
213): SourceControlProviderInfo | null {
214 const host = parseRemoteHost(remoteUrl);
215 if (!host) {
216 return null;
217 }
218 const hostname = parseHostName(host);
219​
220 if (isGitCafeHost(hostname)) {
221 return {
222 kind: SourceControlProviderKind.make("gitcafe"),
223 name: "GitCafe",
224 baseUrl: toBaseUrl(hostname),
225 };
226 }
227​
228 if (
229 hostname === "codeberg.org" ||
230 hasDnsLabel(hostname, "forgejo") ||
231 hasDnsLabel(hostname, "gitea")
232 ) {
233 return {
234 kind: SourceControlProviderKind.make("forgejo"),
235 name: "Forgejo",
236 baseUrl: /^https?:/iu.test(remoteUrl.trim())
237 ? new URL(remoteUrl.trim()).origin
238 : toBaseUrl(host),
239 };
240 }
241​
242 if (isGitHubHost(hostname)) {
243 return {
244 kind: SourceControlProviderKind.make("github"),
245 name: hostname === "github.com" ? "GitHub" : "GitHub Self-Hosted",
246 baseUrl: toBaseUrl(host),
247 };
248 }
249​
250 if (isGitLabHost(hostname)) {
251 return {
252 kind: SourceControlProviderKind.make("gitlab"),
253 name: hostname === "gitlab.com" ? "GitLab" : "GitLab Self-Hosted",
254 baseUrl: toBaseUrl(host),
255 };
256 }
257​
258 if (isAzureDevOpsHost(hostname)) {
259 return {
260 kind: SourceControlProviderKind.make("azure-devops"),
261 name: "Azure DevOps",
262 baseUrl: toBaseUrl(host),
263 };
264 }
265​
266 if (isBitbucketHost(hostname)) {
267 return {
268 kind: SourceControlProviderKind.make("bitbucket"),
269 name: hostname === "bitbucket.org" ? "Bitbucket" : "Bitbucket Self-Hosted",
270 baseUrl: toBaseUrl(host),
271 };
272 }
273​
274 return {
275 kind: SourceControlProviderKind.make("unknown"),
276 name: host,
277 baseUrl: toBaseUrl(host),
278 };
280​
281/**
282 * Azure DevOps is the exception: `az repos pr list --repository` takes a repository name, and
283 * takes the organisation and project from the checkout it detects — so the recorded
284 * `org/project/_git/repo` path is refused outright and the whole repository reads as
285 * unavailable. Its name is the last segment, which is what this hands over.
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;
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 );