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

definition.ts

153 lines6.4 KB

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