Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

definition.ts

119 lines4.3 KB

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