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 return {
69 list: createEnvironmentRpcQueryAtomFamily(runtime, {
70 label: "environment-data:pull-requests:list",
71 tag: WS_METHODS.pullRequestsList,
72 staleTimeMs: 30_000,
73 }),
74 /**
75 * The line counts for rows the listing has already handed over. Its own query because the
76 * listing is quicker without them — measured over twelve repositories, ~4.0s against ~7.1s —
77 * so the rows arrive first and their stats a moment later. Kept longer than the listing:
78 * a change request's size only moves when somebody pushes to it.
79 */
80 listStats: createEnvironmentRpcQueryAtomFamily(runtime, {
81 label: "environment-data:pull-requests:list-stats",
82 tag: WS_METHODS.pullRequestsListStats,
83 staleTimeMs: 60_000,
84 }),
85 detail: createEnvironmentRpcQueryAtomFamily(runtime, {
86 label: "environment-data:pull-requests:detail",
87 tag: WS_METHODS.pullRequestsDetail,
88 staleTimeMs: 15_000,
89 }),
90 activity: createEnvironmentRpcQueryAtomFamily(runtime, {
91 label: "environment-data:pull-requests:activity",
92 tag: WS_METHODS.pullRequestsActivity,
93 staleTimeMs: 15_000,
94 }),
95 threadComments: createEnvironmentRpcCommand(runtime, {
96 label: "environment-data:pull-requests:thread-comments",
97 tag: WS_METHODS.pullRequestsThreadComments,
98 scheduler: commandScheduler,
99 concurrency: {
100 mode: "singleFlight",
101 key: ({ environmentId, input }) =>
102 JSON.stringify([environmentId, input.threadId, input.cursor]),
103 },
104 }),
105 diff: createEnvironmentQueryAtomFamily(runtime, {
106 label: "environment-data:pull-requests:diff",
107 staleTimeMs: 60_000,
108 execute: (input: PullRequestDiffInput) =>
109 Effect.gen(function* () {
110 const supervisor = yield* EnvironmentSupervisor;
111 const loader = yield* PullRequestDiffLoader;
112 const prepared = yield* SubscriptionRef.get(supervisor.prepared);
113 if (Option.isNone(prepared)) {
114 return yield* new EnvironmentHttpConnectionNotReadyError({
115 message: "The environment HTTP connection is not ready.",
116 });
117 }
118 return yield* loader.load(prepared.value, input);
119 }),
120 }),
121 diffFileContents: createEnvironmentRpcCommand(runtime, {
122 label: "environment-data:pull-requests:diff-file-contents",
123 tag: WS_METHODS.pullRequestsDiffFileContents,
124 scheduler: commandScheduler,
125 concurrency: {
126 mode: "singleFlight",
127 key: ({ environmentId, input }) =>
128 JSON.stringify([
129 environmentId,
130 input.projectId,
131 input.repository,
132 input.number,
133 input.commit ?? null,
134 input.changeType,
135 input.oldPath,
136 input.newPath,
137 ]),
138 },
139 }),
140 runAction: createEnvironmentRpcCommand(runtime, {
141 label: "environment-data:pull-requests:run-action",
142 tag: WS_METHODS.pullRequestsRunAction,
143 scheduler: commandScheduler,
144 concurrency: serialPerEnvironment,
145 }),
146 update: createEnvironmentRpcCommand(runtime, {
147 label: "environment-data:pull-requests:update",
148 tag: WS_METHODS.pullRequestsUpdate,
149 scheduler: commandScheduler,
150 concurrency: serialPerEnvironment,
151 }),
152 comment: createEnvironmentRpcCommand(runtime, {
153 label: "environment-data:pull-requests:comment",
154 tag: WS_METHODS.pullRequestsComment,
155 scheduler: commandScheduler,
156 concurrency: serialPerEnvironment,
157 }),
158 updateComment: createEnvironmentRpcCommand(runtime, {
159 label: "environment-data:pull-requests:update-comment",
160 tag: WS_METHODS.pullRequestsUpdateComment,
161 scheduler: commandScheduler,
162 concurrency: serialPerEnvironment,
163 }),
164 submitReview: createEnvironmentRpcCommand(runtime, {
165 label: "environment-data:pull-requests:submit-review",
166 tag: WS_METHODS.pullRequestsSubmitReview,
167 scheduler: commandScheduler,
168 concurrency: serialPerEnvironment,
169 }),
170 replyToThread: createEnvironmentRpcCommand(runtime, {
171 label: "environment-data:pull-requests:reply-to-thread",
172 tag: WS_METHODS.pullRequestsReplyToThread,
173 scheduler: commandScheduler,
174 concurrency: serialPerEnvironment,
175 }),
176 /**
177 * Its own query rather than part of the detail: the people who may be asked are only wanted
178 * once somebody opens the reviewer menu, so this atom is read then and not before. Kept fresh
179 * for a minute, because who has access to a repository changes far more slowly than the
180 * change request it is being read for.
181 */
182 reviewerCandidates: createEnvironmentRpcQueryAtomFamily(runtime, {
183 label: "environment-data:pull-requests:reviewer-candidates",
184 tag: WS_METHODS.pullRequestsReviewerCandidates,
185 staleTimeMs: 60_000,
186 }),
187 requestReviewers: createEnvironmentRpcCommand(runtime, {
188 label: "environment-data:pull-requests:request-reviewers",
189 tag: WS_METHODS.pullRequestsRequestReviewers,
190 scheduler: commandScheduler,
191 concurrency: serialPerEnvironment,
192 }),
193 setThreadResolution: createEnvironmentRpcCommand(runtime, {
194 label: "environment-data:pull-requests:set-thread-resolution",
195 tag: WS_METHODS.pullRequestsSetThreadResolution,
196 scheduler: commandScheduler,
197 concurrency: serialPerEnvironment,
198 }),
199 setReaction: createEnvironmentRpcCommand(runtime, {
200 label: "environment-data:pull-requests:set-reaction",
201 tag: WS_METHODS.pullRequestsSetReaction,
202 scheduler: commandScheduler,
203 concurrency: serialPerEnvironment,
204 }),
205 /**
206 * Explicit refresh: forget the server's cached answers, then re-run the reads. A separate
207 * request rather than a flag on a read, so only a person's refresh spends host requests
208 * while every silent re-read shares the cache.
209 */
210 invalidate: createEnvironmentRpcCommand(runtime, {
211 label: "environment-data:pull-requests:invalidate",
212 tag: WS_METHODS.pullRequestsInvalidate,
213 scheduler: commandScheduler,
214 concurrency: serialPerEnvironment,
215 }),
216 };
217}