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