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/** 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]}` };
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 isTanStackSourceHost(url.hostname)
112 ? repositoryUrlFromPagePath(url, REPOSITORY_PAGE_PATH)
113 : null,
114 changeRequestUrlFromReferenceAutolink: () => null,
115 changeRequestActions: new Set([
116 "merge",
117 "ready",
118 "draft",
119 "close",
120 "reopen",
121 "update-branch",
122 "enable-auto-merge",
123 "disable-auto-merge",
124 "revert",
125 ] as const),
126 checkoutCommand: ({ number }) => `src pr checkout ${number}`,
127 changeRequestCloseCommand: /\bsrc\s+pr\s+(?:merge|close)\b/u,
128 changeRequestCreateCommand: /\bsrc\s+pr\s+(?:create|new)\b/u,
129 authorProfileUrl: (login) =>
130 login.endsWith("[bot]")
131 ? null
132 : `https://${TANSTACK_SOURCE_HOST}/@${encodeURIComponent(login)}`,
133 reviewSummaryRequired: () => false,
134 checkoutCommandArgument: (input) => CHECKOUT_COMMAND.exec(input)?.[1]?.trim() ?? null,
135 isChangeRequestReference: (url) => parsePullRequestUrl(url) !== null,
136 checkoutChangeRequestHost: () => null,
137 changeRequestHeadRef: (number) => `refs/pull/${number}/head`,
138 repositoryKeyFromRemote,
139 canonicalRepositoryKey,
140 recognizeRemote: ({ hostname }) =>
141 isTanStackSourceHost(hostname)
142 ? { kind: KIND, name: "TanStack Source", baseUrl: `https://${TANSTACK_SOURCE_HOST}` }
143 : null,
144 isChangeRequestInRepository: (identity, link) =>
145 isChangeRequestInProjectRepository(projectChangeRequestHost(identity, KIND), identity, link),
146 canReadChangeRequestOnHost: (identity, link) =>
147 isChangeRequestOnProjectHost(projectChangeRequestHost(identity, KIND), identity, link),
148 settings,
149});
150
151export function parsePullRequestUrl(value: string) {
152 try {
153 return parseChangeRequestUrl(new URL(value));
154 } catch {
155 return null;
156 }
157}