Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

SourceControlProvider.ts

102 lines3.6 KB

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;
18​
19export interface SourceControlRefSelector {
20 readonly refName: string;
21 readonly owner?: string;
22 readonly repository?: string;
24​
25export function parseSourceControlOwnerRef(
26 headSelector: string,
27): SourceControlRefSelector | undefined {
28 const match = /^([^:/\s]+):(.+)$/u.exec(headSelector.trim());
29 const owner = match?.[1]?.trim();
30 const refName = match?.[2]?.trim();
31 return owner && refName ? { owner, refName } : undefined;
33​
34export function normalizeSourceBranch(headSelector: string): string {
35 return parseSourceControlOwnerRef(headSelector)?.refName ?? headSelector.trim();
37​
38export function sourceBranch(input: {
39 readonly headSelector: string;
40 readonly source?: SourceControlRefSelector;
41}): string {
42 return input.source?.refName ?? normalizeSourceBranch(input.headSelector);
44​
45export function sourceControlRefFromInput(input: {
46 readonly headSelector: string;
47 readonly source?: SourceControlRefSelector;
48}): SourceControlRefSelector | undefined {
49 return input.source ?? parseSourceControlOwnerRef(input.headSelector);
51​
52export interface SourceControlProviderShape {
53 readonly kind: SourceControlProviderKind;
54 readonly listChangeRequests: (input: {
55 readonly cwd: string;
56 readonly context?: SourceControlProviderContext;
57 readonly source?: SourceControlRefSelector;
58 readonly headSelector: string;
59 readonly state: ChangeRequestState | "all";
60 readonly limit?: number;
61 }) => Effect.Effect<ReadonlyArray<ChangeRequest>, SourceControlProviderError>;
62 readonly getChangeRequest: (input: {
63 readonly cwd: string;
64 readonly context?: SourceControlProviderContext;
65 readonly reference: string;
66 }) => Effect.Effect<ChangeRequest, SourceControlProviderError>;
67 readonly createChangeRequest: (input: {
68 readonly cwd: string;
69 readonly context?: SourceControlProviderContext;
70 readonly source?: SourceControlRefSelector;
71 readonly target?: SourceControlRefSelector;
72 readonly baseRefName: string;
73 readonly headSelector: string;
74 readonly title: string;
75 readonly bodyFile: string;
76 }) => Effect.Effect<void, SourceControlProviderError>;
77 readonly getRepositoryCloneUrls: (input: {
78 readonly cwd: string;
79 readonly context?: SourceControlProviderContext;
80 readonly repository: string;
81 }) => Effect.Effect<SourceControlRepositoryCloneUrls, SourceControlProviderError>;
82 readonly createRepository: (input: {
83 readonly cwd: string;
84 readonly repository: string;
85 readonly visibility: SourceControlRepositoryVisibility;
86 }) => Effect.Effect<SourceControlRepositoryCloneUrls, SourceControlProviderError>;
87 readonly getDefaultBranch: (input: {
88 readonly cwd: string;
89 readonly context?: SourceControlProviderContext;
90 }) => Effect.Effect<string | null, SourceControlProviderError>;
91 readonly checkoutChangeRequest: (input: {
92 readonly cwd: string;
93 readonly context?: SourceControlProviderContext;
94 readonly reference: string;
95 readonly force?: boolean;
96 }) => Effect.Effect<void, SourceControlProviderError>;
98​
99export class SourceControlProvider extends Context.Service<
100 SourceControlProvider,
101 SourceControlProviderShape
102>()("t3/sourceControl/SourceControlProvider") {}