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

definition.ts

113 lines3.8 KB

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