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

definition.ts

141 lines5.7 KB

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 }
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);
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 // tea also takes `pull` for `pulls` and `m` for `merge`.
97 changeRequestCloseCommand: /\b(?:tea\s+(?:pr|pulls?)|fj\s+pr)\s+(?:merge|m|close)\b/u,
98 authorProfileUrl: () => null,
99 hardLineBreaks: true,
100 // Forgejo refuses a change request review without a summary, even with inline comments.
101 reviewSummaryRequired: (verdict) => verdict === "request-changes",
102 checkoutCommandArgument: (input) => CHECKOUT_COMMAND.exec(input)?.[1]?.trim() ?? null,
103 isChangeRequestReference: (url) => CHANGE_REQUEST_REFERENCE.test(url),
104 checkoutChangeRequestHost: () => null,
105 changeRequestHeadRef,
106 recognizeRemote: ({ remoteUrl, host, hostname }) =>
107 hostname === "codeberg.org" ||
108 hostname.split(".").includes("forgejo") ||
109 hostname.split(".").includes("gitea")
110 ? {
111 kind: KIND,
112 name: "Forgejo",
113 baseUrl: /^https?:/iu.test(remoteUrl) ? new URL(remoteUrl).origin : `https://${host}`,
114 }
115 : null,
116 changeRequestHost,
117 isChangeRequestInRepository: (identity, link) => {
118 if (!isChangeRequestAuthorityOfProject(identity, link)) return false;
119 const web = resolvedWebUrl(identity);
120 if (!web)
121 return isChangeRequestInProjectRepository(changeRequestHost(identity), identity, link);
122 return (
123 web.host.toLowerCase() === (link.authority ?? link.host).toLowerCase() &&
124 web.pathname.replace(/^\/+|\/+$/g, "").toLowerCase() === link.repository.toLowerCase()
125 );
126 },
127 // A server mounted below a path serves only repositories under that mount.
128 canReadChangeRequestOnHost: (identity, link) => {
129 const web = resolvedWebUrl(identity);
130 if (!web) return isChangeRequestOnProjectHost(changeRequestHost(identity), identity, link);
131 const mount = web.pathname
132 .replace(/^\/+|\/+$/g, "")
133 .split("/")
134 .slice(0, -2)
135 .join("/");
136 return (
137 web.host.toLowerCase() === (link.authority ?? link.host).toLowerCase() &&
138 (!mount || link.repository.toLowerCase().startsWith(`${mount.toLowerCase()}/`))
139 );
140 },
141});