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