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

definition.ts

142 lines5.2 KB

1import { makeProviderSettingsSchema, SourceControlProviderKind } from "@t3tools/contracts";
2import {
3 buildCommitUrl,
4 changeRequestUrlBelow,
5 defineSourceControl,
6 hostCredentialSettingsFields,
7 isChangeRequestInProjectRepository,
8 isChangeRequestOnProjectHost,
9 parseChangeRequestPath,
10 parseCommitPath,
11 parseIssuePath,
12 projectChangeRequestHost,
13 repositoryUrlFromPagePath,
14} from "@t3tools/source-control-core/shared/definition";
15​
16import { icon } from "./icon.ts";
17​
18export const TANSTACK_SOURCE_HOST = "source.tanstack.com";
19​
20/** Every web page and clone URL names the owner with a leading `@`, as `/@TanStack/router`. */
21const CHANGE_REQUEST_PATH = /^\/(?:@|%40)?([^/@%]+\/[^/]+)\/pull\/(\d+)(?:\/|$)/u;
22const ISSUE_PATH = /^\/(?:@|%40)?([^/@%]+\/[^/]+)\/issues\/(\d+)(?:\/|$)/u;
23const COMMIT_PATH = /^\/(?:@|%40)?([^/@%]+\/[^/]+)\/commit\/([0-9a-f]+)\/?$/iu;
24const REPOSITORY_PAGE_PATH = /^\/(@[^/@]+\/[^/]+)\/(?:tree|blob)\/[^/]/u;
25const CHECKOUT_COMMAND = /^src\s+pr\s+checkout\s+(.+)$/iu;
26​
27const KIND = SourceControlProviderKind.make("tanstack-source");
28​
29const isTanStackSourceHost = (hostname: string) => hostname.toLowerCase() === TANSTACK_SOURCE_HOST;
30​
31function parseChangeRequestUrl(url: URL) {
32 const link = isTanStackSourceHost(url.hostname)
33 ? parseChangeRequestPath(url, CHANGE_REQUEST_PATH)
34 : null;
35 return link === null
36 ? null
37 : { ...link, repositoryUrl: `${url.origin}/@${CHANGE_REQUEST_PATH.exec(url.pathname)![1]}` };
39​
40/** Keys drop the owner's `@`, so a project's repository is `owner/name` as the API names it. */
41function repositoryKeyFromRemote(key: string): string | null {
42 // Remote parsing spells the `@` percent-encoded.
43 const match = /^source\.tanstack\.com\/(?:@|%40)([^/@%]+\/[^/]+)$/u.exec(key);
44 return match?.[1] === undefined
45 ? null
46 : `${TANSTACK_SOURCE_HOST}/${match[1].replace(/\.git$/u, "")}`;
48​
49export const settings = makeProviderSettingsSchema(hostCredentialSettingsFields);
50export type TanStackSourceSettings = typeof settings.Type;
51​
52export const definition = defineSourceControl({
53 kind: KIND,
54 label: "TanStack Source",
55 pickerLabel: "TanStack Source",
56 icon,
57 searchKeywords: ["tanstack", "src"],
58 credentials: {
59 cliName: "src",
60 cliLabel: "TanStack Source CLI",
61 loginCommand: "src auth login --web",
62 tokenEnvVar: "SRC_TOKEN",
63 tokenAccess: "read and write access to repositories, issues and pull requests",
64 newTokenUrl: (host) => `https://${host}/settings/tokens`,
65 },
66 changeRequest: {
67 shortLabel: "PR",
68 shortLabelWithArticle: "a PR",
69 singular: "pull request",
70 plural: "pull requests",
71 },
72 repositoryPathHint: "owner/repo",
73 publicHost: TANSTACK_SOURCE_HOST,
74 publishDescription: TANSTACK_SOURCE_HOST,
75 publishHost: () => TANSTACK_SOURCE_HOST,
76 newRepositoryOwner: (account) => ({ owner: account }),
77 defaultCloneTransport: "https",
78 changeRequestUrl: ({ host, repository, number, webUrl }) =>
79 !Number.isSafeInteger(number) || number < 1
80 ? null
81 : webUrl
82 ? changeRequestUrlBelow(webUrl, `pull/${number}`)
83 : `https://${host}/@${repository.replace(/^@/u, "")}/pull/${number}`,
84 parseChangeRequestUrl,
85 commitUrl: (input) =>
86 buildCommitUrl(
87 input.webUrl
88 ? input
89 : { ...input, webUrl: `https://${input.host}/@${input.repository.replace(/^@/u, "")}` },
90 "commit",
91 ),
92 parseIssueUrl: (url) =>
93 isTanStackSourceHost(url.hostname) ? parseIssuePath(url, ISSUE_PATH) : null,
94 parseCommitUrl: (url) =>
95 isTanStackSourceHost(url.hostname) ? parseCommitPath(url, COMMIT_PATH) : null,
96 repositoryUrlFromPage: (url) =>
97 isTanStackSourceHost(url.hostname)
98 ? repositoryUrlFromPagePath(url, REPOSITORY_PAGE_PATH)
99 : null,
100 changeRequestUrlFromReferenceAutolink: () => null,
101 changeRequestActions: new Set([
102 "merge",
103 "ready",
104 "draft",
105 "close",
106 "reopen",
107 "update-branch",
108 "enable-auto-merge",
109 "disable-auto-merge",
110 "revert",
111 ] as const),
112 checkoutCommand: ({ number }) => `src pr checkout ${number}`,
113 changeRequestCloseCommand: /\bsrc\s+pr\s+(?:merge|close)\b/u,
114 changeRequestCreateCommand: /\bsrc\s+pr\s+(?:create|new)\b/u,
115 authorProfileUrl: (login) =>
116 login.endsWith("[bot]")
117 ? null
118 : `https://${TANSTACK_SOURCE_HOST}/@${encodeURIComponent(login)}`,
119 reviewSummaryRequired: () => false,
120 checkoutCommandArgument: (input) => CHECKOUT_COMMAND.exec(input)?.[1]?.trim() ?? null,
121 isChangeRequestReference: (url) => parsePullRequestUrl(url) !== null,
122 checkoutChangeRequestHost: () => null,
123 changeRequestHeadRef: (number) => `refs/pull/${number}/head`,
124 repositoryKeyFromRemote,
125 recognizeRemote: ({ hostname }) =>
126 isTanStackSourceHost(hostname)
127 ? { kind: KIND, name: "TanStack Source", baseUrl: `https://${TANSTACK_SOURCE_HOST}` }
128 : null,
129 isChangeRequestInRepository: (identity, link) =>
130 isChangeRequestInProjectRepository(projectChangeRequestHost(identity, KIND), identity, link),
131 canReadChangeRequestOnHost: (identity, link) =>
132 isChangeRequestOnProjectHost(projectChangeRequestHost(identity, KIND), identity, link),
133 settings,
134});
135​
136export function parsePullRequestUrl(value: string) {
137 try {
138 return parseChangeRequestUrl(new URL(value));
139 } catch {
140 return null;
141 }