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

definition.ts

169 lines6.3 KB

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 repositoryUrlFromPagePath,
18 projectChangeRequestHost,
19} from "@t3tools/source-control-core/shared/definition";
20import { parseUrl } from "@t3tools/shared/url";
21import { icon } from "./icon.ts";
22​
23const safeShellArgument = /^[A-Za-z0-9._/@+=,-]+$/;
24const repositoryName = /^[A-Za-z0-9._-]+\/[A-Za-z0-9._-]+$/;
25​
26const CHANGE_REQUEST_PATH = /^\/([^/]+\/[^/]+)\/pull-requests\/(\d+)(?:\/|$)/u;
27const COMMIT_PATH = /^\/([^/]+\/[^/]+)\/commits\/([0-9a-f]+)\/?$/iu;
28const REPOSITORY_PAGE_PATH = /^\/([^/]+\/[^/]+)\/src\/[^/]/u;
29​
30const KIND = SourceControlProviderKind.make("bitbucket");
31​
32// Bitbucket Cloud clones over SSH from these hosts but names every repository on `bitbucket.org`.
33const CLOUD_SSH_HOST_KEY = /^(?:alt)?ssh\.bitbucket\.org\/([^/]+\/[^/]+)$/u;
34​
35function canonicalRepositoryKey(key: string): string | null {
36 const repository = CLOUD_SSH_HOST_KEY.exec(key)?.[1];
37 return repository === undefined ? null : `bitbucket.org/${repository}`;
39​
40function repositoryKeyFromRemote(key: string): string | null {
41 return canonicalRepositoryKey(key)?.replace(/\.git$/u, "") ?? null;
43​
44// Saved identities reach the predicates as written, so a key from before this mapping still matches.
45function withCanonicalKey<Identity extends { readonly canonicalKey: string }>(
46 identity: Identity,
47): Identity {
48 const canonicalKey = canonicalRepositoryKey(identity.canonicalKey);
49 return canonicalKey === null ? identity : { ...identity, canonicalKey };
51​
52function isBitbucketHost(hostname: string): boolean {
53 const host = hostname.toLowerCase();
54 return (
55 host === "bitbucket.org" ||
56 host.endsWith(".bitbucket.org") ||
57 host.split(".").includes("bitbucket")
58 );
60​
61const text = (annotations: Schema.Annotations.Key<string>) =>
62 TrimmedString.pipe(
63 Schema.withDecodingDefault(Effect.succeed("")),
64 Schema.annotateKey(annotations),
65 );
66​
67const secret = (annotations: Schema.Annotations.Key<SettingsSecretValue>) =>
68 SettingsSecretValue.pipe(
69 Schema.withDecodingDefault(Effect.succeed("")),
70 Schema.annotateKey({ ...annotations, providerSettingsForm: { control: "password" } }),
71 );
72​
73export const settings = makeProviderSettingsSchema(
74 {
75 accessToken: secret({
76 title: "Access token",
77 description:
78 "Scoped to one repository, project, or workspace. Create it in that item's Bitbucket settings.",
79 }),
80 email: text({
81 title: "Atlassian account email",
82 description: "With an API token instead of an access token.",
83 providerSettingsForm: { placeholder: "you@example.com" },
84 }),
85 apiToken: secret({
86 title: "API token",
87 description:
88 "Reaches every repository your Atlassian account can. Give it read and write access to repositories and pull requests, plus read:user:bitbucket and read:workspace:bitbucket.",
89 }),
90 },
91 { order: ["accessToken", "email", "apiToken"] },
92);
93export type BitbucketSettings = typeof settings.Type;
94​
95export const definition = defineSourceControl({
96 kind: KIND,
97 label: "Bitbucket",
98 pickerLabel: "Bitbucket",
99 icon,
100 changeRequest: {
101 shortLabel: "PR",
102 shortLabelWithArticle: "a PR",
103 singular: "pull request",
104 plural: "pull requests",
105 },
106 repositoryPathHint: "workspace/repository",
107 publicHost: "bitbucket.org",
108 publishDescription: "bitbucket.org",
109 publishHost: () => "bitbucket.org",
110 // The signed-in account does not identify a workspace.
111 newRepositoryOwner: () => null,
112 defaultCloneTransport: "ssh",
113 changeRequestUrl: ({ host, repository, number, webUrl }) =>
114 webUrl
115 ? changeRequestUrlBelow(webUrl, `pull-requests/${number}`)
116 : `https://${host}/${repository}/pull-requests/${number}`,
117 parseChangeRequestUrl: (url) =>
118 isBitbucketHost(url.hostname) ? parseChangeRequestPath(url, CHANGE_REQUEST_PATH) : null,
119 commitUrl: (input) => buildCommitUrl(input, "commits"),
120 parseCommitUrl: (url) =>
121 isBitbucketHost(url.hostname) ? parseCommitPath(url, COMMIT_PATH) : null,
122 repositoryUrlFromPage: (url) =>
123 isBitbucketHost(url.hostname) ? repositoryUrlFromPagePath(url, REPOSITORY_PAGE_PATH) : null,
124 changeRequestUrlFromReferenceAutolink: () => null,
125 // No endpoint reopens a declined pull request, and nothing documented moves one in or out of
126 // draft, so neither is offered rather than failing when pressed.
127 changeRequestActions: new Set(["merge", "close"] as const),
128 // Bitbucket has no checkout CLI.
129 checkoutCommand: ({ number, headBranch, headRepositoryNameWithOwner }) =>
130 headRepositoryNameWithOwner &&
131 repositoryName.test(headRepositoryNameWithOwner) &&
132 safeShellArgument.test(headBranch)
133 ? `git clone --single-branch --branch ${headBranch} https://bitbucket.org/${headRepositoryNameWithOwner}.git t3code-pr-${number}`
134 : null,
135 authorProfileUrl: () => null,
136 reviewSummaryRequired: () => false,
137 checkoutCommandArgument: () => null,
138 isChangeRequestReference: (reference) => {
139 const url = parseUrl(reference);
140 return (
141 url !== null &&
142 isBitbucketHost(url.hostname) &&
143 parseChangeRequestPath(url, CHANGE_REQUEST_PATH) !== null
144 );
145 },
146 checkoutChangeRequestHost: () => null,
147 // Cloud also clones from `ssh.bitbucket.org` and `altssh.bitbucket.org`.
148 recognizeRemote: ({ host, hostname }) =>
149 hostname === "bitbucket.org" || hostname.endsWith(".bitbucket.org")
150 ? { kind: KIND, name: "Bitbucket", baseUrl: "https://bitbucket.org" }
151 : hostname.split(".").includes("bitbucket")
152 ? { kind: KIND, name: "Bitbucket Self-Hosted", baseUrl: `https://${host}` }
153 : null,
154 repositoryKeyFromRemote,
155 canonicalRepositoryKey,
156 isChangeRequestInRepository: (saved, link) => {
157 const identity = withCanonicalKey(saved);
158 return isChangeRequestInProjectRepository(
159 projectChangeRequestHost(identity, KIND),
160 identity,
161 link,
162 );
163 },
164 canReadChangeRequestOnHost: (saved, link) => {
165 const identity = withCanonicalKey(saved);
166 return isChangeRequestOnProjectHost(projectChangeRequestHost(identity, KIND), identity, link);
167 },
168 settings,
169});