@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 03:37 UTC

main
ts 167 lines 6.4 KB
Raw
1 import {
2 IssueTrackerKind,
3 type IssueRef,
4 type IssueTrackerConnectionList,
5 } from "@t3tools/contracts";
6 import {
7 LINEAR_HOST,
8 LINEAR_KIND,
9 parseLinearIssueUrl,
10 } from "@t3tools/issue-tracker-linear/shared/issueUrl";
11
12 import { sourceControlClients } from "./sourceControlClients.ts";
13 import type { EnvironmentProject } from "./state/shell.ts";
14
15 type LinkProject = Pick<EnvironmentProject, "id" | "environmentId" | "repositoryIdentity">;
16
17 /** The hosts and scopes a project's git-host tracker is bound to, the way the server resolves them. */
18 function projectIssueBindings(project: LinkProject) {
19 return sourceControlClients
20 .issueBindings(project.repositoryIdentity)
21 .map((binding) => ({ ...binding, scope: binding.scope.toLowerCase() }));
22 }
23
24 function linkAuthority(url: string): string | null {
25 try {
26 return new URL(url).host.toLowerCase();
27 } catch {
28 return null;
29 }
30 }
31
32 /** A host whose hostname does not name it, such as GitHub Enterprise Server, is vouched for by a project on it. */
33 function parseIssueLink(
34 url: string,
35 authority: string | null,
36 projects: ReadonlyArray<LinkProject>,
37 ) {
38 const parsed = sourceControlClients.parseIssueUrl(url);
39 if (parsed !== null || authority === null) return parsed;
40 const hostname = new URL(url).hostname.toLowerCase();
41 for (const project of projects) {
42 const bindings = projectIssueBindings(project);
43 if (!bindings.some((binding) => binding.host === hostname || binding.host === authority))
44 continue;
45 const known = sourceControlClients.parseIssueUrl(url, project.repositoryIdentity?.provider);
46 if (known !== null) return known;
47 }
48 return null;
49 }
50
51 function parseStandaloneIssueLink(url: string) {
52 try {
53 const link = parseLinearIssueUrl(new URL(url));
54 return link === null ? null : { tracker: LINEAR_KIND, link };
55 } catch {
56 return null;
57 }
58 }
59
60 /** Whether `url` is a standalone tracker's issue link, which only bindings can resolve. */
61 export function isStandaloneIssueLink(url: string): boolean {
62 return parseStandaloneIssueLink(url) !== null;
63 }
64
65 /** A cheap pre-check, so text without such a link never asks for the bindings. */
66 export function mayContainStandaloneIssueLink(text: string): boolean {
67 return text.includes(`${LINEAR_HOST}/`);
68 }
69
70 /**
71 * A standalone tracker link resolves through the project bound to its scope, else any bound to
72 * the tracker, and only when it names the workspace the credential reaches: team keys repeat
73 * across workspaces, so a link elsewhere stays an ordinary link.
74 */
75 function resolveStandaloneIssueLink<Project extends LinkProject>(
76 url: string,
77 projects: ReadonlyArray<Project>,
78 trackers: IssueTrackerConnectionList | undefined,
79 preferredProjectId: string | undefined,
80 ): { readonly project: Project; readonly ref: IssueRef } | null {
81 const parsed = trackers === undefined ? null : parseStandaloneIssueLink(url);
82 if (parsed === null || trackers === undefined) return null;
83 const { tracker, link } = parsed;
84 const workspace = trackers.trackers.find(
85 (connection) => connection.tracker === tracker && connection.account !== null,
86 )?.workspace;
87 if (workspace?.toLowerCase() !== link.workspace.toLowerCase()) return null;
88 const { bindings } = trackers;
89 const onTracker = projects.flatMap((project) => {
90 const binding = bindings[project.id];
91 return binding?.tracker === tracker ? [{ project, binding }] : [];
92 });
93 const preferFirst = (candidates: typeof onTracker) =>
94 candidates.find((candidate) => candidate.project.id === preferredProjectId) ?? candidates[0];
95 const scope = link.scope.toLowerCase();
96 const own = preferFirst(
97 onTracker.filter((candidate) => candidate.binding.scope.toLowerCase() === scope),
98 );
99 const lender = own ?? preferFirst(onTracker);
100 if (lender === undefined) return null;
101 return {
102 project: lender.project,
103 ref: {
104 projectId: lender.project.id,
105 tracker: lender.binding.tracker,
106 host: link.host,
107 scope: own === undefined ? link.scope : lender.binding.scope,
108 key: link.key,
109 },
110 };
111 }
112
113 /**
114 * The reference an issue link resolves to, and the project it is read through: the project whose
115 * tracker binding is that host and scope, else any project on the same tracker and host lending
116 * its credentials for a read-only view. The same rule as the server's `IssueService.resolveUrl`,
117 * applied on the client so a lookalike hostname matches no project and stays an ordinary link.
118 */
119 export function resolveIssueLinkTarget<Project extends LinkProject>(input: {
120 readonly url: string;
121 /** The projects of the environment the link is read in. */
122 readonly projects: ReadonlyArray<Project>;
123 readonly preferredProjectId?: string | undefined;
124 /** The environment's standalone trackers and bindings; without them only git-host links resolve. */
125 readonly trackers?: IssueTrackerConnectionList | undefined;
126 }): { readonly project: Project; readonly ref: IssueRef } | null {
127 // A link names its server's port; a binding does only when its remote or web URL did.
128 const authority = linkAuthority(input.url);
129 const parsed = parseIssueLink(input.url, authority, input.projects);
130 if (parsed === null)
131 return resolveStandaloneIssueLink(
132 input.url,
133 input.projects,
134 input.trackers,
135 input.preferredProjectId,
136 );
137 const { definition, link } = parsed;
138 const onHost = input.projects.flatMap((project) =>
139 projectIssueBindings(project)
140 .filter(
141 (binding) =>
142 binding.tracker === definition.kind &&
143 (binding.host === link.host || binding.host === authority),
144 )
145 .map((binding) => ({ project, binding })),
146 );
147 const onAuthority = onHost.filter((candidate) => candidate.binding.host === authority);
148 const bound = onAuthority.length > 0 ? onAuthority : onHost;
149 const preferFirst = (candidates: typeof bound) =>
150 candidates.find((candidate) => candidate.project.id === input.preferredProjectId) ??
151 candidates[0];
152 const own =
153 preferFirst(bound.filter((candidate) => candidate.binding.scope === link.scope)) ??
154 preferFirst(bound.filter((candidate) => candidate.binding.scope === link.scopeAlias));
155 const lender = own ?? preferFirst(bound);
156 if (lender === undefined) return null;
157 return {
158 project: lender.project,
159 ref: {
160 projectId: lender.project.id,
161 tracker: IssueTrackerKind.make(lender.binding.tracker),
162 host: lender.binding.host,
163 scope: own === undefined ? link.scope : lender.binding.scope,
164 key: link.key,
165 },
166 };
167 }