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

gitHubPullRequestEvents.ts

282 lines10.7 KB

1import { normalizeHost } from "@t3tools/source-control-core/server/pullRequestUtils";
2import * as Clock from "effect/Clock";
3import * as Duration from "effect/Duration";
4import * as Effect from "effect/Effect";
5import * as Option from "effect/Option";
6import * as Random from "effect/Random";
7import * as Redacted from "effect/Redacted";
8import * as Schema from "effect/Schema";
9import * as Stream from "effect/Stream";
10import * as Sse from "effect/encoding/Sse";
11import { HttpClient, HttpClientRequest } from "effect/http";
12​
13import * as GitHubRest from "./GitHubRest.ts";
14​
15/** Events only say that something changed; readers fetch the pull request for what. */
16const EVENT_TYPES = [
17 "com.github.pull_request.review.submitted.v1",
18 "com.github.pull_request.head.changed.v1",
19 "com.github.pull_request.state.changed.v1",
20 "com.github.pull_request.merge_conflict.v1",
21];
22const API_ORIGIN = "https://api.github.com";
23/** GitHub sends a keep-alive at least every 15 seconds while a stream is idle. */
24const SILENCE_LIMIT = Duration.seconds(45);
25/** A connection open this long was healthy, however it ended. */
26const HEALTHY_AFTER = Duration.minutes(2);
27/** GitHub enables event streams per account, so a refusal holds for a while. */
28const REFUSED_PAUSE = Duration.hours(1);
29/** Another watch ending may free a slot under the per-account subscription limit. */
30const LIMIT_PAUSE = Duration.minutes(10);
31const MAX_FAILURES = 6;
32const MAX_BACKOFF_MS = 60_000;
33​
34const Subscription = Schema.Struct({
35 id: Schema.Union([Schema.String, Schema.Number]),
36 events_url: Schema.String,
37});
38const decodeSubscription = Schema.decodeUnknownOption(Schema.fromJsonString(Subscription));
39​
40const decodeStreamEnd = Schema.decodeUnknownOption(
41 Schema.fromJsonString(Schema.Struct({ outcome: Schema.String })),
42);
43​
44/**
45 * How a connection ended. `renew` means the subscription expired or lost access, and `personal`
46 * that the app's token was refused where the user's own may still read.
47 */
48type Ending = "reconnect" | "renew" | "personal" | "stop";
49​
50const endingOf = (data: string): Ending =>
51 Option.match(decodeStreamEnd(data), {
52 onNone: () => "reconnect",
53 onSome: ({ outcome }) => (outcome === "subscription_ended" ? "renew" : "reconnect"),
54 });
55​
56export interface PullRequestEventsInput {
57 readonly host: string;
58 readonly repository: string;
59 readonly number: number;
61​
62/**
63 * Emits when a connection opens, so a reader catches up on what it missed, then once per change
64 * GitHub reports. Ends quietly when GitHub refuses the subscription; the caller polls meanwhile.
65 */
66export const make = Effect.gen(function* () {
67 const rest = yield* GitHubRest.GitHubRest;
68 const httpClient = yield* HttpClient.HttpClient;
69 /** Keyed by host for account-wide refusals, and by pull request for one it cannot read. */
70 const pausedUntil = new Map<string, number>();
71​
72 const subscribe = Effect.fn("GitHubPullRequestEvents.subscribe")(function* (
73 input: PullRequestEventsInput,
74 ) {
75 const resourceUrl = `https://${input.host}/${input.repository}/pull/${input.number}`;
76 const now = yield* Clock.currentTimeMillis;
77 if (Math.max(pausedUntil.get(input.host) ?? 0, pausedUntil.get(resourceUrl) ?? 0) > now) {
78 return Option.none();
79 }
80 const pause = (key: string, duration: Duration.Duration) =>
81 Effect.sync(() => pausedUntil.set(key, now + Duration.toMillis(duration)));
82 const response = yield* rest
83 .rest({
84 host: input.host,
85 operation: "subscribePullRequestEvents",
86 method: "POST",
87 path: "event-subscriptions",
88 body: { resource_url: resourceUrl, event_types: EVENT_TYPES },
89 })
90 .pipe(
91 Effect.asSome,
92 Effect.catchTags({
93 GitHubApiNotFoundError: () =>
94 pause(resourceUrl, REFUSED_PAUSE).pipe(Effect.as(Option.none())),
95 GitHubApiResponseError: (error) =>
96 (error.status === 403
97 ? pause(input.host, REFUSED_PAUSE)
98 : error.status === 422
99 ? pause(input.host, LIMIT_PAUSE)
100 : Effect.void
101 ).pipe(Effect.as(Option.none())),
102 }),
103 Effect.catch((error) =>
104 Effect.logDebug("pull request event subscription failed", { error }).pipe(
105 Effect.as(Option.none()),
106 ),
107 ),
108 );
109 if (Option.isNone(response)) return Option.none();
110 // The stream request carries the token, so its URL must stay on GitHub's API.
111 return decodeSubscription(response.value.body).pipe(
112 Option.filter(({ events_url }) => URL.parse(events_url)?.origin === API_ORIGIN),
113 );
114 });
115​
116 const unsubscribe = Effect.fn("GitHubPullRequestEvents.unsubscribe")(function* (
117 host: string,
118 id: string | number,
119 ) {
120 yield* rest
121 .rest({
122 host,
123 operation: "unsubscribePullRequestEvents",
124 method: "DELETE",
125 path: `event-subscriptions/${encodeURIComponent(String(id))}`,
126 })
127 .pipe(Effect.ignore);
128 });
129​
130 /** One SSE connection: "changed" when it opens and per event, and last, how it ended. */
131 const connection = (
132 host: string,
133 eventsUrl: string,
134 personal: boolean,
135 ): Stream.Stream<"changed" | Ending> =>
136 Stream.unwrap(
137 Effect.gen(function* () {
138 const held = yield* rest.credential(host, { personal });
139 const response = yield* httpClient
140 .execute(
141 HttpClientRequest.get(eventsUrl).pipe(
142 HttpClientRequest.setHeaders({
143 accept: "text/event-stream",
144 "x-github-api-version": "2022-11-28",
145 "user-agent": "t3code",
146 authorization: `Bearer ${Redacted.value(held.token)}`,
147 }),
148 ),
149 )
150 // Client spans would carry the subscription URL.
151 .pipe(Effect.provideService(HttpClient.TracerDisabledWhen, () => true));
152 if (response.status === 403 || response.status === 404) {
153 return Stream.succeed<Ending>(held.source === "app" ? "personal" : "stop");
154 }
155 if (response.status !== 200) return Stream.succeed<Ending>("reconnect");
156 return response.stream.pipe(
157 Stream.timeoutOrElse({ duration: SILENCE_LIMIT, orElse: () => Stream.fail("silent") }),
158 Stream.decodeText,
159 Stream.pipeThroughChannel(Sse.decode()),
160 Stream.map((frame): "changed" | Ending =>
161 frame.event === "github.stream.end" ? endingOf(frame.data) : "changed",
162 ),
163 Stream.prepend(["changed" as const]),
164 // A close without GitHub's end frame is reconnected like one with it.
165 Stream.concat(Stream.succeed<Ending>("reconnect")),
166 Stream.takeUntil((element) => element !== "changed"),
167 );
168 }),
169 ).pipe(
170 Stream.catchCause((cause) =>
171 Stream.fromEffect(
172 Effect.logDebug("pull request event stream failed", { cause }).pipe(
173 Effect.as<Ending>("reconnect"),
174 ),
175 ),
176 ),
177 );
178​
179 const backoff = Effect.fn("GitHubPullRequestEvents.backoff")(function* (failures: number) {
180 const jitter = yield* Random.next;
181 yield* Effect.sleep(
182 Duration.millis(Math.min(MAX_BACKOFF_MS, 1_000 * 2 ** failures) * (0.5 + jitter / 2)),
183 );
184 });
185​
186 return (input: PullRequestEventsInput): Stream.Stream<void> => {
187 const target = { ...input, host: normalizeHost(input.host) };
188 if (target.host !== "github.com") return Stream.empty;
189​
190 /** Follows one subscription; sets `session.renew` when it needs replacing. */
191 const follow = (
192 session: { renew: boolean },
193 eventsUrl: string,
194 personal: boolean,
195 failuresSoFar: number,
196 ): Stream.Stream<void> =>
197 Stream.unwrap(
198 Clock.currentTimeMillis.pipe(
199 Effect.map((openedAt) => {
200 let ending: Ending = "reconnect";
201 let changes = 0;
202 return connection(target.host, eventsUrl, personal).pipe(
203 Stream.flatMap((element) => {
204 if (element === "changed") {
205 changes += 1;
206 return Stream.succeed(undefined);
207 }
208 ending = element;
209 return Stream.empty;
210 }),
211 Stream.concat(
212 Stream.unwrap(
213 Clock.currentTimeMillis.pipe(
214 Effect.map((closedAt) => {
215 // The first change is the opening; an event or a long life proves it worked.
216 const healthy =
217 changes > 1 || closedAt - openedAt >= Duration.toMillis(HEALTHY_AFTER);
218 const failures = healthy ? 0 : failuresSoFar + 1;
219 switch (ending) {
220 case "stop":
221 return Stream.empty;
222 case "renew":
223 session.renew = true;
224 return Stream.empty;
225 case "personal":
226 return personal
227 ? Stream.empty
228 : follow(session, eventsUrl, true, failuresSoFar);
229 case "reconnect":
230 return failures > MAX_FAILURES
231 ? Stream.empty
232 : Stream.fromEffectDrain(backoff(failures)).pipe(
233 Stream.concat(follow(session, eventsUrl, personal, failures)),
234 );
235 }
236 }),
237 ),
238 ),
239 ),
240 );
241 }),
242 ),
243 );
244​
245 /** One subscription, deleted once its stream has closed. */
246 const subscription = (session: { renew: boolean }): Stream.Stream<void> =>
247 Stream.unwrap(
248 Effect.acquireRelease(subscribe(target), (held) =>
249 Option.isSome(held) ? unsubscribe(target.host, held.value.id) : Effect.void,
250 ).pipe(
251 Effect.map(
252 Option.match({
253 onNone: () => Stream.empty,
254 onSome: ({ events_url }) => follow(session, events_url, false, 0),
255 }),
256 ),
257 ),
258 );
259​
260 const subscribed = (): Stream.Stream<void> =>
261 Stream.suspend(() => {
262 const session = { renew: false };
263 return subscription(session).pipe(
264 Stream.concat(
265 Stream.suspend(() =>
266 session.renew
267 ? Stream.fromEffectDrain(backoff(0)).pipe(Stream.concat(subscribed()))
268 : Stream.empty,
269 ),
270 ),
271 );
272 });
273​
274 // Event streams are a GitHub preview, so only the app sign-in, itself opt-in, uses them.
275 return Stream.unwrap(
276 rest.appCredential(target.host).pipe(
277 Effect.map((app) => (Option.isSome(app) ? subscribed() : Stream.empty)),
278 Effect.orElseSucceed(() => Stream.empty),
279 ),
280 );
281 };
282});