Open History to see the last commit
1import { WS_METHODS, type PullRequestDiffInput } from "@t3tools/contracts";
2import * as Data from "effect/Data";
3import * as Effect from "effect/Effect";
4import * as Option from "effect/Option";
5import * as SubscriptionRef from "effect/SubscriptionRef";
6import { Atom } from "effect/unstable/reactivity";
7
8import {
9 createAtomCommandScheduler,
10 createEnvironmentRpcCommand,
11 createEnvironmentRpcQueryAtomFamily,
12 createEnvironmentQueryAtomFamily,
13} from "./runtime.ts";
14import { PullRequestDiffLoader } from "./pullRequestDiffHttp.ts";
15import type { EnvironmentRegistry } from "../connection/registry.ts";
16import { EnvironmentSupervisor } from "../connection/supervisor.ts";
17
18export { PullRequestDiffLoader, pullRequestDiffLoaderLayer } from "./pullRequestDiffHttp.ts";
19
20export class EnvironmentHttpConnectionNotReadyError extends Data.TaggedError(
21 "EnvironmentHttpConnectionNotReadyError",
22)<{ readonly message: string }> {}
23
24/**
25 * Every read shells out to the GitHub CLI, so results are reused for a short while and
26 * refreshed explicitly. Mutations run serially per environment: `gh` actions on the same
27 * pull request are order-sensitive, and the detail view refetches after each one.
28 */
29export function createPullRequestEnvironmentAtoms<R, E>(
30 runtime: Atom.AtomRuntime<EnvironmentRegistry | PullRequestDiffLoader | R, E>,
31) {
32 const commandScheduler = createAtomCommandScheduler();
33 const serialPerEnvironment = {
34 mode: "serial",
35 key: ({ environmentId }: { readonly environmentId: string }) => environmentId,
36 } as const;
37 return {
38 list: createEnvironmentRpcQueryAtomFamily(runtime, {
39 label: "environment-data:pull-requests:list",
40 tag: WS_METHODS.pullRequestsList,
41 staleTimeMs: 30_000,
42 }),
43 /**
44 * The line counts for rows the listing has already handed over. Its own query because the
45 * listing is quicker without them — measured over twelve repositories, ~4.0s against ~7.1s —
46 * so the rows arrive first and their stats a moment later. Kept longer than the listing:
47 * a change request's size only moves when somebody pushes to it.
48 */
49 listStats: createEnvironmentRpcQueryAtomFamily(runtime, {
50 label: "environment-data:pull-requests:list-stats",
51 tag: WS_METHODS.pullRequestsListStats,
52 staleTimeMs: 60_000,
53 }),
54 detail: createEnvironmentRpcQueryAtomFamily(runtime, {
55 label: "environment-data:pull-requests:detail",
56 tag: WS_METHODS.pullRequestsDetail,
57 staleTimeMs: 15_000,
58 }),
59 activity: createEnvironmentRpcQueryAtomFamily(runtime, {
60 label: "environment-data:pull-requests:activity",
61 tag: WS_METHODS.pullRequestsActivity,
62 staleTimeMs: 15_000,
63 }),
64 threadComments: createEnvironmentRpcCommand(runtime, {
65 label: "environment-data:pull-requests:thread-comments",
66 tag: WS_METHODS.pullRequestsThreadComments,
67 scheduler: commandScheduler,
68 concurrency: {
69 mode: "singleFlight",
70 key: ({ environmentId, input }) =>
71 JSON.stringify([environmentId, input.threadId, input.cursor]),
72 },
73 }),
74 diff: createEnvironmentQueryAtomFamily(runtime, {
75 label: "environment-data:pull-requests:diff",
76 staleTimeMs: 60_000,
77 execute: (input: PullRequestDiffInput) =>
78 Effect.gen(function* () {
79 const supervisor = yield* EnvironmentSupervisor;
80 const loader = yield* PullRequestDiffLoader;
81 const prepared = yield* SubscriptionRef.get(supervisor.prepared);
82 if (Option.isNone(prepared)) {
83 return yield* new EnvironmentHttpConnectionNotReadyError({
84 message: "The environment HTTP connection is not ready.",
85 });
86 }
87 return yield* loader.load(prepared.value, input);
88 }),
89 }),
90 diffFileContents: createEnvironmentRpcCommand(runtime, {
91 label: "environment-data:pull-requests:diff-file-contents",
92 tag: WS_METHODS.pullRequestsDiffFileContents,
93 scheduler: commandScheduler,
94 concurrency: {
95 mode: "singleFlight",
96 key: ({ environmentId, input }) =>
97 JSON.stringify([
98 environmentId,
99 input.projectId,
100 input.repository,
101 input.number,
102 input.commit ?? null,
103 input.changeType,
104 input.oldPath,
105 input.newPath,
106 ]),
107 },
108 }),
109 runAction: createEnvironmentRpcCommand(runtime, {
110 label: "environment-data:pull-requests:run-action",
111 tag: WS_METHODS.pullRequestsRunAction,
112 scheduler: commandScheduler,
113 concurrency: serialPerEnvironment,
114 }),
115 update: createEnvironmentRpcCommand(runtime, {
116 label: "environment-data:pull-requests:update",
117 tag: WS_METHODS.pullRequestsUpdate,
118 scheduler: commandScheduler,
119 concurrency: serialPerEnvironment,
120 }),
121 comment: createEnvironmentRpcCommand(runtime, {
122 label: "environment-data:pull-requests:comment",
123 tag: WS_METHODS.pullRequestsComment,
124 scheduler: commandScheduler,
125 concurrency: serialPerEnvironment,
126 }),
127 updateComment: createEnvironmentRpcCommand(runtime, {
128 label: "environment-data:pull-requests:update-comment",
129 tag: WS_METHODS.pullRequestsUpdateComment,
130 scheduler: commandScheduler,
131 concurrency: serialPerEnvironment,
132 }),
133 submitReview: createEnvironmentRpcCommand(runtime, {
134 label: "environment-data:pull-requests:submit-review",
135 tag: WS_METHODS.pullRequestsSubmitReview,
136 scheduler: commandScheduler,
137 concurrency: serialPerEnvironment,
138 }),
139 replyToThread: createEnvironmentRpcCommand(runtime, {
140 label: "environment-data:pull-requests:reply-to-thread",
141 tag: WS_METHODS.pullRequestsReplyToThread,
142 scheduler: commandScheduler,
143 concurrency: serialPerEnvironment,
144 }),
145 /**
146 * Its own query rather than part of the detail: the people who may be asked are only wanted
147 * once somebody opens the reviewer menu, so this atom is read then and not before. Kept fresh
148 * for a minute, because who has access to a repository changes far more slowly than the
149 * change request it is being read for.
150 */
151 reviewerCandidates: createEnvironmentRpcQueryAtomFamily(runtime, {
152 label: "environment-data:pull-requests:reviewer-candidates",
153 tag: WS_METHODS.pullRequestsReviewerCandidates,
154 staleTimeMs: 60_000,
155 }),
156 requestReviewers: createEnvironmentRpcCommand(runtime, {
157 label: "environment-data:pull-requests:request-reviewers",
158 tag: WS_METHODS.pullRequestsRequestReviewers,
159 scheduler: commandScheduler,
160 concurrency: serialPerEnvironment,
161 }),
162 setThreadResolution: createEnvironmentRpcCommand(runtime, {
163 label: "environment-data:pull-requests:set-thread-resolution",
164 tag: WS_METHODS.pullRequestsSetThreadResolution,
165 scheduler: commandScheduler,
166 concurrency: serialPerEnvironment,
167 }),
168 setReaction: createEnvironmentRpcCommand(runtime, {
169 label: "environment-data:pull-requests:set-reaction",
170 tag: WS_METHODS.pullRequestsSetReaction,
171 scheduler: commandScheduler,
172 concurrency: serialPerEnvironment,
173 }),
174 /**
175 * Explicit refresh: forget the server's cached answers, then re-run the reads. A separate
176 * request rather than a flag on a read, so only a person's refresh spends host requests
177 * while every silent re-read shares the cache.
178 */
179 invalidate: createEnvironmentRpcCommand(runtime, {
180 label: "environment-data:pull-requests:invalidate",
181 tag: WS_METHODS.pullRequestsInvalidate,
182 scheduler: commandScheduler,
183 concurrency: serialPerEnvironment,
184 }),
185 };
186}