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

definition.ts

115 lines3.8 KB

1import {
2 makeProviderSettingsSchema,
3 secretSetting,
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​
21/** GitCafe has no self-hosted installs: production and staging are its only hosts. */
22const GITCAFE_HOSTS = new Set(["git.cafe", "staging.git.cafe"]);
23​
24/** GitCafe serves pull requests at `/pull/<n>` and redirects the older `/pulls/<n>` there. */
25const CHANGE_REQUEST_PATH = /^\/([^/]+\/[^/]+)\/pulls?\/(\d+)(?:\/|$)/u;
26const COMMIT_PATH = /^\/([^/]+\/[^/]+)\/commit\/([0-9a-f]+)\/?$/iu;
27​
28function parseChangeRequestUrl(url: URL) {
29 return GITCAFE_HOSTS.has(url.hostname.toLowerCase())
30 ? parseChangeRequestPath(url, CHANGE_REQUEST_PATH)
31 : null;
33​
34const KIND = SourceControlProviderKind.make("gitcafe");
35​
36export const settings = makeProviderSettingsSchema({
37 token: TrimmedString.pipe(
38 secretSetting,
39 Schema.withDecodingDefault(Effect.succeed("")),
40 Schema.annotateKey({
41 title: "Token",
42 description:
43 "A git.cafe token, used before CAFE_TOKEN and the cafe login, so GitCafe works without the CLI signed in.",
44 providerSettingsForm: { control: "password" },
45 }),
46 ),
47});
48export type GitCafeSettings = typeof settings.Type;
49​
50export const definition = defineSourceControl({
51 kind: KIND,
52 label: "GitCafe",
53 pickerLabel: "GitCafe",
54 icon,
55 changeRequest: {
56 shortLabel: "PR",
57 shortLabelWithArticle: "a PR",
58 singular: "pull request",
59 plural: "pull requests",
60 },
61 repositoryPathHint: "owner/repo",
62 publicHost: "git.cafe",
63 publishDescription: "git.cafe",
64 publishHost: () => "git.cafe",
65 newRepositoryOwner: (account) => (account ? { owner: account } : null),
66 defaultCloneTransport: "ssh",
67 changeRequestUrl: ({ host, repository, number, webUrl }) =>
68 webUrl
69 ? changeRequestUrlBelow(webUrl, `pulls/${number}`)
70 : `https://${host}/${repository}/pulls/${number}`,
71 // `/pull/` and `/pulls/` are GitHub's and Forgejo's shapes too, so only GitCafe's own hosts
72 // claim them.
73 parseChangeRequestUrl,
74 commitUrl: (input) => buildCommitUrl(input, "commit"),
75 parseCommitUrl: (url) =>
76 GITCAFE_HOSTS.has(url.hostname.toLowerCase()) ? parseCommitPath(url, COMMIT_PATH) : null,
77 changeRequestUrlFromReferenceAutolink: () => null,
78 changeRequestActions: new Set([
79 "ready",
80 "draft",
81 "close",
82 "reopen",
83 "merge",
84 "update-branch",
85 ] as const),
86 checkoutCommand: ({ number }) => `cafe pr checkout ${number}`,
87 changeRequestCloseCommand: /\bcafe\s+(?:pr|stack)\s+(?:merge|close)\b/u,
88 authorProfileUrl: () => null,
89 reviewSummaryRequired: () => false,
90 checkoutCommandArgument: () => null,
91 isChangeRequestReference: (url) => parsePullRequestUrl(url) !== null,
92 checkoutChangeRequestHost: () => null,
93 recognizeRemote: ({ hostname }) =>
94 GITCAFE_HOSTS.has(hostname)
95 ? { kind: KIND, name: "GitCafe", baseUrl: `https://${hostname}` }
96 : null,
97 isChangeRequestInRepository: (identity, link) =>
98 isChangeRequestInProjectRepository(projectChangeRequestHost(identity, KIND), identity, link),
99 canReadChangeRequestOnHost: (identity, link) =>
100 isChangeRequestOnProjectHost(projectChangeRequestHost(identity, KIND), identity, link),
101 settings,
102});
103​
104export function parsePullRequestUrl(value: string) {
105 try {
106 return parseChangeRequestUrl(new URL(value));
107 } catch {
108 return null;
109 }
111​
112export function canonicalizePullRequestUrl(value: string): string {
113 const link = parsePullRequestUrl(value);
114 return link === null ? value : definition.changeRequestUrl(link);