Open History to see the last commit
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;
12}
13
14export interface ChangeRequestTerminology {
15 readonly shortLabel: string;
16 readonly singular: string;
17}
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 }
93}
94
95export function resolveChangeRequestPresentationForKind(
96 kind: SourceControlProviderKind,
97): ChangeRequestPresentation {
98 return resolveChangeRequestPresentation({ kind, name: "", baseUrl: "" });
99}
100
101export function formatChangeRequestAction(
102 verb: "View" | "Create",
103 presentation: ChangeRequestPresentation,
104): string {
105 return `${verb} ${presentation.shortName}`;
106}
107
108export function formatCreateChangeRequestPhrase(presentation: ChangeRequestPresentation): string {
109 return `create ${presentation.shortName}`;
110}
111
112export function getChangeRequestTerminology(
113 provider: SourceControlProviderInfo | null | undefined,
114): ChangeRequestTerminology {
115 if (!provider) {
116 return DEFAULT_CHANGE_REQUEST_TERMINOLOGY;
117 }
118
119 const presentation = resolveChangeRequestPresentation(provider);
120 return {
121 shortLabel: presentation.shortName,
122 singular: presentation.longName,
123 };
124}
125
126export function getChangeRequestTerminologyForKind(
127 kind: SourceControlProviderKind,
128): ChangeRequestTerminology {
129 const presentation = resolveChangeRequestPresentationForKind(kind);
130 return {
131 shortLabel: presentation.shortName,
132 singular: presentation.longName,
133 };
134}
135
136function parseRemoteHost(remoteUrl: string): string | null {
137 const trimmed = remoteUrl.trim();
138 if (trimmed.length === 0) {
139 return null;
140 }
141
142 if (trimmed.startsWith("git@")) {
143 const hostWithPath = trimmed.slice("git@".length);
144 const separatorIndex = hostWithPath.search(/[:/]/);
145 if (separatorIndex <= 0) {
146 return null;
147 }
148 return hostWithPath.slice(0, separatorIndex).toLowerCase();
149 }
150
151 try {
152 return new URL(trimmed).hostname.toLowerCase();
153 } catch {
154 return null;
155 }
156}
157
158function toBaseUrl(host: string): string {
159 return `https://${host}`;
160}
161
162function isGitHubHost(host: string): boolean {
163 return host === "github.com" || host.includes("github");
164}
165
166function isGitLabHost(host: string): boolean {
167 return host === "gitlab.com" || host.includes("gitlab");
168}
169
170function isAzureDevOpsHost(host: string): boolean {
171 return host === "dev.azure.com" || host.endsWith(".visualstudio.com");
172}
173
174function isBitbucketHost(host: string): boolean {
175 return host === "bitbucket.org" || host.includes("bitbucket");
176}
177
178export function detectSourceControlProviderFromRemoteUrl(
179 remoteUrl: string,
180): SourceControlProviderInfo | null {
181 const host = parseRemoteHost(remoteUrl);
182 if (!host) {
183 return null;
184 }
185
186 if (isGitHubHost(host)) {
187 return {
188 kind: "github",
189 name: host === "github.com" ? "GitHub" : "GitHub Self-Hosted",
190 baseUrl: toBaseUrl(host),
191 };
192 }
193
194 if (isGitLabHost(host)) {
195 return {
196 kind: "gitlab",
197 name: host === "gitlab.com" ? "GitLab" : "GitLab Self-Hosted",
198 baseUrl: toBaseUrl(host),
199 };
200 }
201
202 if (isAzureDevOpsHost(host)) {
203 return {
204 kind: "azure-devops",
205 name: "Azure DevOps",
206 baseUrl: toBaseUrl(host),
207 };
208 }
209
210 if (isBitbucketHost(host)) {
211 return {
212 kind: "bitbucket",
213 name: host === "bitbucket.org" ? "Bitbucket" : "Bitbucket Self-Hosted",
214 baseUrl: toBaseUrl(host),
215 };
216 }
217
218 return {
219 kind: "unknown",
220 name: host,
221 baseUrl: toBaseUrl(host),
222 };
223}