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

issueLinks.ts

168 lines6.5 KB

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