Open History to see the last commit
1import * as Context from "effect/Context";
2import * as Effect from "effect/Effect";
3import type {
4 ChangeRequest,
5 ChangeRequestState,
6 SourceControlProviderError,
7 SourceControlProviderInfo,
8 SourceControlProviderKind,
9 SourceControlRepositoryCloneUrls,
10 SourceControlRepositoryVisibility,
11} from "@t3tools/contracts";
12
13export interface SourceControlLinkSubject {
14 readonly title: string;
15 readonly body: string | null;
16}
17
18/** Return undefined synchronously for unsupported URLs, without starting a lookup. */
19export type ResolveSourceControlLink = (input: {
20 readonly cwd: string;
21 readonly url: URL;
22}) => Effect.Effect<SourceControlLinkSubject, SourceControlProviderError> | undefined;
23
24export interface SourceControlProviderContext {
25 readonly provider: SourceControlProviderInfo;
26 readonly remoteName: string;
27 readonly remoteUrl: string;
28 /** An explicit web authority can disambiguate Forgejo logins sharing an SSH alias. */
29 readonly requestedHost?: string;
30}
31
32export interface SourceControlRefSelector {
33 readonly refName: string;
34 readonly owner?: string;
35 readonly repository?: string;
36}
37
38const MAX_ERROR_TRANSPORT_VALUE_LENGTH = 256;
39
40/**
41 * Sanitizes user-provided source-control identifiers before attaching them to
42 * contract errors. This is intentionally narrower than request validation: it
43 * only strips URL secrets and bounds diagnostic values sent over transport.
44 */
45export function transportSafeSourceControlErrorValue(value: string): string {
46 let printable = "";
47 for (const character of value) {
48 const codePoint = character.codePointAt(0);
49 printable += codePoint !== undefined && (codePoint < 32 || codePoint === 127) ? " " : character;
50 }
51 const normalized = printable.trim().replace(/\s+/gu, " ");
52
53 let safe = normalized;
54 try {
55 const url = new URL(normalized);
56 url.username = "";
57 url.password = "";
58 url.search = "";
59 url.hash = "";
60 safe = url.toString();
61 } catch {
62 // Plain repository and change-request identifiers are not URLs.
63 }
64
65 return safe.slice(0, MAX_ERROR_TRANSPORT_VALUE_LENGTH);
66}
67
68export function parseSourceControlOwnerRef(
69 headSelector: string,
70): SourceControlRefSelector | undefined {
71 const match = /^([^:/\s]+):(.+)$/u.exec(headSelector.trim());
72 const owner = match?.[1]?.trim();
73 const refName = match?.[2]?.trim();
74 return owner && refName ? { owner, refName } : undefined;
75}
76
77function normalizeSourceBranch(headSelector: string): string {
78 return parseSourceControlOwnerRef(headSelector)?.refName ?? headSelector.trim();
79}
80
81export function sourceBranch(input: {
82 readonly headSelector: string;
83 readonly source?: SourceControlRefSelector;
84}): string {
85 return input.source?.refName ?? normalizeSourceBranch(input.headSelector);
86}
87
88export function sourceControlRefFromInput(input: {
89 readonly headSelector: string;
90 readonly source?: SourceControlRefSelector;
91}): SourceControlRefSelector | undefined {
92 return input.source ?? parseSourceControlOwnerRef(input.headSelector);
93}
94
95export class SourceControlProvider extends Context.Service<
96 SourceControlProvider,
97 {
98 readonly kind: SourceControlProviderKind;
99 /** Optional capability for issue and change-request subjects. */
100 readonly resolveLink?: ResolveSourceControlLink;
101 readonly listChangeRequests: (input: {
102 readonly cwd: string;
103 readonly context?: SourceControlProviderContext;
104 readonly source?: SourceControlRefSelector;
105 readonly headSelector: string;
106 readonly state: ChangeRequestState | "all";
107 readonly limit?: number;
108 }) => Effect.Effect<ReadonlyArray<ChangeRequest>, SourceControlProviderError>;
109 readonly getChangeRequest: (input: {
110 readonly cwd: string;
111 readonly context?: SourceControlProviderContext;
112 readonly reference: string;
113 }) => Effect.Effect<ChangeRequest, SourceControlProviderError>;
114 readonly createChangeRequest: (input: {
115 readonly cwd: string;
116 readonly context?: SourceControlProviderContext;
117 readonly source?: SourceControlRefSelector;
118 readonly target?: SourceControlRefSelector;
119 readonly baseRefName: string;
120 readonly headSelector: string;
121 readonly title: string;
122 readonly bodyFile: string;
123 }) => Effect.Effect<void, SourceControlProviderError>;
124 readonly getRepositoryCloneUrls: (input: {
125 readonly cwd: string;
126 readonly context?: SourceControlProviderContext;
127 readonly repository: string;
128 readonly host?: string;
129 }) => Effect.Effect<SourceControlRepositoryCloneUrls, SourceControlProviderError>;
130 readonly createRepository: (input: {
131 readonly cwd: string;
132 readonly repository: string;
133 readonly visibility: SourceControlRepositoryVisibility;
134 readonly host?: string;
135 }) => Effect.Effect<SourceControlRepositoryCloneUrls, SourceControlProviderError>;
136 readonly getDefaultBranch: (input: {
137 readonly cwd: string;
138 readonly context?: SourceControlProviderContext;
139 }) => Effect.Effect<string | null, SourceControlProviderError>;
140 readonly checkoutChangeRequest: (input: {
141 readonly cwd: string;
142 readonly context?: SourceControlProviderContext;
143 readonly reference: string;
144 readonly force?: boolean;
145 }) => Effect.Effect<void, SourceControlProviderError>;
146 }
147>()("t3/sourceControl/SourceControlProvider") {}