The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { |
| 2 | makeProviderSettingsSchema, |
| 3 | SettingsSecretValue, |
| 4 | SourceControlProviderKind, |
| 5 | } from "@t3tools/contracts"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as Schema from "effect/Schema"; |
| 8 | import { |
| 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"; |
| 19 | import { icon } from "./icon.ts"; |
| 20 | |
| 21 | /** GitCafe has no self-hosted installs: production and staging are its only hosts. */ |
| 22 | const 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. */ |
| 25 | const CHANGE_REQUEST_PATH = /^\/([^/]+\/[^/]+)\/pulls?\/(\d+)(?:\/|$)/u; |
| 26 | const COMMIT_PATH = /^\/([^/]+\/[^/]+)\/commit\/([0-9a-f]+)\/?$/iu; |
| 27 | const REPOSITORY_PAGE_PATH = /^\/([^/]+\/[^/]+)\/(?:tree|blob)\/[^/]/u; |
| 28 | |
| 29 | function parseChangeRequestUrl(url: URL) { |
| 30 | return GITCAFE_HOSTS.has(url.hostname.toLowerCase()) |
| 31 | ? parseChangeRequestPath(url, CHANGE_REQUEST_PATH) |
| 32 | : null; |
| 33 | } |
| 34 | |
| 35 | const KIND = SourceControlProviderKind.make("gitcafe"); |
| 36 | |
| 37 | export const settings = makeProviderSettingsSchema({ |
| 38 | token: SettingsSecretValue.pipe( |
| 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 | }); |
| 48 | export type GitCafeSettings = typeof settings.Type; |
| 49 | |
| 50 | export 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 | repositoryUrlFromPage: (url) => |
| 78 | GITCAFE_HOSTS.has(url.hostname.toLowerCase()) |
| 79 | ? repositoryUrlFromPagePath(url, REPOSITORY_PAGE_PATH) |
| 80 | : null, |
| 81 | changeRequestUrlFromReferenceAutolink: () => null, |
| 82 | changeRequestActions: new Set([ |
| 83 | "ready", |
| 84 | "draft", |
| 85 | "close", |
| 86 | "reopen", |
| 87 | "merge", |
| 88 | "update-branch", |
| 89 | ] as const), |
| 90 | checkoutCommand: ({ number }) => `cafe pr checkout ${number}`, |
| 91 | changeRequestCloseCommand: /\bcafe\s+(?:pr|stack)\s+(?:merge|close)\b/u, |
| 92 | authorProfileUrl: () => null, |
| 93 | reviewSummaryRequired: () => false, |
| 94 | checkoutCommandArgument: () => null, |
| 95 | isChangeRequestReference: (url) => parsePullRequestUrl(url) !== null, |
| 96 | checkoutChangeRequestHost: () => null, |
| 97 | recognizeRemote: ({ hostname }) => |
| 98 | GITCAFE_HOSTS.has(hostname) |
| 99 | ? { kind: KIND, name: "GitCafe", baseUrl: `https://${hostname}` } |
| 100 | : null, |
| 101 | isChangeRequestInRepository: (identity, link) => |
| 102 | isChangeRequestInProjectRepository(projectChangeRequestHost(identity, KIND), identity, link), |
| 103 | canReadChangeRequestOnHost: (identity, link) => |
| 104 | isChangeRequestOnProjectHost(projectChangeRequestHost(identity, KIND), identity, link), |
| 105 | settings, |
| 106 | }); |
| 107 | |
| 108 | export function parsePullRequestUrl(value: string) { |
| 109 | try { |
| 110 | return parseChangeRequestUrl(new URL(value)); |
| 111 | } catch { |
| 112 | return null; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | export function canonicalizePullRequestUrl(value: string): string { |
| 117 | const link = parsePullRequestUrl(value); |
| 118 | return link === null ? value : definition.changeRequestUrl(link); |
| 119 | } |