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

definition.ts

98 lines4.2 KB

1import { SourceControlProviderKind } from "@t3tools/contracts";
2import {
3 type ChangeRequestProjectIdentity,
4 defineSourceControlClient,
5 isChangeRequestAuthorityOfProject,
6 isChangeRequestInProjectRepository,
7 isChangeRequestOnProjectHost,
8 isChangeRequestPath,
9} from "@t3tools/source-control-core/client/definition";
10​
11const CHECKOUT_COMMAND = /^tea\s+(?:pr|pulls)\s+checkout\s+(.+)$/i;
12const CHANGE_REQUEST_REFERENCE =
13 /^https?:\/\/[^/\s]+\/(?:[^/\s]+\/)+[^/\s]+\/pulls\/(\d+)(?:[/?#].*)?$/i;
14​
15const KIND = SourceControlProviderKind.make("forgejo");
16​
17/**
18 * The repository's browser URL the server resolved from the signed-in account, which is the
19 * only place a Forgejo mounted below a path says so.
20 */
21function resolvedWebUrl(identity: ChangeRequestProjectIdentity): URL | null {
22 if (!identity.webUrl) return null;
23 try {
24 const url = new URL(identity.webUrl);
25 return url.protocol === "http:" || url.protocol === "https:" ? url : null;
26 } catch {
27 return null;
28 }
30​
31export const definition = defineSourceControlClient({
32 kind: KIND,
33 label: "Forgejo",
34 pickerLabel: "Forgejo / Gitea",
35 icon: "forgejo",
36 changeRequest: { shortLabel: "PR", singular: "pull request" },
37 repositoryPathHint: "owner/repo",
38 // Forgejo and Gitea are self-hosted, so no hostname names them.
39 publicHost: null,
40 publishDescription: "Your signed-in server",
41 publishHost: (signedInHost) => signedInHost ?? "your server",
42 newRepositoryOwner: (account) => (account ? { owner: account } : null),
43 defaultCloneTransport: "https",
44 // The server's resolved web URL wins; otherwise an HTTP remote on the same host names the
45 // origin, which may carry a port. SSH remotes say nothing about the web origin.
46 changeRequestUrl: ({ host, repository, number, remoteUrl, webUrl }) => {
47 if (webUrl) return `${webUrl.replace(/\/+$/, "")}/pulls/${number}`;
48 try {
49 const remote = new URL(remoteUrl ?? "");
50 if (
51 (remote.protocol === "http:" || remote.protocol === "https:") &&
52 (remote.hostname.toLowerCase() === host.toLowerCase() ||
53 remote.host.toLowerCase() === host.toLowerCase())
54 ) {
55 return `${remote.origin}/${repository}/pulls/${number}`;
56 }
57 } catch {}
58 return `https://${host}/${repository}/pulls/${number}`;
59 },
60 changeRequestActions: new Set(["merge", "close", "reopen", "update-branch"] as const),
61 // Neither `fj` nor `tea` checks out by number, so fetch the pull ref from the repository itself.
62 checkoutCommand: ({ number, repositoryUrl }) =>
63 repositoryUrl
64 ? `git fetch '${repositoryUrl.replaceAll("'", "'\\''")}' refs/pull/${number}/head && git checkout -B pulls/${number} FETCH_HEAD`
65 : null,
66 authorProfileUrl: () => null,
67 // Forgejo refuses a change request review without a summary, even with inline comments.
68 referenceAutolinkRepositoryUrl: () => null,
69 reviewSummaryRequired: (verdict) => verdict === "request-changes",
70 checkoutCommandArgument: (input) => CHECKOUT_COMMAND.exec(input)?.[1]?.trim() ?? null,
71 isChangeRequestReference: (url) => CHANGE_REQUEST_REFERENCE.test(url),
72 changeRequestUrlHost: (url) => url.host,
73 checkoutChangeRequestHost: () => null,
74 isChangeRequestInRepository: (identity, link) => {
75 if (!isChangeRequestAuthorityOfProject(identity, link)) return false;
76 const web = resolvedWebUrl(identity);
77 if (!web) return isChangeRequestInProjectRepository(KIND, identity, link);
78 return (
79 web.host.toLowerCase() === (link.authority ?? link.host).toLowerCase() &&
80 web.pathname.replace(/^\/+|\/+$/g, "").toLowerCase() === link.repository.toLowerCase()
81 );
82 },
83 // A server mounted below a path serves only repositories under that mount.
84 canReadChangeRequestOnHost: (identity, link) => {
85 const web = resolvedWebUrl(identity);
86 if (!web) return isChangeRequestOnProjectHost(KIND, identity, link);
87 const mount = web.pathname
88 .replace(/^\/+|\/+$/g, "")
89 .split("/")
90 .slice(0, -2)
91 .join("/");
92 return (
93 web.host.toLowerCase() === (link.authority ?? link.host).toLowerCase() &&
94 (!mount || link.repository.toLowerCase().startsWith(`${mount.toLowerCase()}/`))
95 );
96 },
97 isChangeRequestUrl: (url) => isChangeRequestPath(url, "/pulls/"),
98});