Open History to see the last commit
1import * as Context from "effect/Context";
2import * as Effect from "effect/Effect";
3import type * as Option from "effect/Option";
4import type {
5 RepositoryIdentity,
6 ChangeRequest,
7 ChangeRequestState,
8 SourceControlProviderError,
9 SourceControlProviderInfo,
10 SourceControlProviderKind,
11 SourceControlRepositoryCloneUrls,
12 SourceControlRepositoryVisibility,
13} from "@t3tools/contracts";
14
15export interface SourceControlLinkSubject {
16 readonly title: string;
17 readonly body: string | null;
18}
19
20/** Return undefined synchronously for unsupported URLs, without starting a lookup. */
21export type ResolveSourceControlLink = (input: {
22 readonly cwd: string;
23 readonly url: URL;
24}) => Effect.Effect<SourceControlLinkSubject, SourceControlProviderError> | undefined;
25
26export interface SourceControlProviderContext {
27 readonly provider: SourceControlProviderInfo;
28 readonly remoteName: string;
29 readonly remoteUrl: string;
30 readonly requestedHost?: string;
31}
32
33export interface SourceControlRefSelector {
34 readonly refName: string;
35 readonly owner?: string;
36 readonly repository?: string;
37}
38
39const MAX_ERROR_TRANSPORT_VALUE_LENGTH = 256;
40
41/**
42 * The fixed product text a provider failure carries for the user, if it has one. Any other
43 * detail may be upstream response text, so callers show a generic message instead.
44 */
45export function userFacingSourceControlDetail(cause: unknown): string | undefined {
46 return typeof cause === "object" &&
47 cause !== null &&
48 "userFacingDetail" in cause &&
49 typeof cause.userFacingDetail === "string"
50 ? cause.userFacingDetail
51 : undefined;
52}
53
54export function transportSafeSourceControlErrorValue(value: string): string {
55 let printable = "";
56 for (const character of value) {
57 const codePoint = character.codePointAt(0);
58 printable += codePoint !== undefined && (codePoint < 32 || codePoint === 127) ? " " : character;
59 }
60 const normalized = printable.trim().replace(/\s+/gu, " ");
61
62 let safe = normalized;
63 try {
64 const url = new URL(normalized);
65 url.username = "";
66 url.password = "";
67 url.search = "";
68 url.hash = "";
69 safe = url.toString();
70 } catch {}
71
72 return safe.slice(0, MAX_ERROR_TRANSPORT_VALUE_LENGTH);
73}
74
75export function parseSourceControlOwnerRef(
76 headSelector: string,
77): SourceControlRefSelector | undefined {
78 const match = /^([^:/\s]+):(.+)$/u.exec(headSelector.trim());
79 const owner = match?.[1]?.trim();
80 const refName = match?.[2]?.trim();
81 return owner && refName ? { owner, refName } : undefined;
82}
83
84function normalizeSourceBranch(headSelector: string): string {
85 return parseSourceControlOwnerRef(headSelector)?.refName ?? headSelector.trim();
86}
87
88export function sourceBranch(input: {
89 readonly headSelector: string;
90 readonly source?: SourceControlRefSelector;
91}): string {
92 return input.source?.refName ?? normalizeSourceBranch(input.headSelector);
93}
94
95export function sourceControlRefFromInput(input: {
96 readonly headSelector: string;
97 readonly source?: SourceControlRefSelector;
98}): SourceControlRefSelector | undefined {
99 return input.source ?? parseSourceControlOwnerRef(input.headSelector);
100}
101
102export function repositoryPathFromRemoteUrl(url: string | null): string | null {
103 const trimmed = url?.trim() ?? "";
104 if (trimmed.length === 0) {
105 return null;
106 }
107
108 const match =
109 /^(?:[^@/\s]+@[^:/\s]+:|[^@:/\\\s]{2,}:(?!:)|(?:ssh|https?|git):\/\/[^/]+\/)((?:[^/\s]+\/)+[^/\s]+?)(?:\.git)?\/?$/iu.exec(
110 trimmed,
111 );
112 const path = match?.[1]?.trim() ?? "";
113 return path.length > 0 ? path : null;
114}
115
116export class SourceControlProvider extends Context.Service<
117 SourceControlProvider,
118 {
119 readonly kind: SourceControlProviderKind;
120 /** The caller verifies owners, so hosts may drop unsupported `owner:branch` selectors. */
121 readonly headBranchProbe?: (input: {
122 readonly headSelectors: ReadonlyArray<string>;
123 readonly state: "open" | "all";
124 }) => { readonly headSelectors: ReadonlyArray<string>; readonly limit: number };
125 readonly readChangeRequestTemplate?: (input: {
126 readonly cwd: string;
127 readonly treeish: string;
128 }) => Effect.Effect<Option.Option<string>>;
129 /** Strip installation mounts from remote paths; absent means use the whole path. */
130 readonly repositoryNameFromRemoteUrl?: (url: string) => string | null;
131 readonly refineRepositoryIdentity?: (input: {
132 readonly identity: RepositoryIdentity;
133 readonly resolveContext: (input: {
134 readonly cwd: string;
135 readonly context: SourceControlProviderContext;
136 }) => Effect.Effect<SourceControlProviderContext | null, SourceControlProviderError>;
137 }) => Effect.Effect<RepositoryIdentity, SourceControlProviderError>;
138 /**
139 * The web host serving a remote, for a host whose remote URL may not name it (an SSH alias
140 * for one of several servers). It answers for the context's `requestedHost` when the caller
141 * narrows to one. Absent, or null, means the remote's own host is the web host.
142 */
143 readonly resolveRemoteWebHost?: (input: {
144 readonly cwd: string;
145 readonly context: SourceControlProviderContext;
146 readonly resolveContext: (input: {
147 readonly cwd: string;
148 readonly context: SourceControlProviderContext;
149 }) => Effect.Effect<SourceControlProviderContext | null, SourceControlProviderError>;
150 }) => Effect.Effect<string | null, SourceControlProviderError>;
151 readonly resolveLink?: ResolveSourceControlLink;
152 /**
153 * Whether `createChangeRequest` from `cwd` targets `repository`, the `owner/name` of the
154 * context's remote, for a host whose tools let configuration outside the remotes name another
155 * repository. Absent means it does.
156 */
157 readonly createsChangeRequestsIn?: (input: {
158 readonly cwd: string;
159 readonly context: SourceControlProviderContext;
160 readonly repository: string | null;
161 }) => Effect.Effect<boolean>;
162 /**
163 * Told when T3 records `baseBranch`, a branch name on the host, as the base `branch` was cut
164 * from, for a host whose own tools keep that in git config of their own.
165 */
166 readonly recordBranchBase?: (input: {
167 readonly cwd: string;
168 readonly branch: string;
169 readonly baseBranch: string;
170 }) => Effect.Effect<void>;
171 readonly listChangeRequests: (input: {
172 readonly cwd: string;
173 readonly context?: SourceControlProviderContext;
174 readonly source?: SourceControlRefSelector;
175 readonly headSelector: string;
176 readonly state: ChangeRequestState | "all";
177 readonly limit?: number;
178 }) => Effect.Effect<ReadonlyArray<ChangeRequest>, SourceControlProviderError>;
179 readonly getChangeRequest: (input: {
180 readonly cwd: string;
181 readonly context?: SourceControlProviderContext;
182 readonly reference: string;
183 }) => Effect.Effect<ChangeRequest, SourceControlProviderError>;
184 readonly createChangeRequest: (input: {
185 readonly cwd: string;
186 readonly context?: SourceControlProviderContext;
187 readonly source?: SourceControlRefSelector;
188 readonly target?: SourceControlRefSelector;
189 readonly baseRefName: string;
190 readonly headSelector: string;
191 readonly title: string;
192 readonly bodyFile: string;
193 }) => Effect.Effect<void, SourceControlProviderError>;
194 readonly getRepositoryCloneUrls: (input: {
195 readonly cwd: string;
196 readonly context?: SourceControlProviderContext;
197 readonly repository: string;
198 /** The host the repository is on. Without it, the checkout's host. */
199 readonly host?: string;
200 }) => Effect.Effect<SourceControlRepositoryCloneUrls, SourceControlProviderError>;
201 readonly createRepository: (input: {
202 readonly cwd: string;
203 readonly repository: string;
204 readonly visibility: SourceControlRepositoryVisibility;
205 }) => Effect.Effect<SourceControlRepositoryCloneUrls, SourceControlProviderError>;
206 readonly getDefaultBranch: (input: {
207 readonly cwd: string;
208 readonly context?: SourceControlProviderContext;
209 }) => Effect.Effect<string | null, SourceControlProviderError>;
210 readonly checkoutChangeRequest: (input: {
211 readonly cwd: string;
212 readonly context?: SourceControlProviderContext;
213 readonly reference: string;
214 readonly force?: boolean;
215 }) => Effect.Effect<void, SourceControlProviderError>;
216 }
217>()("@t3tools/source-control-core/server/SourceControlProvider") {}