Open History to see the last commit
1import type { SourceControlProviderInfo, SourceControlProviderKind } from "@v12/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).host.toLowerCase();
153 } catch {
154 return null;
155 }
156}
157
158function parseHostName(host: string): string {
159 try {
160 return new URL(`https://${host}`).hostname.toLowerCase();
161 } catch {
162 return host.replace(/:\d+$/u, "").toLowerCase();
163 }
164}
165
166function toBaseUrl(host: string): string {
167 return `https://${host}`;
168}
169
170function isGitHubHost(host: string): boolean {
171 return host === "github.com" || host.includes("github");
172}
173
174function isGitLabHost(host: string): boolean {
175 return host === "gitlab.com" || host.includes("gitlab");
176}
177
178function isAzureDevOpsHost(host: string): boolean {
179 return host === "dev.azure.com" || host.endsWith(".visualstudio.com");
180}
181
182function isBitbucketHost(host: string): boolean {
183 return host === "bitbucket.org" || host.includes("bitbucket");
184}
185
186export function detectSourceControlProviderFromRemoteUrl(
187 remoteUrl: string,
188): SourceControlProviderInfo | null {
189 const host = parseRemoteHost(remoteUrl);
190 if (!host) {
191 return null;
192 }
193 const hostname = parseHostName(host);
194
195 if (isGitHubHost(hostname)) {
196 return {
197 kind: "github",
198 name: hostname === "github.com" ? "GitHub" : "GitHub Self-Hosted",
199 baseUrl: toBaseUrl(host),
200 };
201 }
202
203 if (isGitLabHost(hostname)) {
204 return {
205 kind: "gitlab",
206 name: hostname === "gitlab.com" ? "GitLab" : "GitLab Self-Hosted",
207 baseUrl: toBaseUrl(host),
208 };
209 }
210
211 if (isAzureDevOpsHost(hostname)) {
212 return {
213 kind: "azure-devops",
214 name: "Azure DevOps",
215 baseUrl: toBaseUrl(host),
216 };
217 }
218
219 if (isBitbucketHost(hostname)) {
220 return {
221 kind: "bitbucket",
222 name: hostname === "bitbucket.org" ? "Bitbucket" : "Bitbucket Self-Hosted",
223 baseUrl: toBaseUrl(host),
224 };
225 }
226
227 return {
228 kind: "unknown",
229 name: host,
230 baseUrl: toBaseUrl(host),
231 };
232}