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

sourceControl.ts

158 lines4.1 KB

1import type { SourceControlProviderInfo, SourceControlProviderKind } from "@piku/contracts";
2​
3export interface ChangeRequestPresentation {
4 readonly icon: "github" | "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 GENERIC_CHANGE_REQUEST_PRESENTATION: ChangeRequestPresentation = {
36 icon: "change-request",
37 providerName: "source control",
38 shortName: "change request",
39 longName: "change request",
40 pluralLongName: "change requests",
41 providerLongName: "change request",
42 urlExample: "#42",
43};
44​
45export function resolveChangeRequestPresentation(
46 provider: SourceControlProviderInfo | null | undefined,
47): ChangeRequestPresentation {
48 switch (provider?.kind) {
49 case "github":
50 case undefined:
51 return GITHUB_CHANGE_REQUEST_PRESENTATION;
52 case "unknown":
53 return GENERIC_CHANGE_REQUEST_PRESENTATION;
54 }
56​
57export function resolveChangeRequestPresentationForKind(
58 kind: SourceControlProviderKind,
59): ChangeRequestPresentation {
60 return resolveChangeRequestPresentation({ kind, name: "", baseUrl: "" });
62​
63export function formatChangeRequestAction(
64 verb: "View" | "Create",
65 presentation: ChangeRequestPresentation,
66): string {
67 return `${verb} ${presentation.shortName}`;
69​
70export function formatCreateChangeRequestPhrase(presentation: ChangeRequestPresentation): string {
71 return `create ${presentation.shortName}`;
73​
74export function getChangeRequestTerminology(
75 provider: SourceControlProviderInfo | null | undefined,
76): ChangeRequestTerminology {
77 if (!provider) {
78 return DEFAULT_CHANGE_REQUEST_TERMINOLOGY;
79 }
80​
81 const presentation = resolveChangeRequestPresentation(provider);
82 return {
83 shortLabel: presentation.shortName,
84 singular: presentation.longName,
85 };
87​
88export function getChangeRequestTerminologyForKind(
89 kind: SourceControlProviderKind,
90): ChangeRequestTerminology {
91 const presentation = resolveChangeRequestPresentationForKind(kind);
92 return {
93 shortLabel: presentation.shortName,
94 singular: presentation.longName,
95 };
97​
98function parseRemoteHost(remoteUrl: string): string | null {
99 const trimmed = remoteUrl.trim();
100 if (trimmed.length === 0) {
101 return null;
102 }
103​
104 if (trimmed.startsWith("git@")) {
105 const hostWithPath = trimmed.slice("git@".length);
106 const separatorIndex = hostWithPath.search(/[:/]/);
107 if (separatorIndex <= 0) {
108 return null;
109 }
110 return hostWithPath.slice(0, separatorIndex).toLowerCase();
111 }
112​
113 try {
114 return new URL(trimmed).host.toLowerCase();
115 } catch {
116 return null;
117 }
119​
120function parseHostName(host: string): string {
121 try {
122 return new URL(`https://${host}`).hostname.toLowerCase();
123 } catch {
124 return host.replace(/:\d+$/u, "").toLowerCase();
125 }
127​
128function toBaseUrl(host: string): string {
129 return `https://${host}`;
131​
132function isGitHubHost(host: string): boolean {
133 return host === "github.com" || host.includes("github");
135​
136export function detectSourceControlProviderFromRemoteUrl(
137 remoteUrl: string,
138): SourceControlProviderInfo | null {
139 const host = parseRemoteHost(remoteUrl);
140 if (!host) {
141 return null;
142 }
143 const hostname = parseHostName(host);
144​
145 if (isGitHubHost(hostname)) {
146 return {
147 kind: "github",
148 name: hostname === "github.com" ? "GitHub" : "GitHub Self-Hosted",
149 baseUrl: toBaseUrl(host),
150 };
151 }
152​
153 return {
154 kind: "unknown",
155 name: host,
156 baseUrl: toBaseUrl(host),
157 };