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

SourceControlProviderRegistry.ts

169 lines5.9 KB

1import { Cache, Context, Duration, Effect, Exit, Layer } from "effect";
2import { SourceControlProviderError } from "@t3tools/contracts";
3import type { SourceControlProviderKind } from "@t3tools/contracts";
4import { detectSourceControlProviderFromRemoteUrl } from "@t3tools/shared/sourceControl";
5​
6import {
7 SourceControlProvider,
8 type SourceControlProviderContext,
9 type SourceControlProviderShape,
10} from "./SourceControlProvider.ts";
11import * as GitHubSourceControlProvider from "./GitHubSourceControlProvider.ts";
12import * as GitLabSourceControlProvider from "./GitLabSourceControlProvider.ts";
13import { VcsDriverRegistry } from "../vcs/VcsDriverRegistry.ts";
14​
15const PROVIDER_DETECTION_CACHE_CAPACITY = 2_048;
16const PROVIDER_DETECTION_CACHE_TTL = Duration.seconds(5);
17​
18export interface SourceControlProviderRegistration {
19 readonly kind: SourceControlProviderKind;
20 readonly provider: SourceControlProviderShape;
22​
23export interface SourceControlProviderHandle {
24 readonly provider: SourceControlProviderShape;
25 readonly context: SourceControlProviderContext | null;
27​
28export interface SourceControlProviderRegistryShape {
29 readonly get: (
30 kind: SourceControlProviderKind,
31 ) => Effect.Effect<SourceControlProviderShape, SourceControlProviderError>;
32 readonly resolveHandle: (input: {
33 readonly cwd: string;
34 }) => Effect.Effect<SourceControlProviderHandle, SourceControlProviderError>;
35 readonly resolve: (input: {
36 readonly cwd: string;
37 }) => Effect.Effect<SourceControlProviderShape, SourceControlProviderError>;
39​
40export class SourceControlProviderRegistry extends Context.Service<
41 SourceControlProviderRegistry,
42 SourceControlProviderRegistryShape
43>()("t3/source-control/SourceControlProviderRegistry") {}
44​
45function unsupportedProvider(kind: SourceControlProviderKind): SourceControlProviderShape {
46 const unsupported = (operation: string) =>
47 Effect.fail(
48 new SourceControlProviderError({
49 provider: kind,
50 operation,
51 detail: `No ${kind} source control provider is registered.`,
52 }),
53 );
54​
55 return SourceControlProvider.of({
56 kind,
57 listChangeRequests: () => unsupported("listChangeRequests"),
58 getChangeRequest: () => unsupported("getChangeRequest"),
59 createChangeRequest: () => unsupported("createChangeRequest"),
60 getRepositoryCloneUrls: () => unsupported("getRepositoryCloneUrls"),
61 createRepository: () => unsupported("createRepository"),
62 getDefaultBranch: () => unsupported("getDefaultBranch"),
63 checkoutChangeRequest: () => unsupported("checkoutChangeRequest"),
64 });
66​
67function providerDetectionError(operation: string, cwd: string, cause: unknown) {
68 return new SourceControlProviderError({
69 provider: "unknown",
70 operation,
71 detail: `Failed to detect source control provider for ${cwd}.`,
72 cause,
73 });
75​
76function selectProviderContext(
77 remotes: ReadonlyArray<{
78 readonly name: string;
79 readonly url: string;
80 }>,
81): SourceControlProviderContext | null {
82 const candidates = remotes
83 .map((remote) => {
84 const provider = detectSourceControlProviderFromRemoteUrl(remote.url);
85 return provider
86 ? {
87 provider,
88 remoteName: remote.name,
89 remoteUrl: remote.url,
90 }
91 : null;
92 })
93 .filter((value): value is SourceControlProviderContext => value !== null);
94​
95 return (
96 candidates.find((candidate) => candidate.remoteName === "origin") ??
97 candidates.find((candidate) => candidate.provider.kind !== "unknown") ??
98 candidates[0] ??
99 null
100 );
102​
103export const makeWithProviders = Effect.fn("makeSourceControlProviderRegistryWithProviders")(
104 function* (registrations: ReadonlyArray<SourceControlProviderRegistration>) {
105 const vcsRegistry = yield* VcsDriverRegistry;
106 const providers = new Map<SourceControlProviderKind, SourceControlProviderShape>(
107 registrations.map((registration) => [registration.kind, registration.provider]),
108 );
109​
110 const get: SourceControlProviderRegistryShape["get"] = (kind) =>
111 Effect.succeed(providers.get(kind) ?? unsupportedProvider(kind));
112​
113 const detectProviderContext = Effect.fn("SourceControlProviderRegistry.detectProviderContext")(
114 function* (cwd: string) {
115 const handle = yield* vcsRegistry
116 .resolve({ cwd })
117 .pipe(Effect.mapError((error) => providerDetectionError("detectProvider", cwd, error)));
118 const remotes = yield* handle.driver
119 .listRemotes(cwd)
120 .pipe(Effect.mapError((error) => providerDetectionError("detectProvider", cwd, error)));
121​
122 return selectProviderContext(remotes.remotes);
123 },
124 );
125​
126 const providerContextCache = yield* Cache.makeWith<
127 string,
128 SourceControlProviderContext | null,
129 SourceControlProviderError
130 >(detectProviderContext, {
131 capacity: PROVIDER_DETECTION_CACHE_CAPACITY,
132 timeToLive: (exit) => (Exit.isSuccess(exit) ? PROVIDER_DETECTION_CACHE_TTL : Duration.zero),
133 });
134​
135 const resolveHandle: SourceControlProviderRegistryShape["resolveHandle"] = (input) =>
136 Cache.get(providerContextCache, input.cwd).pipe(
137 Effect.map((context) => {
138 const kind = context?.provider.kind ?? "unknown";
139 return {
140 provider: providers.get(kind) ?? unsupportedProvider(kind),
141 context,
142 } satisfies SourceControlProviderHandle;
143 }),
144 );
145​
146 return SourceControlProviderRegistry.of({
147 get,
148 resolveHandle,
149 resolve: (input) => resolveHandle(input).pipe(Effect.map((handle) => handle.provider)),
150 });
151 },
152);
153​
154export const make = Effect.fn("makeSourceControlProviderRegistry")(function* () {
155 const github = yield* GitHubSourceControlProvider.make();
156 const gitlab = yield* GitLabSourceControlProvider.make();
157 return yield* makeWithProviders([
158 {
159 kind: "github",
160 provider: github,
161 },
162 {
163 kind: "gitlab",
164 provider: gitlab,
165 },
166 ]);
167});
168​
169export const layer = Layer.effect(SourceControlProviderRegistry, make());