@venizia/vulcan 0.4.9-5 → 0.4.9
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/base/hooks/auto-clean.ts +22 -0
- package/base/hooks/session-start.ts +12 -0
- package/base/skills/retro/SKILL.md +2 -2
- package/base/templates/board.html +4697 -0
- package/package.json +3 -3
- package/src/board/command.ts +200 -0
- package/src/board/common/constants.ts +122 -0
- package/src/board/common/index.ts +2 -0
- package/src/board/common/types.ts +86 -0
- package/src/board/fold.ts +428 -0
- package/src/board/github.ts +255 -0
- package/src/board/index.ts +6 -0
- package/src/board/page.ts +26 -0
- package/src/board/settings.ts +106 -0
- package/src/commands/board.command.ts +5 -0
- package/src/commands/help.command.ts +5 -1
- package/src/commands/index.ts +2 -0
- package/src/common/constants.ts +1 -0
- package/src/config/schema.ts +59 -0
- package/src/doctor/checks.ts +29 -0
- package/src/doctor/common/constants.ts +1 -0
- package/src/doctor/runner.ts +1 -0
- package/src/events/command.ts +21 -2
- package/src/events/common/constants.ts +31 -0
- package/src/events/common/types.ts +15 -1
- package/src/events/index.ts +1 -0
- package/src/events/log.ts +7 -3
- package/src/events/retention.ts +172 -0
- package/src/events/settings.ts +12 -0
- package/src/local/auto-clean-run.ts +99 -0
- package/src/local/auto-clean.ts +147 -0
- package/src/local/clean.ts +33 -1
- package/src/local/constants.ts +1 -0
- package/src/local/index.ts +1 -0
- package/src/local/scan.ts +43 -6
- package/src/manifest/render.ts +36 -2
- package/src/router.ts +8 -0
|
@@ -0,0 +1,428 @@
|
|
|
1
|
+
import { EventKinds, EventStatuses } from '../events/common/index.ts';
|
|
2
|
+
import type { IEvent } from '../events/common/index.ts';
|
|
3
|
+
import {
|
|
4
|
+
BoardConstants,
|
|
5
|
+
BoardKinds,
|
|
6
|
+
BoardSources,
|
|
7
|
+
BoardStages,
|
|
8
|
+
BoardStates,
|
|
9
|
+
BoardStatuses,
|
|
10
|
+
} from './common/index.ts';
|
|
11
|
+
import type {
|
|
12
|
+
IBoardAgent,
|
|
13
|
+
IBoardDecision,
|
|
14
|
+
IBoardEvent,
|
|
15
|
+
IBoardFlow,
|
|
16
|
+
TBoardState,
|
|
17
|
+
} from './common/index.ts';
|
|
18
|
+
|
|
19
|
+
const PR_NUMBER = /^#?(\d+)$/;
|
|
20
|
+
/** A `pr` line with this ref says the pull request was merged: `--ref merged:12`. */
|
|
21
|
+
const MERGED_REF = /^merged:#?(\d+)$/;
|
|
22
|
+
const TO_PREFIX = 'to:';
|
|
23
|
+
/** Kinds that take the stage the flow is at when they happen: a message belongs to the stage it was sent in. */
|
|
24
|
+
const CARRY_KINDS: string[] = [BoardKinds.MSG, BoardKinds.NOTE];
|
|
25
|
+
|
|
26
|
+
/** By time only: lines of one moment keep the order they were given in (the order of the log), which `sort` keeps. */
|
|
27
|
+
const byTime = (left: IBoardEvent, right: IBoardEvent): number => left.ts.localeCompare(right.ts);
|
|
28
|
+
|
|
29
|
+
/** The rules that turn lines of the event log, and of GitHub, into flows with a state, a stage, a holder and attempts. Pure: no clock, no file, no network. */
|
|
30
|
+
export class BoardFold {
|
|
31
|
+
/**
|
|
32
|
+
* The lines of the event log as the page draws them, and what the session lines say of each agent. A session
|
|
33
|
+
* line only moves an agent; it is not part of any flow. A line with no flow id is in `Unsorted`.
|
|
34
|
+
*/
|
|
35
|
+
static fromLog(opts: { events: IEvent[] }): {
|
|
36
|
+
events: IBoardEvent[];
|
|
37
|
+
agents: Record<string, IBoardAgent>;
|
|
38
|
+
} {
|
|
39
|
+
const events: IBoardEvent[] = [];
|
|
40
|
+
const agents: Record<string, IBoardAgent> = {};
|
|
41
|
+
const sorted = [...opts.events].sort((left, right) => left.ts.localeCompare(right.ts));
|
|
42
|
+
|
|
43
|
+
for (const line of sorted) {
|
|
44
|
+
BoardFold.moveAgent({ agents, line });
|
|
45
|
+
|
|
46
|
+
const mapped = BoardFold.eventOf({ line });
|
|
47
|
+
|
|
48
|
+
if (mapped !== undefined) {
|
|
49
|
+
events.push(mapped);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return { events, agents };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* The flows of a set of lines and the last decision of each. A flow is waiting on the owner while its last ask
|
|
58
|
+
* has no later decision and no later progress. It is blocked while its newest CI run or review failed and nothing
|
|
59
|
+
* was committed since. Otherwise it is parked, done or running by its last line.
|
|
60
|
+
*/
|
|
61
|
+
static fold(opts: { events: IBoardEvent[]; owner: string }): {
|
|
62
|
+
flows: IBoardFlow[];
|
|
63
|
+
decisions: Record<string, IBoardDecision>;
|
|
64
|
+
} {
|
|
65
|
+
const byFlow = new Map<string, IBoardEvent[]>();
|
|
66
|
+
|
|
67
|
+
for (const event of opts.events) {
|
|
68
|
+
byFlow.set(event.flowId, [...(byFlow.get(event.flowId) ?? []), event]);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const flows: IBoardFlow[] = [];
|
|
72
|
+
const decisions: Record<string, IBoardDecision> = {};
|
|
73
|
+
|
|
74
|
+
for (const [id, lines] of byFlow) {
|
|
75
|
+
const events = BoardFold.prepare({ lines });
|
|
76
|
+
const decision = BoardFold.lastDecision({ events });
|
|
77
|
+
|
|
78
|
+
if (decision !== undefined) {
|
|
79
|
+
decisions[id] = decision;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
flows.push(BoardFold.flowOf({ id, events, owner: opts.owner }));
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return { flows, decisions };
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
private static eventOf(opts: { line: IEvent }): IBoardEvent | undefined {
|
|
89
|
+
const { line } = opts;
|
|
90
|
+
const base = {
|
|
91
|
+
id: line.id,
|
|
92
|
+
flowId: line.flowId ?? BoardConstants.UNSORTED,
|
|
93
|
+
ts: line.ts,
|
|
94
|
+
actor: line.actor,
|
|
95
|
+
summary: line.summary,
|
|
96
|
+
source: line.source,
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
switch (line.kind) {
|
|
100
|
+
case EventKinds.COMMIT: {
|
|
101
|
+
return { ...base, kind: BoardKinds.COMMIT, stage: BoardStages.CODE };
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
case EventKinds.PR: {
|
|
105
|
+
const merged = MERGED_REF.exec(line.ref ?? '')?.[1];
|
|
106
|
+
|
|
107
|
+
if (merged !== undefined) {
|
|
108
|
+
return {
|
|
109
|
+
...base,
|
|
110
|
+
kind: BoardKinds.MERGE,
|
|
111
|
+
stage: BoardStages.MERGE,
|
|
112
|
+
status: BoardStatuses.DONE,
|
|
113
|
+
ref: { pr: Number(merged) },
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const number = PR_NUMBER.exec(line.ref ?? '')?.[1];
|
|
118
|
+
|
|
119
|
+
return {
|
|
120
|
+
...base,
|
|
121
|
+
kind: BoardKinds.PR,
|
|
122
|
+
stage: BoardStages.PR_AND_CI,
|
|
123
|
+
...(number === undefined ? {} : { ref: { pr: Number(number) } }),
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
case EventKinds.MSG_SENT:
|
|
128
|
+
case EventKinds.MSG_RECEIVED: {
|
|
129
|
+
const to = line.ref?.startsWith(TO_PREFIX) ? line.ref.slice(TO_PREFIX.length) : undefined;
|
|
130
|
+
|
|
131
|
+
return { ...base, kind: BoardKinds.MSG, stage: -1, ...(to === undefined ? {} : { to }) };
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
case EventKinds.NEEDS_OWNER: {
|
|
135
|
+
return { ...base, kind: BoardKinds.NOTE, stage: -1, needsOwner: true };
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
case EventKinds.DECISION: {
|
|
139
|
+
return {
|
|
140
|
+
...base,
|
|
141
|
+
kind: BoardKinds.DECISION,
|
|
142
|
+
stage: BoardStages.APPROVAL,
|
|
143
|
+
...(line.status === EventStatuses.PARK ? { status: BoardStatuses.PARKED } : {}),
|
|
144
|
+
...(line.status === EventStatuses.REJECT ? { status: BoardStatuses.DROPPED } : {}),
|
|
145
|
+
...(line.status === undefined ? {} : { choice: line.status }),
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
default: {
|
|
150
|
+
return undefined;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
private static moveAgent(opts: { agents: Record<string, IBoardAgent>; line: IEvent }): void {
|
|
156
|
+
const { agents, line } = opts;
|
|
157
|
+
const known = agents[line.actor];
|
|
158
|
+
|
|
159
|
+
if (line.kind === EventKinds.SUBAGENT_STOP) {
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
if (line.kind === EventKinds.SESSION_START || line.kind === EventKinds.SESSION_STOP) {
|
|
164
|
+
const state = line.kind === EventKinds.SESSION_START ? 'working' : 'idle';
|
|
165
|
+
|
|
166
|
+
agents[line.actor] = {
|
|
167
|
+
name: line.actor,
|
|
168
|
+
state,
|
|
169
|
+
current: known?.current ?? '',
|
|
170
|
+
since: known?.state === state ? known.since : line.ts,
|
|
171
|
+
checkedAt: line.ts,
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
agents[line.actor] = {
|
|
178
|
+
name: line.actor,
|
|
179
|
+
state: known?.state ?? 'working',
|
|
180
|
+
current: line.summary,
|
|
181
|
+
since: known?.since ?? line.ts,
|
|
182
|
+
checkedAt: line.ts,
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/** Sorts a flow's lines, gives every carried line the stage of the one before it, and every CI run and review its attempt. */
|
|
187
|
+
private static prepare(opts: { lines: IBoardEvent[] }): IBoardEvent[] {
|
|
188
|
+
const sorted = [...opts.lines].sort(byTime);
|
|
189
|
+
const runs = new Map<string, number>();
|
|
190
|
+
let stage: number = BoardStages.REQUEST;
|
|
191
|
+
|
|
192
|
+
return sorted.map((event) => {
|
|
193
|
+
const own =
|
|
194
|
+
event.stage >= 0
|
|
195
|
+
? event.stage
|
|
196
|
+
: CARRY_KINDS.includes(event.kind)
|
|
197
|
+
? stage
|
|
198
|
+
: BoardStages.REQUEST;
|
|
199
|
+
|
|
200
|
+
stage = own;
|
|
201
|
+
|
|
202
|
+
if (event.kind !== BoardKinds.CI && event.kind !== BoardKinds.REVIEW) {
|
|
203
|
+
return { ...event, stage: own };
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const key = `${event.kind}:${event.check ?? ''}`;
|
|
207
|
+
const attempt = event.attempt ?? (runs.get(key) ?? 0) + 1;
|
|
208
|
+
|
|
209
|
+
runs.set(key, attempt);
|
|
210
|
+
|
|
211
|
+
return { ...event, stage: own, attempt };
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
private static lastDecision(opts: { events: IBoardEvent[] }): IBoardDecision | undefined {
|
|
216
|
+
const decided = [...opts.events].reverse().find((event) => event.kind === BoardKinds.DECISION);
|
|
217
|
+
const choice = decided?.choice;
|
|
218
|
+
|
|
219
|
+
if (decided === undefined || choice === undefined) {
|
|
220
|
+
return undefined;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
return {
|
|
224
|
+
choice:
|
|
225
|
+
choice === EventStatuses.PARK
|
|
226
|
+
? 'later'
|
|
227
|
+
: choice === EventStatuses.REJECT
|
|
228
|
+
? 'reject'
|
|
229
|
+
: 'approve',
|
|
230
|
+
at: decided.ts,
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
private static flowOf(opts: { id: string; events: IBoardEvent[]; owner: string }): IBoardFlow {
|
|
235
|
+
const { id, events } = opts;
|
|
236
|
+
// A CI line after its pull request was closed is noise.
|
|
237
|
+
const closed = new Map<string, string>();
|
|
238
|
+
|
|
239
|
+
for (const event of events) {
|
|
240
|
+
if (event.status === BoardStatuses.CLOSED && event.ref?.pr !== undefined) {
|
|
241
|
+
closed.set(`${event.ref.repo ?? ''}#${event.ref.pr}`, event.ts);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const live = events.filter((event) => {
|
|
246
|
+
const at =
|
|
247
|
+
event.kind === BoardKinds.CI && event.ref?.pr !== undefined
|
|
248
|
+
? closed.get(`${event.ref.repo ?? ''}#${event.ref.pr}`)
|
|
249
|
+
: undefined;
|
|
250
|
+
|
|
251
|
+
return at === undefined || event.ts <= at;
|
|
252
|
+
});
|
|
253
|
+
const last = live.at(-1) ?? events[events.length - 1];
|
|
254
|
+
const ask = [...events].reverse().find((event) => event.needsOwner === true);
|
|
255
|
+
const state = BoardFold.stateOf({ events, live, last, ask });
|
|
256
|
+
const stages = [...new Set(events.map((event) => event.stage))].sort(
|
|
257
|
+
(left, right) => left - right,
|
|
258
|
+
);
|
|
259
|
+
const repo = events.find((event) => event.ref?.repo !== undefined)?.ref?.repo;
|
|
260
|
+
|
|
261
|
+
return {
|
|
262
|
+
id,
|
|
263
|
+
title: BoardFold.titleOf({ id, events }),
|
|
264
|
+
...(repo === undefined ? {} : { repo }),
|
|
265
|
+
state,
|
|
266
|
+
holder: BoardFold.holderOf({ events, state, owner: opts.owner }),
|
|
267
|
+
stages,
|
|
268
|
+
cur: last.stage,
|
|
269
|
+
lastId: last.id,
|
|
270
|
+
...(ask === undefined ? {} : { askId: ask.id }),
|
|
271
|
+
ciRuns: events.filter((event) => event.kind === BoardKinds.CI).length,
|
|
272
|
+
reviewRounds: new Set(
|
|
273
|
+
events
|
|
274
|
+
.filter((event) => event.kind === BoardKinds.REVIEW)
|
|
275
|
+
.map((event) => event.attempt ?? 1),
|
|
276
|
+
).size,
|
|
277
|
+
events,
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
private static stateOf(opts: {
|
|
282
|
+
events: IBoardEvent[];
|
|
283
|
+
live: IBoardEvent[];
|
|
284
|
+
last: IBoardEvent;
|
|
285
|
+
ask: IBoardEvent | undefined;
|
|
286
|
+
}): TBoardState {
|
|
287
|
+
const { events, live, last, ask } = opts;
|
|
288
|
+
|
|
289
|
+
// Positions in the sorted lines, not times: an ask and its answer written in the same moment keep their order.
|
|
290
|
+
if (ask !== undefined) {
|
|
291
|
+
const later = events.slice(events.indexOf(ask) + 1);
|
|
292
|
+
const isAnswered =
|
|
293
|
+
later.some((event) => event.kind === BoardKinds.DECISION) ||
|
|
294
|
+
later.some((event) => BoardConstants.PROGRESS_KINDS.includes(event.kind)) ||
|
|
295
|
+
later.some(
|
|
296
|
+
(event) => event.status === BoardStatuses.DONE || event.status === BoardStatuses.PARKED,
|
|
297
|
+
);
|
|
298
|
+
|
|
299
|
+
if (!isAnswered) {
|
|
300
|
+
return BoardStates.YOU;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
// A merge is Done until something reopens it: a commit, a fix or another pull request after it.
|
|
305
|
+
const merged = events.findLastIndex(
|
|
306
|
+
(event) => event.kind === BoardKinds.MERGE || event.status === BoardStatuses.DONE,
|
|
307
|
+
);
|
|
308
|
+
|
|
309
|
+
if (merged >= 0 && !events.slice(merged + 1).some((event) => BoardFold.reopens({ event }))) {
|
|
310
|
+
return BoardStates.DONE;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// A park or reject holds until work starts again after it.
|
|
314
|
+
const parked = events.findLastIndex(
|
|
315
|
+
(event) =>
|
|
316
|
+
event.kind === BoardKinds.DECISION &&
|
|
317
|
+
(event.status === BoardStatuses.PARKED || event.status === BoardStatuses.DROPPED),
|
|
318
|
+
);
|
|
319
|
+
|
|
320
|
+
if (
|
|
321
|
+
parked >= 0 &&
|
|
322
|
+
events.findLastIndex((event) => event.kind === BoardKinds.DECISION) === parked &&
|
|
323
|
+
!events.slice(parked + 1).some((event) => BoardConstants.PROGRESS_KINDS.includes(event.kind))
|
|
324
|
+
) {
|
|
325
|
+
return BoardStates.PARKED;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
if (BoardFold.isBlocked({ live })) {
|
|
329
|
+
return BoardStates.BLOCKED;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
switch (last.status) {
|
|
333
|
+
case BoardStatuses.PARKED:
|
|
334
|
+
case BoardStatuses.DROPPED: {
|
|
335
|
+
return BoardStates.PARKED;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
case BoardStatuses.FAIL:
|
|
339
|
+
case BoardStatuses.BLOCKED: {
|
|
340
|
+
return BoardStates.BLOCKED;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
default: {
|
|
344
|
+
return BoardStates.RUNNING;
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/** Work that starts again after a merge. */
|
|
350
|
+
private static reopens(opts: { event: IBoardEvent }): boolean {
|
|
351
|
+
const { event } = opts;
|
|
352
|
+
|
|
353
|
+
return (
|
|
354
|
+
event.kind === BoardKinds.COMMIT ||
|
|
355
|
+
event.kind === BoardKinds.FIX ||
|
|
356
|
+
(event.kind === BoardKinds.PR && event.status !== BoardStatuses.CLOSED)
|
|
357
|
+
);
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/** The newest CI run or review failed and nothing was committed or merged since. */
|
|
361
|
+
private static isBlocked(opts: { live: IBoardEvent[] }): boolean {
|
|
362
|
+
const judged = [...opts.live]
|
|
363
|
+
.reverse()
|
|
364
|
+
.find((event) => event.kind === BoardKinds.CI || event.kind === BoardKinds.REVIEW);
|
|
365
|
+
|
|
366
|
+
if (judged === undefined || judged.status !== BoardStatuses.FAIL) {
|
|
367
|
+
return false;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
return !opts.live.some(
|
|
371
|
+
(event) =>
|
|
372
|
+
event.ts > judged.ts &&
|
|
373
|
+
(event.kind === BoardKinds.COMMIT ||
|
|
374
|
+
event.kind === BoardKinds.FIX ||
|
|
375
|
+
event.kind === BoardKinds.MERGE ||
|
|
376
|
+
event.status === BoardStatuses.DONE),
|
|
377
|
+
);
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
private static holderOf(opts: {
|
|
381
|
+
events: IBoardEvent[];
|
|
382
|
+
state: TBoardState;
|
|
383
|
+
owner: string;
|
|
384
|
+
}): string {
|
|
385
|
+
if (opts.state === BoardStates.YOU) {
|
|
386
|
+
return opts.owner;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
const reversed = [...opts.events].reverse();
|
|
390
|
+
const work = reversed.find(
|
|
391
|
+
(event) => BoardConstants.WORK_KINDS.includes(event.kind) && event.actor !== opts.owner,
|
|
392
|
+
);
|
|
393
|
+
const named = reversed.find(
|
|
394
|
+
(event) =>
|
|
395
|
+
event.kind === BoardKinds.MSG &&
|
|
396
|
+
event.to !== undefined &&
|
|
397
|
+
!event.to.includes(':') &&
|
|
398
|
+
event.to !== opts.owner,
|
|
399
|
+
);
|
|
400
|
+
|
|
401
|
+
if (named !== undefined && (work === undefined || named.ts > work.ts)) {
|
|
402
|
+
return named.to ?? named.actor;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
const fallback =
|
|
406
|
+
work ??
|
|
407
|
+
reversed.find(
|
|
408
|
+
(event) => event.actor !== BoardConstants.CI_ACTOR && event.kind !== BoardKinds.REVIEW,
|
|
409
|
+
) ??
|
|
410
|
+
reversed[0];
|
|
411
|
+
|
|
412
|
+
return fallback.kind === BoardKinds.ASSIGN && fallback.to !== undefined
|
|
413
|
+
? fallback.to
|
|
414
|
+
: fallback.actor;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
private static titleOf(opts: { id: string; events: IBoardEvent[] }): string {
|
|
418
|
+
if (opts.id === BoardConstants.UNSORTED) {
|
|
419
|
+
return BoardConstants.UNSORTED;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
const pr = opts.events.find(
|
|
423
|
+
(event) => event.kind === BoardKinds.PR && event.source === BoardSources.GITHUB,
|
|
424
|
+
);
|
|
425
|
+
|
|
426
|
+
return (pr ?? opts.events[0]).summary;
|
|
427
|
+
}
|
|
428
|
+
}
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
import { spawnSync } from 'node:child_process';
|
|
2
|
+
import { RecordValue } from '../common/record.ts';
|
|
3
|
+
import { EventFlow } from '../events/flow.ts';
|
|
4
|
+
import {
|
|
5
|
+
BoardConstants,
|
|
6
|
+
BoardKinds,
|
|
7
|
+
BoardSources,
|
|
8
|
+
BoardStages,
|
|
9
|
+
BoardStatuses,
|
|
10
|
+
} from './common/index.ts';
|
|
11
|
+
import type { IBoardEvent } from './common/index.ts';
|
|
12
|
+
|
|
13
|
+
const PR_FIELDS =
|
|
14
|
+
'number,title,author,headRefName,body,createdAt,updatedAt,mergedAt,closedAt,state,url,reviews,statusCheckRollup';
|
|
15
|
+
const PASSED = new Set(['SUCCESS', 'NEUTRAL', 'SKIPPED']);
|
|
16
|
+
const FAILED = new Set([
|
|
17
|
+
'FAILURE',
|
|
18
|
+
'TIMED_OUT',
|
|
19
|
+
'CANCELLED',
|
|
20
|
+
'ACTION_REQUIRED',
|
|
21
|
+
'STARTUP_FAILURE',
|
|
22
|
+
'ERROR',
|
|
23
|
+
]);
|
|
24
|
+
const BODY_FLOW = /\bFlow:\s*(F-\d+)\b/;
|
|
25
|
+
|
|
26
|
+
const text = (opts: { value: unknown }): string =>
|
|
27
|
+
typeof opts.value === 'string' ? opts.value : '';
|
|
28
|
+
|
|
29
|
+
/** Pull requests, their checks and their reviews, read through `gh`: an extra for projects on GitHub. */
|
|
30
|
+
export class BoardGithub {
|
|
31
|
+
/**
|
|
32
|
+
* The lines of the pull requests of `repos` (the current repository when none is named) that changed since `since`.
|
|
33
|
+
* Nothing is posted. When `gh` is missing, not logged in, or the folder is not a GitHub repository, the answer is
|
|
34
|
+
* no lines and a note that says why, never an error: the board is fully useful from the event log alone.
|
|
35
|
+
*/
|
|
36
|
+
static read(opts: { repos: string[]; cwd: string; since: number }): {
|
|
37
|
+
events: IBoardEvent[];
|
|
38
|
+
note: string;
|
|
39
|
+
} {
|
|
40
|
+
if (Bun.which('gh') === null) {
|
|
41
|
+
return { events: [], note: 'github: skipped, gh is not installed' };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const repos = opts.repos.length > 0 ? opts.repos : BoardGithub.currentRepo({ cwd: opts.cwd });
|
|
45
|
+
|
|
46
|
+
if (repos === undefined || repos.length === 0) {
|
|
47
|
+
return {
|
|
48
|
+
events: [],
|
|
49
|
+
note: 'github: skipped, this folder is not a GitHub repository gh can read',
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const events: IBoardEvent[] = [];
|
|
54
|
+
const notes: string[] = [];
|
|
55
|
+
|
|
56
|
+
for (const repo of repos) {
|
|
57
|
+
const listed = spawnSync(
|
|
58
|
+
'gh',
|
|
59
|
+
[
|
|
60
|
+
'pr',
|
|
61
|
+
'list',
|
|
62
|
+
'--repo',
|
|
63
|
+
repo,
|
|
64
|
+
'--state',
|
|
65
|
+
'all',
|
|
66
|
+
'--limit',
|
|
67
|
+
String(BoardConstants.GH_LIMIT),
|
|
68
|
+
'--json',
|
|
69
|
+
PR_FIELDS,
|
|
70
|
+
],
|
|
71
|
+
{ cwd: opts.cwd, encoding: 'utf8', timeout: BoardConstants.GH_TIMEOUT_MS },
|
|
72
|
+
);
|
|
73
|
+
|
|
74
|
+
if (listed.status !== 0) {
|
|
75
|
+
notes.push(
|
|
76
|
+
`${repo} skipped (${(listed.stderr ?? '').trim().split('\n')[0] || 'gh failed'})`,
|
|
77
|
+
);
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
let parsed: unknown;
|
|
82
|
+
|
|
83
|
+
try {
|
|
84
|
+
parsed = JSON.parse(listed.stdout);
|
|
85
|
+
} catch {
|
|
86
|
+
notes.push(`${repo} skipped (gh did not answer with JSON)`);
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const prs = Array.isArray(parsed) ? parsed : [];
|
|
91
|
+
const fresh = prs.filter(
|
|
92
|
+
(pr) => RecordValue.isRecord(pr) && Date.parse(text({ value: pr.updatedAt })) >= opts.since,
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
for (const pr of fresh) {
|
|
96
|
+
events.push(...BoardGithub.eventsOf({ pr, repo }));
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
notes.push(
|
|
100
|
+
`${repo} ${fresh.length} pull request(s) since the date (the newest ${BoardConstants.GH_LIMIT} are asked for)`,
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return { events, note: `github: ${notes.join(', ')}` };
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** `Flow: F-n` in the body of a pull request, else an `F-n` segment of its branch, else none. */
|
|
108
|
+
static flowOf(opts: { body: string; branch: string }): string | undefined {
|
|
109
|
+
return BODY_FLOW.exec(opts.body)?.[1] ?? EventFlow.fromBranchName({ branch: opts.branch });
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** The lines one pull request stands for: opened, each check, each review, merged or closed. */
|
|
113
|
+
static eventsOf(opts: { pr: unknown; repo: string }): IBoardEvent[] {
|
|
114
|
+
const { pr, repo } = opts;
|
|
115
|
+
|
|
116
|
+
if (!RecordValue.isRecord(pr) || typeof pr.number !== 'number') {
|
|
117
|
+
return [];
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const number = pr.number;
|
|
121
|
+
const author = RecordValue.isRecord(pr.author) ? text({ value: pr.author.login }) : '';
|
|
122
|
+
const flowId =
|
|
123
|
+
BoardGithub.flowOf({
|
|
124
|
+
body: text({ value: pr.body }),
|
|
125
|
+
branch: text({ value: pr.headRefName }),
|
|
126
|
+
}) ?? BoardConstants.UNSORTED;
|
|
127
|
+
const base = {
|
|
128
|
+
flowId,
|
|
129
|
+
source: BoardSources.GITHUB,
|
|
130
|
+
ref: { repo, pr: number },
|
|
131
|
+
link: text({ value: pr.url }),
|
|
132
|
+
};
|
|
133
|
+
const id = (part: string): string => `gh:${repo}#${number}:${part}`;
|
|
134
|
+
const events: IBoardEvent[] = [
|
|
135
|
+
{
|
|
136
|
+
...base,
|
|
137
|
+
id: id('pr'),
|
|
138
|
+
ts: text({ value: pr.createdAt }),
|
|
139
|
+
kind: BoardKinds.PR,
|
|
140
|
+
stage: BoardStages.PR_AND_CI,
|
|
141
|
+
actor: author,
|
|
142
|
+
status: BoardStatuses.OPEN,
|
|
143
|
+
summary: `#${number} ${text({ value: pr.title })}`,
|
|
144
|
+
},
|
|
145
|
+
];
|
|
146
|
+
|
|
147
|
+
for (const [index, check] of (Array.isArray(pr.statusCheckRollup)
|
|
148
|
+
? pr.statusCheckRollup
|
|
149
|
+
: []
|
|
150
|
+
).entries()) {
|
|
151
|
+
const line = BoardGithub.checkOf({ check, index, base, id });
|
|
152
|
+
|
|
153
|
+
if (line !== undefined) {
|
|
154
|
+
events.push(line);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
for (const [index, review] of (Array.isArray(pr.reviews) ? pr.reviews : []).entries()) {
|
|
159
|
+
if (RecordValue.isRecord(review) && text({ value: review.submittedAt }) !== '') {
|
|
160
|
+
const state = text({ value: review.state });
|
|
161
|
+
|
|
162
|
+
events.push({
|
|
163
|
+
...base,
|
|
164
|
+
id: id(`review-${index}`),
|
|
165
|
+
ts: text({ value: review.submittedAt }),
|
|
166
|
+
kind: BoardKinds.REVIEW,
|
|
167
|
+
stage: BoardStages.REVIEW,
|
|
168
|
+
actor: RecordValue.isRecord(review.author)
|
|
169
|
+
? text({ value: review.author.login })
|
|
170
|
+
: 'reviewer',
|
|
171
|
+
status: state === 'CHANGES_REQUESTED' ? BoardStatuses.FAIL : BoardStatuses.PASS,
|
|
172
|
+
summary: state.toLowerCase().replace('_', ' '),
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const merged = text({ value: pr.mergedAt });
|
|
178
|
+
const closed = text({ value: pr.closedAt });
|
|
179
|
+
|
|
180
|
+
if (merged !== '') {
|
|
181
|
+
events.push({
|
|
182
|
+
...base,
|
|
183
|
+
id: id('merge'),
|
|
184
|
+
ts: merged,
|
|
185
|
+
kind: BoardKinds.MERGE,
|
|
186
|
+
stage: BoardStages.MERGE,
|
|
187
|
+
actor: author,
|
|
188
|
+
status: BoardStatuses.DONE,
|
|
189
|
+
summary: 'merged',
|
|
190
|
+
});
|
|
191
|
+
} else if (closed !== '' && text({ value: pr.state }) === 'CLOSED') {
|
|
192
|
+
events.push({
|
|
193
|
+
...base,
|
|
194
|
+
id: id('closed'),
|
|
195
|
+
ts: closed,
|
|
196
|
+
kind: BoardKinds.PR,
|
|
197
|
+
stage: BoardStages.PR_AND_CI,
|
|
198
|
+
actor: author,
|
|
199
|
+
status: BoardStatuses.CLOSED,
|
|
200
|
+
summary: 'closed without merge',
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
return events.filter((event) => event.ts !== '');
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
private static checkOf(opts: {
|
|
208
|
+
check: unknown;
|
|
209
|
+
index: number;
|
|
210
|
+
base: Pick<IBoardEvent, 'flowId' | 'source' | 'ref' | 'link'>;
|
|
211
|
+
id: (part: string) => string;
|
|
212
|
+
}): IBoardEvent | undefined {
|
|
213
|
+
const { check } = opts;
|
|
214
|
+
|
|
215
|
+
if (!RecordValue.isRecord(check)) {
|
|
216
|
+
return undefined;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
const name = text({ value: check.name }) || text({ value: check.context }) || 'check';
|
|
220
|
+
const outcome = (
|
|
221
|
+
text({ value: check.conclusion }) || text({ value: check.state })
|
|
222
|
+
).toUpperCase();
|
|
223
|
+
const at = text({ value: check.completedAt }) || text({ value: check.startedAt });
|
|
224
|
+
|
|
225
|
+
if (at === '' || (!PASSED.has(outcome) && !FAILED.has(outcome))) {
|
|
226
|
+
return undefined;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
const url = text({ value: check.detailsUrl }) || text({ value: check.targetUrl });
|
|
230
|
+
|
|
231
|
+
return {
|
|
232
|
+
...opts.base,
|
|
233
|
+
...(url.startsWith('https://') ? { link: url } : {}),
|
|
234
|
+
id: opts.id(`ci-${opts.index}`),
|
|
235
|
+
ts: at,
|
|
236
|
+
kind: BoardKinds.CI,
|
|
237
|
+
stage: BoardStages.PR_AND_CI,
|
|
238
|
+
actor: BoardConstants.CI_ACTOR,
|
|
239
|
+
check: name,
|
|
240
|
+
status: FAILED.has(outcome) ? BoardStatuses.FAIL : BoardStatuses.PASS,
|
|
241
|
+
summary: name,
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
private static currentRepo(opts: { cwd: string }): string[] | undefined {
|
|
246
|
+
const result = spawnSync(
|
|
247
|
+
'gh',
|
|
248
|
+
['repo', 'view', '--json', 'nameWithOwner', '--jq', '.nameWithOwner'],
|
|
249
|
+
{ cwd: opts.cwd, encoding: 'utf8', timeout: BoardConstants.GH_TIMEOUT_MS },
|
|
250
|
+
);
|
|
251
|
+
const name = result.status === 0 ? result.stdout.trim() : '';
|
|
252
|
+
|
|
253
|
+
return name === '' ? undefined : [name];
|
|
254
|
+
}
|
|
255
|
+
}
|