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