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