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

definition.ts

214 lines8.0 KB

1import {
2 type PullRequestAction,
3 pullRequestHostOf,
4 type PullRequestReviewVerdict,
5 type RepositoryIdentity,
6 SourceControlProviderKind,
7} from "@t3tools/contracts";
8import type { ChangeRequestLink } from "@t3tools/shared/changeRequestUrl";
9import * as Schema from "effect/Schema";
10​
11export interface ChangeRequestTerminology {
12 readonly shortLabel: string;
13 readonly singular: string;
15​
16export interface ChangeRequestUrlInput {
17 readonly host: string;
18 readonly repository: string;
19 readonly number: number;
20 readonly remoteUrl?: string | undefined;
21 readonly webUrl?: string | undefined;
23​
24export type ChangeRequestProjectIdentity = Pick<
25 RepositoryIdentity,
26 "canonicalKey" | "locator" | "webUrl" | "displayName" | "owner" | "name"
27>;
28​
29export interface ChangeRequestCheckoutInput {
30 readonly number: number;
31 readonly headBranch: string;
32 readonly headRepositoryNameWithOwner?: string | null | undefined;
33 readonly repositoryUrl?: string | null | undefined;
35​
36export interface SourceControlClientDefinition {
37 readonly kind: SourceControlProviderKind;
38 readonly label: string;
39 readonly pickerLabel: string;
40 readonly icon: string;
41 readonly changeRequest: ChangeRequestTerminology;
42 readonly repositoryPathHint: string;
43 readonly publicHost: string | null;
44 readonly publishDescription: string;
45 readonly publishHost: (signedInHost: string | null) => string;
46 readonly newRepositoryOwner: (account: string | null) => { readonly owner: string | null } | null;
47 readonly defaultCloneTransport: "https" | "ssh";
48 readonly changeRequestUrl: (input: ChangeRequestUrlInput) => string | null;
49 /**
50 * The actions this host can take on a change request, the same list its server provider
51 * declares in `capabilities.actions`.
52 */
53 readonly changeRequestActions: ReadonlySet<PullRequestAction>;
54 readonly checkoutCommand: (changeRequest: ChangeRequestCheckoutInput) => string | null;
55 readonly authorProfileUrl: (login: string, repositoryUrl: string) => string | null;
56 readonly referenceAutolinkRepositoryUrl: (repositoryUrl: string) => string | null;
57 readonly reviewSummaryRequired: (verdict: PullRequestReviewVerdict) => boolean;
58 readonly checkoutCommandArgument: (input: string) => string | null;
59 readonly isChangeRequestReference: (url: string) => boolean;
60 readonly isChangeRequestInRepository: (
61 identity: ChangeRequestProjectIdentity,
62 link: ChangeRequestLink,
63 ) => boolean;
64 readonly canReadChangeRequestOnHost: (
65 identity: ChangeRequestProjectIdentity,
66 link: ChangeRequestLink,
67 ) => boolean;
68 readonly changeRequestUrlHost: (url: URL) => string;
69 readonly checkoutChangeRequestHost: (identity: ChangeRequestProjectIdentity) => string | null;
70 readonly isChangeRequestUrl: (url: string) => boolean;
71 readonly settings?: SourceControlHostSettingsSchema;
73​
74export type SourceControlHostSettingsSchema = {
75 readonly fields: Readonly<Record<string, Schema.Top>>;
76} & Schema.Decoder<object>;
77​
78export function isChangeRequestPath(url: string, route: string): boolean {
79 try {
80 const path = new URL(url).pathname;
81 const at = path.indexOf(route);
82 return at > 0 && /^\d+(?:\/|$)/u.test(path.slice(at + route.length));
83 } catch {
84 return false;
85 }
87​
88export function defineSourceControlClient<const Definition extends SourceControlClientDefinition>(
89 definition: Definition,
90): Definition {
91 return definition;
93​
94export function isChangeRequestAuthorityOfProject(
95 identity: ChangeRequestProjectIdentity,
96 link: ChangeRequestLink,
97): boolean {
98 if (link.authority === undefined) return true;
99 try {
100 const remote = new URL(identity.locator.remoteUrl);
101 if (remote.protocol === "http:" || remote.protocol === "https:") {
102 return remote.host.toLowerCase() === link.authority;
103 }
104 } catch {
105 // SSH remotes do not specify the server's HTTP port.
106 }
107 return true;
109​
110export function isChangeRequestOnProjectHost(
111 kind: SourceControlProviderKind,
112 identity: ChangeRequestProjectIdentity,
113 link: ChangeRequestLink,
114): boolean {
115 if (!isChangeRequestAuthorityOfProject(identity, link)) return false;
116 const host = pullRequestHostOf(identity, kind);
117 return host === link.host.toLowerCase() || host === link.authority;
119​
120function projectRepositoryPath(identity: ChangeRequestProjectIdentity): string | null {
121 return (
122 identity.displayName ??
123 (identity.owner && identity.name ? `${identity.owner}/${identity.name}` : null)
124 );
126​
127export function isChangeRequestInProjectRepository(
128 kind: SourceControlProviderKind,
129 identity: ChangeRequestProjectIdentity,
130 link: ChangeRequestLink,
131): boolean {
132 const repository = projectRepositoryPath(identity);
133 return (
134 repository !== null &&
135 repository.toLowerCase() === link.repository.toLowerCase() &&
136 isChangeRequestOnProjectHost(kind, identity, link)
137 );
139​
140export const UNKNOWN_SOURCE_CONTROL_CLIENT: SourceControlClientDefinition = {
141 kind: SourceControlProviderKind.make("unknown"),
142 label: "source control",
143 pickerLabel: "source control",
144 icon: "change-request",
145 changeRequest: { shortLabel: "change request", singular: "change request" },
146 repositoryPathHint: "URL",
147 publicHost: null,
148 publishDescription: "Your signed-in server",
149 publishHost: (signedInHost) => signedInHost ?? "your server",
150 newRepositoryOwner: () => null,
151 defaultCloneTransport: "ssh",
152 changeRequestUrl: () => null,
153 changeRequestActions: new Set(),
154 checkoutCommand: () => null,
155 authorProfileUrl: () => null,
156 referenceAutolinkRepositoryUrl: () => null,
157 reviewSummaryRequired: () => false,
158 checkoutCommandArgument: () => null,
159 isChangeRequestReference: () => false,
160 changeRequestUrlHost: (url) => url.hostname,
161 checkoutChangeRequestHost: () => null,
162 isChangeRequestInRepository: (identity, link) =>
163 isChangeRequestInProjectRepository(SourceControlProviderKind.make("unknown"), identity, link),
164 canReadChangeRequestOnHost: (identity, link) =>
165 isChangeRequestOnProjectHost(SourceControlProviderKind.make("unknown"), identity, link),
166 isChangeRequestUrl: () => false,
167};
168​
169export interface SourceControlClientRegistry {
170 readonly definitions: ReadonlyArray<SourceControlClientDefinition>;
171 readonly get: (kind: string | null | undefined) => SourceControlClientDefinition;
172 readonly find: (kind: string) => SourceControlClientDefinition | undefined;
173 readonly findByPublicHost: (hostname: string) => SourceControlClientDefinition | undefined;
174 readonly findByChangeRequestUrl: (url: string) => SourceControlClientDefinition | undefined;
175 readonly hostLabelForChangeRequestUrl: (url: string) => string;
177​
178export function makeSourceControlClientRegistry(
179 definitions: ReadonlyArray<SourceControlClientDefinition>,
180): SourceControlClientRegistry {
181 const byKind = new Map<string, SourceControlClientDefinition>();
182 for (const definition of definitions) {
183 if (byKind.has(definition.kind)) {
184 throw new Error(`Source control host '${definition.kind}' is defined more than once.`);
185 }
186 byKind.set(definition.kind, definition);
187 }
188 return {
189 definitions,
190 get: (kind) =>
191 (kind == null ? definitions[0] : byKind.get(kind)) ?? UNKNOWN_SOURCE_CONTROL_CLIENT,
192 find: (kind) => byKind.get(kind),
193 findByPublicHost: (hostname) => {
194 const host = hostname.toLowerCase();
195 return definitions.find((definition) => definition.publicHost === host);
196 },
197 findByChangeRequestUrl: (url) =>
198 definitions.find((definition) => definition.isChangeRequestUrl(url)),
199 hostLabelForChangeRequestUrl: (url) =>
200 definitions.find((definition) => definition.isChangeRequestUrl(url))?.label ?? "the host",
201 };
203​
204export function readSourceControlHostSettings<S extends SourceControlHostSettingsSchema>(
205 schema: S,
206 saved: unknown,
207): S["Type"] {
208 const decode = Schema.decodeUnknownOption(schema as Schema.Decoder<S["Type"]>);
209 const decoded = decode(saved ?? {});
210 if (decoded._tag === "Some") return decoded.value;
211 const fallback = decode({});
212 if (fallback._tag === "Some") return fallback.value;
213 throw new Error("A source control host settings schema must decode an empty object.");