Open History to see the last commit
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/** Web pages name the owner with a leading `@`, as `/@TanStack/router`; clone URLs may omit it. */
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]}` };
38}
39
40/**
41 * `owner/name` as the API names it. The web spells it `@owner/name`, and a client without this
42 * definition percent-encodes that as `%40owner/name`.
43 */
44export const withoutOwnerMark = (repository: string) => repository.replace(/^(?:@|%40)/iu, "");
45
46/** Links stored with the web's spelling compare equal to the project's own key. */
47function canonicalRepositoryKey(key: string): string | null {
48 const slash = key.indexOf("/");
49 return slash > 0 && isTanStackSourceHost(key.slice(0, slash))
50 ? `${TANSTACK_SOURCE_HOST}/${withoutOwnerMark(key.slice(slash + 1))}`
51 : null;
52}
53
54/** Keys drop the owner's `@`, so a project's repository is `owner/name` as the API names it. */
55function repositoryKeyFromRemote(key: string): string | null {
56 // Remote parsing spells the `@` percent-encoded.
57 const match = /^source\.tanstack\.com\/(?:@|%40)([^/@%]+\/[^/]+)$/u.exec(key);
58 return match?.[1] === undefined
59 ? null
60 : `${TANSTACK_SOURCE_HOST}/${match[1].replace(/\.git$/u, "")}`;
61}
62
63export const settings = makeProviderSettingsSchema(hostCredentialSettingsFields);
64export type TanStackSourceSettings = typeof settings.Type;
65
66export const definition = defineSourceControl({
67 kind: KIND,
68 label: "TanStack Source",
69 pickerLabel: "TanStack Source",
70 icon,
71 searchKeywords: ["tanstack", "src"],
72 credentials: {
73 cliName: "src",
74 cliLabel: "TanStack Source CLI",
75 loginCommand: "src auth login --web",
76 tokenEnvVar: "SRC_TOKEN",
77 tokenAccess: "read and write access to repositories, issues and pull requests",
78 newTokenUrl: (host) => `https://${host}/settings/tokens`,
79 },
80 changeRequest: {
81 shortLabel: "PR",
82 shortLabelWithArticle: "a PR",
83 singular: "pull request",
84 plural: "pull requests",
85 },
86 repositoryPathHint: "owner/repo",
87 publicHost: TANSTACK_SOURCE_HOST,
88 publishDescription: TANSTACK_SOURCE_HOST,
89 publishHost: () => TANSTACK_SOURCE_HOST,
90 newRepositoryOwner: (account) => ({ owner: account }),
91 defaultCloneTransport: "https",
92 changeRequestUrl: ({ host, repository, number, webUrl }) =>
93 !Number.isSafeInteger(number) || number < 1
94 ? null
95 : webUrl
96 ? changeRequestUrlBelow(webUrl, `pull/${number}`)
97 : `https://${host}/@${withoutOwnerMark(repository)}/pull/${number}`,
98 parseChangeRequestUrl,
99 commitUrl: (input) =>
100 buildCommitUrl(
101 input.webUrl
102 ? input
103 : { ...input, webUrl: `https://${input.host}/@${withoutOwnerMark(input.repository)}` },
104 "commit",
105 ),
106 parseIssueUrl: (url) =>
107 isTanStackSourceHost(url.hostname) ? parseIssuePath(url, ISSUE_PATH) : null,
108 parseCommitUrl: (url) =>
109 isTanStackSourceHost(url.hostname) ? parseCommitPath(url, COMMIT_PATH) : null,
110 repositoryUrlFromPage: (url) => {
111 if (!isTanStackSourceHost(url.hostname)) return null;
112 const page = new URL(url);
113 page.pathname = page.pathname.replace(/^\/%40/iu, "/@");
114 return repositoryUrlFromPagePath(page, REPOSITORY_PAGE_PATH);
115 },
116 changeRequestUrlFromReferenceAutolink: () => null,
117 changeRequestActions: new Set([
118 "merge",
119 "ready",
120 "draft",
121 "close",
122 "reopen",
123 "update-branch",
124 "enable-auto-merge",
125 "disable-auto-merge",
126 "revert",
127 ] as const),
128 checkoutCommand: ({ number }) => `src pr checkout ${number}`,
129 changeRequestCloseCommand: /\bsrc\s+pr\s+(?:merge|close)\b/u,
130 changeRequestCreateCommand: /\bsrc\s+pr\s+(?:create|new)\b/u,
131 authorProfileUrl: ({ login }) =>
132 login.endsWith("[bot]")
133 ? null
134 : `https://${TANSTACK_SOURCE_HOST}/@${encodeURIComponent(login)}`,
135 reviewSummaryRequired: () => false,
136 checkoutCommandArgument: (input) => CHECKOUT_COMMAND.exec(input)?.[1]?.trim() ?? null,
137 isChangeRequestReference: (url) => parsePullRequestUrl(url) !== null,
138 checkoutChangeRequestHost: () => null,
139 changeRequestHeadRef: (number) => `refs/pull/${number}/head`,
140 repositoryKeyFromRemote,
141 canonicalRepositoryKey,
142 recognizeRemote: ({ hostname }) =>
143 isTanStackSourceHost(hostname)
144 ? { kind: KIND, name: "TanStack Source", baseUrl: `https://${TANSTACK_SOURCE_HOST}` }
145 : null,
146 isChangeRequestInRepository: (identity, link) =>
147 isChangeRequestInProjectRepository(projectChangeRequestHost(identity, KIND), identity, link),
148 canReadChangeRequestOnHost: (identity, link) =>
149 isChangeRequestOnProjectHost(projectChangeRequestHost(identity, KIND), identity, link),
150 settings,
151});
152
153export function parsePullRequestUrl(value: string) {
154 try {
155 return parseChangeRequestUrl(new URL(value));
156 } catch {
157 return null;
158 }
159}