Open History to see the last commit
1import {
2 makeProviderSettingsSchema,
3 SettingsSecretValue,
4 SourceControlProviderKind,
5 TrimmedString,
6} from "@t3tools/contracts";
7import * as Effect from "effect/Effect";
8import * as Schema from "effect/Schema";
9import {
10 buildCommitUrl,
11 changeRequestUrlBelow,
12 defineSourceControl,
13 isChangeRequestInProjectRepository,
14 isChangeRequestOnProjectHost,
15 parseChangeRequestPath,
16 parseCommitPath,
17 projectChangeRequestHost,
18} from "@t3tools/source-control-core/shared/definition";
19import { icon } from "./icon.ts";
20
21const safeShellArgument = /^[A-Za-z0-9._/@+=,-]+$/;
22const repositoryName = /^[A-Za-z0-9._-]+\/[A-Za-z0-9._-]+$/;
23
24const CHANGE_REQUEST_PATH = /^\/([^/]+\/[^/]+)\/pull-requests\/(\d+)(?:\/|$)/u;
25const COMMIT_PATH = /^\/([^/]+\/[^/]+)\/commits\/([0-9a-f]+)\/?$/iu;
26
27const KIND = SourceControlProviderKind.make("bitbucket");
28
29function isBitbucketHost(hostname: string): boolean {
30 const host = hostname.toLowerCase();
31 return (
32 host === "bitbucket.org" ||
33 host.endsWith(".bitbucket.org") ||
34 host.split(".").includes("bitbucket")
35 );
36}
37
38const text = (annotations: Schema.Annotations.Key<string>) =>
39 TrimmedString.pipe(
40 Schema.withDecodingDefault(Effect.succeed("")),
41 Schema.annotateKey(annotations),
42 );
43
44const secret = (annotations: Schema.Annotations.Key<SettingsSecretValue>) =>
45 SettingsSecretValue.pipe(
46 Schema.withDecodingDefault(Effect.succeed("")),
47 Schema.annotateKey({ ...annotations, providerSettingsForm: { control: "password" } }),
48 );
49
50export const settings = makeProviderSettingsSchema(
51 {
52 accessToken: secret({
53 title: "Access token",
54 description:
55 "Scoped to one repository, project, or workspace. Create it in that item's Bitbucket settings.",
56 }),
57 email: text({
58 title: "Atlassian account email",
59 description: "With an API token instead of an access token.",
60 providerSettingsForm: { placeholder: "you@example.com" },
61 }),
62 apiToken: secret({
63 title: "API token",
64 description:
65 "Reaches every repository your Atlassian account can. Give it read and write access to repositories and pull requests, and read:user:bitbucket.",
66 }),
67 },
68 { order: ["accessToken", "email", "apiToken"] },
69);
70export type BitbucketSettings = typeof settings.Type;
71
72export const definition = defineSourceControl({
73 kind: KIND,
74 label: "Bitbucket",
75 pickerLabel: "Bitbucket",
76 icon,
77 changeRequest: {
78 shortLabel: "PR",
79 shortLabelWithArticle: "a PR",
80 singular: "pull request",
81 plural: "pull requests",
82 },
83 repositoryPathHint: "workspace/repository",
84 publicHost: "bitbucket.org",
85 publishDescription: "bitbucket.org",
86 publishHost: () => "bitbucket.org",
87 // The signed-in account does not identify a workspace.
88 newRepositoryOwner: () => null,
89 defaultCloneTransport: "ssh",
90 changeRequestUrl: ({ host, repository, number, webUrl }) =>
91 webUrl
92 ? changeRequestUrlBelow(webUrl, `pull-requests/${number}`)
93 : `https://${host}/${repository}/pull-requests/${number}`,
94 parseChangeRequestUrl: (url) =>
95 isBitbucketHost(url.hostname) ? parseChangeRequestPath(url, CHANGE_REQUEST_PATH) : null,
96 commitUrl: (input) => buildCommitUrl(input, "commits"),
97 parseCommitUrl: (url) =>
98 isBitbucketHost(url.hostname) ? parseCommitPath(url, COMMIT_PATH) : null,
99 changeRequestUrlFromReferenceAutolink: () => null,
100 // No endpoint reopens a declined pull request, and nothing documented moves one in or out of
101 // draft, so neither is offered rather than failing when pressed.
102 changeRequestActions: new Set(["merge", "close"] as const),
103 // Bitbucket has no checkout CLI.
104 checkoutCommand: ({ number, headBranch, headRepositoryNameWithOwner }) =>
105 headRepositoryNameWithOwner &&
106 repositoryName.test(headRepositoryNameWithOwner) &&
107 safeShellArgument.test(headBranch)
108 ? `git clone --single-branch --branch ${headBranch} https://bitbucket.org/${headRepositoryNameWithOwner}.git t3code-pr-${number}`
109 : null,
110 authorProfileUrl: () => null,
111 reviewSummaryRequired: () => false,
112 checkoutCommandArgument: () => null,
113 isChangeRequestReference: (reference) => {
114 const url = URL.parse(reference);
115 return (
116 url !== null &&
117 isBitbucketHost(url.hostname) &&
118 parseChangeRequestPath(url, CHANGE_REQUEST_PATH) !== null
119 );
120 },
121 checkoutChangeRequestHost: () => null,
122 recognizeRemote: ({ host, hostname }) =>
123 hostname === "bitbucket.org" || hostname.split(".").includes("bitbucket")
124 ? {
125 kind: KIND,
126 name: hostname === "bitbucket.org" ? "Bitbucket" : "Bitbucket Self-Hosted",
127 baseUrl: `https://${host}`,
128 }
129 : null,
130 isChangeRequestInRepository: (identity, link) =>
131 isChangeRequestInProjectRepository(projectChangeRequestHost(identity, KIND), identity, link),
132 canReadChangeRequestOnHost: (identity, link) =>
133 isChangeRequestOnProjectHost(projectChangeRequestHost(identity, KIND), identity, link),
134 settings,
135});