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

usePullRequestLinking.ts

104 lines4.4 KB

1import { useMemo } from "react";
2import { sourceControlRepositorySelector } from "@t3tools/shared/sourceControl";
3import type {
4 EnvironmentId,
5 ScopedThreadRef,
6 ThreadLinkedPullRequest,
7 ThreadPullRequestLink,
8} from "@t3tools/contracts";
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 findProjectOnChangeRequestHost,
24 matchesLinkedPullRequestUrl,
25 parseChangeRequestUrl,
26} from "~/lib/openPullRequestLink";
27import { useProjects, useServerConfigs } from "~/state/entities";
28import { threadEnvironment } from "~/state/threads";
29import { useAtomCommand } from "~/state/use-atom-command";
30​
31/** Routes link actions through the command advertised by this environment. */
32export function usePullRequestLinking(environmentId: EnvironmentId | null | undefined) {
33 const configs = useServerConfigs();
34 const projects = useProjects();
35 const capabilities =
36 environmentId == null ? undefined : configs.get(environmentId)?.environment.capabilities;
37 const mode = threadPullRequestLinkMode(capabilities);
38 const link = useAtomCommand(threadEnvironment.linkPullRequest, { reportFailure: false });
39 const unlink = useAtomCommand(threadEnvironment.unlinkPullRequest, { reportFailure: false });
40 const updateMetadata = useAtomCommand(threadEnvironment.updateMetadata, { reportFailure: false });
41 return useMemo(() => {
42 const environmentProjects = projects.filter(
43 (project) => project.environmentId === environmentId,
44 );
45 const canLink = (url: string) => {
46 const parsed = parseChangeRequestUrl(url);
47 if (parsed === null || mode === "unsupported") return false;
48 return (
49 (mode === "multiple" ? findProjectOnChangeRequestHost : findProjectForChangeRequest)(
50 environmentProjects,
51 parsed,
52 ) !== undefined
53 );
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 }, [capabilities, environmentId, link, mode, projects, unlink, updateMetadata]);