Open History to see the last commit
1/**
2 * Bitbucket's client definition. Browser- and React Native-safe.
3 *
4 * @module source-control-bitbucket/client/definition
5 */
6import {
7 makeProviderSettingsSchema,
8 SourceControlProviderKind,
9 TrimmedString,
10} from "@t3tools/contracts";
11import * as Effect from "effect/Effect";
12import * as Schema from "effect/Schema";
13import {
14 defineSourceControlClient,
15 isChangeRequestInProjectRepository,
16 isChangeRequestOnProjectHost,
17 isChangeRequestPath,
18} from "@t3tools/source-control-core/client/definition";
19
20const safeShellArgument = /^[A-Za-z0-9._/@+=,-]+$/;
21const repositoryName = /^[A-Za-z0-9._-]+\/[A-Za-z0-9._-]+$/;
22
23const KIND = SourceControlProviderKind.make("bitbucket");
24
25const text = (annotations: Schema.Annotations.Key<string>) =>
26 TrimmedString.pipe(
27 Schema.withDecodingDefault(Effect.succeed("")),
28 Schema.annotateKey(annotations),
29 );
30
31/**
32 * Bitbucket API credentials, used before the `T3CODE_BITBUCKET_*` environment variables: an
33 * access token, or an Atlassian account email with an API token. The access token wins when both
34 * are saved.
35 */
36export const settings = makeProviderSettingsSchema(
37 {
38 accessToken: text({
39 title: "Access token",
40 description:
41 "Scoped to one repository, project, or workspace. Create it in that item's Bitbucket settings.",
42 providerSettingsForm: { control: "password", secret: true },
43 }),
44 email: text({
45 title: "Atlassian account email",
46 description: "With an API token instead of an access token.",
47 providerSettingsForm: { placeholder: "you@example.com" },
48 }),
49 apiToken: text({
50 title: "API token",
51 description:
52 "Reaches every repository your Atlassian account can. Give it read and write access to repositories and pull requests, and read:user:bitbucket.",
53 providerSettingsForm: { control: "password", secret: true },
54 }),
55 },
56 { order: ["accessToken", "email", "apiToken"] },
57);
58export type BitbucketSettings = typeof settings.Type;
59
60export const definition = defineSourceControlClient({
61 kind: KIND,
62 label: "Bitbucket",
63 pickerLabel: "Bitbucket",
64 icon: "bitbucket",
65 changeRequest: { shortLabel: "PR", singular: "pull request" },
66 repositoryPathHint: "workspace/repository",
67 publicHost: "bitbucket.org",
68 publishDescription: "bitbucket.org",
69 publishHost: () => "bitbucket.org",
70 // A new repository belongs to a workspace, which the signed-in account does not name.
71 newRepositoryOwner: () => null,
72 defaultCloneTransport: "ssh",
73 changeRequestUrl: ({ host, repository, number }) =>
74 `https://${host}/${repository}/pull-requests/${number}`,
75 // No endpoint reopens a declined pull request, and nothing documented moves one in or out of
76 // draft, so neither is offered rather than failing when pressed.
77 changeRequestActions: new Set(["merge", "close"] as const),
78 // Bitbucket has no checkout CLI, so clone the head branch from its own repository.
79 checkoutCommand: ({ number, headBranch, headRepositoryNameWithOwner }) =>
80 headRepositoryNameWithOwner &&
81 repositoryName.test(headRepositoryNameWithOwner) &&
82 safeShellArgument.test(headBranch)
83 ? `git clone --single-branch --branch ${headBranch} https://bitbucket.org/${headRepositoryNameWithOwner}.git t3code-pr-${number}`
84 : null,
85 authorProfileUrl: () => null,
86 referenceAutolinkRepositoryUrl: () => null,
87 reviewSummaryRequired: () => false,
88 // No checkout CLI, and the reference field never accepted Bitbucket URLs.
89 checkoutCommandArgument: () => null,
90 isChangeRequestReference: () => false,
91 changeRequestUrlHost: (url) => url.hostname,
92 checkoutChangeRequestHost: () => null,
93 isChangeRequestInRepository: (identity, link) =>
94 isChangeRequestInProjectRepository(KIND, identity, link),
95 canReadChangeRequestOnHost: (identity, link) =>
96 isChangeRequestOnProjectHost(KIND, identity, link),
97 isChangeRequestUrl: (url) => isChangeRequestPath(url, "/pull-requests/"),
98 settings,
99});