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 diff: createEnvironmentQueryAtomFamily(runtime, {
65 label: "environment-data:pull-requests:diff",
66 staleTimeMs: 60_000,
67 execute: (input: PullRequestDiffInput) =>
68 Effect.gen(function* () {
69 const supervisor = yield* EnvironmentSupervisor;
70 const loader = yield* PullRequestDiffLoader;
71 const prepared = yield* SubscriptionRef.get(supervisor.prepared);
72 if (Option.isNone(prepared)) {
73 return yield* new EnvironmentHttpConnectionNotReadyError({
74 message: "The environment HTTP connection is not ready.",
75 });
76 }
77 return yield* loader.load(prepared.value, input);
78 }),
79 }),
80 diffFileContents: createEnvironmentRpcCommand(runtime, {
81 label: "environment-data:pull-requests:diff-file-contents",
82 tag: WS_METHODS.pullRequestsDiffFileContents,
83 scheduler: commandScheduler,
84 concurrency: {
85 mode: "singleFlight",
86 key: ({ environmentId, input }) =>
87 JSON.stringify([
88 environmentId,
89 input.projectId,
90 input.repository,
91 input.number,
92 input.commit ?? null,
93 input.changeType,
94 input.oldPath,
95 input.newPath,
96 ]),
97 },
98 }),
99 runAction: createEnvironmentRpcCommand(runtime, {
100 label: "environment-data:pull-requests:run-action",
101 tag: WS_METHODS.pullRequestsRunAction,
102 scheduler: commandScheduler,
103 concurrency: serialPerEnvironment,
104 }),
105 comment: createEnvironmentRpcCommand(runtime, {
106 label: "environment-data:pull-requests:comment",
107 tag: WS_METHODS.pullRequestsComment,
108 scheduler: commandScheduler,
109 concurrency: serialPerEnvironment,
110 }),
111 submitReview: createEnvironmentRpcCommand(runtime, {
112 label: "environment-data:pull-requests:submit-review",
113 tag: WS_METHODS.pullRequestsSubmitReview,
114 scheduler: commandScheduler,
115 concurrency: serialPerEnvironment,
116 }),
117 replyToThread: createEnvironmentRpcCommand(runtime, {
118 label: "environment-data:pull-requests:reply-to-thread",
119 tag: WS_METHODS.pullRequestsReplyToThread,
120 scheduler: commandScheduler,
121 concurrency: serialPerEnvironment,
122 }),
123 /**
124 * Its own query rather than part of the detail: the people who may be asked are only wanted
125 * once somebody opens the reviewer menu, so this atom is read then and not before. Kept fresh
126 * for a minute, because who has access to a repository changes far more slowly than the
127 * change request it is being read for.
128 */
129 reviewerCandidates: createEnvironmentRpcQueryAtomFamily(runtime, {
130 label: "environment-data:pull-requests:reviewer-candidates",
131 tag: WS_METHODS.pullRequestsReviewerCandidates,
132 staleTimeMs: 60_000,
133 }),
134 requestReviewers: createEnvironmentRpcCommand(runtime, {
135 label: "environment-data:pull-requests:request-reviewers",
136 tag: WS_METHODS.pullRequestsRequestReviewers,
137 scheduler: commandScheduler,
138 concurrency: serialPerEnvironment,
139 }),
140 setThreadResolution: createEnvironmentRpcCommand(runtime, {
141 label: "environment-data:pull-requests:set-thread-resolution",
142 tag: WS_METHODS.pullRequestsSetThreadResolution,
143 scheduler: commandScheduler,
144 concurrency: serialPerEnvironment,
145 }),
146 /**
147 * Explicit refresh: forget the server's cached answers, then re-run the reads. A separate
148 * request rather than a flag on a read, so only a person's refresh spends host requests
149 * while every silent re-read shares the cache.
150 */
151 invalidate: createEnvironmentRpcCommand(runtime, {
152 label: "environment-data:pull-requests:invalidate",
153 tag: WS_METHODS.pullRequestsInvalidate,
154 scheduler: commandScheduler,
155 concurrency: serialPerEnvironment,
156 }),
157 };
158}