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