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
41export function transportSafeSourceControlErrorValue(value: string): string {
42 let printable = "";
43 for (const character of value) {
44 const codePoint = character.codePointAt(0);
45 printable += codePoint !== undefined && (codePoint < 32 || codePoint === 127) ? " " : character;
46 }
47 const normalized = printable.trim().replace(/\s+/gu, " ");
48
49 let safe = normalized;
50 try {
51 const url = new URL(normalized);
52 url.username = "";
53 url.password = "";
54 url.search = "";
55 url.hash = "";
56 safe = url.toString();
57 } catch {}
58
59 return safe.slice(0, MAX_ERROR_TRANSPORT_VALUE_LENGTH);
60}
61
62export function parseSourceControlOwnerRef(
63 headSelector: string,
64): SourceControlRefSelector | undefined {
65 const match = /^([^:/\s]+):(.+)$/u.exec(headSelector.trim());
66 const owner = match?.[1]?.trim();
67 const refName = match?.[2]?.trim();
68 return owner && refName ? { owner, refName } : undefined;
69}
70
71function normalizeSourceBranch(headSelector: string): string {
72 return parseSourceControlOwnerRef(headSelector)?.refName ?? headSelector.trim();
73}
74
75export function sourceBranch(input: {
76 readonly headSelector: string;
77 readonly source?: SourceControlRefSelector;
78}): string {
79 return input.source?.refName ?? normalizeSourceBranch(input.headSelector);
80}
81
82export function sourceControlRefFromInput(input: {
83 readonly headSelector: string;
84 readonly source?: SourceControlRefSelector;
85}): SourceControlRefSelector | undefined {
86 return input.source ?? parseSourceControlOwnerRef(input.headSelector);
87}
88
89export function repositoryPathFromRemoteUrl(url: string | null): string | null {
90 const trimmed = url?.trim() ?? "";
91 if (trimmed.length === 0) {
92 return null;
93 }
94
95 const match =
96 /^(?:[^@/\s]+@[^:/\s]+:|(?:ssh|https?|git):\/\/[^/]+\/)((?:[^/\s]+\/)+[^/\s]+?)(?:\.git)?\/?$/iu.exec(
97 trimmed,
98 );
99 const path = match?.[1]?.trim() ?? "";
100 return path.length > 0 ? path : null;
101}
102
103export class SourceControlProvider extends Context.Service<
104 SourceControlProvider,
105 {
106 readonly kind: SourceControlProviderKind;
107 /**
108 * The caller checks the owner against what comes back, so a host that cannot search
109 * `owner:branch` can drop those selectors without losing anything.
110 */
111 readonly headBranchProbe?: (input: {
112 readonly headSelectors: ReadonlyArray<string>;
113 readonly state: "open" | "all";
114 }) => { readonly headSelectors: ReadonlyArray<string>; readonly limit: number };
115 readonly readChangeRequestTemplate?: (input: {
116 readonly cwd: string;
117 readonly treeish: string;
118 }) => Effect.Effect<Option.Option<string>>;
119 /**
120 * The repository's `owner/name` from a remote URL, for a host whose remote paths can carry
121 * more than that (Forgejo's HTTP installation mount). Absent means the whole path.
122 */
123 readonly repositoryNameFromRemoteUrl?: (url: string) => string | null;
124 readonly refineRepositoryIdentity?: (input: {
125 readonly identity: RepositoryIdentity;
126 readonly resolveContext: (input: {
127 readonly cwd: string;
128 readonly context: SourceControlProviderContext;
129 }) => Effect.Effect<SourceControlProviderContext | null, SourceControlProviderError>;
130 }) => Effect.Effect<RepositoryIdentity, SourceControlProviderError>;
131 readonly resolveLink?: ResolveSourceControlLink;
132 readonly listChangeRequests: (input: {
133 readonly cwd: string;
134 readonly context?: SourceControlProviderContext;
135 readonly source?: SourceControlRefSelector;
136 readonly headSelector: string;
137 readonly state: ChangeRequestState | "all";
138 readonly limit?: number;
139 }) => Effect.Effect<ReadonlyArray<ChangeRequest>, SourceControlProviderError>;
140 readonly getChangeRequest: (input: {
141 readonly cwd: string;
142 readonly context?: SourceControlProviderContext;
143 readonly reference: string;
144 }) => Effect.Effect<ChangeRequest, SourceControlProviderError>;
145 readonly createChangeRequest: (input: {
146 readonly cwd: string;
147 readonly context?: SourceControlProviderContext;
148 readonly source?: SourceControlRefSelector;
149 readonly target?: SourceControlRefSelector;
150 readonly baseRefName: string;
151 readonly headSelector: string;
152 readonly title: string;
153 readonly bodyFile: string;
154 }) => Effect.Effect<void, SourceControlProviderError>;
155 readonly getRepositoryCloneUrls: (input: {
156 readonly cwd: string;
157 readonly context?: SourceControlProviderContext;
158 readonly repository: string;
159 }) => Effect.Effect<SourceControlRepositoryCloneUrls, SourceControlProviderError>;
160 readonly createRepository: (input: {
161 readonly cwd: string;
162 readonly repository: string;
163 readonly visibility: SourceControlRepositoryVisibility;
164 }) => Effect.Effect<SourceControlRepositoryCloneUrls, SourceControlProviderError>;
165 readonly getDefaultBranch: (input: {
166 readonly cwd: string;
167 readonly context?: SourceControlProviderContext;
168 }) => Effect.Effect<string | null, SourceControlProviderError>;
169 readonly checkoutChangeRequest: (input: {
170 readonly cwd: string;
171 readonly context?: SourceControlProviderContext;
172 readonly reference: string;
173 readonly force?: boolean;
174 }) => Effect.Effect<void, SourceControlProviderError>;
175 }
176>()("@t3tools/source-control-core/server/SourceControlProvider") {}