Open History to see the last commit
1import type { EnvironmentProject } from "@t3tools/client-runtime/state/models";
2
3export type PullRequestEnvironmentTarget = {
4 readonly environmentId: string;
5 readonly projectId: string;
6 readonly repository: string;
7 readonly host?: string;
8};
9
10export function pullRequestFallbackEnvironment(
11 target: PullRequestEnvironmentTarget,
12 projects: ReadonlyArray<EnvironmentProject>,
13 connectedEnvironmentIds: ReadonlySet<string>,
14) {
15 if (connectedEnvironmentIds.has(target.environmentId)) return null;
16 const own = projects.find(
17 (project) => project.environmentId === target.environmentId && project.id === target.projectId,
18 );
19 const host = target.host ?? own?.repositoryIdentity?.canonicalKey.split("/")[0];
20 if (!host) return null;
21 const key = `${host}/${target.repository}`.toLowerCase();
22 const copy = projects.find(
23 (project) =>
24 project.environmentId !== target.environmentId &&
25 connectedEnvironmentIds.has(project.environmentId) &&
26 project.repositoryIdentity?.canonicalKey.toLowerCase() === key,
27 );
28 return copy ? { environmentId: String(copy.environmentId), projectId: String(copy.id) } : null;
29}