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

sourceControl.ts

236 lines6.5 KB

1import type { SourceControlProviderInfo, SourceControlProviderKind } from "@t3tools/contracts";
2​
3export interface ChangeRequestPresentation {
4 readonly icon: "github" | "gitlab" | "azure-devops" | "bitbucket" | "change-request";
5 readonly providerName: string;
6 readonly shortName: string;
7 readonly longName: string;
8 readonly pluralLongName: string;
9 readonly providerLongName: string;
10 readonly checkoutCommandExample?: string;
11 readonly urlExample: string;
13​
14export interface ChangeRequestTerminology {
15 readonly shortLabel: string;
16 readonly singular: string;
18​
19export const DEFAULT_CHANGE_REQUEST_TERMINOLOGY: ChangeRequestTerminology = {
20 shortLabel: "PR",
21 singular: "pull request",
22};
23​
24const GITHUB_CHANGE_REQUEST_PRESENTATION: ChangeRequestPresentation = {
25 icon: "github",
26 providerName: "GitHub",
27 shortName: "PR",
28 longName: "pull request",
29 pluralLongName: "pull requests",
30 providerLongName: "GitHub pull request",
31 checkoutCommandExample: "gh pr checkout 123",
32 urlExample: "https://github.com/owner/repo/pull/42",
33};
34​
35const GITLAB_CHANGE_REQUEST_PRESENTATION: ChangeRequestPresentation = {
36 icon: "gitlab",
37 providerName: "GitLab",
38 shortName: "MR",
39 longName: "merge request",
40 pluralLongName: "merge requests",
41 providerLongName: "GitLab merge request",
42 checkoutCommandExample: "glab mr checkout 123",
43 urlExample: "https://gitlab.com/group/project/-/merge_requests/42",
44};
45​
46const AZURE_DEVOPS_CHANGE_REQUEST_PRESENTATION: ChangeRequestPresentation = {
47 icon: "azure-devops",
48 providerName: "Azure DevOps",
49 shortName: "PR",
50 longName: "pull request",
51 pluralLongName: "pull requests",
52 providerLongName: "Azure DevOps pull request",
53 checkoutCommandExample: "az repos pr checkout --id 123",
54 urlExample: "https://dev.azure.com/org/project/_git/repo/pullrequest/42",
55};
56​
57const BITBUCKET_CHANGE_REQUEST_PRESENTATION: ChangeRequestPresentation = {
58 icon: "bitbucket",
59 providerName: "Bitbucket",
60 shortName: "PR",
61 longName: "pull request",
62 pluralLongName: "pull requests",
63 providerLongName: "Bitbucket pull request",
64 urlExample: "https://bitbucket.org/workspace/repo/pull-requests/42",
65};
66​
67const GENERIC_CHANGE_REQUEST_PRESENTATION: ChangeRequestPresentation = {
68 icon: "change-request",
69 providerName: "source control",
70 shortName: "change request",
71 longName: "change request",
72 pluralLongName: "change requests",
73 providerLongName: "change request",
74 urlExample: "#42",
75};
76​
77export function resolveChangeRequestPresentation(
78 provider: SourceControlProviderInfo | null | undefined,
79): ChangeRequestPresentation {
80 switch (provider?.kind) {
81 case "github":
82 case undefined:
83 return GITHUB_CHANGE_REQUEST_PRESENTATION;
84 case "gitlab":
85 return GITLAB_CHANGE_REQUEST_PRESENTATION;
86 case "azure-devops":
87 return AZURE_DEVOPS_CHANGE_REQUEST_PRESENTATION;
88 case "bitbucket":
89 return BITBUCKET_CHANGE_REQUEST_PRESENTATION;
90 case "unknown":
91 return GENERIC_CHANGE_REQUEST_PRESENTATION;
92 }
94​
95function resolveChangeRequestPresentationForKind(
96 kind: SourceControlProviderKind,
97): ChangeRequestPresentation {
98 return resolveChangeRequestPresentation({ kind, name: "", baseUrl: "" });
100​
101export function getChangeRequestTerminology(
102 provider: SourceControlProviderInfo | null | undefined,
103): ChangeRequestTerminology {
104 if (!provider) {
105 return DEFAULT_CHANGE_REQUEST_TERMINOLOGY;
106 }
107​
108 const presentation = resolveChangeRequestPresentation(provider);
109 return {
110 shortLabel: presentation.shortName,
111 singular: presentation.longName,
112 };
114​
115export function getChangeRequestTerminologyForKind(
116 kind: SourceControlProviderKind,
117): ChangeRequestTerminology {
118 const presentation = resolveChangeRequestPresentationForKind(kind);
119 return {
120 shortLabel: presentation.shortName,
121 singular: presentation.longName,
122 };
124​
125const SCP_SSH_REMOTE_PATTERN = /^[a-zA-Z0-9._-]+@([^:/]+):/;
126​
127export function isSshRemoteUrl(remoteUrl: string): boolean {
128 const trimmed = remoteUrl.trim();
129 return SCP_SSH_REMOTE_PATTERN.test(trimmed) || trimmed.toLowerCase().startsWith("ssh://");
131​
132function parseRemoteHost(remoteUrl: string): string | null {
133 const trimmed = remoteUrl.trim();
134 if (trimmed.length === 0) {
135 return null;
136 }
137​
138 const scpMatch = SCP_SSH_REMOTE_PATTERN.exec(trimmed);
139 if (scpMatch?.[1]) {
140 return scpMatch[1].toLowerCase();
141 }
142​
143 try {
144 return new URL(trimmed).host.toLowerCase();
145 } catch {
146 return null;
147 }
149​
150function parseHostName(host: string): string {
151 try {
152 return new URL(`https://${host}`).hostname.toLowerCase();
153 } catch {
154 return host.replace(/:\d+$/u, "").toLowerCase();
155 }
157​
158function toBaseUrl(host: string): string {
159 return `https://${host}`;
161​
162function hasDnsLabel(host: string, label: string): boolean {
163 return host.split(".").includes(label);
165​
166function isGitHubHost(host: string): boolean {
167 return host === "github.com" || hasDnsLabel(host, "github");
169​
170function isGitLabHost(host: string): boolean {
171 return host === "gitlab.com" || hasDnsLabel(host, "gitlab");
173​
174function isAzureDevOpsHost(host: string): boolean {
175 // `ssh.dev.azure.com` is the default Azure DevOps SSH clone host
176 // (git@ssh.dev.azure.com:v3/org/project/repo), so match any `*.dev.azure.com`
177 // subdomain, not just the bare `dev.azure.com`. Legacy hosts stay under
178 // `.visualstudio.com` (including `vs-ssh.visualstudio.com`).
179 return (
180 host === "dev.azure.com" ||
181 host.endsWith(".dev.azure.com") ||
182 host.endsWith(".visualstudio.com")
183 );
185​
186function isBitbucketHost(host: string): boolean {
187 return host === "bitbucket.org" || hasDnsLabel(host, "bitbucket");
189​
190export function detectSourceControlProviderFromRemoteUrl(
191 remoteUrl: string,
192): SourceControlProviderInfo | null {
193 const host = parseRemoteHost(remoteUrl);
194 if (!host) {
195 return null;
196 }
197 const hostname = parseHostName(host);
198​
199 if (isGitHubHost(hostname)) {
200 return {
201 kind: "github",
202 name: hostname === "github.com" ? "GitHub" : "GitHub Self-Hosted",
203 baseUrl: toBaseUrl(host),
204 };
205 }
206​
207 if (isGitLabHost(hostname)) {
208 return {
209 kind: "gitlab",
210 name: hostname === "gitlab.com" ? "GitLab" : "GitLab Self-Hosted",
211 baseUrl: toBaseUrl(host),
212 };
213 }
214​
215 if (isAzureDevOpsHost(hostname)) {
216 return {
217 kind: "azure-devops",
218 name: "Azure DevOps",
219 baseUrl: toBaseUrl(host),
220 };
221 }
222​
223 if (isBitbucketHost(hostname)) {
224 return {
225 kind: "bitbucket",
226 name: hostname === "bitbucket.org" ? "Bitbucket" : "Bitbucket Self-Hosted",
227 baseUrl: toBaseUrl(host),
228 };
229 }
230​
231 return {
232 kind: "unknown",
233 name: host,
234 baseUrl: toBaseUrl(host),
235 };