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

gitHubPullRequestEvents.ts

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