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

definition.ts

133 lines4.6 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 repositoryUrlFromPagePath,
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;
27const REPOSITORY_PAGE_PATH = /^\/([^/]+\/[^/]+)\/(?:tree|blob)\/[^/]/u;
28​
29function parseChangeRequestUrl(url: URL) {
30 return GITCAFE_HOSTS.has(url.hostname.toLowerCase())
31 ? parseChangeRequestPath(url, CHANGE_REQUEST_PATH)
32 : null;
34​
35const KIND = SourceControlProviderKind.make("gitcafe");
36​
37export function decodeGitCafePath(path: string): string | null {
38 try {
39 const segments = path.split("/").map(decodeURIComponent);
40 return segments.some((segment) => /[\s/]/u.test(segment)) ? null : segments.join("/");
41 } catch {
42 return null;
43 }
45​
46export const settings = makeProviderSettingsSchema({
47 token: SettingsSecretValue.pipe(
48 Schema.withDecodingDefault(Effect.succeed("")),
49 Schema.annotateKey({
50 title: "Token",
51 description:
52 "A git.cafe token, used before CAFE_TOKEN and the cafe login, so GitCafe works without the CLI signed in.",
53 providerSettingsForm: { control: "password" },
54 }),
55 ),
56});
57export type GitCafeSettings = typeof settings.Type;
58​
59export const definition = defineSourceControl({
60 kind: KIND,
61 label: "GitCafe",
62 pickerLabel: "GitCafe",
63 icon,
64 changeRequest: {
65 shortLabel: "PR",
66 shortLabelWithArticle: "a PR",
67 singular: "pull request",
68 plural: "pull requests",
69 },
70 repositoryPathHint: "owner/repo",
71 // Escaped and plain spellings of one remote must select, and list, the same repository.
72 repositorySelector: ({ displayName, owner, name }) => {
73 const path = displayName || (owner && name ? `${owner}/${name}` : "");
74 return decodeGitCafePath(path)?.replace(/\.git$/u, "") || null;
75 },
76 publicHost: "git.cafe",
77 publishDescription: "git.cafe",
78 publishHost: () => "git.cafe",
79 newRepositoryOwner: (account) => (account ? { owner: account } : null),
80 defaultCloneTransport: "ssh",
81 changeRequestUrl: ({ host, repository, number, webUrl }) =>
82 webUrl
83 ? changeRequestUrlBelow(webUrl, `pulls/${number}`)
84 : `https://${host}/${repository}/pulls/${number}`,
85 // `/pull/` and `/pulls/` are GitHub's and Forgejo's shapes too, so only GitCafe's own hosts
86 // claim them.
87 parseChangeRequestUrl,
88 commitUrl: (input) => buildCommitUrl(input, "commit"),
89 parseCommitUrl: (url) =>
90 GITCAFE_HOSTS.has(url.hostname.toLowerCase()) ? parseCommitPath(url, COMMIT_PATH) : null,
91 repositoryUrlFromPage: (url) =>
92 GITCAFE_HOSTS.has(url.hostname.toLowerCase())
93 ? repositoryUrlFromPagePath(url, REPOSITORY_PAGE_PATH)
94 : null,
95 changeRequestUrlFromReferenceAutolink: () => null,
96 changeRequestActions: new Set([
97 "ready",
98 "draft",
99 "close",
100 "reopen",
101 "merge",
102 "update-branch",
103 ] as const),
104 checkoutCommand: ({ number }) => `cafe pr checkout ${number}`,
105 changeRequestCloseCommand: /\bcafe\s+(?:pr|stack)\s+(?:merge|close)\b/u,
106 authorProfileUrl: () => null,
107 reviewSummaryRequired: () => false,
108 checkoutCommandArgument: () => null,
109 isChangeRequestReference: (url) => parsePullRequestUrl(url) !== null,
110 checkoutChangeRequestHost: () => null,
111 recognizeRemote: ({ hostname }) =>
112 GITCAFE_HOSTS.has(hostname)
113 ? { kind: KIND, name: "GitCafe", baseUrl: `https://${hostname}` }
114 : null,
115 isChangeRequestInRepository: (identity, link) =>
116 isChangeRequestInProjectRepository(projectChangeRequestHost(identity, KIND), identity, link),
117 canReadChangeRequestOnHost: (identity, link) =>
118 isChangeRequestOnProjectHost(projectChangeRequestHost(identity, KIND), identity, link),
119 settings,
120});
121​
122export function parsePullRequestUrl(value: string) {
123 try {
124 return parseChangeRequestUrl(new URL(value));
125 } catch {
126 return null;
127 }
129​
130export function canonicalizePullRequestUrl(value: string): string {
131 const link = parsePullRequestUrl(value);
132 return link === null ? value : definition.changeRequestUrl(link);