@niwa-labs/forge 0.0.0-stage → 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/observe.ts ADDED
@@ -0,0 +1,464 @@
1
+ /**
2
+ * Watching a repository: what the module records of a server it polls.
3
+ *
4
+ * One pass (`sync`) reads the server and writes what changed since the journal last said:
5
+ *
6
+ * - The project's event log gives pushes, pull requests opened, reopened, merged and closed, approvals
7
+ * and comments. A push, an approval or a comment is written under the server's event id, so an
8
+ * event read twice is one record, and the journal is the cursor: the next pass reads on after
9
+ * the last event it holds. A transition of a pull request has one id whichever of the log and a
10
+ * look at the server tells it first (the pull request and the number of the transition), and the
11
+ * record's `source` says which did.
12
+ * - Pull requests, pipelines, jobs and checks have no events of their own. Their state is compared
13
+ * with the last one the journal holds; a change is written as a new version of the observation,
14
+ * with the state before it. A record of a version never carries the time of the pass, so a pass
15
+ * that is made again writes the same record.
16
+ *
17
+ * Passes of one repository never overlap, so a version is never claimed twice.
18
+ */
19
+
20
+ import { createHash } from "node:crypto";
21
+
22
+ import { IntegrityError, RecordRefused, seq } from "@niwa-labs/core";
23
+ import type { EmitOptions, JournalReader, RecordType, StoredRecord } from "@niwa-labs/core";
24
+
25
+ import { ForgeError } from "./gitlab.ts";
26
+ import type { GitLab } from "./gitlab.ts";
27
+ import { checksOf, lookupsOf, pipelineOf, pullRequestOf, runStateOf } from "./normalize.ts";
28
+ import type { GitLabJob, GitLabMr, GitLabPipeline } from "./normalize.ts";
29
+ import { settleMerges } from "./merge.ts";
30
+ import {
31
+ ChecksObserved,
32
+ JobObserved,
33
+ PipelineObserved,
34
+ PrClosed,
35
+ PrComment,
36
+ PrHead,
37
+ PrMerged,
38
+ PrOpened,
39
+ PrReview,
40
+ PrUpdated,
41
+ Pushed,
42
+ } from "./record-types.ts";
43
+ import type { ChecksState, PrRef, Repo, RunState, Source } from "./record-types.ts";
44
+ import { projectPath } from "./servers.ts";
45
+
46
+ /** An event of a project's log, as much of it as the module reads. */
47
+ export interface GitLabEvent {
48
+ id: number;
49
+ action_name: string;
50
+ target_type?: string | null;
51
+ target_iid?: number | null;
52
+ author?: { username?: string } | null;
53
+ push_data?: { ref?: string | null; ref_type?: string; commit_from?: string | null; commit_to?: string | null } | null;
54
+ note?: { id: number; body?: string; noteable_type?: string; noteable_iid?: number; author?: { username?: string } } | null;
55
+ created_at?: string;
56
+ }
57
+
58
+ /** How a pass writes: a record of the module's own type under an id of its own. */
59
+ export type Emit = <T>(type: RecordType<T>, data: T, opts: EmitOptions) => Promise<StoredRecord>;
60
+
61
+ /** What a pass needs. */
62
+ export interface SyncPlace {
63
+ readonly gitlab: GitLab;
64
+ readonly journal: JournalReader;
65
+ readonly emit: Emit;
66
+ /** What was absorbed of the repository's records in earlier passes. Default: nothing, read afresh. */
67
+ readonly seen?: Seen;
68
+ /** Called when the event log was read only down to a bound, so events older than the oldest read were not seen. */
69
+ readonly gap?: () => void;
70
+ /** How many pages of the event log one pass reads at most. Default: 100, which is 10000 events. */
71
+ readonly eventPages?: number;
72
+ }
73
+
74
+ const NULL_SHA = /^0+$/;
75
+
76
+ /** How many pages of the event log a pass reads at most. */
77
+ const EVENT_PAGES = 100;
78
+
79
+ /** The states of a pipeline that has not ended and is not waiting for a person: its jobs still move. */
80
+ const ACTIVE = new Set<RunState>(["created", "waiting_for_resource", "preparing", "pending", "running", "scheduled"]);
81
+
82
+ /** The id of a record a pass writes: `forge:<host>:<project>:<what>`. */
83
+ function idOf(repo: Repo, what: string): string {
84
+ return `forge:${repo.host}:${String(repo.projectId)}:${what}`;
85
+ }
86
+
87
+ /** The digest of foreign text, as a record holds it. */
88
+ function digestOf(text: string): string {
89
+ return `sha256:${createHash("sha256").update(text).digest("hex")}`;
90
+ }
91
+
92
+ const SEEN_TYPES = [Pushed, PrOpened, PrUpdated, PrHead, PrReview, PrComment, PrMerged, PrClosed, PipelineObserved, JobObserved, ChecksObserved];
93
+
94
+ /**
95
+ * What the journal holds of one repository: a projection kept from pass to pass, so that a pass does
96
+ * not read the whole history of the host again. It changes only by absorbing the records the journal
97
+ * gained since the last one it took, so a record is never counted twice.
98
+ */
99
+ export class Seen {
100
+ /** The position of the last record absorbed. */
101
+ #upto = 0;
102
+ /** The highest event id written. */
103
+ lastEvent = 0;
104
+ /**
105
+ * Where each pull request stands by the records, and how many times it was opened and closed:
106
+ * the transitions are numbered, so each one has a single identity whoever tells it first.
107
+ */
108
+ readonly life = new Map<number, { state: "open" | "closed" | "merged"; opens: number; closes: number }>();
109
+ /** The last head, fields and checks written of each pull request, and how many observations of each. */
110
+ readonly heads = new Map<number, { head: string; version: number }>();
111
+ readonly fields = new Map<number, { title: string; base: string; draft: boolean; version: number }>();
112
+ readonly checks = new Map<number, { head: string; state: ChecksState; version: number }>();
113
+ /** The last state and version of each pipeline and job. */
114
+ readonly pipelines = new Map<number, { state: RunState; version: number }>();
115
+ readonly jobs = new Map<number, { state: RunState; version: number }>();
116
+ /**
117
+ * The pipelines, by id and status, that the server shows and the module cannot bind to a pair of
118
+ * commits, so records no version of: they are known, and not read again as news. Not from the journal.
119
+ */
120
+ readonly unbound = new Set<string>();
121
+
122
+ readonly #repo: Repo;
123
+
124
+ constructor(repo: Repo) {
125
+ this.#repo = repo;
126
+ }
127
+
128
+ /** The repository this projection is of. */
129
+ get repo(): Repo {
130
+ return this.#repo;
131
+ }
132
+
133
+ /** Take the records the journal gained since the last absorbed one. */
134
+ absorb(journal: JournalReader): void {
135
+ const repo = this.#repo;
136
+ const ours = (data: { repo?: Repo; pr?: PrRef }) => (data.repo ?? data.pr?.repo)?.projectId === repo.projectId && (data.repo ?? data.pr?.repo)?.host === repo.host;
137
+ const byName = new Map<string, RecordType<any>>(SEEN_TYPES.map((type) => [type.name, type]));
138
+ for (const stored of journal.since(seq(this.#upto), SEEN_TYPES.map((type) => type.name))) {
139
+ this.#upto = Number(stored.seq);
140
+ const type = byName.get(stored.type);
141
+ const parsed = type?.schema.safeParse(stored.data);
142
+ if (parsed === undefined || !parsed.success) continue;
143
+ const data = parsed.data as { repo?: Repo; pr?: PrRef; source: Source } & Record<string, unknown>;
144
+ if (!ours(data)) continue;
145
+ if (data.source.kind === "event") this.lastEvent = Math.max(this.lastEvent, data.source.id);
146
+ const version = data.source.kind === "observed" ? data.source.version : 0;
147
+ const number = data.pr?.number ?? -1;
148
+ const at = <V extends { version: number }>(map: Map<number, V>, key: number, value: Omit<V, "version">) =>
149
+ map.set(key, { ...value, version: Math.max(version, map.get(key)?.version ?? 0) } as V);
150
+ switch (stored.type) {
151
+ case PrOpened.name: {
152
+ const was = this.life.get(number);
153
+ this.life.set(number, { state: "open", opens: (was?.opens ?? 0) + 1, closes: was?.closes ?? 0 });
154
+ const opened = data as unknown as { title: string; base: string };
155
+ if (!this.fields.has(number)) this.fields.set(number, { title: opened.title, base: opened.base, draft: false, version: 0 });
156
+ break;
157
+ }
158
+ case PrMerged.name:
159
+ this.life.set(number, { opens: 0, closes: 0, ...this.life.get(number), state: "merged" });
160
+ break;
161
+ case PrClosed.name: {
162
+ const was = this.life.get(number);
163
+ this.life.set(number, { opens: was?.opens ?? 0, closes: (was?.closes ?? 0) + 1, state: "closed" });
164
+ break;
165
+ }
166
+ case PrHead.name:
167
+ at(this.heads, number, { head: data["head"] as string });
168
+ break;
169
+ case PrUpdated.name:
170
+ at(this.fields, number, { title: data["title"] as string, base: data["base"] as string, draft: data["draft"] as boolean });
171
+ break;
172
+ case ChecksObserved.name:
173
+ at(this.checks, number, { head: data["head"] as string, state: data["state"] as ChecksState });
174
+ break;
175
+ case PipelineObserved.name:
176
+ at(this.pipelines, data["pipeline"] as number, { state: data["state"] as RunState });
177
+ break;
178
+ case JobObserved.name:
179
+ at(this.jobs, data["job"] as number, { state: data["state"] as RunState });
180
+ break;
181
+ }
182
+ }
183
+ }
184
+ }
185
+
186
+ /**
187
+ * One pass over a repository: what changed is written, and how many records that made. `since` is
188
+ * the last event of the log when the repository was watched: what came before is not news.
189
+ */
190
+ export async function sync(place: SyncPlace, repo: Repo, since = 0): Promise<number> {
191
+ const { gitlab } = place;
192
+ const seen = place.seen ?? new Seen(repo);
193
+ seen.absorb(place.journal);
194
+ let written = 0;
195
+ const write = async <T>(type: RecordType<T>, data: T, id: string): Promise<void> => {
196
+ try {
197
+ await place.emit(type, data, { id });
198
+ written += 1;
199
+ seen.absorb(place.journal);
200
+ } catch (error) {
201
+ // The same id with other content: a pass that was cut off wrote this version already.
202
+ if (!(error instanceof IntegrityError) && !(error instanceof RecordRefused && error.code === "stale-merge")) throw error;
203
+ }
204
+ };
205
+ const mrOf = async (iid: number): Promise<GitLabMr | undefined> => {
206
+ try {
207
+ return await gitlab.get<GitLabMr>(projectPath(repo, `/merge_requests/${String(iid)}`));
208
+ } catch (error) {
209
+ if (error instanceof ForgeError && error.code === "not-found") return undefined;
210
+ throw error;
211
+ }
212
+ };
213
+ const pr = (number: number): PrRef => ({ repo, number });
214
+
215
+ // The event log, newest first until an event the journal holds, then written oldest first.
216
+ const after = Math.max(seen.lastEvent, since);
217
+ const read = await gitlab.pages<GitLabEvent>(projectPath(repo, "/events"), { sort: "desc" }, { enough: (page) => page.some((event) => event.id <= after), maxPages: place.eventPages ?? EVENT_PAGES });
218
+ // The log cannot be asked for from an id: past the bound what lay between the last event the journal
219
+ // holds and the oldest one read is out of reach. What was read is written, and the gap is reported.
220
+ if (read.truncated) place.gap?.();
221
+ const events = read.items.filter((event) => event.id > after).sort((a, b) => a.id - b.id);
222
+ for (const event of events) {
223
+ const source: Source = { kind: "event", id: event.id };
224
+ const id = idOf(repo, `event:${String(event.id)}`);
225
+ const author = event.author?.username;
226
+ const push = event.push_data;
227
+ if (push?.ref !== undefined && push.ref !== null && /^pushed|^deleted/.test(event.action_name)) {
228
+ const before = push.commit_from ?? undefined;
229
+ const after = push.commit_to ?? undefined;
230
+ await write(
231
+ Pushed,
232
+ {
233
+ repo,
234
+ ref: push.ref,
235
+ refType: push.ref_type === "tag" ? "tag" : "branch",
236
+ ...(before === undefined || NULL_SHA.test(before) ? {} : { before }),
237
+ ...(after === undefined || NULL_SHA.test(after) ? {} : { after }),
238
+ ...(author === undefined ? {} : { author }),
239
+ source,
240
+ },
241
+ id,
242
+ );
243
+ continue;
244
+ }
245
+ const note = event.note;
246
+ if (note !== undefined && note !== null && note.noteable_type === "MergeRequest" && note.noteable_iid !== undefined) {
247
+ const body = note.body ?? "";
248
+ await write(
249
+ PrComment,
250
+ {
251
+ pr: pr(note.noteable_iid),
252
+ comment: note.id,
253
+ author: note.author?.username ?? author ?? "",
254
+ size: Buffer.byteLength(body, "utf8"),
255
+ digest: digestOf(body),
256
+ source,
257
+ },
258
+ id,
259
+ );
260
+ continue;
261
+ }
262
+ if (event.target_type !== "MergeRequest" || event.target_iid === undefined || event.target_iid === null) continue;
263
+ const number = event.target_iid;
264
+ if (event.action_name === "approved" || event.action_name === "unapproved") {
265
+ await write(PrReview, { pr: pr(number), reviewer: author ?? "", state: event.action_name, source }, id);
266
+ continue;
267
+ }
268
+ // What the journal already says of the pull request decides whether the event tells anything new:
269
+ // a transition seen first by a look at the server is the same one when its event comes late.
270
+ const stand = () => seen.life.get(number);
271
+ const action = event.action_name;
272
+ if (action === "opened" && stand() !== undefined) continue;
273
+ if (action === "reopened" && stand()?.state !== "closed") continue;
274
+ if (action === "accepted" && stand()?.state === "merged") continue;
275
+ if (action === "closed" && (stand()?.state === "closed" || stand()?.state === "merged")) continue;
276
+ if (!["opened", "reopened", "accepted", "closed"].includes(action)) continue;
277
+ const mr = await mrOf(number);
278
+ if (mr === undefined) continue;
279
+ if (action === "opened" || action === "reopened") {
280
+ const opens = (stand()?.opens ?? 0) + 1;
281
+ await write(
282
+ PrOpened,
283
+ { pr: pr(number), title: mr.title, branch: mr.source_branch, base: mr.target_branch, ...(author === undefined ? {} : { author }), ...(action === "reopened" ? { reopened: true } : {}), source },
284
+ idOf(repo, `pr:${String(number)}:opened:${String(opens)}`),
285
+ );
286
+ } else if (action === "accepted") {
287
+ const made = pullRequestOf(repo, mr);
288
+ await write(PrMerged, { pr: pr(number), base: mr.target_branch, head: mr.sha, ...(made.mergeSha === undefined ? {} : { mergeSha: made.mergeSha }), ...(author === undefined ? {} : { by: author }), source }, idOf(repo, `pr:${String(number)}:merged`));
289
+ } else {
290
+ const closes = (stand()?.closes ?? 0) + 1;
291
+ await write(PrClosed, { pr: pr(number), ...(author === undefined ? {} : { by: author }), source }, idOf(repo, `pr:${String(number)}:closed:${String(closes)}`));
292
+ }
293
+ }
294
+
295
+ // Pull requests: the open ones, and the ones the journal holds as open that are not any more.
296
+ const known = [...seen.life].filter(([, one]) => one.state === "open").map(([number]) => number);
297
+ const open = await gitlab.list<GitLabMr>(projectPath(repo, "/merge_requests"), { state: "opened" });
298
+ const ended = await Promise.all(known.filter((number) => !open.some((mr) => mr.iid === number)).map((number) => mrOf(number)));
299
+ for (const mr of [...open, ...ended]) {
300
+ if (mr === undefined) continue;
301
+ const number = mr.iid;
302
+ const observed = (version: number): Source => ({ kind: "observed", version });
303
+ const author = mr.author?.username === undefined ? {} : { author: mr.author.username };
304
+ // First seen, or open again after the journal closed it: a transition is numbered by how many came before.
305
+ const before = seen.life.get(number);
306
+ if (before === undefined || (before.state === "closed" && mr.state === "opened")) {
307
+ const opens = (before?.opens ?? 0) + 1;
308
+ await write(
309
+ PrOpened,
310
+ { pr: pr(number), title: mr.title, branch: mr.source_branch, base: mr.target_branch, ...author, ...(before === undefined ? {} : { reopened: true }), source: observed(opens) },
311
+ idOf(repo, `pr:${String(number)}:opened:${String(opens)}`),
312
+ );
313
+ }
314
+ const head = seen.heads.get(number);
315
+ if (head?.head !== mr.sha) {
316
+ const version = (head?.version ?? 0) + 1;
317
+ await write(PrHead, { pr: pr(number), head: mr.sha, ...(head === undefined ? {} : { previous: head.head }), source: observed(version) }, idOf(repo, `pr:${String(number)}:head:v${String(version)}`));
318
+ }
319
+ const fields = seen.fields.get(number);
320
+ const draft = mr.draft ?? mr.work_in_progress ?? false;
321
+ if (fields !== undefined && (fields.title !== mr.title || fields.base !== mr.target_branch || fields.draft !== draft)) {
322
+ const version = fields.version + 1;
323
+ await write(PrUpdated, { pr: pr(number), title: mr.title, base: mr.target_branch, draft, source: observed(version) }, idOf(repo, `pr:${String(number)}:updated:v${String(version)}`));
324
+ }
325
+ if (mr.state === "merged" && seen.life.get(number)?.state !== "merged") {
326
+ const made = pullRequestOf(repo, mr);
327
+ await write(PrMerged, { pr: pr(number), base: mr.target_branch, head: mr.sha, ...(made.mergeSha === undefined ? {} : { mergeSha: made.mergeSha }), source: observed(1) }, idOf(repo, `pr:${String(number)}:merged`));
328
+ }
329
+ if (mr.state === "closed" && seen.life.get(number)?.state === "open") {
330
+ const closes = (seen.life.get(number)?.closes ?? 0) + 1;
331
+ await write(PrClosed, { pr: pr(number), source: observed(closes) }, idOf(repo, `pr:${String(number)}:closed:${String(closes)}`));
332
+ }
333
+ }
334
+
335
+ // Pipelines and their jobs: a state that differs from the last one written is a new version.
336
+ const lookups = lookupsOf(gitlab, repo);
337
+ // Newest update first, read page by page until a page that holds nothing new: a burst of pipelines
338
+ // between two polls is read whole, the page the news ends on included, and a quiet poll reads one.
339
+ const key = (one: GitLabPipeline) => `${String(one.id)}:${one.status}`;
340
+ const settled = (one: GitLabPipeline) => seen.pipelines.get(one.id)?.state === runStateOf(one.status) || seen.unbound.has(key(one));
341
+ const raw = await gitlab.list<GitLabPipeline>(projectPath(repo, "/pipelines"), { order_by: "updated_at", sort: "desc" }, (page) => page.every(settled));
342
+ for (const one of [...raw].sort((a, b) => a.id - b.id)) {
343
+ const last = seen.pipelines.get(one.id);
344
+ const state = runStateOf(one.status);
345
+ const changed = last?.state !== state;
346
+ // A pipeline that is still moving has jobs that move with it, whether or not its own state did.
347
+ if (!changed && !ACTIVE.has(state)) continue;
348
+ const pipeline = changed ? await pipelineOf(repo, one, lookups) : undefined;
349
+ if (changed && pipeline === undefined) {
350
+ seen.unbound.add(key(one));
351
+ continue;
352
+ }
353
+ // The jobs are written before the pipeline's new version: a pass cut off in between leaves the
354
+ // pipeline changed, and the next pass reads its jobs again.
355
+ for (const job of await gitlab.list<GitLabJob>(projectPath(repo, `/pipelines/${String(one.id)}/jobs`))) {
356
+ const was = seen.jobs.get(job.id);
357
+ const jobState = runStateOf(job.status);
358
+ if (was?.state === jobState) continue;
359
+ const jobVersion = (was?.version ?? 0) + 1;
360
+ await write(
361
+ JobObserved,
362
+ { repo, pipeline: one.id, job: job.id, name: job.name, stage: job.stage, state: jobState, ...(was === undefined ? {} : { previous: was.state }), source: { kind: "observed", version: jobVersion } },
363
+ idOf(repo, `job:${String(job.id)}:v${String(jobVersion)}`),
364
+ );
365
+ }
366
+ if (pipeline === undefined) continue;
367
+ const version = (last?.version ?? 0) + 1;
368
+ await write(PipelineObserved, { ...pipeline, ...(last === undefined ? {} : { previous: last.state }), source: { kind: "observed", version } }, idOf(repo, `pipeline:${String(one.id)}:v${String(version)}`));
369
+ }
370
+
371
+ // Checks of the open pull requests, from the pipelines of their heads.
372
+ for (const mr of open) {
373
+ const pipelines = await gitlab.list<GitLabPipeline>(projectPath(repo, `/merge_requests/${String(mr.iid)}/pipelines`));
374
+ const made = await Promise.all(pipelines.map((one) => pipelineOf(repo, one, lookups)));
375
+ const checks = checksOf(made.filter((one) => one !== undefined && one.sourceSha === mr.sha).map((one) => one!));
376
+ const last = seen.checks.get(mr.iid);
377
+ if (last?.head === mr.sha && last.state === checks.state) continue;
378
+ const version = (last?.version ?? 0) + 1;
379
+ await write(
380
+ ChecksObserved,
381
+ { pr: pr(mr.iid), head: mr.sha, ...checks, ...(last === undefined ? {} : { previous: last.state }), source: { kind: "observed", version } },
382
+ idOf(repo, `pr:${String(mr.iid)}:checks:v${String(version)}`),
383
+ );
384
+ }
385
+ // Merges the server took for later and dropped on its own, or that somebody called off there.
386
+ await settleMerges({ gitlab, journal: place.journal, write: (type, data, id) => write(type, data, id) }, repo);
387
+ return written;
388
+ }
389
+
390
+ /** How long to wait between two passes over a repository. */
391
+ export interface PollIntervals {
392
+ /** While a run waits on the repository. Default: 20 seconds. */
393
+ readonly awaited: number;
394
+ /** While something changed in the last hour. Default: 2 minutes. */
395
+ readonly background: number;
396
+ /** After an hour with no change. Default: 5 minutes. */
397
+ readonly quiet: number;
398
+ }
399
+
400
+ export const DEFAULT_INTERVALS: PollIntervals = { awaited: 20_000, background: 120_000, quiet: 300_000 };
401
+
402
+ /** An hour: how long with no change before a repository is polled as quiet. */
403
+ const QUIET_AFTER_MS = 3_600_000;
404
+
405
+ /** The interval of the next pass over a repository. */
406
+ export function pollInterval(state: { awaited: boolean; lastChangeAt: number; now: number }, intervals: PollIntervals = DEFAULT_INTERVALS): number {
407
+ if (state.awaited) return intervals.awaited;
408
+ return state.now - state.lastChangeAt >= QUIET_AFTER_MS ? intervals.quiet : intervals.background;
409
+ }
410
+
411
+ /** What the poller knows of one repository. */
412
+ interface Polled {
413
+ lastPassAt: number;
414
+ lastChangeAt: number;
415
+ awaitedUntil: number;
416
+ }
417
+
418
+ /**
419
+ * When each watched repository is polled next: a repository a run waits on every 20 seconds, one that
420
+ * changed in the last hour every 2 minutes, a quiet one every 5. A server whose limit is spent is not
421
+ * asked before it may be.
422
+ */
423
+ export class Poller {
424
+ readonly #polled = new Map<string, Polled>();
425
+ readonly #intervals: PollIntervals;
426
+ /** How long a read by a caller keeps a repository awaited. */
427
+ readonly #awaitedFor: number;
428
+
429
+ constructor(intervals: PollIntervals = DEFAULT_INTERVALS, awaitedFor = 600_000) {
430
+ this.#intervals = intervals;
431
+ this.#awaitedFor = awaitedFor;
432
+ }
433
+
434
+ /** A caller read the repository: something waits on it now. */
435
+ touch(key: string, now: number): void {
436
+ const known = this.#of(key, now);
437
+ known.awaitedUntil = now + this.#awaitedFor;
438
+ }
439
+
440
+ /** The repositories of `keys` whose next pass is due at `now`. */
441
+ due(keys: readonly string[], now: number, waitMs: (key: string) => number): string[] {
442
+ return keys.filter((key) => {
443
+ const known = this.#polled.get(key);
444
+ if (known === undefined) return waitMs(key) === 0;
445
+ const interval = pollInterval({ awaited: known.awaitedUntil > now, lastChangeAt: known.lastChangeAt, now }, this.#intervals);
446
+ return now - known.lastPassAt >= interval && waitMs(key) === 0;
447
+ });
448
+ }
449
+
450
+ /** A pass over a repository ended, having written `written` records. */
451
+ passed(key: string, now: number, written: number): void {
452
+ const known = this.#of(key, now);
453
+ known.lastPassAt = now;
454
+ if (written > 0) known.lastChangeAt = now;
455
+ }
456
+
457
+ #of(key: string, now: number): Polled {
458
+ const known = this.#polled.get(key);
459
+ if (known !== undefined) return known;
460
+ const made: Polled = { lastPassAt: Number.NEGATIVE_INFINITY, lastChangeAt: now, awaitedUntil: 0 };
461
+ this.#polled.set(key, made);
462
+ return made;
463
+ }
464
+ }
package/src/prs.ts ADDED
@@ -0,0 +1,122 @@
1
+ /**
2
+ * The pull requests the module observed, folded from its records.
3
+ *
4
+ * A pull request is known from `forge.pr.opened`; its title, base and draft flag follow `forge.pr.updated`, its head
5
+ * `forge.pr.head`, its checks `forge.checks`, and its end `forge.pr.merged` or `forge.pr.closed`. The pipelines are
6
+ * those of `forge.pipeline` that name the pull request. Nothing here asks the server, so a pull request is as old as
7
+ * the last poll of its repository.
8
+ */
9
+
10
+ import { seq } from "@niwa-labs/core";
11
+ import type { JournalReader } from "@niwa-labs/core";
12
+
13
+ import type { ObservedPr, PrPipeline, PrState } from "./actions.ts";
14
+ import { ChecksObserved, PipelineObserved, PrClosed, PrHead, PrMerged, PrOpened, PrUpdated } from "./record-types.ts";
15
+ import type { PrRef } from "./record-types.ts";
16
+ import type { RepoName } from "./targets.ts";
17
+
18
+ /** The most pipelines a pull request lists: the latest ones. */
19
+ export const PIPELINES_SHOWN = 10;
20
+
21
+ const TYPES = [PrOpened, PrUpdated, PrHead, ChecksObserved, PipelineObserved, PrMerged, PrClosed] as const;
22
+
23
+ interface Folded {
24
+ pr: PrRef;
25
+ state: PrState;
26
+ title: string;
27
+ branch: string;
28
+ base: string;
29
+ draft: boolean;
30
+ author: string | undefined;
31
+ head: string | undefined;
32
+ checks: ObservedPr["checks"];
33
+ pipelines: Map<number, PrPipeline>;
34
+ /** The position of the last record that touched it. */
35
+ changed: number;
36
+ }
37
+
38
+ const keyOf = (pr: PrRef): string => `${pr.repo.host}\0${String(pr.repo.projectId)}\0${String(pr.number)}`;
39
+
40
+ /** Every pull request of the journal, the open ones first and the latest changed first. */
41
+ export function observedPrs(journal: JournalReader | undefined, filter: { repo?: RepoName | undefined; state?: PrState | undefined } = {}): ObservedPr[] {
42
+ if (journal === undefined) return [];
43
+ const found = new Map<string, Folded>();
44
+ const touch = (pr: PrRef, at: number): Folded | undefined => {
45
+ const one = found.get(keyOf(pr));
46
+ if (one !== undefined) one.changed = at;
47
+ return one;
48
+ };
49
+ for (const stored of journal.since(seq(0), TYPES.map((type) => type.name))) {
50
+ const at = Number(stored.seq);
51
+ switch (stored.type) {
52
+ case PrOpened.name: {
53
+ const parsed = PrOpened.schema.safeParse(stored.data);
54
+ if (!parsed.success) break;
55
+ const { pr, title, branch, base, author, reopened } = parsed.data;
56
+ const before = found.get(keyOf(pr));
57
+ found.set(keyOf(pr), {
58
+ pr,
59
+ state: reopened === true ? "open" : (before?.state ?? "open"),
60
+ title,
61
+ branch,
62
+ base,
63
+ draft: before?.draft ?? false,
64
+ author,
65
+ head: before?.head,
66
+ checks: before?.checks ?? "none",
67
+ pipelines: before?.pipelines ?? new Map(),
68
+ changed: at,
69
+ });
70
+ break;
71
+ }
72
+ case PrUpdated.name: {
73
+ const parsed = PrUpdated.schema.safeParse(stored.data);
74
+ const one = parsed.success ? touch(parsed.data.pr, at) : undefined;
75
+ if (parsed.success && one !== undefined) Object.assign(one, { title: parsed.data.title, base: parsed.data.base, draft: parsed.data.draft });
76
+ break;
77
+ }
78
+ case PrHead.name: {
79
+ const parsed = PrHead.schema.safeParse(stored.data);
80
+ const one = parsed.success ? touch(parsed.data.pr, at) : undefined;
81
+ if (parsed.success && one !== undefined) one.head = parsed.data.head;
82
+ break;
83
+ }
84
+ case ChecksObserved.name: {
85
+ const parsed = ChecksObserved.schema.safeParse(stored.data);
86
+ const one = parsed.success ? touch(parsed.data.pr, at) : undefined;
87
+ if (parsed.success && one !== undefined) one.checks = parsed.data.state;
88
+ break;
89
+ }
90
+ case PipelineObserved.name: {
91
+ const parsed = PipelineObserved.schema.safeParse(stored.data);
92
+ if (!parsed.success || parsed.data.pr === undefined) break;
93
+ const one = touch({ repo: parsed.data.repo, number: parsed.data.pr }, at);
94
+ if (one === undefined) break;
95
+ const { pipeline, kind, state, sourceSha, targetSha, mergeSha } = parsed.data;
96
+ one.pipelines.set(pipeline, { pipeline, kind, state, sourceSha, ...(targetSha === undefined ? {} : { targetSha }), ...(mergeSha === undefined ? {} : { mergeSha }) });
97
+ break;
98
+ }
99
+ case PrMerged.name: {
100
+ const parsed = PrMerged.schema.safeParse(stored.data);
101
+ const one = parsed.success ? touch(parsed.data.pr, at) : undefined;
102
+ if (parsed.success && one !== undefined) Object.assign(one, { state: "merged", head: parsed.data.head ?? one.head });
103
+ break;
104
+ }
105
+ case PrClosed.name: {
106
+ const parsed = PrClosed.schema.safeParse(stored.data);
107
+ const one = parsed.success ? touch(parsed.data.pr, at) : undefined;
108
+ if (parsed.success && one !== undefined) one.state = "closed";
109
+ break;
110
+ }
111
+ }
112
+ }
113
+ return [...found.values()]
114
+ .filter((one) => (filter.state === undefined || one.state === filter.state) && (filter.repo === undefined || (one.pr.repo.host === filter.repo.host && one.pr.repo.path === filter.repo.path)))
115
+ .sort((a, b) => Number(b.state === "open") - Number(a.state === "open") || b.changed - a.changed)
116
+ .map(({ changed: _changed, pipelines, author, head, ...rest }) => ({
117
+ ...rest,
118
+ ...(author === undefined ? {} : { author }),
119
+ ...(head === undefined ? {} : { head }),
120
+ pipelines: [...pipelines.values()].sort((a, b) => b.pipeline - a.pipeline).slice(0, PIPELINES_SHOWN),
121
+ }));
122
+ }