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

PullRequestProviderRegistry.ts

59 lines2.7 KB

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 GitHubCli from "../sourceControl/GitHubCli.ts";
9import * as GitLabCli from "../sourceControl/GitLabCli.ts";
10import * as AzureDevOpsPullRequestCli from "./AzureDevOpsPullRequestCli.ts";
11import * as AzureDevOpsPullRequestProvider from "./AzureDevOpsPullRequestProvider.ts";
12import * as BitbucketPullRequestApi from "./BitbucketPullRequestApi.ts";
13import * as BitbucketPullRequestProvider from "./BitbucketPullRequestProvider.ts";
14import * as GitHubPullRequestCli from "./GitHubPullRequestCli.ts";
15import * as GitHubPullRequestProvider from "./GitHubPullRequestProvider.ts";
16import * as GitLabPullRequestCli from "./GitLabPullRequestCli.ts";
17import * as GitLabPullRequestProvider from "./GitLabPullRequestProvider.ts";
18import type { PullRequestProviderApi } from "./PullRequestProvider.ts";
19​
20export class PullRequestProviderRegistry extends Context.Service<
21 PullRequestProviderRegistry,
22 {
23 /** Null for a host with no implementation, which the service reports as unsupported. */
24 readonly get: (kind: SourceControlProviderKind) => PullRequestProviderApi | null;
25 readonly kinds: ReadonlyArray<SourceControlProviderKind>;
26 }
27>()("t3/pullRequest/PullRequestProviderRegistry") {}
28​
29/** Exported for tests, which stand a registry up from providers they supply themselves. */
30export function fromProviders(
31 providers: ReadonlyArray<PullRequestProviderApi>,
32): PullRequestProviderRegistry["Service"] {
33 const byKind = new Map(providers.map((provider) => [provider.kind, provider]));
34 return {
35 get: (kind) => byKind.get(kind) ?? null,
36 kinds: providers.map((provider) => provider.kind),
37 };
39​
40/**
41 * The hosts this build can read change requests from. A host with no entry here still shows up
42 * in the provider list as unimplemented, so its projects are explained rather than missing.
43 */
44export const make = Effect.map(
45 Effect.all([
46 GitHubPullRequestProvider.make,
47 GitLabPullRequestProvider.make,
48 BitbucketPullRequestProvider.make,
49 AzureDevOpsPullRequestProvider.make,
50 ]),
51 fromProviders,
52);
53​
54export const layer = Layer.effect(PullRequestProviderRegistry, make).pipe(
55 Layer.provide(GitHubPullRequestCli.layer.pipe(Layer.provide(GitHubCli.layer))),
56 Layer.provide(GitLabPullRequestCli.layer.pipe(Layer.provide(GitLabCli.layer))),
57 Layer.provide(BitbucketPullRequestApi.layer.pipe(Layer.provide(BitbucketApi.layer))),
58 Layer.provide(AzureDevOpsPullRequestCli.layer.pipe(Layer.provide(AzureDevOpsCli.layer))),
59);