Open History to see the last commit
1import * as Effect from "effect/Effect";
2import * as Layer from "effect/Layer";
3import * as Option from "effect/Option";
4import { SourceControlProviderError, type ChangeRequest } from "@t3tools/contracts";
5
6import * as BitbucketApi from "./BitbucketApi.ts";
7import * as BitbucketPullRequests from "./bitbucketPullRequests.ts";
8import * as SourceControlProvider from "./SourceControlProvider.ts";
9import type * as SourceControlProviderDiscovery from "./SourceControlProviderDiscovery.ts";
10
11function providerError(
12 operation: string,
13 cause: BitbucketApi.BitbucketApiError,
14): SourceControlProviderError {
15 return new SourceControlProviderError({
16 provider: "bitbucket",
17 operation,
18 detail: cause.detail,
19 cause,
20 });
21}
22
23function toChangeRequest(
24 summary: BitbucketPullRequests.NormalizedBitbucketPullRequestRecord,
25): ChangeRequest {
26 return {
27 provider: "bitbucket",
28 number: summary.number,
29 title: summary.title,
30 url: summary.url,
31 baseRefName: summary.baseRefName,
32 headRefName: summary.headRefName,
33 state: summary.state,
34 updatedAt: summary.updatedAt ?? Option.none(),
35 ...(summary.isCrossRepository !== undefined
36 ? { isCrossRepository: summary.isCrossRepository }
37 : {}),
38 ...(summary.headRepositoryNameWithOwner !== undefined
39 ? { headRepositoryNameWithOwner: summary.headRepositoryNameWithOwner }
40 : {}),
41 ...(summary.headRepositoryOwnerLogin !== undefined
42 ? { headRepositoryOwnerLogin: summary.headRepositoryOwnerLogin }
43 : {}),
44 };
45}
46
47export const make = Effect.fn("makeBitbucketSourceControlProvider")(function* () {
48 const bitbucket = yield* BitbucketApi.BitbucketApi;
49
50 return SourceControlProvider.SourceControlProvider.of({
51 kind: "bitbucket",
52 listChangeRequests: (input) => {
53 const source = SourceControlProvider.sourceControlRefFromInput(input);
54 return bitbucket
55 .listPullRequests({
56 cwd: input.cwd,
57 ...(input.context ? { context: input.context } : {}),
58 headSelector: input.headSelector,
59 ...(source ? { source } : {}),
60 state: input.state,
61 ...(input.limit !== undefined ? { limit: input.limit } : {}),
62 })
63 .pipe(
64 Effect.map((items) => items.map(toChangeRequest)),
65 Effect.mapError((error) => providerError("listChangeRequests", error)),
66 );
67 },
68 getChangeRequest: (input) =>
69 bitbucket.getPullRequest(input).pipe(
70 Effect.map(toChangeRequest),
71 Effect.mapError((error) => providerError("getChangeRequest", error)),
72 ),
73 createChangeRequest: (input) => {
74 const source = SourceControlProvider.sourceControlRefFromInput(input);
75 return bitbucket
76 .createPullRequest({
77 cwd: input.cwd,
78 ...(input.context ? { context: input.context } : {}),
79 baseBranch: input.baseRefName,
80 headSelector: input.headSelector,
81 ...(source ? { source } : {}),
82 ...(input.target ? { target: input.target } : {}),
83 title: input.title,
84 bodyFile: input.bodyFile,
85 })
86 .pipe(Effect.mapError((error) => providerError("createChangeRequest", error)));
87 },
88 getRepositoryCloneUrls: (input) =>
89 bitbucket
90 .getRepositoryCloneUrls(input)
91 .pipe(Effect.mapError((error) => providerError("getRepositoryCloneUrls", error))),
92 createRepository: (input) =>
93 bitbucket
94 .createRepository(input)
95 .pipe(Effect.mapError((error) => providerError("createRepository", error))),
96 getDefaultBranch: (input) =>
97 bitbucket
98 .getDefaultBranch({
99 cwd: input.cwd,
100 ...(input.context ? { context: input.context } : {}),
101 })
102 .pipe(Effect.mapError((error) => providerError("getDefaultBranch", error))),
103 checkoutChangeRequest: (input) =>
104 bitbucket
105 .checkoutPullRequest({
106 cwd: input.cwd,
107 ...(input.context ? { context: input.context } : {}),
108 reference: input.reference,
109 ...(input.force !== undefined ? { force: input.force } : {}),
110 })
111 .pipe(Effect.mapError((error) => providerError("checkoutChangeRequest", error))),
112 });
113});
114
115export const layer = Layer.effect(SourceControlProvider.SourceControlProvider, make());
116
117export const makeDiscovery = Effect.fn("makeBitbucketSourceControlProviderDiscovery")(function* () {
118 const bitbucket = yield* BitbucketApi.BitbucketApi;
119
120 return {
121 type: "api",
122 kind: "bitbucket",
123 label: "Bitbucket",
124 installHint:
125 "Set T3CODE_BITBUCKET_EMAIL and T3CODE_BITBUCKET_API_TOKEN on the server (use a Bitbucket API token with pull request and repository scopes).",
126 probeAuth: bitbucket.probeAuth,
127 } satisfies SourceControlProviderDiscovery.SourceControlApiDiscoverySpec;
128});