Open History to see the last commit
1import { Context, Effect } from "effect";
2import type {
3 ChangeRequest,
4 ChangeRequestState,
5 SourceControlProviderError,
6 SourceControlProviderInfo,
7 SourceControlProviderKind,
8 SourceControlRepositoryCloneUrls,
9 SourceControlRepositoryVisibility,
10} from "@t3tools/contracts";
11
12export interface SourceControlProviderContext {
13 readonly provider: SourceControlProviderInfo;
14 readonly remoteName: string;
15 readonly remoteUrl: string;
16}
17
18export interface SourceControlRefSelector {
19 readonly refName: string;
20 readonly owner?: string;
21 readonly repository?: string;
22}
23
24export function parseSourceControlOwnerRef(
25 headSelector: string,
26): SourceControlRefSelector | undefined {
27 const match = /^([^:/\s]+):(.+)$/u.exec(headSelector.trim());
28 const owner = match?.[1]?.trim();
29 const refName = match?.[2]?.trim();
30 return owner && refName ? { owner, refName } : undefined;
31}
32
33export function normalizeSourceBranch(headSelector: string): string {
34 return parseSourceControlOwnerRef(headSelector)?.refName ?? headSelector.trim();
35}
36
37export function sourceBranch(input: {
38 readonly headSelector: string;
39 readonly source?: SourceControlRefSelector;
40}): string {
41 return input.source?.refName ?? normalizeSourceBranch(input.headSelector);
42}
43
44export function sourceControlRefFromInput(input: {
45 readonly headSelector: string;
46 readonly source?: SourceControlRefSelector;
47}): SourceControlRefSelector | undefined {
48 return input.source ?? parseSourceControlOwnerRef(input.headSelector);
49}
50
51export interface SourceControlProviderShape {
52 readonly kind: SourceControlProviderKind;
53 readonly listChangeRequests: (input: {
54 readonly cwd: string;
55 readonly context?: SourceControlProviderContext;
56 readonly source?: SourceControlRefSelector;
57 readonly headSelector: string;
58 readonly state: ChangeRequestState | "all";
59 readonly limit?: number;
60 }) => Effect.Effect<ReadonlyArray<ChangeRequest>, SourceControlProviderError>;
61 readonly getChangeRequest: (input: {
62 readonly cwd: string;
63 readonly context?: SourceControlProviderContext;
64 readonly reference: string;
65 }) => Effect.Effect<ChangeRequest, SourceControlProviderError>;
66 readonly createChangeRequest: (input: {
67 readonly cwd: string;
68 readonly context?: SourceControlProviderContext;
69 readonly source?: SourceControlRefSelector;
70 readonly target?: SourceControlRefSelector;
71 readonly baseRefName: string;
72 readonly headSelector: string;
73 readonly title: string;
74 readonly bodyFile: string;
75 }) => Effect.Effect<void, SourceControlProviderError>;
76 readonly getRepositoryCloneUrls: (input: {
77 readonly cwd: string;
78 readonly context?: SourceControlProviderContext;
79 readonly repository: string;
80 }) => Effect.Effect<SourceControlRepositoryCloneUrls, SourceControlProviderError>;
81 readonly createRepository: (input: {
82 readonly cwd: string;
83 readonly repository: string;
84 readonly visibility: SourceControlRepositoryVisibility;
85 }) => Effect.Effect<SourceControlRepositoryCloneUrls, SourceControlProviderError>;
86 readonly getDefaultBranch: (input: {
87 readonly cwd: string;
88 readonly context?: SourceControlProviderContext;
89 }) => Effect.Effect<string | null, SourceControlProviderError>;
90 readonly checkoutChangeRequest: (input: {
91 readonly cwd: string;
92 readonly context?: SourceControlProviderContext;
93 readonly reference: string;
94 readonly force?: boolean;
95 }) => Effect.Effect<void, SourceControlProviderError>;
96}
97
98export class SourceControlProvider extends Context.Service<
99 SourceControlProvider,
100 SourceControlProviderShape
101>()("t3/source-control/SourceControlProvider") {}