Open History to see the last commit
1import * as Context from "effect/Context";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4import type { SourceControlProviderKind } from "@t3tools/contracts";
5
6import * as AzureDevOpsCli from "../sourceControl/AzureDevOpsCli.ts";
7import * as BitbucketApi from "../sourceControl/BitbucketApi.ts";
8import * as GitHubApi from "../sourceControl/GitHubApi.ts";
9import * as GitLabCli from "../sourceControl/GitLabCli.ts";
10import * as ForgejoCli from "../sourceControl/ForgejoCli.ts";
11import * as ForgejoPullRequestProvider from "./ForgejoPullRequestProvider.ts";
12import * as AzureDevOpsPullRequestCli from "./AzureDevOpsPullRequestCli.ts";
13import * as AzureDevOpsPullRequestProvider from "./AzureDevOpsPullRequestProvider.ts";
14import * as BitbucketPullRequestApi from "./BitbucketPullRequestApi.ts";
15import * as BitbucketPullRequestProvider from "./BitbucketPullRequestProvider.ts";
16import * as GitHubPullRequestApi from "./GitHubPullRequestApi.ts";
17import * as GitHubPullRequestProvider from "./GitHubPullRequestProvider.ts";
18import * as GitLabPullRequestCli from "./GitLabPullRequestCli.ts";
19import * as GitLabPullRequestProvider from "./GitLabPullRequestProvider.ts";
20import type { PullRequestProviderApi } from "./PullRequestProvider.ts";
21
22export class PullRequestProviderRegistry extends Context.Service<
23 PullRequestProviderRegistry,
24 {
25 /** Null for a host with no implementation, which the service reports as unsupported. */
26 readonly get: (kind: SourceControlProviderKind) => PullRequestProviderApi | null;
27 readonly kinds: ReadonlyArray<SourceControlProviderKind>;
28 }
29>()("t3/pullRequest/PullRequestProviderRegistry") {}
30
31/** Exported for tests, which stand a registry up from providers they supply themselves. */
32export function fromProviders(
33 providers: ReadonlyArray<PullRequestProviderApi>,
34): PullRequestProviderRegistry["Service"] {
35 const byKind = new Map(providers.map((provider) => [provider.kind, provider]));
36 return {
37 get: (kind) => byKind.get(kind) ?? null,
38 kinds: providers.map((provider) => provider.kind),
39 };
40}
41
42/**
43 * The hosts this build can read change requests from. A host with no entry here still shows up
44 * in the provider list as unimplemented, so its projects are explained rather than missing.
45 *
46 * @public Service construction is part of the canonical Effect module API.
47 */
48export const make = Effect.map(
49 Effect.all([
50 GitHubPullRequestProvider.make,
51 GitLabPullRequestProvider.make,
52 ForgejoPullRequestProvider.make,
53 BitbucketPullRequestProvider.make,
54 AzureDevOpsPullRequestProvider.make,
55 ]),
56 fromProviders,
57);
58
59export const layer = Layer.effect(PullRequestProviderRegistry, make).pipe(
60 Layer.provide(
61 GitHubPullRequestApi.layer.pipe(
62 Layer.provide(
63 // Built here too, so a registry built alone still has one budget and pause per host.
64 GitHubApi.layerWithDependencies,
65 ),
66 ),
67 ),
68 Layer.provide(GitLabPullRequestCli.layer.pipe(Layer.provide(GitLabCli.layer))),
69 Layer.provide(ForgejoCli.layer),
70 Layer.provide(BitbucketPullRequestApi.layer.pipe(Layer.provide(BitbucketApi.layer))),
71 Layer.provide(AzureDevOpsPullRequestCli.layer.pipe(Layer.provide(AzureDevOpsCli.layer))),
72);