mandrel 2.93.0 → 2.94.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/.agents/docs/configuration.md +8 -3
- package/.agents/schemas/lifecycle/merge.unlanded.schema.json +2 -1
- package/.agents/schemas/story-deliver-terminal.schema.json +1 -0
- package/.agents/scripts/lib/duplicate-search.js +52 -18
- package/.agents/scripts/lib/feedback-loop/prior-feedback-fetcher.js +47 -20
- package/.agents/scripts/lib/gh-exec.js +94 -24
- package/.agents/scripts/lib/gh-pr-rest.js +371 -0
- package/.agents/scripts/lib/orchestration/board-graphql-latch.js +75 -0
- package/.agents/scripts/lib/orchestration/ci-rerun-guard.js +73 -29
- package/.agents/scripts/lib/orchestration/column-sync.js +23 -21
- package/.agents/scripts/lib/orchestration/merge-block-class.js +3 -1
- package/.agents/scripts/lib/orchestration/merge-queue.js +49 -4
- package/.agents/scripts/lib/orchestration/project-meta-resolver.js +2 -2
- package/.agents/scripts/lib/orchestration/required-checks.js +72 -12
- package/.agents/scripts/lib/orchestration/single-story-close/phases/auto-merge.js +132 -58
- package/.agents/scripts/lib/orchestration/single-story-close/phases/confirm-merge.js +92 -9
- package/.agents/scripts/lib/orchestration/single-story-close/phases/graphql-preflight.js +41 -18
- package/.agents/scripts/lib/orchestration/ticketing/transition.js +1 -1
- package/.agents/scripts/providers/github/board-add.js +3 -3
- package/.agents/scripts/providers/github/compose.js +2 -0
- package/.agents/scripts/providers/github/errors.js +41 -0
- package/.agents/scripts/providers/github/sub-issues-rest.js +55 -0
- package/.agents/scripts/providers/github/sub-issues.js +9 -1
- package/.agents/scripts/single-story-confirm-merge.js +2 -0
- package/docs/CHANGELOG.md +12 -0
- package/package.json +1 -1
|
@@ -0,0 +1,371 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* gh-pr-rest.js — the `gh pr` facade over repo-scoped REST, for a session
|
|
3
|
+
* whose GitHub proxy refuses GraphQL (which every `gh pr` subcommand needs).
|
|
4
|
+
*
|
|
5
|
+
* Same signatures as `createGh().pr`, and rows in `gh`'s camelCase `--json`
|
|
6
|
+
* shape for exactly the fields current callers request. Only the flags those
|
|
7
|
+
* callers pass are parsed: any other flag or field throws, so a new caller
|
|
8
|
+
* cannot silently lose one. Every request is `repos/{owner}/{repo}/…` — `gh
|
|
9
|
+
* api` fills the placeholders from the current checkout — so the REST path
|
|
10
|
+
* never touches `graphql`, `search/*`, or a user/org endpoint.
|
|
11
|
+
*
|
|
12
|
+
* Takes its `api` and error class by injection so `gh-exec.js` can build it
|
|
13
|
+
* without an import cycle.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/** Arm outcome when the CCR auto-merge route refuses: the merge wait merges on green. */
|
|
17
|
+
export const REST_MERGE_ON_GREEN = 'rest-merge-on-green';
|
|
18
|
+
|
|
19
|
+
const REPO = 'repos/{owner}/{repo}';
|
|
20
|
+
|
|
21
|
+
/** Flag grammar per subcommand: `value` takes the next argv entry, `bool` does not. */
|
|
22
|
+
const FLAG_SPECS = Object.freeze({
|
|
23
|
+
list: {
|
|
24
|
+
'--head': 'value',
|
|
25
|
+
'--state': 'value',
|
|
26
|
+
'--base': 'value',
|
|
27
|
+
'--limit': 'value',
|
|
28
|
+
},
|
|
29
|
+
create: {
|
|
30
|
+
'--base': 'value',
|
|
31
|
+
'--head': 'value',
|
|
32
|
+
'--title': 'value',
|
|
33
|
+
'--body': 'value',
|
|
34
|
+
'--draft': 'bool',
|
|
35
|
+
},
|
|
36
|
+
edit: { '--title': 'value', '--body': 'value' },
|
|
37
|
+
merge: {
|
|
38
|
+
'--auto': 'bool',
|
|
39
|
+
'--squash': 'bool',
|
|
40
|
+
'--delete-branch': 'bool',
|
|
41
|
+
'--disable-auto': 'bool',
|
|
42
|
+
'--match-head-commit': 'value',
|
|
43
|
+
},
|
|
44
|
+
'update-branch': {},
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
function upper(value, fallback = '') {
|
|
48
|
+
return typeof value === 'string' && value ? value.toUpperCase() : fallback;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function mapAutoMerge(autoMerge) {
|
|
52
|
+
if (!autoMerge) return null;
|
|
53
|
+
return {
|
|
54
|
+
enabledAt: null,
|
|
55
|
+
mergeMethod: upper(autoMerge.merge_method),
|
|
56
|
+
enabledBy: { login: autoMerge.enabled_by?.login ?? null },
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Field → reader over a REST pull object; `statusCheckRollup` is read separately. */
|
|
61
|
+
const PULL_FIELDS = Object.freeze({
|
|
62
|
+
number: (p) => p.number,
|
|
63
|
+
url: (p) => p.html_url,
|
|
64
|
+
title: (p) => p.title,
|
|
65
|
+
state: (p) => (p.merged === true || p.merged_at ? 'MERGED' : upper(p.state)),
|
|
66
|
+
mergedAt: (p) => p.merged_at ?? null,
|
|
67
|
+
createdAt: (p) => p.created_at ?? null,
|
|
68
|
+
id: (p) => p.node_id,
|
|
69
|
+
headRefOid: (p) => p.head?.sha ?? null,
|
|
70
|
+
headRefName: (p) => p.head?.ref ?? null,
|
|
71
|
+
baseRefName: (p) => p.base?.ref ?? null,
|
|
72
|
+
isDraft: (p) => p.draft === true,
|
|
73
|
+
mergeStateStatus: (p) => upper(p.mergeable_state, 'UNKNOWN'),
|
|
74
|
+
autoMergeRequest: (p) => mapAutoMerge(p.auto_merge),
|
|
75
|
+
// REST has no review rollup, and nothing on the close path gates on it.
|
|
76
|
+
reviewDecision: () => null,
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
/** Fields a list row lacks (the list endpoint omits `mergeable_state`). */
|
|
80
|
+
const DETAIL_ONLY_FIELDS = new Set(['mergeStateStatus']);
|
|
81
|
+
|
|
82
|
+
const ROLLUP_FIELD = 'statusCheckRollup';
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* @param {{ api: Function, GhExecError: typeof Error }} deps
|
|
86
|
+
*/
|
|
87
|
+
export function createRestPr({ api, GhExecError }) {
|
|
88
|
+
function refuse(message, args) {
|
|
89
|
+
return new GhExecError(`gh-pr-rest: ${message}`, { args });
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async function call(method, endpoint, body) {
|
|
93
|
+
const result = await api({ method, endpoint, body });
|
|
94
|
+
return parseApiJson(result);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function parseFlags(sub, flags) {
|
|
98
|
+
const spec = FLAG_SPECS[sub];
|
|
99
|
+
const out = {};
|
|
100
|
+
const list = Array.isArray(flags) ? flags : [];
|
|
101
|
+
for (let i = 0; i < list.length; i += 1) {
|
|
102
|
+
const flag = list[i];
|
|
103
|
+
const kind = Object.hasOwn(spec, flag) ? spec[flag] : null;
|
|
104
|
+
if (!kind) {
|
|
105
|
+
throw refuse(`\`gh pr ${sub}\` flag ${flag} has no REST mapping`, [
|
|
106
|
+
'pr',
|
|
107
|
+
sub,
|
|
108
|
+
...list,
|
|
109
|
+
]);
|
|
110
|
+
}
|
|
111
|
+
if (kind === 'bool') {
|
|
112
|
+
out[flag] = true;
|
|
113
|
+
} else {
|
|
114
|
+
i += 1;
|
|
115
|
+
out[flag] = list[i];
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return out;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function prNumberOf(id, sub) {
|
|
122
|
+
const text = String(id ?? '');
|
|
123
|
+
const match = /^(\d+)$/.exec(text) ?? /\/pull\/(\d+)/.exec(text);
|
|
124
|
+
if (!match) {
|
|
125
|
+
throw refuse(`\`gh pr ${sub}\` needs a PR number or URL, got "${text}"`, [
|
|
126
|
+
'pr',
|
|
127
|
+
sub,
|
|
128
|
+
text,
|
|
129
|
+
]);
|
|
130
|
+
}
|
|
131
|
+
return Number(match[1]);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function assertFields(sub, fields) {
|
|
135
|
+
if (!Array.isArray(fields) || fields.length === 0) {
|
|
136
|
+
throw refuse(`\`gh pr ${sub}\` needs --json fields over REST`, [
|
|
137
|
+
'pr',
|
|
138
|
+
sub,
|
|
139
|
+
]);
|
|
140
|
+
}
|
|
141
|
+
for (const field of fields) {
|
|
142
|
+
if (field !== ROLLUP_FIELD && !Object.hasOwn(PULL_FIELDS, field)) {
|
|
143
|
+
throw refuse(
|
|
144
|
+
`\`gh pr ${sub}\` --json field ${field} has no REST mapping`,
|
|
145
|
+
['pr', sub, '--json', fields.join(',')],
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const getPull = (n) => call('GET', `${REPO}/pulls/${n}`);
|
|
152
|
+
|
|
153
|
+
async function readRollup(sha) {
|
|
154
|
+
if (!sha) return [];
|
|
155
|
+
const [runs, status] = await Promise.all([
|
|
156
|
+
call('GET', `${REPO}/commits/${sha}/check-runs?per_page=100`),
|
|
157
|
+
call('GET', `${REPO}/commits/${sha}/status`),
|
|
158
|
+
]);
|
|
159
|
+
return [
|
|
160
|
+
...(runs?.check_runs ?? []).map(toCheckRunNode),
|
|
161
|
+
...(status?.statuses ?? []).map(toStatusContextNode),
|
|
162
|
+
];
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
async function project(pull, fields) {
|
|
166
|
+
const row = {};
|
|
167
|
+
for (const field of fields) {
|
|
168
|
+
if (field === ROLLUP_FIELD) {
|
|
169
|
+
row[field] = await readRollup(pull.head?.sha);
|
|
170
|
+
} else {
|
|
171
|
+
row[field] = PULL_FIELDS[field](pull);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
return row;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
async function view(id, fields) {
|
|
178
|
+
const n = prNumberOf(id, 'view');
|
|
179
|
+
assertFields('view', fields);
|
|
180
|
+
return project(await getPull(n), fields);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
async function list(flags = [], fields) {
|
|
184
|
+
const opts = parseFlags('list', flags);
|
|
185
|
+
assertFields('list', fields);
|
|
186
|
+
const rows = await call('GET', listEndpoint(opts));
|
|
187
|
+
const wanted = (Array.isArray(rows) ? rows : []).filter(
|
|
188
|
+
(p) => opts['--state'] !== 'merged' || p.merged_at,
|
|
189
|
+
);
|
|
190
|
+
const needsDetail = fields.some((f) => DETAIL_ONLY_FIELDS.has(f));
|
|
191
|
+
const out = [];
|
|
192
|
+
for (const row of wanted) {
|
|
193
|
+
out.push(
|
|
194
|
+
await project(needsDetail ? await getPull(row.number) : row, fields),
|
|
195
|
+
);
|
|
196
|
+
}
|
|
197
|
+
return out;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
async function create(flags = []) {
|
|
201
|
+
const opts = parseFlags('create', flags);
|
|
202
|
+
const pull = await call('POST', `${REPO}/pulls`, {
|
|
203
|
+
title: opts['--title'],
|
|
204
|
+
head: opts['--head'],
|
|
205
|
+
base: opts['--base'],
|
|
206
|
+
body: opts['--body'] ?? '',
|
|
207
|
+
...(opts['--draft'] ? { draft: true } : {}),
|
|
208
|
+
});
|
|
209
|
+
return envelope(`${pull?.html_url ?? ''}\n`);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
async function edit(id, flags = []) {
|
|
213
|
+
const n = prNumberOf(id, 'edit');
|
|
214
|
+
const opts = parseFlags('edit', flags);
|
|
215
|
+
const patch = {};
|
|
216
|
+
if (opts['--title'] !== undefined) patch.title = opts['--title'];
|
|
217
|
+
if (opts['--body'] !== undefined) patch.body = opts['--body'];
|
|
218
|
+
const pull = await call('PATCH', `${REPO}/pulls/${n}`, patch);
|
|
219
|
+
return envelope(`${pull?.html_url ?? ''}\n`);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* `--auto` arms through the CCR route; its refusal resolves (not throws)
|
|
224
|
+
* with `mergeOnGreen`, since the wait then owns the merge.
|
|
225
|
+
*/
|
|
226
|
+
async function armAuto(n) {
|
|
227
|
+
try {
|
|
228
|
+
await call('PUT', `${REPO}/pulls/${n}/ccr/auto_merge`, {
|
|
229
|
+
merge_method: 'squash',
|
|
230
|
+
});
|
|
231
|
+
return envelope('');
|
|
232
|
+
} catch (err) {
|
|
233
|
+
return { ...envelope(''), mergeOnGreen: true, detail: describe(err) };
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/** Squash-merge pinned to the head SHA, then reap the head ref (best-effort). */
|
|
238
|
+
async function squashMerge(n, opts) {
|
|
239
|
+
const pull = await getPull(n);
|
|
240
|
+
const sha = opts['--match-head-commit'] ?? pull.head?.sha;
|
|
241
|
+
const merged = await call('PUT', `${REPO}/pulls/${n}/merge`, {
|
|
242
|
+
merge_method: 'squash',
|
|
243
|
+
...(sha ? { sha } : {}),
|
|
244
|
+
});
|
|
245
|
+
if (opts['--delete-branch'] && sameRepo(pull)) {
|
|
246
|
+
await call(
|
|
247
|
+
'DELETE',
|
|
248
|
+
`${REPO}/git/refs/heads/${encodeRef(pull.head.ref)}`,
|
|
249
|
+
).catch(() => null);
|
|
250
|
+
}
|
|
251
|
+
return {
|
|
252
|
+
...envelope(''),
|
|
253
|
+
merged: merged?.merged === true,
|
|
254
|
+
sha: merged?.sha ?? null,
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
async function merge(id, flags = []) {
|
|
259
|
+
const n = prNumberOf(id, 'merge');
|
|
260
|
+
const opts = parseFlags('merge', flags);
|
|
261
|
+
if (opts['--disable-auto']) {
|
|
262
|
+
await call('DELETE', `${REPO}/pulls/${n}/ccr/auto_merge`);
|
|
263
|
+
return envelope('');
|
|
264
|
+
}
|
|
265
|
+
if (opts['--auto']) return armAuto(n);
|
|
266
|
+
if (!opts['--squash']) {
|
|
267
|
+
throw refuse('a REST merge supports --squash only', [
|
|
268
|
+
'pr',
|
|
269
|
+
'merge',
|
|
270
|
+
...flags,
|
|
271
|
+
]);
|
|
272
|
+
}
|
|
273
|
+
return squashMerge(n, opts);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
async function updateBranch(id, flags = []) {
|
|
277
|
+
const n = prNumberOf(id, 'update-branch');
|
|
278
|
+
parseFlags('update-branch', flags);
|
|
279
|
+
await call('PUT', `${REPO}/pulls/${n}/update-branch`, {});
|
|
280
|
+
return envelope('');
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
return { view, list, create, edit, merge, updateBranch };
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
function envelope(stdout) {
|
|
287
|
+
return { stdout, stderr: '', code: 0 };
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
function describe(err) {
|
|
291
|
+
const stderr = String(err?.stderr ?? '').trim();
|
|
292
|
+
const message = String(err?.message ?? err);
|
|
293
|
+
return stderr ? `${message}: ${stderr.split('\n').at(-1)}` : message;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
function sameRepo(pull) {
|
|
297
|
+
const head = pull?.head?.repo?.full_name;
|
|
298
|
+
const base = pull?.base?.repo?.full_name;
|
|
299
|
+
return Boolean(pull?.head?.ref) && (!head || !base || head === base);
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
function encodeRef(ref) {
|
|
303
|
+
return String(ref).split('/').map(encodeURIComponent).join('/');
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
function listEndpoint(opts) {
|
|
307
|
+
const params = [];
|
|
308
|
+
if (opts['--head'])
|
|
309
|
+
params.push(`head={owner}:${encodeURIComponent(opts['--head'])}`);
|
|
310
|
+
if (opts['--base']) params.push(`base=${encodeURIComponent(opts['--base'])}`);
|
|
311
|
+
const state = opts['--state'] ?? 'open';
|
|
312
|
+
params.push(`state=${state === 'merged' ? 'closed' : state}`);
|
|
313
|
+
if (opts['--limit']) params.push(`per_page=${Number(opts['--limit']) || 30}`);
|
|
314
|
+
return `${REPO}/pulls?${params.join('&')}`;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/** A REST check run as the `CheckRun` node `gh`'s rollup carries. */
|
|
318
|
+
function toCheckRunNode(run) {
|
|
319
|
+
return {
|
|
320
|
+
__typename: 'CheckRun',
|
|
321
|
+
name: run?.name ?? '',
|
|
322
|
+
status: upper(run?.status),
|
|
323
|
+
conclusion: upper(run?.conclusion),
|
|
324
|
+
startedAt: run?.started_at ?? '',
|
|
325
|
+
completedAt: run?.completed_at ?? '',
|
|
326
|
+
detailsUrl: run?.details_url ?? run?.html_url ?? '',
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/** A REST commit status as the `StatusContext` node `gh`'s rollup carries. */
|
|
331
|
+
function toStatusContextNode(status) {
|
|
332
|
+
return {
|
|
333
|
+
__typename: 'StatusContext',
|
|
334
|
+
context: status?.context ?? '',
|
|
335
|
+
state: upper(status?.state),
|
|
336
|
+
targetUrl: status?.target_url ?? '',
|
|
337
|
+
startedAt: status?.created_at ?? '',
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/**
|
|
342
|
+
* `gh api` output (`{ stdout }`, a string, or an already-parsed value) as JSON.
|
|
343
|
+
* An empty body (a 204) reads as `null`.
|
|
344
|
+
*
|
|
345
|
+
* @param {unknown} result
|
|
346
|
+
* @returns {unknown}
|
|
347
|
+
*/
|
|
348
|
+
export function parseApiJson(result) {
|
|
349
|
+
if (result && typeof result === 'object' && !('stdout' in result))
|
|
350
|
+
return result;
|
|
351
|
+
const text = typeof result === 'string' ? result : result?.stdout;
|
|
352
|
+
const trimmed = String(text ?? '').trim();
|
|
353
|
+
return trimmed ? JSON.parse(trimmed) : null;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* The active rulesets on `base` (`GET …/rules/branches/{base}`).
|
|
358
|
+
*
|
|
359
|
+
* @param {{ api: Function, base: string, timeoutMs?: number }} args
|
|
360
|
+
* @returns {Promise<Array<{ type?: string, parameters?: object }>>}
|
|
361
|
+
*/
|
|
362
|
+
export async function readBranchRules({ api, base, timeoutMs }) {
|
|
363
|
+
const rules = parseApiJson(
|
|
364
|
+
await api({
|
|
365
|
+
method: 'GET',
|
|
366
|
+
endpoint: `${REPO}/rules/branches/${encodeRef(base)}`,
|
|
367
|
+
...(timeoutMs ? { execOpts: { timeoutMs } } : {}),
|
|
368
|
+
}),
|
|
369
|
+
);
|
|
370
|
+
return Array.isArray(rules) ? rules : [];
|
|
371
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* board-graphql-latch — a cloud session's proxy refuses GraphQL, and
|
|
3
|
+
* Projects v2 is GraphQL-only, so board seams report `graphql-blocked` and
|
|
4
|
+
* warn once per process. Wraps `board-access-latch.js`.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { isGraphqlBlocked } from '../../providers/github/errors.js';
|
|
8
|
+
import { Logger } from '../Logger.js';
|
|
9
|
+
import {
|
|
10
|
+
boardScopeFailure,
|
|
11
|
+
isProjectAccessFailure,
|
|
12
|
+
reportBoardScopeFailure,
|
|
13
|
+
SCOPE_FAILURE,
|
|
14
|
+
} from './board-access-latch.js';
|
|
15
|
+
|
|
16
|
+
const GRAPHQL_BLOCKED_FAILURE = Object.freeze({
|
|
17
|
+
reason: 'graphql-blocked',
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
const REMEDY =
|
|
21
|
+
'GitHub Projects v2 board updates are skipped: this session refuses GitHub GraphQL (Claude Code cloud proxy), and Projects v2 is GraphQL-only, so board adds and Status flips cannot sync from a cloud session (labels still move). Run `node .agents/scripts/board-backfill.js` from a local session to catch the board up.';
|
|
22
|
+
|
|
23
|
+
const warnedSinks = new WeakSet();
|
|
24
|
+
|
|
25
|
+
function reportBoardGraphqlBlocked({ logger = Logger } = {}) {
|
|
26
|
+
if (warnedSinks.has(logger)) return false;
|
|
27
|
+
warnedSinks.add(logger);
|
|
28
|
+
logger.warn?.(REMEDY);
|
|
29
|
+
return true;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* @param {unknown} err
|
|
34
|
+
* @returns {boolean}
|
|
35
|
+
*/
|
|
36
|
+
export function isBoardAccessFailure(err) {
|
|
37
|
+
return isGraphqlBlocked(err) || isProjectAccessFailure(err);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* @param {unknown} err
|
|
42
|
+
* @returns {'graphql-blocked' | 'insufficient-scope' | null}
|
|
43
|
+
*/
|
|
44
|
+
export function boardAccessFailureReason(err) {
|
|
45
|
+
if (isGraphqlBlocked(err)) return GRAPHQL_BLOCKED_FAILURE.reason;
|
|
46
|
+
return isProjectAccessFailure(err) ? SCOPE_FAILURE.reason : null;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The Status-sync outcome for `reason`: a blocked session skips, a scope
|
|
51
|
+
* refusal fails.
|
|
52
|
+
*
|
|
53
|
+
* @param {'graphql-blocked' | 'insufficient-scope'} reason
|
|
54
|
+
* @param {{ logger?: { warn?: Function } }} [opts]
|
|
55
|
+
* @returns {{ status: string, reason: string, scope?: string }}
|
|
56
|
+
*/
|
|
57
|
+
export function reportBoardAccessFailure(reason, { logger } = {}) {
|
|
58
|
+
if (reason === GRAPHQL_BLOCKED_FAILURE.reason) {
|
|
59
|
+
reportBoardGraphqlBlocked({ logger });
|
|
60
|
+
return { status: 'skipped', ...GRAPHQL_BLOCKED_FAILURE };
|
|
61
|
+
}
|
|
62
|
+
reportBoardScopeFailure({ logger });
|
|
63
|
+
return { status: 'failed', ...SCOPE_FAILURE };
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* @param {unknown} err
|
|
68
|
+
* @param {{ logger?: { warn?: Function } }} [opts]
|
|
69
|
+
* @returns {{ reason: string, scope?: string } | null}
|
|
70
|
+
*/
|
|
71
|
+
export function boardAccessFailure(err, { logger } = {}) {
|
|
72
|
+
if (!isGraphqlBlocked(err)) return boardScopeFailure(err, { logger });
|
|
73
|
+
reportBoardGraphqlBlocked({ logger });
|
|
74
|
+
return GRAPHQL_BLOCKED_FAILURE;
|
|
75
|
+
}
|
|
@@ -22,7 +22,7 @@ import {
|
|
|
22
22
|
} from 'node:fs';
|
|
23
23
|
import path from 'node:path';
|
|
24
24
|
import { resolveConfig } from '../config-resolver.js';
|
|
25
|
-
import { runGh } from '../gh-exec.js';
|
|
25
|
+
import { prSurfaceUsesRest, runGh } from '../gh-exec.js';
|
|
26
26
|
import { Logger } from '../Logger.js';
|
|
27
27
|
import { createProvider } from '../provider-factory.js';
|
|
28
28
|
import { isRerunPermitted } from './check-state.js';
|
|
@@ -108,6 +108,65 @@ function ghRunLogTail({ runId, cwd, spawnFn, maxLines = 40 }) {
|
|
|
108
108
|
return lines.slice(-maxLines).join('\n');
|
|
109
109
|
}
|
|
110
110
|
|
|
111
|
+
/**
|
|
112
|
+
* Parsed JSON stdout of a `gh` run, or `null` on an unparseable one — or a
|
|
113
|
+
* failed one, unless `anyExit` (`gh pr checks` exits non-zero on a red or
|
|
114
|
+
* pending check and still prints the rows).
|
|
115
|
+
*/
|
|
116
|
+
function readGhJson(args, { cwd, spawnFn, anyExit = false }) {
|
|
117
|
+
const result = runGh(args, { cwd }, spawnFn);
|
|
118
|
+
if (!anyExit && (result?.status ?? 1) !== 0) return null;
|
|
119
|
+
try {
|
|
120
|
+
return JSON.parse((result.stdout ?? '').trim() || 'null');
|
|
121
|
+
} catch {
|
|
122
|
+
return null;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** The PR number in a number, numeric string, or PR URL; `null` otherwise. */
|
|
127
|
+
function prNumberOfRef(prRef) {
|
|
128
|
+
const match = /^(\d+)$|\/pull\/(\d+)/.exec(String(prRef ?? ''));
|
|
129
|
+
return match ? (match[1] ?? match[2]) : null;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* The head SHA over the REST transport (GraphQL answers 403 there, and
|
|
134
|
+
* `gh pr view` needs it).
|
|
135
|
+
*
|
|
136
|
+
* @returns {string|null}
|
|
137
|
+
*/
|
|
138
|
+
function resolvePrHeadShaRest({ prRef, cwd, spawnFn }) {
|
|
139
|
+
const n = prNumberOfRef(prRef);
|
|
140
|
+
if (!n) return null;
|
|
141
|
+
const pull = readGhJson(['api', `repos/{owner}/{repo}/pulls/${n}`], {
|
|
142
|
+
cwd,
|
|
143
|
+
spawnFn,
|
|
144
|
+
});
|
|
145
|
+
const sha = pull?.head?.sha;
|
|
146
|
+
return typeof sha === 'string' && sha.length > 0 ? sha : null;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** `[{ name, link }]` for the PR head's check runs, as `gh pr checks --json` gives it. */
|
|
150
|
+
function readCheckLinks({ prRef, cwd, spawnFn }) {
|
|
151
|
+
if (!prSurfaceUsesRest()) {
|
|
152
|
+
return readGhJson(['pr', 'checks', prRef, '--json', 'name,link'], {
|
|
153
|
+
cwd,
|
|
154
|
+
spawnFn,
|
|
155
|
+
anyExit: true,
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
const sha = resolvePrHeadShaRest({ prRef, cwd, spawnFn });
|
|
159
|
+
if (!sha) return null;
|
|
160
|
+
const runs = readGhJson(
|
|
161
|
+
['api', `repos/{owner}/{repo}/commits/${sha}/check-runs?per_page=100`],
|
|
162
|
+
{ cwd, spawnFn },
|
|
163
|
+
);
|
|
164
|
+
return (runs?.check_runs ?? []).map((run) => ({
|
|
165
|
+
name: run?.name,
|
|
166
|
+
link: run?.details_url ?? run?.html_url ?? null,
|
|
167
|
+
}));
|
|
168
|
+
}
|
|
169
|
+
|
|
111
170
|
/**
|
|
112
171
|
* The failing check's run id and URL (the intake filing needs the link).
|
|
113
172
|
* Best-effort.
|
|
@@ -115,22 +174,13 @@ function ghRunLogTail({ runId, cwd, spawnFn, maxLines = 40 }) {
|
|
|
115
174
|
* @returns {{ runId: string|null, url: string|null }}
|
|
116
175
|
*/
|
|
117
176
|
function resolveFailingCheckRun({ prRef, checkName, cwd, spawnFn }) {
|
|
118
|
-
const
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
);
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
const entry = Array.isArray(parsed)
|
|
126
|
-
? parsed.find((e) => e?.name === checkName)
|
|
127
|
-
: null;
|
|
128
|
-
const link = entry?.link ? String(entry.link) : null;
|
|
129
|
-
const m = link ? /\/runs\/(\d+)/.exec(link) : null;
|
|
130
|
-
return { runId: m ? m[1] : null, url: link };
|
|
131
|
-
} catch {
|
|
132
|
-
return { runId: null, url: null };
|
|
133
|
-
}
|
|
177
|
+
const parsed = readCheckLinks({ prRef, cwd, spawnFn });
|
|
178
|
+
const entry = Array.isArray(parsed)
|
|
179
|
+
? parsed.find((e) => e?.name === checkName)
|
|
180
|
+
: null;
|
|
181
|
+
const link = entry?.link ? String(entry.link) : null;
|
|
182
|
+
const m = link ? /\/runs\/(\d+)/.exec(link) : null;
|
|
183
|
+
return { runId: m ? m[1] : null, url: link };
|
|
134
184
|
}
|
|
135
185
|
|
|
136
186
|
/**
|
|
@@ -141,19 +191,13 @@ function resolveFailingCheckRun({ prRef, checkName, cwd, spawnFn }) {
|
|
|
141
191
|
* @returns {string|null}
|
|
142
192
|
*/
|
|
143
193
|
export function resolvePrHeadSha({ prRef, cwd, spawnFn }) {
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
194
|
+
if (prSurfaceUsesRest()) return resolvePrHeadShaRest({ prRef, cwd, spawnFn });
|
|
195
|
+
const parsed = readGhJson(['pr', 'view', prRef, '--json', 'headRefOid'], {
|
|
196
|
+
cwd,
|
|
147
197
|
spawnFn,
|
|
148
|
-
);
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
const parsed = JSON.parse((result.stdout ?? '').trim() || '{}');
|
|
152
|
-
const sha = parsed?.headRefOid;
|
|
153
|
-
return typeof sha === 'string' && sha.length > 0 ? sha : null;
|
|
154
|
-
} catch {
|
|
155
|
-
return null;
|
|
156
|
-
}
|
|
198
|
+
});
|
|
199
|
+
const sha = parsed?.headRefOid;
|
|
200
|
+
return typeof sha === 'string' && sha.length > 0 ? sha : null;
|
|
157
201
|
}
|
|
158
202
|
|
|
159
203
|
/**
|
|
@@ -4,15 +4,16 @@
|
|
|
4
4
|
* GraphQL. Soft no-op when no project is configured, the Status field or
|
|
5
5
|
* option is missing, or the issue is not on the board. A token that cannot
|
|
6
6
|
* reach the board (missing `project` scope) reports `{ status: 'failed',
|
|
7
|
-
* reason: 'insufficient-scope' }`
|
|
7
|
+
* reason: 'insufficient-scope' }` (a GraphQL-blocked session: `skipped` /
|
|
8
|
+
* `graphql-blocked`) and trips a warn-once latch instead.
|
|
8
9
|
*/
|
|
9
10
|
|
|
10
11
|
import { AGENT_LABELS } from '../label-constants.js';
|
|
11
12
|
import {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
} from './board-
|
|
13
|
+
boardAccessFailureReason,
|
|
14
|
+
isBoardAccessFailure,
|
|
15
|
+
reportBoardAccessFailure,
|
|
16
|
+
} from './board-graphql-latch.js';
|
|
16
17
|
import {
|
|
17
18
|
invalidateProjectMetaCache,
|
|
18
19
|
readProjectMetaCache,
|
|
@@ -128,8 +129,8 @@ export class ColumnSync {
|
|
|
128
129
|
this._meta = null; // lazy-cached { projectId, fieldId, options: Map<name, id> }
|
|
129
130
|
// Disk-hydrated meta may be stale; a failed mutation then invalidates it.
|
|
130
131
|
this._metaFromDiskCache = false;
|
|
131
|
-
//
|
|
132
|
-
this.
|
|
132
|
+
// Access-failure reason seen this process; memory only, never on disk.
|
|
133
|
+
this._accessFailure = null;
|
|
133
134
|
}
|
|
134
135
|
|
|
135
136
|
/**
|
|
@@ -157,8 +158,8 @@ export class ColumnSync {
|
|
|
157
158
|
try {
|
|
158
159
|
return await this.#setColumnOnBoard(issueId, column);
|
|
159
160
|
} catch (err) {
|
|
160
|
-
if (!
|
|
161
|
-
return this.#
|
|
161
|
+
if (!isBoardAccessFailure(err)) throw err;
|
|
162
|
+
return this.#accessFailure(err);
|
|
162
163
|
}
|
|
163
164
|
}
|
|
164
165
|
|
|
@@ -184,26 +185,27 @@ export class ColumnSync {
|
|
|
184
185
|
// Likely a reconfigured board: drop cached meta so the next flip
|
|
185
186
|
// self-heals, and re-throw for the caller's handling. A scope refusal
|
|
186
187
|
// is not staleness, so the cache stays.
|
|
187
|
-
if (!
|
|
188
|
+
if (!isBoardAccessFailure(err)) this.#invalidateMetaCache();
|
|
188
189
|
throw err;
|
|
189
190
|
}
|
|
190
191
|
return { status: 'synced', column };
|
|
191
192
|
}
|
|
192
193
|
|
|
193
|
-
/** No meta: a latched
|
|
194
|
+
/** No meta: a latched access refusal, or a board without a Status field. */
|
|
194
195
|
#metaMiss() {
|
|
195
|
-
if (this.
|
|
196
|
+
if (this._accessFailure) return this.#accessFailure();
|
|
196
197
|
return { status: 'skipped', reason: 'no-meta' };
|
|
197
198
|
}
|
|
198
199
|
|
|
199
200
|
/**
|
|
200
|
-
* Latch the
|
|
201
|
-
*
|
|
201
|
+
* Latch the access failure (first reason holds) and report it; never
|
|
202
|
+
* throws, so the label transition still completes.
|
|
202
203
|
*/
|
|
203
|
-
#
|
|
204
|
-
this.
|
|
205
|
-
|
|
206
|
-
|
|
204
|
+
#accessFailure(err) {
|
|
205
|
+
this._accessFailure ??= boardAccessFailureReason(err);
|
|
206
|
+
return reportBoardAccessFailure(this._accessFailure, {
|
|
207
|
+
logger: this.logger,
|
|
208
|
+
});
|
|
207
209
|
}
|
|
208
210
|
|
|
209
211
|
/**
|
|
@@ -231,7 +233,7 @@ export class ColumnSync {
|
|
|
231
233
|
}
|
|
232
234
|
|
|
233
235
|
async #loadMeta() {
|
|
234
|
-
if (this.
|
|
236
|
+
if (this._accessFailure) return null;
|
|
235
237
|
if (this._meta !== null) return this._meta || null;
|
|
236
238
|
// Board meta is repo-invariant; the disk cache spares each cold CLI
|
|
237
239
|
// process the resolve round-trips.
|
|
@@ -282,8 +284,8 @@ export class ColumnSync {
|
|
|
282
284
|
this._metaFromDiskCache = false;
|
|
283
285
|
return this._meta;
|
|
284
286
|
} catch (err) {
|
|
285
|
-
if (
|
|
286
|
-
this.
|
|
287
|
+
if (isBoardAccessFailure(err)) {
|
|
288
|
+
this._accessFailure = boardAccessFailureReason(err);
|
|
287
289
|
return null;
|
|
288
290
|
}
|
|
289
291
|
this.logger.warn?.(
|
|
@@ -26,7 +26,8 @@ export const BLOCK_CLASSES = Object.freeze([
|
|
|
26
26
|
|
|
27
27
|
/**
|
|
28
28
|
* Plus classes emitted directly, never by the classifier: `predicate-refused`
|
|
29
|
-
* (kept so archived records validate)
|
|
29
|
+
* (kept so archived records validate), the advisory-gate pair, and the REST
|
|
30
|
+
* transport's merge-queue refusal (`merge-queue.js` owns its constant).
|
|
30
31
|
*/
|
|
31
32
|
export const MERGE_UNLANDED_BLOCK_CLASSES = Object.freeze([
|
|
32
33
|
...BLOCK_CLASSES,
|
|
@@ -34,6 +35,7 @@ export const MERGE_UNLANDED_BLOCK_CLASSES = Object.freeze([
|
|
|
34
35
|
// Sourced from the gate's own constants so the vocabulary cannot drift.
|
|
35
36
|
ADVISORY_GATE_RED_CLASS,
|
|
36
37
|
ADVISORY_GATE_INCONCLUSIVE_CLASS,
|
|
38
|
+
'merge-queue-rest-unsupported',
|
|
37
39
|
]);
|
|
38
40
|
|
|
39
41
|
const BLOCK_CLASS_SET = new Set(MERGE_UNLANDED_BLOCK_CLASSES);
|