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