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

sourceControl.ts

225 lines6.1 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 checkoutCommandExample: "bb pr checkout 123",
65 urlExample: "https://bitbucket.org/workspace/repo/pull-requests/42",
66};
67​
68const GENERIC_CHANGE_REQUEST_PRESENTATION: ChangeRequestPresentation = {
69 icon: "change-request",
70 providerName: "source control",
71 shortName: "change request",
72 longName: "change request",
73 pluralLongName: "change requests",
74 providerLongName: "change request",
75 checkoutCommandExample: "123",
76 urlExample: "#42",
77};
78​
79export function resolveChangeRequestPresentation(
80 provider: SourceControlProviderInfo | null | undefined,
81): ChangeRequestPresentation {
82 switch (provider?.kind) {
83 case "github":
84 case undefined:
85 return GITHUB_CHANGE_REQUEST_PRESENTATION;
86 case "gitlab":
87 return GITLAB_CHANGE_REQUEST_PRESENTATION;
88 case "azure-devops":
89 return AZURE_DEVOPS_CHANGE_REQUEST_PRESENTATION;
90 case "bitbucket":
91 return BITBUCKET_CHANGE_REQUEST_PRESENTATION;
92 case "unknown":
93 return GENERIC_CHANGE_REQUEST_PRESENTATION;
94 }
96​
97export function resolveChangeRequestPresentationForKind(
98 kind: SourceControlProviderKind,
99): ChangeRequestPresentation {
100 return resolveChangeRequestPresentation({ kind, name: "", baseUrl: "" });
102​
103export function formatChangeRequestAction(
104 verb: "View" | "Create",
105 presentation: ChangeRequestPresentation,
106): string {
107 return `${verb} ${presentation.shortName}`;
109​
110export function formatCreateChangeRequestPhrase(presentation: ChangeRequestPresentation): string {
111 return `create ${presentation.shortName}`;
113​
114export function getChangeRequestTerminology(
115 provider: SourceControlProviderInfo | null | undefined,
116): ChangeRequestTerminology {
117 if (!provider) {
118 return DEFAULT_CHANGE_REQUEST_TERMINOLOGY;
119 }
120​
121 const presentation = resolveChangeRequestPresentation(provider);
122 return {
123 shortLabel: presentation.shortName,
124 singular: presentation.longName,
125 };
127​
128export function getChangeRequestTerminologyForKind(
129 kind: SourceControlProviderKind,
130): ChangeRequestTerminology {
131 const presentation = resolveChangeRequestPresentationForKind(kind);
132 return {
133 shortLabel: presentation.shortName,
134 singular: presentation.longName,
135 };
137​
138function parseRemoteHost(remoteUrl: string): string | null {
139 const trimmed = remoteUrl.trim();
140 if (trimmed.length === 0) {
141 return null;
142 }
143​
144 if (trimmed.startsWith("git@")) {
145 const hostWithPath = trimmed.slice("git@".length);
146 const separatorIndex = hostWithPath.search(/[:/]/);
147 if (separatorIndex <= 0) {
148 return null;
149 }
150 return hostWithPath.slice(0, separatorIndex).toLowerCase();
151 }
152​
153 try {
154 return new URL(trimmed).hostname.toLowerCase();
155 } catch {
156 return null;
157 }
159​
160function toBaseUrl(host: string): string {
161 return `https://${host}`;
163​
164function isGitHubHost(host: string): boolean {
165 return host === "github.com" || host.includes("github");
167​
168function isGitLabHost(host: string): boolean {
169 return host === "gitlab.com" || host.includes("gitlab");
171​
172function isAzureDevOpsHost(host: string): boolean {
173 return host === "dev.azure.com" || host.endsWith(".visualstudio.com");
175​
176function isBitbucketHost(host: string): boolean {
177 return host === "bitbucket.org" || host.includes("bitbucket");
179​
180export function detectSourceControlProviderFromRemoteUrl(
181 remoteUrl: string,
182): SourceControlProviderInfo | null {
183 const host = parseRemoteHost(remoteUrl);
184 if (!host) {
185 return null;
186 }
187​
188 if (isGitHubHost(host)) {
189 return {
190 kind: "github",
191 name: host === "github.com" ? "GitHub" : "GitHub Self-Hosted",
192 baseUrl: toBaseUrl(host),
193 };
194 }
195​
196 if (isGitLabHost(host)) {
197 return {
198 kind: "gitlab",
199 name: host === "gitlab.com" ? "GitLab" : "GitLab Self-Hosted",
200 baseUrl: toBaseUrl(host),
201 };
202 }
203​
204 if (isAzureDevOpsHost(host)) {
205 return {
206 kind: "azure-devops",
207 name: "Azure DevOps",
208 baseUrl: toBaseUrl(host),
209 };
210 }
211​
212 if (isBitbucketHost(host)) {
213 return {
214 kind: "bitbucket",
215 name: host === "bitbucket.org" ? "Bitbucket" : "Bitbucket Self-Hosted",
216 baseUrl: toBaseUrl(host),
217 };
218 }
219​
220 return {
221 kind: "unknown",
222 name: host,
223 baseUrl: toBaseUrl(host),
224 };