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

definition.ts

134 lines5.4 KB

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