Open History to see the last commit
1import { sourceControlRepositorySelector } from "@t3tools/shared/sourceControl";
2import type {
3 EnvironmentId,
4 ScopedThreadRef,
5 ThreadLinkedPullRequest,
6 ThreadPullRequestLink,
7} from "@t3tools/contracts";
8import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell";
9import {
10 isAtomCommandInterrupted,
11 squashAtomCommandFailure,
12} from "@t3tools/client-runtime/state/runtime";
13import {
14 planThreadPullRequestMutation,
15 threadPullRequestLinkMode,
16} from "@t3tools/client-runtime/thread-pull-request-compatibility";
17import {
18 threadPullRequestKeysEqual,
19 visibleThreadPullRequests,
20} from "@t3tools/shared/threadPullRequests";
21import {
22 findProjectForChangeRequest,
23 matchesLinkedPullRequestUrl,
24 parseChangeRequestUrl,
25 type ChangeRequestLink,
26} from "~/lib/openPullRequestLink";
27import { useProjects, useServerConfigs } from "~/state/entities";
28import { threadEnvironment } from "~/state/threads";
29import { useAtomCommand } from "~/state/use-atom-command";
30
31export function canLinkChangeRequest(
32 mode: ReturnType<typeof threadPullRequestLinkMode>,
33 projects: ReadonlyArray<EnvironmentProject>,
34 parsed: ChangeRequestLink,
35): boolean {
36 if (mode === "unsupported") return false;
37 if (mode === "multiple") return true;
38 return findProjectForChangeRequest(projects, parsed) !== undefined;
39}
40
41export function usePullRequestLinking(environmentId: EnvironmentId | null | undefined) {
42 const configs = useServerConfigs();
43 const projects = useProjects();
44 const capabilities =
45 environmentId == null ? undefined : configs.get(environmentId)?.environment.capabilities;
46 const mode = threadPullRequestLinkMode(capabilities);
47 const link = useAtomCommand(threadEnvironment.linkPullRequest, { reportFailure: false });
48 const unlink = useAtomCommand(threadEnvironment.unlinkPullRequest, { reportFailure: false });
49 const updateMetadata = useAtomCommand(threadEnvironment.updateMetadata, { reportFailure: false });
50 const environmentProjects = projects.filter((project) => project.environmentId === environmentId);
51 const canLink = (url: string) => {
52 const parsed = parseChangeRequestUrl(url);
53 return parsed !== null && canLinkChangeRequest(mode, environmentProjects, parsed);
54 };
55 const isLinked = (
56 thread: {
57 readonly pullRequests?: readonly ThreadPullRequestLink[];
58 readonly linkedPullRequest?: ThreadLinkedPullRequest | null | undefined;
59 } | null,
60 url: string,
61 ) => {
62 if (thread === null || mode === "unsupported") return false;
63 if (mode !== "multiple")
64 return (
65 thread.linkedPullRequest != null &&
66 matchesLinkedPullRequestUrl(thread.linkedPullRequest, url)
67 );
68 const parsed = parseChangeRequestUrl(url);
69 return (
70 parsed !== null &&
71 visibleThreadPullRequests(thread.pullRequests ?? []).some((entry) =>
72 threadPullRequestKeysEqual(entry, parsed),
73 )
74 );
75 };
76 const changeLink = async (threadRef: ScopedThreadRef, url: string, linked: boolean) => {
77 const parsed = parseChangeRequestUrl(url);
78 if (parsed === null || threadRef.environmentId !== environmentId || (linked && !canLink(url)))
79 throw new Error("The pull request is not available in this environment.");
80 const legacyProject = findProjectForChangeRequest(environmentProjects, parsed);
81 const mutation = planThreadPullRequestMutation({
82 capabilities,
83 threadId: threadRef.threadId,
84 reference: { ...parsed, url },
85 legacyProjectId: legacyProject?.id ?? null,
86 legacyRepository:
87 sourceControlRepositorySelector(legacyProject?.repositoryIdentity) ?? undefined,
88 linked,
89 });
90 if (mutation === null)
91 throw new Error("This environment does not support linking this pull request.");
92 const result = await (mutation.type === "thread.meta.update"
93 ? updateMetadata({ environmentId: threadRef.environmentId, input: mutation.input })
94 : mutation.type === "thread.pull-request.link"
95 ? link({ environmentId: threadRef.environmentId, input: mutation.input })
96 : unlink({ environmentId: threadRef.environmentId, input: mutation.input }));
97 if (result._tag === "Failure") {
98 if (isAtomCommandInterrupted(result)) throw new Error("Link update interrupted.");
99 throw squashAtomCommandFailure(result);
100 }
101 };
102 return { mode, canLink, isLinked, changeLink };
103}