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

sourceControl.ts

258 lines9.6 KB

1import * as Schema from "effect/Schema";
2import { PositiveInt, TrimmedNonEmptyString } from "./baseSchemas.ts";
3import { VcsDriverKind } from "./vcs.ts";
4​
5/**
6 * Open on purpose, like `ProviderDriverKind`: a server can report a host this client does not
7 * ship a definition for, and clients fall back to a generic presentation instead of failing to
8 * decode.
9 */
10export const SourceControlProviderKind = TrimmedNonEmptyString.check(
11 Schema.isMaxLength(64),
12 Schema.isPattern(/^[a-z][a-z0-9-]*$/),
13).pipe(Schema.brand("SourceControlProviderKind"));
14export type SourceControlProviderKind = typeof SourceControlProviderKind.Type;
15​
16export const SourceControlProviderInfo = Schema.Struct({
17 kind: SourceControlProviderKind,
18 name: TrimmedNonEmptyString,
19 baseUrl: Schema.String,
20});
21export type SourceControlProviderInfo = typeof SourceControlProviderInfo.Type;
22​
23export const SourceControlHostOverride = Schema.Struct({
24 kind: Schema.Literals(["github", "gitlab", "forgejo", "bitbucket", "azure-devops", "gitcafe"]),
25 baseUrl: Schema.optionalKey(
26 TrimmedNonEmptyString.check(
27 Schema.makeFilter(
28 (value) => {
29 try {
30 const url = new URL(value);
31 return (
32 (url.protocol === "https:" || url.protocol === "http:") &&
33 !url.username &&
34 !url.password &&
35 !url.search &&
36 !url.hash
37 );
38 } catch {
39 return false;
40 }
41 },
42 { expected: "an HTTP or HTTPS base URL without credentials, query, or fragment" },
43 ),
44 ),
45 ),
46});
47export type SourceControlHostOverride = typeof SourceControlHostOverride.Type;
48​
49export const SourceControlHostName = Schema.String.check(
50 Schema.makeFilter(
51 (value) => {
52 if (
53 !/^(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)*|\[[a-f0-9:]+\])(?::\d+)?$/iu.test(
54 value,
55 )
56 )
57 return false;
58 try {
59 const url = new URL(`https://${value}`);
60 return url.hostname.length > 0 && url.port !== "0";
61 } catch {
62 return false;
63 }
64 },
65 { expected: "an exact hostname, optionally with a port" },
66 ),
67);
68​
69export const SourceControlHostOverrides = Schema.Record(
70 Schema.String,
71 SourceControlHostOverride,
72).check(
73 Schema.makeFilter((entries) => Object.keys(entries).every(Schema.is(SourceControlHostName)), {
74 expected: "host mappings keyed by exact hostname, optionally with a port",
75 }),
76);
77export type SourceControlHostOverrides = typeof SourceControlHostOverrides.Type;
78​
79export const ChangeRequestState = Schema.Literals(["open", "closed", "merged"]);
80export type ChangeRequestState = typeof ChangeRequestState.Type;
81​
82export const ChangeRequest = Schema.Struct({
83 provider: SourceControlProviderKind,
84 number: PositiveInt,
85 title: TrimmedNonEmptyString,
86 url: Schema.String,
87 baseRefName: TrimmedNonEmptyString,
88 headRefName: TrimmedNonEmptyString,
89 headSha: Schema.optional(TrimmedNonEmptyString),
90 state: ChangeRequestState,
91 isDraft: Schema.optional(Schema.Boolean),
92 closedAt: Schema.optional(Schema.NullOr(Schema.String)),
93 mergedAt: Schema.optional(Schema.NullOr(Schema.String)),
94 updatedAt: Schema.Option(Schema.DateTimeUtc),
95 isCrossRepository: Schema.optional(Schema.Boolean),
96 headRepositoryNameWithOwner: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),
97 headRepositoryOwnerLogin: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),
98});
99export type ChangeRequest = typeof ChangeRequest.Type;
100​
101export const SourceControlRepositoryCloneUrls = Schema.Struct({
102 nameWithOwner: TrimmedNonEmptyString,
103 url: TrimmedNonEmptyString,
104 sshUrl: TrimmedNonEmptyString,
105});
106export type SourceControlRepositoryCloneUrls = typeof SourceControlRepositoryCloneUrls.Type;
107​
108export const SourceControlRepositoryVisibility = Schema.Literals(["private", "public"]);
109export type SourceControlRepositoryVisibility = typeof SourceControlRepositoryVisibility.Type;
110​
111export const SourceControlCloneProtocol = Schema.Literals(["auto", "ssh", "https"]);
112export type SourceControlCloneProtocol = typeof SourceControlCloneProtocol.Type;
113​
114export const SourceControlRepositoryInfo = Schema.Struct({
115 provider: SourceControlProviderKind,
116 nameWithOwner: TrimmedNonEmptyString,
117 url: TrimmedNonEmptyString,
118 sshUrl: TrimmedNonEmptyString,
119});
120export type SourceControlRepositoryInfo = typeof SourceControlRepositoryInfo.Type;
121​
122export const SourceControlRepositoryLookupInput = Schema.Struct({
123 provider: SourceControlProviderKind,
124 repository: TrimmedNonEmptyString,
125 cwd: Schema.optional(TrimmedNonEmptyString),
126});
127export type SourceControlRepositoryLookupInput = typeof SourceControlRepositoryLookupInput.Type;
128​
129export const SourceControlCloneRepositoryInput = Schema.Struct({
130 provider: Schema.optional(SourceControlProviderKind),
131 repository: Schema.optional(TrimmedNonEmptyString),
132 remoteUrl: Schema.optional(TrimmedNonEmptyString),
133 destinationPath: TrimmedNonEmptyString,
134 protocol: Schema.optional(SourceControlCloneProtocol),
135});
136export type SourceControlCloneRepositoryInput = typeof SourceControlCloneRepositoryInput.Type;
137​
138export const SourceControlCloneRepositoryResult = Schema.Struct({
139 cwd: TrimmedNonEmptyString,
140 remoteUrl: TrimmedNonEmptyString,
141 repository: Schema.NullOr(SourceControlRepositoryInfo),
142});
143export type SourceControlCloneRepositoryResult = typeof SourceControlCloneRepositoryResult.Type;
144​
145export const SourceControlPublishRepositoryInput = Schema.Struct({
146 cwd: TrimmedNonEmptyString,
147 provider: SourceControlProviderKind,
148 repository: TrimmedNonEmptyString,
149 visibility: SourceControlRepositoryVisibility,
150 remoteName: Schema.optional(TrimmedNonEmptyString),
151 protocol: Schema.optional(SourceControlCloneProtocol),
152});
153export type SourceControlPublishRepositoryInput = typeof SourceControlPublishRepositoryInput.Type;
154​
155export const SourceControlPublishStatus = Schema.Literals(["pushed", "remote_added"]);
156export type SourceControlPublishStatus = typeof SourceControlPublishStatus.Type;
157​
158export const SourceControlPublishRepositoryResult = Schema.Struct({
159 repository: SourceControlRepositoryInfo,
160 remoteName: TrimmedNonEmptyString,
161 remoteUrl: TrimmedNonEmptyString,
162 branch: TrimmedNonEmptyString,
163 upstreamBranch: Schema.optional(TrimmedNonEmptyString),
164 status: SourceControlPublishStatus,
165});
166export type SourceControlPublishRepositoryResult = typeof SourceControlPublishRepositoryResult.Type;
167​
168export const SourceControlDiscoveryStatus = Schema.Literals(["available", "missing"]);
169export type SourceControlDiscoveryStatus = typeof SourceControlDiscoveryStatus.Type;
170​
171export const SourceControlProviderAuthStatus = Schema.Literals([
172 "authenticated",
173 "unauthenticated",
174 "unknown",
175]);
176export type SourceControlProviderAuthStatus = typeof SourceControlProviderAuthStatus.Type;
177​
178export const SourceControlProviderAuth = Schema.Struct({
179 status: SourceControlProviderAuthStatus,
180 account: Schema.Option(TrimmedNonEmptyString),
181 host: Schema.Option(TrimmedNonEmptyString),
182 detail: Schema.Option(TrimmedNonEmptyString),
183 accounts: Schema.optionalKey(
184 Schema.Array(
185 Schema.Struct({
186 host: TrimmedNonEmptyString,
187 account: TrimmedNonEmptyString,
188 active: Schema.Boolean,
189 authenticated: Schema.Boolean,
190 error: Schema.optionalKey(TrimmedNonEmptyString),
191 /** Set when the login comes from a token variable such as `GH_TOKEN`, which wins over Settings. */
192 environmentVariable: Schema.optionalKey(TrimmedNonEmptyString),
193 }),
194 ),
195 ),
196});
197export type SourceControlProviderAuth = typeof SourceControlProviderAuth.Type;
198​
199const SourceControlDiscoverySharedFields = {
200 label: TrimmedNonEmptyString,
201 executable: Schema.optional(TrimmedNonEmptyString),
202 status: SourceControlDiscoveryStatus,
203 version: Schema.Option(TrimmedNonEmptyString),
204 installHint: TrimmedNonEmptyString,
205 detail: Schema.Option(TrimmedNonEmptyString),
206} as const;
207​
208export const VcsDiscoveryItem = Schema.Struct({
209 kind: VcsDriverKind,
210 implemented: Schema.Boolean,
211 ...SourceControlDiscoverySharedFields,
212});
213export type VcsDiscoveryItem = typeof VcsDiscoveryItem.Type;
214​
215export const SourceControlProviderDiscoveryItem = Schema.Struct({
216 kind: SourceControlProviderKind,
217 ...SourceControlDiscoverySharedFields,
218 auth: SourceControlProviderAuth,
219});
220export type SourceControlProviderDiscoveryItem = typeof SourceControlProviderDiscoveryItem.Type;
221​
222export const SourceControlDiscoveryResult = Schema.Struct({
223 versionControlSystems: Schema.Array(VcsDiscoveryItem),
224 sourceControlProviders: Schema.Array(SourceControlProviderDiscoveryItem),
225});
226export type SourceControlDiscoveryResult = typeof SourceControlDiscoveryResult.Type;
227​
228export class SourceControlProviderError extends Schema.TaggedError<SourceControlProviderError>()(
229 "SourceControlProviderError",
230 {
231 provider: SourceControlProviderKind,
232 operation: Schema.String,
233 cwd: Schema.String,
234 command: Schema.optional(Schema.String),
235 repository: Schema.optional(Schema.String),
236 reference: Schema.optional(Schema.String),
237 detail: Schema.String,
238 cause: Schema.optional(Schema.Defect()),
239 },
240) {
241 override get message(): string {
242 return `Source control provider ${this.provider} failed in ${this.operation}: ${this.detail}`;
243 }
245​
246export class SourceControlRepositoryError extends Schema.TaggedError<SourceControlRepositoryError>()(
247 "SourceControlRepositoryError",
248 {
249 provider: SourceControlProviderKind,
250 operation: Schema.String,
251 detail: Schema.String,
252 cause: Schema.optional(Schema.Defect()),
253 },
254) {
255 override get message(): string {
256 return `Source control repository operation ${this.operation} failed for ${this.provider}: ${this.detail}`;
257 }