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

definition.ts

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