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

pullRequests.ts

225 lines8.7 KB

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