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

issueLinks.ts

171 lines6.6 KB

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