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