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

definition.ts

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