Open History to see the last commit
1import type {
2 EnvironmentId,
3 ProjectId,
4 PullRequestRef,
5 RepositoryIdentity,
6} from "@t3tools/contracts";
7import { canonicalRepositoryPath } from "@t3tools/shared/git";
8import type { ChangeRequestLink } from "@t3tools/source-control-core/shared/definition";
9
10import { sourceControlClients } from "./sourceControlClients.ts";
11
12interface ChangeRequestProject {
13 readonly id: ProjectId;
14 readonly environmentId: EnvironmentId;
15 readonly repositoryIdentity?: RepositoryIdentity | null | undefined;
16}
17
18/** The project whose own repository holds the change request, not just one on its host. */
19export function findProjectForChangeRequest<Project extends ChangeRequestProject>(
20 projects: ReadonlyArray<Project>,
21 link: ChangeRequestLink,
22): Project | undefined {
23 return projects.find((project) => {
24 const identity = project.repositoryIdentity;
25 return (
26 identity?.provider !== undefined &&
27 sourceControlClients.get(identity.provider).isChangeRequestInRepository(identity, link)
28 );
29 });
30}
31
32/** Prefers the project checked out from the link's repository, else lends any project on its host. */
33export function findProjectOnChangeRequestHost<Project extends ChangeRequestProject>(
34 projects: ReadonlyArray<Project>,
35 link: ChangeRequestLink,
36): Project | undefined {
37 return (
38 findProjectForChangeRequest(projects, link) ??
39 projects.find((project) => {
40 const identity = project.repositoryIdentity;
41 return (
42 identity?.provider !== undefined &&
43 sourceControlClients.get(identity.provider).canReadChangeRequestOnHost(identity, link)
44 );
45 })
46 );
47}
48
49/**
50 * Where every client opens a change request link in the app, or null for the browser. A link
51 * opened into a pull request view may borrow any project on its host, so a mirror's or a
52 * sibling repository's link still opens in the app.
53 */
54export function resolveChangeRequestLinkTarget<Project extends ChangeRequestProject>({
55 url,
56 projects,
57 configLoaded,
58 environmentId,
59 projectId,
60 primaryEnvironmentId,
61 lendHostProject,
62}: {
63 url: string;
64 projects: ReadonlyArray<Project>;
65 configLoaded: (environmentId: EnvironmentId) => boolean;
66 /** The link's own environment; without one, any environment that reads change requests. */
67 environmentId?: EnvironmentId | undefined;
68 /** The project the link came from, tried first. */
69 projectId?: ProjectId | undefined;
70 primaryEnvironmentId?: EnvironmentId | null | undefined;
71 lendHostProject: boolean;
72}): {
73 readonly project: Project;
74 readonly repository: string;
75 readonly number: number;
76 readonly host?: string;
77} | null {
78 const link = sourceControlClients.parseChangeRequestUrl(url)?.link;
79 if (link === undefined) return null;
80 // Matching remotes do not make project ids usable outside the link's environment.
81 const candidates = (
82 environmentId === undefined
83 ? projects.filter((project) => configLoaded(project.environmentId))
84 : projects.filter((project) => project.environmentId === environmentId)
85 ).sort(
86 (left, right) =>
87 Number(right.id === projectId) - Number(left.id === projectId) ||
88 Number(right.environmentId === primaryEnvironmentId) -
89 Number(left.environmentId === primaryEnvironmentId),
90 );
91 const project =
92 findProjectForChangeRequest(candidates, link) ??
93 (lendHostProject
94 ? (findProjectOnChangeRequestHost(candidates, link) ??
95 // A folder of several repositories has no remote of its own; the server reads its
96 // pull requests through its tracked folders.
97 candidates.find((candidate) => candidate.id === projectId && !candidate.repositoryIdentity))
98 : undefined);
99 if (project === undefined || !configLoaded(project.environmentId)) return null;
100 return {
101 project,
102 repository: link.repository,
103 number: link.number,
104 host: link.authority ?? link.host,
105 };
106}
107
108export function resolvePullRequestPreviewTarget({
109 environmentId,
110 projects,
111 pullRequestsEnabled,
112 url,
113}: {
114 environmentId: EnvironmentId | null;
115 projects: ReadonlyArray<ChangeRequestProject>;
116 pullRequestsEnabled: boolean;
117 url: string;
118}): { environmentId: EnvironmentId; input: PullRequestRef } | null {
119 if (!pullRequestsEnabled || environmentId === null) return null;
120 const parsed = sourceControlClients.parseChangeRequestUrl(url)?.link;
121 if (parsed === undefined) return null;
122 const project = findProjectForChangeRequest(
123 projects.filter((candidate) => candidate.environmentId === environmentId),
124 parsed,
125 );
126 if (project === undefined) return null;
127 return {
128 environmentId,
129 input: {
130 projectId: project.id,
131 host: parsed.authority ?? parsed.host,
132 repository:
133 sourceControlClients.repositorySelector(project.repositoryIdentity) ?? parsed.repository,
134 number: parsed.number,
135 },
136 };
137}
138
139function trimmedRepositoryPath(path: string): string {
140 return canonicalRepositoryPath(path.replace(/^\/+|\/+$/g, ""));
141}
142
143function webUrlRepositoryPath(webUrl: string | undefined): string | null {
144 if (webUrl === undefined) return null;
145 try {
146 const path = trimmedRepositoryPath(new URL(webUrl).pathname);
147 return path === "" ? null : path;
148 } catch {
149 return null;
150 }
151}
152
153/** The project host's browser URL for one of its change requests, before its detail loads. */
154export function projectChangeRequestUrl(
155 identity: RepositoryIdentity | null | undefined,
156 repository: string,
157 number: number,
158): string | null {
159 if (identity?.provider === undefined) return null;
160 const [host, ...path] = identity.canonicalKey.split("/");
161 if (!host) return null;
162 // The RPC selector can be shorter than the browser path (Azure's `web` for
163 // `org/project/_git/web`), so the project's own repository uses its full key. A host
164 // mounted under a path prefix names it with that prefix, which only its webUrl carries.
165 const ownRepository =
166 sourceControlClients.repositorySelector(identity) === repository ||
167 webUrlRepositoryPath(identity.webUrl) === trimmedRepositoryPath(repository);
168 return sourceControlClients.get(identity.provider).changeRequestUrl({
169 host,
170 repository: ownRepository ? path.join("/") : repository,
171 number,
172 remoteUrl: identity.locator.remoteUrl.trim(),
173 ...(ownRepository && identity.webUrl !== undefined ? { webUrl: identity.webUrl } : {}),
174 });
175}
176
177export function projectCommitUrl(
178 identity: RepositoryIdentity | null | undefined,
179 sha: string,
180): string | null {
181 if (identity?.provider === undefined) return null;
182 const [host, ...path] = identity.canonicalKey.split("/");
183 if (!host) return null;
184 return (
185 sourceControlClients.get(identity.provider).commitUrl?.({
186 host,
187 repository: path.join("/"),
188 sha,
189 remoteUrl: identity.locator.remoteUrl.trim(),
190 ...(identity.webUrl !== undefined ? { webUrl: identity.webUrl } : {}),
191 }) ?? null
192 );
193}