@williacj/rigger 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +370 -2
- package/package.json +56 -4
- package/scripts/doc-reference-check.mjs +59 -0
- package/scripts/gate-check.mjs +249 -0
- package/scripts/lib/pack-result.mjs +9 -0
- package/scripts/record-commit.mjs +67 -0
- package/src/cli/doctor.mjs +1021 -0
- package/src/cli/init.mjs +384 -0
- package/src/cli/once.mjs +489 -0
- package/src/cli/pause.mjs +56 -0
- package/src/cli/plan.mjs +106 -0
- package/src/cli/recording.mjs +52 -0
- package/src/cli/report.mjs +242 -0
- package/src/cli/resume.mjs +96 -0
- package/src/cli/rigger.mjs +12 -0
- package/src/cli/run.mjs +10 -0
- package/src/cli/setup-board.mjs +108 -0
- package/src/cli/verbs.mjs +82 -0
- package/src/cli/verdict.mjs +180 -0
- package/src/config/validate.mjs +960 -0
- package/src/execution/groups.mjs +185 -0
- package/src/execution/kept-output.mjs +21 -0
- package/src/execution/role.mjs +171 -0
- package/src/execution/run.mjs +490 -0
- package/src/execution/step.mjs +26 -0
- package/src/execution/workspace.mjs +866 -0
- package/src/observation/push.mjs +103 -0
- package/src/observation/report.mjs +415 -0
- package/src/observation/sink.mjs +271 -0
- package/src/package-commit.json +1 -0
- package/src/scheduling/admission.mjs +390 -0
- package/src/scheduling/clock.mjs +98 -0
- package/src/scheduling/lane.mjs +160 -0
- package/src/scheduling/loop.mjs +1738 -0
- package/src/scheduling/pull-order.mjs +46 -0
- package/src/substrate/data-ref.mjs +147 -0
- package/src/substrate/forge/assets.mjs +5 -0
- package/src/substrate/forge/graphql.mjs +258 -0
- package/src/substrate/forge/item-write.mjs +74 -0
- package/src/substrate/forge/read.mjs +998 -0
- package/src/substrate/forge/repository-write.mjs +152 -0
- package/src/substrate/forge/runners.mjs +510 -0
- package/src/substrate/forge/schema-write.mjs +70 -0
- package/src/substrate/forge/write-made.mjs +40 -0
- package/src/substrate/git-environment.mjs +54 -0
- package/src/substrate/host.mjs +23 -0
- package/src/substrate/launchd.mjs +23 -0
- package/src/substrate/process-table.sh +48 -0
- package/src/substrate/process.mjs +2474 -0
- package/src/substrate/providers/adapters.mjs +8 -0
- package/src/substrate/providers/claude.mjs +427 -0
- package/src/substrate/providers/codex.mjs +349 -0
- package/src/substrate/standard-error.mjs +134 -0
- package/src/substrate/worktrees.mjs +517 -0
- package/src/workflow/clock-mark.mjs +40 -0
- package/src/workflow/escalation.mjs +110 -0
- package/src/workflow/facts.mjs +92 -0
- package/src/workflow/first-above.mjs +17 -0
- package/src/workflow/form-check.mjs +88 -0
- package/src/workflow/gate.mjs +62 -0
- package/src/workflow/judges.mjs +695 -0
- package/src/workflow/marker.mjs +265 -0
- package/src/workflow/next-action.mjs +456 -0
- package/src/workflow/status-issue.mjs +268 -0
- package/src/workflow/transitions.mjs +588 -0
- package/templates/claude/agents/architect.md +116 -0
- package/templates/claude/agents/engineer.md +57 -0
- package/templates/claude/agents/pm.md +114 -0
- package/templates/claude/agents/reviewer.md +37 -0
- package/templates/claude/agents/spike-engineer.md +80 -0
- package/templates/claude/hooks/refuse-reserved-git-commands.mjs +909 -0
- package/templates/claude/settings.json +50 -0
- package/templates/claude/skills/acceptance/SKILL.md +275 -0
- package/templates/claude/skills/agent-style/SKILL.md +78 -0
- package/templates/claude/skills/code-review/SKILL.md +242 -0
- package/templates/claude/skills/proposal/SKILL.md +175 -0
- package/templates/claude/skills/spec-style/SKILL.md +112 -0
- package/templates/claude/skills/tdd/SKILL.md +230 -0
- package/templates/codex/config.toml +19 -0
- package/templates/doc-reference-check.md +25 -0
- package/templates/githooks/pre-push +24 -0
- package/templates/github/workflows/rigger-gate.yml +209 -0
- package/templates/launchd/com.williacj.rigger.plist +31 -0
- package/templates/rigger.config.mjs +78 -0
|
@@ -0,0 +1,998 @@
|
|
|
1
|
+
// ABOUTME: The forge adapter's read side: the board's cards, columns and single-select fields, the
|
|
2
|
+
// repository's labels, branches, default branch, visibility, Actions event policies, issues, pull
|
|
3
|
+
// requests and files at a commit, and the IDs a write names, each read through the read runner.
|
|
4
|
+
|
|
5
|
+
import { literal } from './graphql.mjs';
|
|
6
|
+
import { API_VERSION_HEADER, COLUMNS, DIFF_HEADER, firstLine, graphqlRequest, readRunner } from './runners.mjs';
|
|
7
|
+
|
|
8
|
+
/** The most nodes GitHub answers in one page of a connection. */
|
|
9
|
+
const PAGE = 100;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The user or organisation that holds the board: the `owner` the config declares under `board`,
|
|
13
|
+
* and where it declares none, the repository's owner, named before the slash in `board.repo`.
|
|
14
|
+
* This is the only place the repository's owner stands in for the key (`ARCHITECTURE.md`, below
|
|
15
|
+
* the extension-point table).
|
|
16
|
+
*/
|
|
17
|
+
const ownerOf = (board) => board.owner ?? board.repo.split('/')[0];
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* What a failure says of a request that found the board by its owner and number, so a board gh
|
|
21
|
+
* says is not there is named as the one asked for, and a wrong owner shows.
|
|
22
|
+
*/
|
|
23
|
+
const asking = (board) => `asking for ${ownerOf(board)}'s board ${board.project}, `;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The data `gh` answered to a request made for `operation` on `board`. Where `gh` did not answer
|
|
27
|
+
* 0, it throws an error naming the operation, the board number, what the request `addressed` if
|
|
28
|
+
* it found the board, and the first line `gh` said.
|
|
29
|
+
*/
|
|
30
|
+
export function answerOf(operation, board, said, addressed = '') {
|
|
31
|
+
if (said.status !== 0) throw new Error(`${operation} on board ${board.project} failed: ${addressed}${firstLine(said)}`);
|
|
32
|
+
return JSON.parse(said.stdout).data;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* What `gh` answered to the read `query`, made for `operation` on `board` and sent as `via` says:
|
|
37
|
+
* the read runner's `send`, `emitter` and `timeout`.
|
|
38
|
+
*/
|
|
39
|
+
const asked = async (operation, board, query, via, addressed) =>
|
|
40
|
+
answerOf(operation, board, await readRunner(graphqlRequest(query), via), addressed);
|
|
41
|
+
|
|
42
|
+
/** Throws the error a read gives when the board answered something it cannot use. */
|
|
43
|
+
function fail(operation, board, why, code) {
|
|
44
|
+
const failure = new Error(`${operation} on board ${board.project} failed: ${why}`);
|
|
45
|
+
if (code !== undefined) failure.code = code;
|
|
46
|
+
throw failure;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* A query selecting `selection` on the board numbered `board.project` among the boards of its
|
|
51
|
+
* owner.
|
|
52
|
+
*/
|
|
53
|
+
function boardQuery(operation, board, selection) {
|
|
54
|
+
if (!Number.isInteger(board.project)) {
|
|
55
|
+
throw new Error(`${operation} failed: the board number is ${board.project}, which is not a board number`);
|
|
56
|
+
}
|
|
57
|
+
return `query { repositoryOwner(login: ${literal(ownerOf(board))}) { ... on ProjectV2Owner { projectV2(number: ${board.project}) { ${selection} } } } }`;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** A query selecting `selection` on the repository `board.repo` names as `owner/name`. */
|
|
61
|
+
function repositoryQuery(board, selection) {
|
|
62
|
+
const [owner, name] = board.repo.split('/');
|
|
63
|
+
return `query { repository(owner: ${literal(owner)}, name: ${literal(name)}) { ${selection} } }`;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Every node of a connection, read a page at a time. `query` builds a page's document from its
|
|
68
|
+
* `after` argument, and `connectionOf` finds the connection in what `gh` answered. A page that
|
|
69
|
+
* fails fails the whole read, so no part of it is returned. `addressed` is what a failure says of
|
|
70
|
+
* a read that finds the board, and is empty for one that does not.
|
|
71
|
+
*/
|
|
72
|
+
async function everyPage(operation, board, via, query, connectionOf, addressed = '') {
|
|
73
|
+
const nodes = [];
|
|
74
|
+
let after = '';
|
|
75
|
+
for (;;) {
|
|
76
|
+
const connection = connectionOf(await asked(operation, board, query(`first: ${PAGE}${after}`), via, addressed));
|
|
77
|
+
if (!connection) fail(operation, board, `${addressed}gh answered no such board`);
|
|
78
|
+
nodes.push(...connection.nodes);
|
|
79
|
+
if (!connection.pageInfo.hasNextPage) return nodes;
|
|
80
|
+
after = `, after: ${literal(connection.pageInfo.endCursor)}`;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* The board numbered `board.project` among the boards of its owner: its ID, and the ID and
|
|
86
|
+
* options of its field holding the columns, each option as `{ id, name, color, description }` in
|
|
87
|
+
* board order, which is all a write adding an option has to send back of it.
|
|
88
|
+
*/
|
|
89
|
+
export async function boardOf(operation, board, via) {
|
|
90
|
+
const query = boardQuery(operation, board, `id field(name: ${literal(COLUMNS)}) { ... on ProjectV2SingleSelectField { id options { id name color description } } }`);
|
|
91
|
+
const project = (await asked(operation, board, query, via, asking(board)))?.repositoryOwner?.projectV2;
|
|
92
|
+
if (!project) fail(operation, board, `${asking(board)}gh answered no such board`);
|
|
93
|
+
if (!project.field?.options) fail(operation, board, `the board has no single-select field named ${COLUMNS}`);
|
|
94
|
+
return { id: project.id, columns: project.field };
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** The ID of the repository `board.repo` names as `owner/name`. */
|
|
98
|
+
export async function repositoryOf(operation, board, via) {
|
|
99
|
+
return (await asked(operation, board, repositoryQuery(board, 'id'), via)).repository.id;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** The fields of an item the read asks for: its field values, and an issue's facts. */
|
|
103
|
+
const ITEM = `id fieldValues(first: ${PAGE}) { pageInfo { hasNextPage } nodes { ... on ProjectV2ItemFieldSingleSelectValue { name field { ... on ProjectV2FieldCommon { name } } } } } content { __typename ... on Issue { number title body repository { nameWithOwner } labels(first: ${PAGE}) { pageInfo { hasNextPage } nodes { name } } } }`;
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Whether the repository an item read answered as `nameWithOwner` is the board's repository,
|
|
107
|
+
* `board.repo`. GitHub reads an owner and repository name alike whatever their case, so the names
|
|
108
|
+
* are compared that way too. Every read deciding whether an item is the repository's asks this.
|
|
109
|
+
*/
|
|
110
|
+
const ofRepository = (board, nameWithOwner) => nameWithOwner.toLowerCase() === board.repo.toLowerCase();
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Whether the item read answered `node` as a card: an issue in the board's repository. A draft
|
|
114
|
+
* issue, a pull request and another repository's issue are items and not cards.
|
|
115
|
+
*/
|
|
116
|
+
const isCard = (board, { content }) => content?.__typename === 'Issue' && ofRepository(board, content.repository.nameWithOwner);
|
|
117
|
+
|
|
118
|
+
/** The option an item read's `node` holds in the single-select field named `field`, or null. */
|
|
119
|
+
const valueIn = (node, field) => node.fieldValues.nodes.find((value) => value.field?.name === field)?.name ?? null;
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* The card an item read answered as `node`: its board item ID, which a move names, the issue's
|
|
123
|
+
* number, title, body and labels, and the column, which is the option it holds in the field
|
|
124
|
+
* holding the columns, or null where it holds none.
|
|
125
|
+
*/
|
|
126
|
+
function cardOf(operation, board, node) {
|
|
127
|
+
const { content, fieldValues } = node;
|
|
128
|
+
if (content.labels.pageInfo.hasNextPage || fieldValues.pageInfo.hasNextPage) {
|
|
129
|
+
fail(operation, board, `issue #${content.number} holds more than ${PAGE} labels or field values, and a card is read whole or not at all`);
|
|
130
|
+
}
|
|
131
|
+
return {
|
|
132
|
+
id: node.id,
|
|
133
|
+
number: content.number,
|
|
134
|
+
title: content.title,
|
|
135
|
+
body: content.body,
|
|
136
|
+
labels: content.labels.nodes.map((label) => label.name),
|
|
137
|
+
column: valueIn(node, COLUMNS),
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Every node of the board's items connection, each item selected as `item`, in board order, each
|
|
143
|
+
* once. The board can change between one page and the next, so an item moved meanwhile can be
|
|
144
|
+
* answered on both: it is kept where it first appeared.
|
|
145
|
+
*/
|
|
146
|
+
async function itemNodes(operation, board, via, item) {
|
|
147
|
+
const query = (page) => boardQuery(operation, board, `items(${page}) { pageInfo { hasNextPage endCursor } nodes { ${item} } }`);
|
|
148
|
+
const nodes = await everyPage(operation, board, via, query, (data) => data?.repositoryOwner?.projectV2?.items, asking(board));
|
|
149
|
+
const seen = new Set();
|
|
150
|
+
return nodes.filter((node) => !seen.has(node.id) && seen.add(node.id));
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** The board's item nodes that are cards, in board order, each once. */
|
|
154
|
+
const cardNodes = async (operation, board, via) => (await itemNodes(operation, board, via, ITEM)).filter((node) => isCard(board, node));
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* What the report of other repositories selects of an item: its type, and the repository of an
|
|
158
|
+
* issue or a pull request. A draft issue answers its content as an empty object, and an item the
|
|
159
|
+
* reader cannot see answers the type `REDACTED` (`ProjectV2ItemType`).
|
|
160
|
+
*/
|
|
161
|
+
const OWNED_ITEM = 'id type content { ... on Issue { repository { nameWithOwner } } ... on PullRequest { repository { nameWithOwner } } }';
|
|
162
|
+
|
|
163
|
+
/** The type GitHub gives a board item whose content the reader cannot see. */
|
|
164
|
+
const REDACTED = 'REDACTED';
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* What the board holds that is not the repository's: `repositories`, every other repository an
|
|
168
|
+
* issue or pull request on it belongs to, as `owner/name` in the order first met, and
|
|
169
|
+
* `unreadable`, the count of items whose content GitHub withheld.
|
|
170
|
+
*/
|
|
171
|
+
async function otherRepositories(operation, board, via) {
|
|
172
|
+
const repositories = [];
|
|
173
|
+
let unreadable = 0;
|
|
174
|
+
for (const { type, content } of await itemNodes(operation, board, via, OWNED_ITEM)) {
|
|
175
|
+
if (type === REDACTED) unreadable += 1;
|
|
176
|
+
const named = content?.repository?.nameWithOwner;
|
|
177
|
+
if (named && !ofRepository(board, named) && !repositories.includes(named)) repositories.push(named);
|
|
178
|
+
}
|
|
179
|
+
return { repositories, unreadable };
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Every field on the board, whatever its type, as GitHub answers it: its `name` and `dataType`,
|
|
184
|
+
* and a single-select field's `options` too. Listed rather than asked for by name, because `gh`
|
|
185
|
+
* answers a field name the board does not hold by exiting 1 (measured with gh 2.99.0 on board 6,
|
|
186
|
+
* 2026-09-25).
|
|
187
|
+
*/
|
|
188
|
+
function typedFields(operation, board, via) {
|
|
189
|
+
const query = (page) => boardQuery(operation, board, `fields(${page}) { pageInfo { hasNextPage endCursor } nodes { ... on ProjectV2FieldCommon { name dataType } ... on ProjectV2SingleSelectField { options { name } } } }`);
|
|
190
|
+
return everyPage(operation, board, via, query, (data) => data?.repositoryOwner?.projectV2?.fields, asking(board));
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* The options of the board's single-select field named `name`, in board order. Every field is
|
|
195
|
+
* read with its name and type, so a field that is not on the board and a field of another type
|
|
196
|
+
* each fail the read, naming the field, and the type where it has one.
|
|
197
|
+
*/
|
|
198
|
+
async function priorityField(operation, board, via, name) {
|
|
199
|
+
const field = (await typedFields(operation, board, via)).find((held) => held.name === name);
|
|
200
|
+
if (!field) fail(operation, board, `the board has no field named ${name}`);
|
|
201
|
+
if (!field.options) fail(operation, board, `the board's field ${name} is a ${field.dataType} field, not a single-select field`);
|
|
202
|
+
return field.options.map((option) => option.name);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* The board's single-select fields, each as `{ name, options }` with its option names in board
|
|
207
|
+
* order. A field of any other type answers as an empty object, and is left out.
|
|
208
|
+
*/
|
|
209
|
+
async function singleSelectFields(operation, board, via) {
|
|
210
|
+
const query = (page) => boardQuery(operation, board, `fields(${page}) { pageInfo { hasNextPage endCursor } nodes { ... on ProjectV2SingleSelectField { name options { name } } } }`);
|
|
211
|
+
return (await everyPage(operation, board, via, query, (data) => data?.repositoryOwner?.projectV2?.fields, asking(board)))
|
|
212
|
+
.filter((field) => field.options)
|
|
213
|
+
.map((field) => ({ name: field.name, options: field.options.map((option) => option.name) }));
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* The reads on `board`, which names its `repo`, its `project` number, its `columns`, the display
|
|
218
|
+
* names the config declares by key, and, where the config declares one, its `owner`. Every read is
|
|
219
|
+
* sent through the read runner with `emitter`, the `L0` emitter its kills are recorded through, and
|
|
220
|
+
* `timeout`, the runner's own where it is not given. `send` stands in for the runners' spawn in
|
|
221
|
+
* tests.
|
|
222
|
+
*/
|
|
223
|
+
export function readSide(board, { send, emitter, timeout } = {}) {
|
|
224
|
+
const via = { send, emitter, timeout };
|
|
225
|
+
return {
|
|
226
|
+
/**
|
|
227
|
+
* The columns the config declares, by its keys, each the option of the field holding the
|
|
228
|
+
* columns that the config names. This is where a declared column the board lacks is caught:
|
|
229
|
+
* the read fails naming every one, by key and display name.
|
|
230
|
+
*/
|
|
231
|
+
readColumns: async () => {
|
|
232
|
+
const status = (await singleSelectFields('readColumns', board, via)).find((field) => field.name === COLUMNS);
|
|
233
|
+
if (!status) fail('readColumns', board, `the board has no single-select field named ${COLUMNS}`);
|
|
234
|
+
const missing = Object.entries(board.columns).filter(([, name]) => !status.options.includes(name));
|
|
235
|
+
if (missing.length > 0) {
|
|
236
|
+
const named = missing.map(([key, name]) => `${key} (${name})`).join(', ');
|
|
237
|
+
fail('readColumns', board, `its ${COLUMNS} field has no option for the declared column${missing.length > 1 ? 's' : ''} ${named}`);
|
|
238
|
+
}
|
|
239
|
+
return { ...board.columns };
|
|
240
|
+
},
|
|
241
|
+
/** The board's single-select fields but the one holding the columns, as `{ name, options }`. */
|
|
242
|
+
readFields: async () => (await singleSelectFields('readFields', board, via)).filter((field) => field.name !== COLUMNS),
|
|
243
|
+
/** Every field on the board, whatever its type, as `{ name, type }`, the type as GitHub's `dataType` names it. */
|
|
244
|
+
readFieldTypes: async () => (await typedFields('readFieldTypes', board, via)).map((field) => ({ name: field.name, type: field.dataType })),
|
|
245
|
+
/** The names of the labels the board's repository holds. */
|
|
246
|
+
readLabels: async () => {
|
|
247
|
+
const query = (page) => repositoryQuery(board, `labels(${page}) { pageInfo { hasNextPage endCursor } nodes { name } }`);
|
|
248
|
+
return (await everyPage('readLabels', board, via, query, (data) => data?.repository?.labels)).map((label) => label.name);
|
|
249
|
+
},
|
|
250
|
+
/**
|
|
251
|
+
* What the board holds from outside the repository: every other repository whose issue or
|
|
252
|
+
* pull request is on it, and how many items it holds that cannot be read. Its own read of the
|
|
253
|
+
* items, so the item read `readItems` sends is left as it is.
|
|
254
|
+
*/
|
|
255
|
+
readOtherRepositories: async () => otherRepositories('readOtherRepositories', board, via),
|
|
256
|
+
/** Every card on the board, in board order, each once. */
|
|
257
|
+
readItems: async () => (await cardNodes('readItems', board, via)).map((node) => cardOf('readItems', board, node)),
|
|
258
|
+
/**
|
|
259
|
+
* Issue `number`'s state, as `{ state }`: `open`, `closed`, or `none` where the repository
|
|
260
|
+
* holds no such issue, as `issueIn` reads it. No label is read or answered.
|
|
261
|
+
*/
|
|
262
|
+
readIssueState: (number) => {
|
|
263
|
+
const addressed = reading(board, `issue #${number}'s state`);
|
|
264
|
+
return labelled('readIssueState', board, addressed, async () => {
|
|
265
|
+
const query = repositoryQuery(board, `issue(number: ${numbered('readIssueState', board, number)}) { state repository { nameWithOwner } }`);
|
|
266
|
+
const issue = issueIn('readIssueState', board, await readRunner(graphqlRequest(query), via), addressed);
|
|
267
|
+
return { state: issue === null ? 'none' : stateOf('readIssueState', board, issue, addressed) };
|
|
268
|
+
});
|
|
269
|
+
},
|
|
270
|
+
/**
|
|
271
|
+
* What L0 hands L3 to rank the board's cards by, from the config's priority declaration, a
|
|
272
|
+
* field and its options highest rank first. It returns `items`, every card as `readItems`
|
|
273
|
+
* reads it with its `priority` as `{ value, declared }`: the option it holds in the declared
|
|
274
|
+
* field, or null, and whether the declaration names that option. Beside them, `declared` is
|
|
275
|
+
* the declared order and `options` the field's own options in board order. Where the config
|
|
276
|
+
* declares no priority, no field is read, and every card's `priority`, `declared` and
|
|
277
|
+
* `options` are null.
|
|
278
|
+
*/
|
|
279
|
+
readPriority: async () => {
|
|
280
|
+
const declaration = board.priority;
|
|
281
|
+
const options = declaration ? await priorityField('readPriority', board, via, declaration.field) : null;
|
|
282
|
+
const priorityOf = (node) => {
|
|
283
|
+
if (!declaration) return null;
|
|
284
|
+
const value = valueIn(node, declaration.field);
|
|
285
|
+
return { value, declared: declaration.options.includes(value) };
|
|
286
|
+
};
|
|
287
|
+
const items = (await cardNodes('readPriority', board, via)).map((node) => ({ ...cardOf('readPriority', board, node), priority: priorityOf(node) }));
|
|
288
|
+
return { items, declared: declaration ? [...declaration.options] : null, options };
|
|
289
|
+
},
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* What a failure of a repository read says it was reading: `what`, in the configured repository,
|
|
295
|
+
* so a failure names the branch, issue or pull request asked for.
|
|
296
|
+
*/
|
|
297
|
+
const reading = (board, what) => `reading ${what} in ${board.repo}, `;
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* `number`, where it is an issue or pull request number, or the failure of `operation`: a number
|
|
301
|
+
* is written into a query and a path, so anything else is refused before a request is made.
|
|
302
|
+
*/
|
|
303
|
+
function numbered(operation, board, number) {
|
|
304
|
+
if (!Number.isInteger(number) || number < 1) fail(operation, board, `${number} is not an issue or pull request number`);
|
|
305
|
+
return number;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/** What gh answered of `field` under the repository, or the failure naming what was read. */
|
|
309
|
+
function heldIn(operation, board, data, field, addressed) {
|
|
310
|
+
const held = data?.repository?.[field];
|
|
311
|
+
if (!held) fail(operation, board, `${addressed}gh answered no ${data?.repository ? field : 'repository'}`);
|
|
312
|
+
return held;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* What gh printed to the REST read `path` under the repository, sent with `headers`, or the
|
|
317
|
+
* failure naming what was read and the first line gh said.
|
|
318
|
+
*/
|
|
319
|
+
async function restRead(operation, board, via, path, headers, addressed) {
|
|
320
|
+
const said = await readRunner(['api', `repos/${board.repo}/${path}`, '-X', 'GET', ...headers], via);
|
|
321
|
+
if (said.status !== 0) fail(operation, board, `${addressed}${firstLine(said)}`);
|
|
322
|
+
return said.stdout;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/** An environment endpoint's JSON answer, or null only on Not Found (HTTP 404). Other failures name the read. */
|
|
326
|
+
async function environmentRead(operation, board, via, path, addressed) {
|
|
327
|
+
const said = await readRunner(['api', `repos/${board.repo}/${path}`, '-X', 'GET'], via);
|
|
328
|
+
if (said.status !== 0) {
|
|
329
|
+
if (!said.timedOut && /Not Found \(HTTP 404\)/.test(firstLine(said))) return null;
|
|
330
|
+
fail(operation, board, `${addressed}${firstLine(said)}`);
|
|
331
|
+
}
|
|
332
|
+
return JSON.parse(said.stdout);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/** An environment's bypass setting, branch-policy facts and custom policies, or null if absent. An unreadable policy list is null. */
|
|
336
|
+
async function environment(board, via, name, addressed) {
|
|
337
|
+
const path = `environments/${encodeURIComponent(name)}`;
|
|
338
|
+
const held = await environmentRead('readEnvironment', board, via, path, addressed);
|
|
339
|
+
if (held === null) return null;
|
|
340
|
+
if (!Object.hasOwn(held, 'deployment_branch_policy')) fail('readEnvironment', board, `${addressed}gh answered no deployment-branch policy`);
|
|
341
|
+
let policies = [];
|
|
342
|
+
if (held.deployment_branch_policy?.custom_branch_policies === true) {
|
|
343
|
+
for (let page = 1;; page += 1) {
|
|
344
|
+
const listed = await environmentRead('readEnvironment', board, via, `${path}/deployment-branch-policies?per_page=${PAGE}&page=${page}`, `${addressed}deployment-branch policy list, `);
|
|
345
|
+
if (listed === null) { policies = null; break; }
|
|
346
|
+
if (!Array.isArray(listed.branch_policies)) fail('readEnvironment', board, `${addressed}gh answered no deployment-branch policy list`);
|
|
347
|
+
policies.push(...listed.branch_policies.map(({ name, type }) => ({ name, type })));
|
|
348
|
+
if (listed.branch_policies.length < PAGE) break;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
const branchPolicy = held.deployment_branch_policy;
|
|
352
|
+
return {
|
|
353
|
+
canAdminsBypass: held.can_admins_bypass,
|
|
354
|
+
deploymentBranchPolicy: branchPolicy === null ? null : {
|
|
355
|
+
protectedBranches: branchPolicy.protected_branches,
|
|
356
|
+
customBranchPolicies: branchPolicy.custom_branch_policies,
|
|
357
|
+
},
|
|
358
|
+
policies,
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/** Every REST page of a commit's checks or statuses. */
|
|
363
|
+
async function commitPages(operation, board, via, sha, kind, addressed) {
|
|
364
|
+
const records = [];
|
|
365
|
+
for (let page = 1;; page += 1) {
|
|
366
|
+
const body = JSON.parse(await restRead(operation, board, via, `commits/${encodeURIComponent(sha)}/${kind}?per_page=${PAGE}&page=${page}`, [], addressed));
|
|
367
|
+
const batch = kind === 'check-runs' ? body.check_runs : body;
|
|
368
|
+
if (!Array.isArray(batch)) fail(operation, board, `${addressed}gh answered no ${kind}`);
|
|
369
|
+
records.push(...batch);
|
|
370
|
+
if (batch.length < PAGE) return records;
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/** GitHub's state of each required context at `sha`, with each commit status's creator. */
|
|
375
|
+
async function checkStates(board, via, sha, required, addressed) {
|
|
376
|
+
const runs = await commitPages('readCheckStates', board, via, sha, 'check-runs', addressed);
|
|
377
|
+
const statuses = await commitPages('readCheckStates', board, via, sha, 'statuses', addressed);
|
|
378
|
+
const result = {};
|
|
379
|
+
for (const { context, integration_id: pin = null } of required) {
|
|
380
|
+
const matching = runs.filter((run) => run.name === context && (pin === null || run.app?.id === pin));
|
|
381
|
+
const run = matching.sort((a, b) => b.id - a.id)[0];
|
|
382
|
+
const status = statuses.find((record) => record.context === context);
|
|
383
|
+
const runState = !run ? null : run.status !== 'completed' ? 'pending'
|
|
384
|
+
: ['success', 'neutral', 'skipped'].includes(run.conclusion) ? 'passed' : 'failed';
|
|
385
|
+
const statusState = !status ? null : status.state === 'success' ? 'passed'
|
|
386
|
+
: status.state === 'pending' ? 'pending' : 'failed';
|
|
387
|
+
const states = [runState, statusState];
|
|
388
|
+
const state = states.includes('failed') ? 'failed' : states.includes('pending') ? 'pending' : states.includes('passed') ? 'passed' : 'absent';
|
|
389
|
+
const prior = result[context]?.state;
|
|
390
|
+
const combined = [prior, state].includes('failed') ? 'failed' : [prior, state].includes('pending') ? 'pending'
|
|
391
|
+
: [prior, state].includes('absent') ? 'absent' : 'passed';
|
|
392
|
+
result[context] = { state: combined, creator: status ? { login: status.creator?.login ?? null, id: status.creator?.id ?? null } : null };
|
|
393
|
+
}
|
|
394
|
+
return result;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
/** The `code` of the failure of the effective-rules read that says the forge offers no such feature to this repository. */
|
|
398
|
+
export const EFFECTIVE_RULES_UNAVAILABLE = 'EFFECTIVE_RULES_UNAVAILABLE';
|
|
399
|
+
|
|
400
|
+
/** The first line `gh` prints where GitHub refuses a private repository on the Free plan its rules (gh 2.103.3, 2026-11-05). */
|
|
401
|
+
const RULES_FEATURE_REFUSED = 'gh: Upgrade to GitHub Pro or make this repository public to enable this feature. (HTTP 403)';
|
|
402
|
+
|
|
403
|
+
/**
|
|
404
|
+
* Required contexts enforced on `branch` by active rulesets and classic protection. A rules read
|
|
405
|
+
* the forge refuses in exactly `RULES_FEATURE_REFUSED`'s words fails with the code
|
|
406
|
+
* `EFFECTIVE_RULES_UNAVAILABLE`, and any other failure with none.
|
|
407
|
+
*/
|
|
408
|
+
async function effectiveRules(board, via, branch, addressed) {
|
|
409
|
+
const checks = new Map();
|
|
410
|
+
const requireCheck = (context, integration) => {
|
|
411
|
+
const pin = integration === -1 ? null : integration ?? null;
|
|
412
|
+
const held = checks.get(context) ?? new Set();
|
|
413
|
+
if (pin === null && held.size === 0) held.add(null);
|
|
414
|
+
if (pin !== null) {
|
|
415
|
+
held.delete(null);
|
|
416
|
+
held.add(pin);
|
|
417
|
+
}
|
|
418
|
+
checks.set(context, held);
|
|
419
|
+
};
|
|
420
|
+
for (let page = 1;; page += 1) {
|
|
421
|
+
const path = `rules/branches/${encodeURIComponent(branch)}?per_page=${PAGE}&page=${page}`;
|
|
422
|
+
const answered = await readRunner(['api', `repos/${board.repo}/${path}`, '-X', 'GET'], via);
|
|
423
|
+
if (answered.status !== 0) fail('readEffectiveRules', board, `${addressed}${firstLine(answered)}`, firstLine(answered) === RULES_FEATURE_REFUSED ? EFFECTIVE_RULES_UNAVAILABLE : undefined);
|
|
424
|
+
const rules = JSON.parse(answered.stdout);
|
|
425
|
+
if (!Array.isArray(rules)) fail('readEffectiveRules', board, `${addressed}gh answered no rules`);
|
|
426
|
+
for (const rule of rules) {
|
|
427
|
+
if (rule.type !== 'required_status_checks') continue;
|
|
428
|
+
for (const check of rule.parameters?.required_status_checks ?? []) {
|
|
429
|
+
requireCheck(check.context, check.integration_id);
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
if (rules.length < PAGE) break;
|
|
433
|
+
}
|
|
434
|
+
const path = `repos/${board.repo}/branches/${encodeURIComponent(branch)}/protection`;
|
|
435
|
+
const said = await readRunner(['api', path, '-X', 'GET'], via);
|
|
436
|
+
if (said.status !== 0 && !/Branch not protected \(HTTP 404\)/.test(firstLine(said))) {
|
|
437
|
+
fail('readEffectiveRules', board, `${addressed}${firstLine(said)}`);
|
|
438
|
+
}
|
|
439
|
+
if (said.status === 0) {
|
|
440
|
+
const classic = JSON.parse(said.stdout).required_status_checks;
|
|
441
|
+
for (const check of classic?.checks ?? []) {
|
|
442
|
+
requireCheck(check.context, check.app_id);
|
|
443
|
+
}
|
|
444
|
+
for (const context of classic?.contexts ?? []) {
|
|
445
|
+
requireCheck(context, null);
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
return [...checks].flatMap(([context, pins]) => [...pins].map((integration_id) => ({ context, integration_id })));
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
/**
|
|
452
|
+
* What `read` answers, or a failure naming `operation`, what was `addressed` and why: a failure
|
|
453
|
+
* the read gave already names them, and any other, such as a request that never reached `gh` or
|
|
454
|
+
* an answer that is not JSON, is given them, keeping its `code` and carrying it as its cause.
|
|
455
|
+
*/
|
|
456
|
+
async function labelled(operation, board, addressed, read) {
|
|
457
|
+
try {
|
|
458
|
+
return await read();
|
|
459
|
+
} catch (error) {
|
|
460
|
+
const named = `${operation} on board ${board.project} failed: `;
|
|
461
|
+
if (error.message.startsWith(named)) throw error;
|
|
462
|
+
const failure = new Error(`${named}${addressed}${error.message}`, { cause: error });
|
|
463
|
+
if (error.code !== undefined) failure.code = error.code;
|
|
464
|
+
throw failure;
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
/**
|
|
469
|
+
* The data gh printed where a GraphQL read failed only because the forge holds nothing at
|
|
470
|
+
* `path`, the field path of what was read, or null where it failed otherwise or did not fail. gh
|
|
471
|
+
* exits 1 on any GraphQL error, printing the whole response on stdout, and GitHub answers each
|
|
472
|
+
* thing it holds nothing at as a `NOT_FOUND` error at that field's path, with the field null
|
|
473
|
+
* (measured with gh 2.99.0 on this repository on 2026-10-11, for an issue and a file).
|
|
474
|
+
*/
|
|
475
|
+
function notFoundAt(said, path) {
|
|
476
|
+
if (said.status === 0 || said.timedOut) return null;
|
|
477
|
+
let printed;
|
|
478
|
+
try {
|
|
479
|
+
printed = JSON.parse(said.stdout);
|
|
480
|
+
} catch {
|
|
481
|
+
return null;
|
|
482
|
+
}
|
|
483
|
+
if (printed === null || typeof printed !== 'object') return null;
|
|
484
|
+
const { errors } = printed;
|
|
485
|
+
const only = Array.isArray(errors) && errors.length > 0
|
|
486
|
+
&& errors.every((error) => error?.type === 'NOT_FOUND' && JSON.stringify(error.path) === JSON.stringify(path));
|
|
487
|
+
const data = printed.data;
|
|
488
|
+
const held = path.reduce((value, key) => (value === null || value === undefined ? undefined : value[key]), data);
|
|
489
|
+
return only && held === null ? data : null;
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
/**
|
|
493
|
+
* The issue gh answered in `said` to `operation`'s read of one issue, or null where the repository
|
|
494
|
+
* holds no such issue: gh's not-found answer for it, or an issue the forge answers in another
|
|
495
|
+
* repository, as it answers one transferred out. Any other failure throws naming what was `addressed`.
|
|
496
|
+
*/
|
|
497
|
+
function issueIn(operation, board, said, addressed) {
|
|
498
|
+
if (notFoundAt(said, ['repository', 'issue'])) return null;
|
|
499
|
+
const issue = heldIn(operation, board, answerOf(operation, board, said, addressed), 'issue', addressed);
|
|
500
|
+
return ofRepository(board, issue.repository.nameWithOwner) ? issue : null;
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
/** `issue`'s state as the reads answer it, `open` or `closed`, or `operation`'s failure for any other. */
|
|
504
|
+
function stateOf(operation, board, issue, addressed) {
|
|
505
|
+
const state = { OPEN: 'open', CLOSED: 'closed' }[issue.state];
|
|
506
|
+
if (state === undefined) fail(operation, board, `${addressed}gh answered state ${issue.state}`);
|
|
507
|
+
return state;
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
/** A commit's full SHA, as the forge answers one and as a read at a commit is given one. */
|
|
511
|
+
const FULL_SHA = /^[0-9a-f]{40}$/i;
|
|
512
|
+
|
|
513
|
+
/** A pull request as the reads answer it: its number, head SHA and base branch. */
|
|
514
|
+
const pullRequestOf = ({ number, headRefOid, baseRefName }) => ({ number, head: headRefOid, base: baseRefName });
|
|
515
|
+
|
|
516
|
+
/**
|
|
517
|
+
* The role `author` holds on the repository, as the collaborator permission endpoint names it, or
|
|
518
|
+
* `none` where it answers Not Found.
|
|
519
|
+
*/
|
|
520
|
+
async function permissionOf(operation, board, via, author, addressed) {
|
|
521
|
+
const said = await readRunner(['api', `repos/${board.repo}/collaborators/${encodeURIComponent(author)}/permission`, '-X', 'GET'], via);
|
|
522
|
+
if (said.status !== 0) {
|
|
523
|
+
if (/\b404\b/.test(firstLine(said))) return 'none';
|
|
524
|
+
fail(operation, board, `${addressed}permission for ${author}: ${firstLine(said)}`);
|
|
525
|
+
}
|
|
526
|
+
const role = JSON.parse(said.stdout).role_name;
|
|
527
|
+
if (typeof role !== 'string') fail(operation, board, `${addressed}permission for ${author}: gh answered no role_name`);
|
|
528
|
+
return role;
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
/** What a read of comments selects of each, which `commentsOf` answers. */
|
|
532
|
+
const COMMENT_FIELDS = 'body createdAt id author { login } lastEditedAt includesCreatedEdit';
|
|
533
|
+
|
|
534
|
+
/** Comment nodes as L0 answers them, with each author's repository permission read once. */
|
|
535
|
+
async function commentsOf(operation, board, via, nodes, addressed) {
|
|
536
|
+
const permissions = new Map();
|
|
537
|
+
const permissionsOf = async (author) => {
|
|
538
|
+
if (author === null) return 'none';
|
|
539
|
+
if (!permissions.has(author)) permissions.set(author, permissionOf(operation, board, via, author, addressed));
|
|
540
|
+
return permissions.get(author);
|
|
541
|
+
};
|
|
542
|
+
const comments = [];
|
|
543
|
+
for (const node of nodes) {
|
|
544
|
+
const author = node.author?.login ?? null;
|
|
545
|
+
comments.push({ body: node.body, createdAt: node.createdAt, id: node.id, author, permission: await permissionsOf(author), edited: node.lastEditedAt != null || node.includesCreatedEdit === true });
|
|
546
|
+
}
|
|
547
|
+
return comments;
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
/**
|
|
551
|
+
* The kinds of entry the timeline read answers, by the item type it asks GitHub for: each entry's
|
|
552
|
+
* `kind`, and the `type` GitHub names its node by. A comment answers as `commentsOf` answers one,
|
|
553
|
+
* with its author's permission and whether it was edited; any other entry answers its time, its
|
|
554
|
+
* actor's login, or null where the account was deleted, and, for a label change, the label's name.
|
|
555
|
+
*/
|
|
556
|
+
const TIMELINE_ENTRIES = {
|
|
557
|
+
ISSUE_COMMENT: { kind: 'comment', type: 'IssueComment' },
|
|
558
|
+
LABELED_EVENT: { kind: 'labeled', type: 'LabeledEvent', label: true },
|
|
559
|
+
UNLABELED_EVENT: { kind: 'unlabeled', type: 'UnlabeledEvent', label: true },
|
|
560
|
+
CLOSED_EVENT: { kind: 'closed', type: 'ClosedEvent' },
|
|
561
|
+
REOPENED_EVENT: { kind: 'reopened', type: 'ReopenedEvent' },
|
|
562
|
+
};
|
|
563
|
+
|
|
564
|
+
/** What the timeline read selects of each kind of entry. */
|
|
565
|
+
const TIMELINE_SELECTION = Object.values(TIMELINE_ENTRIES).map(({ type, kind, label }) => `... on ${type} { ${kind === 'comment' ? COMMENT_FIELDS : `createdAt actor { login }${label ? ' label { name }' : ''}`} }`).join(' ');
|
|
566
|
+
|
|
567
|
+
/**
|
|
568
|
+
* The reads of a card's facts held in the repository `board.repo` names: its line of work's
|
|
569
|
+
* branch and pull requests, a pull request's diff, merge base and comments, an issue's comments,
|
|
570
|
+
* the repository's default branch, and one issue or pull request by its number. Each is sent
|
|
571
|
+
* through the read runner, as `readSide`'s reads are, with `send`, `emitter` and `timeout`. Each
|
|
572
|
+
* is given what it reads, and what a failure says it was reading. A read that fails rejects
|
|
573
|
+
* naming what it read and why, and never answers none in its place.
|
|
574
|
+
*/
|
|
575
|
+
/**
|
|
576
|
+
* The oldest gh release Rigger runs with, as `<major>.<minor>.<patch>`: `readDiff` sends
|
|
577
|
+
* `--allow-escape-sequences`, which gh accepts only from this release (the owner's ruling M8-U91
|
|
578
|
+
* on #1293). `README.md`'s "Prerequisites" states it, and `doctor` compares the version read
|
|
579
|
+
* below with it.
|
|
580
|
+
*/
|
|
581
|
+
export const GH_MINIMUM = '2.97.0';
|
|
582
|
+
|
|
583
|
+
/** The first line `gh --version` prints for a release: its numbers, then a space or nothing. */
|
|
584
|
+
const GH_RELEASE = /^gh version (\d+\.\d+\.\d+)(?: |$)/;
|
|
585
|
+
|
|
586
|
+
/**
|
|
587
|
+
* What `gh --version` answered, read through the read runner and sent as `via` says: gh's exit
|
|
588
|
+
* `status`, whether the timeout ended it and that `timeout`, the first `line` of its standard
|
|
589
|
+
* output, and the `version` that line names as `<major>.<minor>.<patch>`, or null where it names
|
|
590
|
+
* none. A gh L0 could not start rejects as the runner rejects.
|
|
591
|
+
*
|
|
592
|
+
* gh owns which release it is (`D16` rule 1). Measured with gh 2.103.3 on macOS on 2026-11-21, it
|
|
593
|
+
* exits 0 and prints `gh version 2.103.3 (2026-10-26)` and then the release's URL. Where this
|
|
594
|
+
* reading can differ from gh's own answer (`D16` rule 3):
|
|
595
|
+
* - a pre-release such as `gh version 2.97.0-rc.1` names a release gh has not shipped, and is read
|
|
596
|
+
* as naming none, whichever release it precedes;
|
|
597
|
+
* - a build gh does not number, which prints `gh version DEV`, is read as naming none;
|
|
598
|
+
* - only standard output's first line is read, so an update notice gh prints on standard error
|
|
599
|
+
* is never taken for the version, and a gh that moved its version off that line would read as
|
|
600
|
+
* naming none rather than as some other number;
|
|
601
|
+
* - the numbers are read as written, so a number with a leading zero is the number it spells.
|
|
602
|
+
* `test/forge-runners.test.mjs` asks the host's gh each run, and holds this read to its first line.
|
|
603
|
+
*/
|
|
604
|
+
export async function readGhVersion(via = {}) {
|
|
605
|
+
const said = await readRunner(['--version'], via);
|
|
606
|
+
const line = (said.stdout ?? '').split('\n')[0].replace(/\r$/, '');
|
|
607
|
+
const version = said.status === 0 && !said.timedOut ? (line.match(GH_RELEASE)?.[1] ?? null) : null;
|
|
608
|
+
return { status: said.status, timedOut: Boolean(said.timedOut), timeout: said.timeout, line, version };
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
const REPOSITORY_READS = {
|
|
612
|
+
/**
|
|
613
|
+
* The pull requests whose head is `branch` in the repository, as `{ open, merged, closed }`, each a list
|
|
614
|
+
* of `{ number, head, base }` in the order gh answers them. A fork's branch of the same name is
|
|
615
|
+
* another repository's, and is left out.
|
|
616
|
+
*/
|
|
617
|
+
readPullRequests: {
|
|
618
|
+
reading: (branch) => `the pull requests from branch ${branch}`,
|
|
619
|
+
read: async (board, via, branch, addressed) => {
|
|
620
|
+
const query = (page) => repositoryQuery(board, `pullRequests(headRefName: ${literal(branch)}, states: [OPEN, MERGED, CLOSED], ${page}) { pageInfo { hasNextPage endCursor } nodes { number state headRefOid baseRefName headRepository { nameWithOwner } } }`);
|
|
621
|
+
const nodes = (await everyPage('readPullRequests', board, via, query, (data) => heldIn('readPullRequests', board, data, 'pullRequests', addressed), addressed))
|
|
622
|
+
.filter((node) => node.headRepository && ofRepository(board, node.headRepository.nameWithOwner));
|
|
623
|
+
for (const node of nodes.filter((node) => node.state === 'CLOSED')) {
|
|
624
|
+
if (!Number.isSafeInteger(node.number) || node.number <= 0) fail('readPullRequests', board, `${addressed}gh answered an invalid CLOSED pull request number`);
|
|
625
|
+
if (typeof node.headRefOid !== 'string' || node.headRefOid.length !== 40 || !FULL_SHA.test(node.headRefOid)) fail('readPullRequests', board, `${addressed}gh answered an invalid CLOSED pull request headRefOid`);
|
|
626
|
+
}
|
|
627
|
+
const inState = (state) => nodes.filter((node) => node.state === state).map(pullRequestOf);
|
|
628
|
+
return { open: inState('OPEN'), merged: inState('MERGED'), closed: inState('CLOSED') };
|
|
629
|
+
},
|
|
630
|
+
},
|
|
631
|
+
/**
|
|
632
|
+
* For each of `names`, whether the repository holds a branch of that name, as an object keyed by
|
|
633
|
+
* name, read from the repository's branches a page at a time.
|
|
634
|
+
*/
|
|
635
|
+
readBranches: {
|
|
636
|
+
reading: (names) => `the branches ${names.join(', ')}`,
|
|
637
|
+
read: async (board, via, names, addressed) => {
|
|
638
|
+
const query = (page) => repositoryQuery(board, `refs(refPrefix: "refs/heads/", ${page}) { pageInfo { hasNextPage endCursor } nodes { name } }`);
|
|
639
|
+
const held = new Set((await everyPage('readBranches', board, via, query, (data) => heldIn('readBranches', board, data, 'refs', addressed), addressed)).map((ref) => ref.name));
|
|
640
|
+
return Object.fromEntries(names.map((name) => [name, held.has(name)]));
|
|
641
|
+
},
|
|
642
|
+
},
|
|
643
|
+
/**
|
|
644
|
+
* Pull request `number`'s diff, as the forge serves it under the diff's media type. A diff the
|
|
645
|
+
* forge declines to serve rejects with the forge's reason.
|
|
646
|
+
*/
|
|
647
|
+
readDiff: {
|
|
648
|
+
reading: (number) => `pull request #${number}'s diff`,
|
|
649
|
+
read: (board, via, number, addressed) => restRead('readDiff', board, via, `pulls/${numbered('readDiff', board, number)}`, ['-H', DIFF_HEADER, '--allow-escape-sequences'], addressed),
|
|
650
|
+
},
|
|
651
|
+
/**
|
|
652
|
+
* The merge base the forge computes pull request `number`'s diff from, as `{ base, head,
|
|
653
|
+
* mergeBase }`: its base branch and head SHA, read first, and the merge base of the two the
|
|
654
|
+
* forge's comparison answers, which is what a pull request's diff is taken against.
|
|
655
|
+
*/
|
|
656
|
+
readMergeBase: {
|
|
657
|
+
reading: (number) => `pull request #${number}'s merge base`,
|
|
658
|
+
read: async (board, via, number, addressed) => {
|
|
659
|
+
const query = repositoryQuery(board, `pullRequest(number: ${numbered('readMergeBase', board, number)}) { baseRefName headRefOid }`);
|
|
660
|
+
const { baseRefName: base, headRefOid: head } = heldIn('readMergeBase', board, await asked('readMergeBase', board, query, via, addressed), 'pullRequest', addressed);
|
|
661
|
+
const compared = JSON.parse(await restRead('readMergeBase', board, via, `compare/${base}...${head}`, [], addressed));
|
|
662
|
+
const mergeBase = compared?.merge_base_commit?.sha;
|
|
663
|
+
if (!mergeBase) fail('readMergeBase', board, `${addressed}gh answered no merge base`);
|
|
664
|
+
return { base, head, mergeBase };
|
|
665
|
+
},
|
|
666
|
+
},
|
|
667
|
+
/** The forge's current mergeability answer for pull request `number`. */
|
|
668
|
+
readMergeable: {
|
|
669
|
+
reading: (number) => `pull request #${number}'s mergeability`,
|
|
670
|
+
read: async (board, via, number, addressed) => {
|
|
671
|
+
const query = repositoryQuery(board, `pullRequest(number: ${numbered('readMergeable', board, number)}) { mergeable }`);
|
|
672
|
+
const pull = heldIn('readMergeable', board, await asked('readMergeable', board, query, via, addressed), 'pullRequest', addressed);
|
|
673
|
+
const states = { MERGEABLE: 'mergeable', CONFLICTING: 'conflicting', UNKNOWN: 'unknown' };
|
|
674
|
+
if (!Object.hasOwn(states, pull.mergeable)) fail('readMergeable', board, `${addressed}gh answered mergeable ${pull.mergeable}`);
|
|
675
|
+
return states[pull.mergeable];
|
|
676
|
+
},
|
|
677
|
+
},
|
|
678
|
+
/** Every comment on pull request `number`, oldest first, with its author, edit and permission. */
|
|
679
|
+
readComments: {
|
|
680
|
+
reading: (number) => `pull request #${number}'s comments`,
|
|
681
|
+
read: async (board, via, number, addressed) => {
|
|
682
|
+
const query = (page) => repositoryQuery(board, `pullRequest(number: ${numbered('readComments', board, number)}) { comments(${page}) { pageInfo { hasNextPage endCursor } nodes { ${COMMENT_FIELDS} } } }`);
|
|
683
|
+
const nodes = await everyPage('readComments', board, via, query, (data) => heldIn('readComments', board, data, 'pullRequest', addressed).comments, addressed);
|
|
684
|
+
return commentsOf('readComments', board, via, nodes, addressed);
|
|
685
|
+
},
|
|
686
|
+
},
|
|
687
|
+
/** Every comment on issue `number`, oldest first, with its author, edit and permission. */
|
|
688
|
+
readIssueComments: {
|
|
689
|
+
reading: (number) => `issue #${number}'s comments`,
|
|
690
|
+
read: async (board, via, number, addressed) => {
|
|
691
|
+
const query = (page) => repositoryQuery(board, `issue(number: ${numbered('readIssueComments', board, number)}) { comments(${page}) { pageInfo { hasNextPage endCursor } nodes { ${COMMENT_FIELDS} } } }`);
|
|
692
|
+
const nodes = await everyPage('readIssueComments', board, via, query, (data) => heldIn('readIssueComments', board, data, 'issue', addressed).comments, addressed);
|
|
693
|
+
return commentsOf('readIssueComments', board, via, nodes, addressed);
|
|
694
|
+
},
|
|
695
|
+
},
|
|
696
|
+
/**
|
|
697
|
+
* The repository's default branch, as `{ name, head }`: its name and the full SHA of its head
|
|
698
|
+
* commit. A repository with no default branch, as an empty one has none, rejects naming it.
|
|
699
|
+
*/
|
|
700
|
+
readDefaultBranch: {
|
|
701
|
+
reading: () => 'the default branch',
|
|
702
|
+
read: async (board, via, target, addressed) => {
|
|
703
|
+
const data = await asked('readDefaultBranch', board, repositoryQuery(board, 'defaultBranchRef { name target { oid } }'), via, addressed);
|
|
704
|
+
if (!data?.repository) fail('readDefaultBranch', board, `${addressed}gh answered no repository`);
|
|
705
|
+
const branch = data.repository.defaultBranchRef;
|
|
706
|
+
if (!branch) fail('readDefaultBranch', board, `${addressed}${board.repo} holds no default branch`);
|
|
707
|
+
if (typeof branch.name !== 'string' || !FULL_SHA.test(branch.target?.oid ?? '')) fail('readDefaultBranch', board, `${addressed}gh answered no name or head commit`);
|
|
708
|
+
return { name: branch.name, head: branch.target.oid };
|
|
709
|
+
},
|
|
710
|
+
},
|
|
711
|
+
/**
|
|
712
|
+
* Issue `number`, open or closed, as `{ number, title, body, state, author, labels }`: its title,
|
|
713
|
+
* its body, the empty string where it has none, `open` or `closed`, its author's login, or null
|
|
714
|
+
* where the account was deleted, and every label's name across pages, in the order gh answers them.
|
|
715
|
+
*/
|
|
716
|
+
readIssue: {
|
|
717
|
+
reading: (number) => `issue #${number}`,
|
|
718
|
+
read: async (board, via, number, addressed) => {
|
|
719
|
+
const query = (page) => repositoryQuery(board, `issue(number: ${numbered('readIssue', board, number)}) { number title body state author { login } repository { nameWithOwner } labels(${page}) { pageInfo { hasNextPage endCursor } nodes { name } } }`);
|
|
720
|
+
const labels = [];
|
|
721
|
+
for (let after = '';;) {
|
|
722
|
+
const issue = issueIn('readIssue', board, await readRunner(graphqlRequest(query(`first: ${PAGE}${after}`)), via), addressed);
|
|
723
|
+
if (issue === null) return null;
|
|
724
|
+
labels.push(...issue.labels.nodes.map((label) => label.name));
|
|
725
|
+
const state = stateOf('readIssue', board, issue, addressed);
|
|
726
|
+
if (!issue.labels.pageInfo.hasNextPage) return { number: issue.number, title: issue.title, body: issue.body, state, author: issue.author?.login ?? null, labels };
|
|
727
|
+
after = `, after: ${literal(issue.labels.pageInfo.endCursor)}`;
|
|
728
|
+
}
|
|
729
|
+
},
|
|
730
|
+
},
|
|
731
|
+
/**
|
|
732
|
+
* The issue's opening, current body and body revisions, oldest first.
|
|
733
|
+
*
|
|
734
|
+
* D16 rule 3 measurements:
|
|
735
|
+
* A, 2026-11-10: gh api graphql -f query='query { repository(owner: "williacj", name: "rigger") { issue(number: 1302) { createdAt author { login } body userContentEdits(first: 100) { pageInfo { hasNextPage endCursor } nodes { editedAt editor { login } deletedAt diff } } } } }'
|
|
736
|
+
* A returned four full bodies (10021, 9926, 8192, 8209 characters); newest diff byte-equal
|
|
737
|
+
* to body, oldest editedAt equal to createdAt. The fallback is inactive for that capture (W7).
|
|
738
|
+
* A's edit times have second precision; the record cannot supply finer edit times (W9).
|
|
739
|
+
* B, 2026-11-10: gh api graphql -f query='query { __type(name: "UserContentEdit") { name fields { name type { kind name ofType { kind name ofType { kind name } } } } } }'
|
|
740
|
+
* B declares nullable deletedAt, deletedBy and diff. C's null deleted snapshot kept its
|
|
741
|
+
* flag and identity, without digest/items (W1, W13).
|
|
742
|
+
* C, 2026-11-11 UTC: npm test -- --test-timeout=120000 test/acceptance-history.test.mjs
|
|
743
|
+
* C's edit after the last move produced no further read or history after manual time advanced
|
|
744
|
+
* a minute: edits beyond the last column change remain outside its snapshots (W2).
|
|
745
|
+
* C's failed first/continuation page, deadline and unavailable undeleted body left only the
|
|
746
|
+
* accepted transition; an L0 write-record or transition refusal sent no history read (W8).
|
|
747
|
+
* C's null opener/editor, including a deleted revision's editor, refused the whole read with
|
|
748
|
+
* issue/revision context; L2 retained its accepted transition without history (M15).
|
|
749
|
+
* C retained all 201 same-second revisions in connection order, without inventing precision (W9).
|
|
750
|
+
*/
|
|
751
|
+
readIssueHistory: {
|
|
752
|
+
reading: (number) => `issue #${number}'s body history`,
|
|
753
|
+
read: async (board, via, number, addressed) => {
|
|
754
|
+
const loginOf = (actor, revision) => {
|
|
755
|
+
if (typeof actor?.login !== 'string' || actor.login === '') fail('readIssueHistory', board, `${addressed}${revision} has no login`);
|
|
756
|
+
return actor.login;
|
|
757
|
+
};
|
|
758
|
+
const query = (page) => repositoryQuery(board, `issue(number: ${numbered('readIssueHistory', board, number)}) { createdAt author { login } body userContentEdits(${page}) { pageInfo { hasNextPage endCursor } nodes { editedAt editor { login } deletedAt diff } } }`);
|
|
759
|
+
let opened, body;
|
|
760
|
+
const nodes = await everyPage('readIssueHistory', board, via, query, (data) => {
|
|
761
|
+
const found = heldIn('readIssueHistory', board, data, 'issue', addressed);
|
|
762
|
+
const by = loginOf(found.author, 'opening revision');
|
|
763
|
+
opened ??= { at: found.createdAt, by };
|
|
764
|
+
body ??= found.body;
|
|
765
|
+
return found.userContentEdits;
|
|
766
|
+
}, addressed);
|
|
767
|
+
return {
|
|
768
|
+
opened, body,
|
|
769
|
+
revisions: nodes.toReversed().map(({ editedAt, editor, deletedAt, diff }, index) => {
|
|
770
|
+
if (deletedAt === null && typeof diff !== 'string') fail('readIssueHistory', board, `${addressed}revision ${index + 1} at ${editedAt} has no available body`);
|
|
771
|
+
return { editedAt, editor: loginOf(editor, `revision ${index + 1} at ${editedAt}`), deleted: deletedAt !== null, body: diff };
|
|
772
|
+
}),
|
|
773
|
+
};
|
|
774
|
+
},
|
|
775
|
+
},
|
|
776
|
+
/**
|
|
777
|
+
* Issue `number`'s timeline of comments, label changes, closes and reopens, oldest first, across
|
|
778
|
+
* every page, as `TIMELINE_ENTRIES` answers each entry. A page that fails fails the whole read.
|
|
779
|
+
*/
|
|
780
|
+
readIssueTimeline: {
|
|
781
|
+
reading: (number) => `issue #${number}'s timeline`,
|
|
782
|
+
read: async (board, via, number, addressed) => {
|
|
783
|
+
const query = (page) => repositoryQuery(board, `issue(number: ${numbered('readIssueTimeline', board, number)}) { timelineItems(itemTypes: [${Object.keys(TIMELINE_ENTRIES).join(', ')}], ${page}) { pageInfo { hasNextPage endCursor } nodes { __typename ${TIMELINE_SELECTION} } } }`);
|
|
784
|
+
const nodes = await everyPage('readIssueTimeline', board, via, query, (data) => heldIn('readIssueTimeline', board, data, 'issue', addressed).timelineItems, addressed);
|
|
785
|
+
const kinds = Object.values(TIMELINE_ENTRIES).map(({ type }) => type);
|
|
786
|
+
const unknown = nodes.find((node) => !kinds.includes(node.__typename));
|
|
787
|
+
if (unknown) fail('readIssueTimeline', board, `${addressed}gh answered an entry of type ${unknown.__typename}`);
|
|
788
|
+
const comments = await commentsOf('readIssueTimeline', board, via, nodes.filter((node) => node.__typename === 'IssueComment'), addressed);
|
|
789
|
+
return nodes.map((node) => {
|
|
790
|
+
const [, { kind, label }] = Object.entries(TIMELINE_ENTRIES).find(([, { type }]) => type === node.__typename);
|
|
791
|
+
if (kind === 'comment') return { kind, ...comments.shift() };
|
|
792
|
+
return { kind, createdAt: node.createdAt, actor: node.actor?.login ?? null, ...(label && { label: node.label.name }) };
|
|
793
|
+
});
|
|
794
|
+
},
|
|
795
|
+
},
|
|
796
|
+
/**
|
|
797
|
+
* The numbers of the repository's open issues carrying `label`, read by label from its issues,
|
|
798
|
+
* which hold no pull request, and never through the search index, across every page.
|
|
799
|
+
*/
|
|
800
|
+
readLabelledIssues: {
|
|
801
|
+
reading: (label) => `the open issues labelled ${label}`,
|
|
802
|
+
read: async (board, via, label, addressed) => {
|
|
803
|
+
const query = (page) => repositoryQuery(board, `issues(states: [OPEN], labels: [${literal(label)}], ${page}) { pageInfo { hasNextPage endCursor } nodes { number } }`);
|
|
804
|
+
return (await everyPage('readLabelledIssues', board, via, query, (data) => heldIn('readLabelledIssues', board, data, 'issues', addressed), addressed)).map((node) => node.number);
|
|
805
|
+
},
|
|
806
|
+
},
|
|
807
|
+
/**
|
|
808
|
+
* The number of the repository's newest issue or pull request, or 0 where it holds none: the
|
|
809
|
+
* issues list holds pull requests beside issues, newest created first.
|
|
810
|
+
*/
|
|
811
|
+
readNewestNumber: {
|
|
812
|
+
reading: () => 'the newest issue or pull request',
|
|
813
|
+
read: async (board, via, target, addressed) => {
|
|
814
|
+
const [newest] = JSON.parse(await restRead('readNewestNumber', board, via, 'issues?state=all&sort=created&direction=desc&per_page=1', [], addressed));
|
|
815
|
+
return newest?.number ?? 0;
|
|
816
|
+
},
|
|
817
|
+
},
|
|
818
|
+
/**
|
|
819
|
+
* The repository's visibility and the kind of account that owns it, as `{ visibility, owner }`:
|
|
820
|
+
* `public`, `private` or `internal`, and `user` or `organization`.
|
|
821
|
+
*/
|
|
822
|
+
readVisibility: {
|
|
823
|
+
reading: () => 'the visibility',
|
|
824
|
+
read: async (board, via, target, addressed) => {
|
|
825
|
+
const repository = (await asked('readVisibility', board, repositoryQuery(board, 'visibility owner { __typename }'), via, addressed))?.repository;
|
|
826
|
+
const visibility = named({ PUBLIC: 'public', PRIVATE: 'private', INTERNAL: 'internal' }, repository?.visibility);
|
|
827
|
+
const owner = named({ User: 'user', Organization: 'organization' }, repository?.owner?.__typename);
|
|
828
|
+
if (!visibility || !owner) fail('readVisibility', board, `${addressed}gh answered no visibility or owner`);
|
|
829
|
+
return { visibility, owner };
|
|
830
|
+
},
|
|
831
|
+
},
|
|
832
|
+
/**
|
|
833
|
+
* The repository's own Actions policies, those whose `source_type` is `Repository`, each as
|
|
834
|
+
* `eventPolicyOf` answers its detail read, in the order the list answers them. Every page of the
|
|
835
|
+
* list is read, then each own policy's detail, which alone answers its conditions and rules
|
|
836
|
+
* (measured on williacj/rigger, 2026-11-09). A policy inherited from an organization or
|
|
837
|
+
* enterprise is never read past the list. A read that fails fails the whole read.
|
|
838
|
+
*/
|
|
839
|
+
readEventPolicies: {
|
|
840
|
+
reading: () => 'the Actions event policies',
|
|
841
|
+
read: async (board, via, target, addressed) => {
|
|
842
|
+
const own = [];
|
|
843
|
+
for (let page = 1, listed = []; page === 1 || listed.length === PAGE; page += 1) {
|
|
844
|
+
listed = JSON.parse(await restRead('readEventPolicies', board, via, `actions/policies?per_page=${PAGE}&page=${page}`, POLICY_HEADERS, addressed))?.policies;
|
|
845
|
+
if (!Array.isArray(listed)) fail('readEventPolicies', board, `${addressed}gh answered no policies`);
|
|
846
|
+
own.push(...listed.filter((policy) => policy?.source_type === 'Repository').map(({ id }) => (Number.isSafeInteger(id) && id > 0 ? id : fail('readEventPolicies', board, `${addressed}gh answered a policy with no id`))));
|
|
847
|
+
}
|
|
848
|
+
const policies = [];
|
|
849
|
+
for (const id of own) policies.push(eventPolicyOf(board, JSON.parse(await restRead('readEventPolicies', board, via, `actions/policies/${id}`, POLICY_HEADERS, `${addressed}policy ${id}, `)), id, addressed));
|
|
850
|
+
return policies;
|
|
851
|
+
},
|
|
852
|
+
},
|
|
853
|
+
/** The role account `login` holds on the repository, as `permissionOf` answers it. */
|
|
854
|
+
readPermission: {
|
|
855
|
+
reading: (login) => `${login}'s permission`,
|
|
856
|
+
read: (board, via, login, addressed) => permissionOf('readPermission', board, via, login, addressed),
|
|
857
|
+
},
|
|
858
|
+
/**
|
|
859
|
+
* Pull request `number`, whatever its state or the repository its head is in, as `{ number,
|
|
860
|
+
* head, branch, base, state, headRepository, fromRepository }`: `head` and `base` as
|
|
861
|
+
* `pullRequestOf` names them, `branch` its head branch's name, `state` one of `open`, `closed` or `merged`, `headRepository` the head's repository
|
|
862
|
+
* as `owner/name`, or null where it was deleted, and `fromRepository` whether that is the board's
|
|
863
|
+
* repository. A fork's pull request is answered, not left out: refusing one is its caller's call.
|
|
864
|
+
*/
|
|
865
|
+
readPullRequestByNumber: {
|
|
866
|
+
reading: (number) => `pull request #${number}`,
|
|
867
|
+
read: async (board, via, number, addressed) => {
|
|
868
|
+
const query = repositoryQuery(board, `pullRequest(number: ${numbered('readPullRequestByNumber', board, number)}) { number state headRefOid headRefName baseRefName headRepository { nameWithOwner } }`);
|
|
869
|
+
const pull = heldIn('readPullRequestByNumber', board, await asked('readPullRequestByNumber', board, query, via, addressed), 'pullRequest', addressed);
|
|
870
|
+
const states = { OPEN: 'open', CLOSED: 'closed', MERGED: 'merged' };
|
|
871
|
+
if (!Object.hasOwn(states, pull.state)) fail('readPullRequestByNumber', board, `${addressed}gh answered state ${pull.state}`);
|
|
872
|
+
const headRepository = pull.headRepository?.nameWithOwner ?? null;
|
|
873
|
+
const { head, base } = pullRequestOf(pull);
|
|
874
|
+
return { number: pull.number, head, branch: pull.headRefName, base, state: states[pull.state], headRepository, fromRepository: headRepository !== null && ofRepository(board, headRepository) };
|
|
875
|
+
},
|
|
876
|
+
},
|
|
877
|
+
};
|
|
878
|
+
|
|
879
|
+
/** What a read of a GitHub enum answers for `said`, by `names`, or undefined for any other. */
|
|
880
|
+
const named = (names, said) => (typeof said === 'string' && Object.hasOwn(names, said) ? names[said] : undefined);
|
|
881
|
+
|
|
882
|
+
/** What the policy reads send after `-X GET`: the API version GitHub serves the policy endpoints at. */
|
|
883
|
+
const POLICY_HEADERS = ['-H', API_VERSION_HEADER];
|
|
884
|
+
|
|
885
|
+
/** Whether `value` is an array of strings, as a policy's patterns and events are answered. */
|
|
886
|
+
const strings = (value) => Array.isArray(value) && value.every((each) => typeof each === 'string');
|
|
887
|
+
|
|
888
|
+
/**
|
|
889
|
+
* One policy as its detail read answered it, as `{ id, name, enforcement, workflowPath, events }`:
|
|
890
|
+
* `workflowPath` null where it has no `workflow_path` condition, else its `include` and
|
|
891
|
+
* `exclude` patterns; `events` each `restrict_action_events` rule's allowed events. Any other
|
|
892
|
+
* rule is left out, and an answer of any other shape fails the read.
|
|
893
|
+
*/
|
|
894
|
+
function eventPolicyOf(board, held, id, addressed) {
|
|
895
|
+
const refuse = (why) => fail('readEventPolicies', board, `${addressed}policy ${id}, gh answered ${why}`);
|
|
896
|
+
if (held?.id !== id || typeof held.name !== 'string' || typeof held.enforcement !== 'string' || !Array.isArray(held.rules ?? [])) refuse('no policy id, name, enforcement or list of rules');
|
|
897
|
+
const path = held.conditions?.workflow_path ?? null;
|
|
898
|
+
if (path !== null && (!strings(path.include ?? []) || !strings(path.exclude ?? []))) refuse('a workflow_path that is not lists of patterns');
|
|
899
|
+
const events = (held.rules ?? []).filter((rule) => rule?.type === 'restrict_action_events').map((rule) => (strings(rule.parameters?.allowed_events) ? [...rule.parameters.allowed_events] : refuse('a restrict_action_events rule with no list of allowed events')));
|
|
900
|
+
return { id, name: held.name, enforcement: held.enforcement, workflowPath: path && { include: [...(path.include ?? [])], exclude: [...(path.exclude ?? [])] }, events };
|
|
901
|
+
}
|
|
902
|
+
|
|
903
|
+
/** The git modes of a file whose text a read answers: a regular file, and an executable one. */
|
|
904
|
+
const FILE_MODES = [0o100644, 0o100755];
|
|
905
|
+
|
|
906
|
+
/** What each other git mode a tree entry can hold is, as a failure names it. */
|
|
907
|
+
const ENTRY_KINDS = { [0o040000]: 'a directory', [0o120000]: 'a symbolic link', [0o160000]: 'a submodule' };
|
|
908
|
+
|
|
909
|
+
/**
|
|
910
|
+
* Whether `path` names an entry within a commit's tree as a read sends it: not empty, not from the
|
|
911
|
+
* root, and with no empty, `.` or `..` segment, each of which names something else or nothing.
|
|
912
|
+
*/
|
|
913
|
+
const withinCommit = (path) => typeof path === 'string' && path !== '' && !path.startsWith('/')
|
|
914
|
+
&& path.split('/').every((segment) => !['', '.', '..'].includes(segment));
|
|
915
|
+
|
|
916
|
+
/**
|
|
917
|
+
* The text of the file at `path` in `commit`, or null where the forge's answer shows the commit
|
|
918
|
+
* holding no entry there. An answer that does not show the commit rejects naming it, so a missing
|
|
919
|
+
* commit is never read as a missing file. A directory, a symbolic link, a submodule and a binary
|
|
920
|
+
* file each reject naming the path and what it is, the link's target never answered in its place;
|
|
921
|
+
* so does a file the forge does not send whole, with no part of its text.
|
|
922
|
+
*/
|
|
923
|
+
async function fileAt(board, via, commit, path, addressed) {
|
|
924
|
+
const refuse = (why) => fail('readFileAt', board, `${addressed}${why}`);
|
|
925
|
+
if (typeof commit !== 'string' || !FULL_SHA.test(commit)) refuse(`${commit} is not a commit's full SHA`);
|
|
926
|
+
if (!withinCommit(path)) refuse(`${JSON.stringify(path)} is not a path within a commit`);
|
|
927
|
+
const query = repositoryQuery(board, `object(oid: ${literal(commit)}) { ... on Commit { oid file(path: ${literal(path)}) { mode type object { ... on Blob { text isBinary isTruncated byteSize } } } } }`);
|
|
928
|
+
const said = await readRunner(graphqlRequest(query), via);
|
|
929
|
+
const data = notFoundAt(said, ['repository', 'object', 'file']) ?? answerOf('readFileAt', board, said, addressed);
|
|
930
|
+
if (!data?.repository) refuse('gh answered no repository');
|
|
931
|
+
const { object } = data.repository;
|
|
932
|
+
if (typeof object?.oid !== 'string' || object.oid.toLowerCase() !== commit.toLowerCase() || !Object.hasOwn(object, 'file')) {
|
|
933
|
+
refuse(`gh answered no commit ${commit}`);
|
|
934
|
+
}
|
|
935
|
+
const { file } = object;
|
|
936
|
+
if (file === null) return null;
|
|
937
|
+
if (!FILE_MODES.includes(file?.mode) || file.type !== 'blob') {
|
|
938
|
+
refuse(`${path} is ${ENTRY_KINDS[file?.mode] ?? `an entry of mode ${file?.mode} and type ${file?.type}`}, not a file`);
|
|
939
|
+
}
|
|
940
|
+
const blob = file.object;
|
|
941
|
+
if (blob?.isBinary !== false) refuse(`${path} is a binary file`);
|
|
942
|
+
if (blob.isTruncated !== false || typeof blob.text !== 'string' || Buffer.byteLength(blob.text) !== blob.byteSize) {
|
|
943
|
+
refuse(`gh did not send ${path} whole, ${blob.byteSize} bytes`);
|
|
944
|
+
}
|
|
945
|
+
return blob.text;
|
|
946
|
+
}
|
|
947
|
+
|
|
948
|
+
/**
|
|
949
|
+
* `REPOSITORY_READS` on `board`, each taking what it reads, sent as `send`, `emitter` and
|
|
950
|
+
* `timeout` say.
|
|
951
|
+
*/
|
|
952
|
+
export function repositoryReads(board, { send, emitter, timeout } = {}) {
|
|
953
|
+
const via = { send, emitter, timeout };
|
|
954
|
+
const reads = Object.fromEntries(Object.entries(REPOSITORY_READS).map(([operation, { reading: what, read }]) => [
|
|
955
|
+
operation,
|
|
956
|
+
(target) => {
|
|
957
|
+
const addressed = reading(board, what(target));
|
|
958
|
+
return labelled(operation, board, addressed, () => read(board, via, target, addressed));
|
|
959
|
+
},
|
|
960
|
+
]));
|
|
961
|
+
reads.readCheckStates = (sha, required) => {
|
|
962
|
+
const addressed = reading(board, `checks at ${sha}`);
|
|
963
|
+
return labelled('readCheckStates', board, addressed, () => checkStates(board, via, sha, required, addressed));
|
|
964
|
+
};
|
|
965
|
+
reads.readEffectiveRules = (branch) => {
|
|
966
|
+
const addressed = reading(board, `effective rules for branch ${branch}`);
|
|
967
|
+
return labelled('readEffectiveRules', board, addressed, () => effectiveRules(board, via, branch, addressed));
|
|
968
|
+
};
|
|
969
|
+
reads.readEnvironment = (name) => {
|
|
970
|
+
const addressed = reading(board, `environment ${name}`);
|
|
971
|
+
return labelled('readEnvironment', board, addressed, () => environment(board, via, name, addressed));
|
|
972
|
+
};
|
|
973
|
+
/** The named environment variable's value, or null when its endpoint answers not found. */
|
|
974
|
+
reads.readEnvironmentVariable = (name, variable) => {
|
|
975
|
+
const addressed = reading(board, `environment ${name}'s variable ${variable}`);
|
|
976
|
+
return labelled('readEnvironmentVariable', board, addressed, async () => {
|
|
977
|
+
const held = await environmentRead('readEnvironmentVariable', board, via, `environments/${encodeURIComponent(name)}/variables/${encodeURIComponent(variable)}`, addressed);
|
|
978
|
+
if (held === null) return null;
|
|
979
|
+
if (typeof held.value !== 'string') fail('readEnvironmentVariable', board, `${addressed}gh answered no variable value`);
|
|
980
|
+
return held.value;
|
|
981
|
+
});
|
|
982
|
+
};
|
|
983
|
+
/** Presence by name, without reading a secret value; false only when its endpoint answers not found. */
|
|
984
|
+
reads.readEnvironmentSecret = (name, secret) => {
|
|
985
|
+
const addressed = reading(board, `environment ${name}'s secret ${secret}`);
|
|
986
|
+
return labelled('readEnvironmentSecret', board, addressed, async () => {
|
|
987
|
+
const held = await environmentRead('readEnvironmentSecret', board, via, `environments/${encodeURIComponent(name)}/secrets/${encodeURIComponent(secret)}`, addressed);
|
|
988
|
+
if (held === null) return false;
|
|
989
|
+
if (held.name !== secret) fail('readEnvironmentSecret', board, `${addressed}gh answered no secret name`);
|
|
990
|
+
return true;
|
|
991
|
+
});
|
|
992
|
+
};
|
|
993
|
+
reads.readFileAt = (commit, path) => {
|
|
994
|
+
const addressed = reading(board, `${path} at ${commit}`);
|
|
995
|
+
return labelled('readFileAt', board, addressed, () => fileAt(board, via, commit, path, addressed));
|
|
996
|
+
};
|
|
997
|
+
return reads;
|
|
998
|
+
}
|