@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 164 lines 6.8 KB
Raw
1 import { SourceControlProviderKind } from "@t3tools/contracts";
2 import { canonicalRepositoryPath } from "@t3tools/shared/git";
3 import {
4 buildCommitUrl,
5 changeRequestUrlBelow,
6 type ChangeRequestHostIdentity,
7 defineSourceControl,
8 isChangeRequestAuthorityOfProject,
9 isChangeRequestInProjectRepository,
10 isChangeRequestOnProjectHost,
11 parseChangeRequestPath,
12 parseIssuePath,
13 parseCommitPath,
14 repositoryUrlFromPagePath,
15 projectChangeRequestHost,
16 remoteWebOrigin,
17 } from "@t3tools/source-control-core/shared/definition";
18 import { icon } from "./icon.ts";
19
20 const CHECKOUT_COMMAND = /^tea\s+(?:pr|pulls?)\s+(?:checkout|co)\s+(.+)$/i;
21 const CHANGE_REQUEST_REFERENCE =
22 /^https?:\/\/[^/\s]+\/(?:[^/\s]+\/)+[^/\s]+\/pulls\/(\d+)(?:[/?#].*)?$/i;
23
24 // Forgejo and Gitea run on any hostname, possibly mounted below a path.
25 const CHANGE_REQUEST_PATH = /^\/([^/]+(?:\/[^/]+)+)\/pulls\/(\d+)(?:\/|$)/u;
26 const COMMIT_PATH = /^\/([^/]+(?:\/[^/]+)+)\/commit\/([0-9a-f]+)\/?$/iu;
27 const ISSUE_PATH = /^\/([^/]+(?:\/[^/]+)+)\/issues\/(\d+)(?:\/|$)/u;
28 const REPOSITORY_PAGE_PATH = /^\/([^/]+(?:\/[^/]+)+?)\/src\/(?:branch|tag|commit)\/[^/]/u;
29
30 const KIND = SourceControlProviderKind.make("forgejo");
31
32 function isForgejoHost(hostname: string): boolean {
33 const host = hostname.toLowerCase();
34 return (
35 host === "codeberg.org" ||
36 host.split(".").includes("forgejo") ||
37 host.split(".").includes("gitea")
38 );
39 }
40
41 const changeRequestHeadRef = (number: number) => `refs/pull/${number}/head`;
42
43 /**
44 * The repository's browser URL the server resolved from the signed-in account, which is the
45 * only place a Forgejo mounted below a path says so.
46 */
47 function resolvedWebUrl(identity: ChangeRequestHostIdentity): URL | null {
48 if (!identity?.webUrl) return null;
49 try {
50 const url = new URL(identity.webUrl);
51 return url.protocol === "http:" || url.protocol === "https:" ? url : null;
52 } catch {
53 return null;
54 }
55 }
56
57 /** A server's identity includes its port, which only its web URL or an HTTP remote names. */
58 function changeRequestHost(identity: ChangeRequestHostIdentity): string {
59 const web = resolvedWebUrl(identity);
60 if (web) return web.host.toLowerCase();
61 try {
62 const remote = new URL(identity?.locator?.remoteUrl ?? "");
63 if (remote.protocol === "http:" || remote.protocol === "https:") {
64 return remote.host.toLowerCase();
65 }
66 } catch {
67 // SSH remotes do not name the web server.
68 }
69 return projectChangeRequestHost(identity, KIND);
70 }
71
72 export const definition = defineSourceControl({
73 kind: KIND,
74 label: "Forgejo",
75 pickerLabel: "Forgejo / Gitea",
76 icon,
77 searchKeywords: ["gitea", "tea", "codeberg"],
78 changeRequest: {
79 shortLabel: "PR",
80 shortLabelWithArticle: "a PR",
81 singular: "pull request",
82 plural: "pull requests",
83 },
84 repositoryPathHint: "owner/repo",
85 // Codeberg is the public Forgejo; any other server is self-hosted.
86 publicHost: "codeberg.org",
87 publishDescription: "Your signed-in server",
88 publishHost: (signedInHost) => signedInHost ?? "your server",
89 newRepositoryOwner: (account) => (account ? { owner: account } : null),
90 defaultCloneTransport: "https",
91 // The server's resolved web URL wins; otherwise an HTTP remote on the same host names the
92 // origin, which may carry a port. SSH remotes say nothing about the web origin.
93 changeRequestUrl: ({ host, repository, number, remoteUrl, webUrl }) =>
94 webUrl
95 ? changeRequestUrlBelow(webUrl, `pulls/${number}`)
96 : `${remoteWebOrigin(host, remoteUrl)}/${repository}/pulls/${number}`,
97 // A server's identity includes its port, so the link carries it.
98 parseChangeRequestUrl: (url) => {
99 const link = parseChangeRequestPath(url, CHANGE_REQUEST_PATH);
100 return link === null ? null : { ...link, authority: url.host.toLowerCase() };
101 },
102 // `/issues/` is too generic a path to believe from a host not named after Forgejo or Gitea.
103 parseIssueUrl: (url) => (isForgejoHost(url.hostname) ? parseIssuePath(url, ISSUE_PATH) : null),
104 changeRequestUrlFromReferenceAutolink: () => null,
105 commitUrl: (input) => buildCommitUrl(input, "commit"),
106 parseCommitUrl: (url) => parseCommitPath(url, COMMIT_PATH),
107 repositoryUrlFromPage: (url, known) => {
108 // A mount path may itself contain `src/branch/`, so a `.git` path is always the clone URL.
109 if (url.pathname.replace(/\/$/u, "").endsWith(".git")) return null;
110 const repositoryUrl = repositoryUrlFromPagePath(url, REPOSITORY_PAGE_PATH);
111 // Elsewhere the same path is a GitLab project nested under groups named `src` and `branch`.
112 return repositoryUrl === null || known ? repositoryUrl : url.href;
113 },
114 changeRequestActions: new Set(["merge", "close", "reopen", "update-branch"] as const),
115 // Neither `fj` nor `tea` checks out by number.
116 checkoutCommand: ({ number, repositoryUrl }) =>
117 repositoryUrl
118 ? `git fetch '${repositoryUrl.replaceAll("'", "'\\''")}' ${changeRequestHeadRef(number)} && git checkout -B pulls/${number} FETCH_HEAD`
119 : null,
120 // tea also takes `pull` for `pulls` and `m` for `merge`.
121 changeRequestCloseCommand: /\b(?:tea\s+(?:pr|pulls?)|fj\s+pr)\s+(?:merge|m|close)\b/u,
122 authorProfileUrl: () => null,
123 hardLineBreaks: true,
124 // Forgejo refuses a change request review without a summary, even with inline comments.
125 reviewSummaryRequired: (verdict) => verdict === "request-changes",
126 checkoutCommandArgument: (input) => CHECKOUT_COMMAND.exec(input)?.[1]?.trim() ?? null,
127 isChangeRequestReference: (url) => CHANGE_REQUEST_REFERENCE.test(url),
128 checkoutChangeRequestHost: () => null,
129 changeRequestHeadRef,
130 recognizeRemote: ({ remoteUrl, host, hostname }) =>
131 isForgejoHost(hostname)
132 ? {
133 kind: KIND,
134 name: "Forgejo",
135 baseUrl: /^https?:/iu.test(remoteUrl) ? new URL(remoteUrl).origin : `https://${host}`,
136 }
137 : null,
138 changeRequestHost,
139 isChangeRequestInRepository: (identity, link) => {
140 if (!isChangeRequestAuthorityOfProject(identity, link)) return false;
141 const web = resolvedWebUrl(identity);
142 if (!web)
143 return isChangeRequestInProjectRepository(changeRequestHost(identity), identity, link);
144 return (
145 web.host.toLowerCase() === (link.authority ?? link.host).toLowerCase() &&
146 canonicalRepositoryPath(web.pathname.replace(/^\/+|\/+$/g, "")) ===
147 canonicalRepositoryPath(link.repository)
148 );
149 },
150 // A server mounted below a path serves only repositories under that mount.
151 canReadChangeRequestOnHost: (identity, link) => {
152 const web = resolvedWebUrl(identity);
153 if (!web) return isChangeRequestOnProjectHost(changeRequestHost(identity), identity, link);
154 const mount = web.pathname
155 .replace(/^\/+|\/+$/g, "")
156 .split("/")
157 .slice(0, -2)
158 .join("/");
159 return (
160 web.host.toLowerCase() === (link.authority ?? link.host).toLowerCase() &&
161 (!mount || link.repository.toLowerCase().startsWith(`${mount.toLowerCase()}/`))
162 );
163 },
164 });