next-issue 2.21.1 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +89 -124
- package/dist/agent-reply.mjs +13 -0
- package/dist/agent-session.mjs +56 -0
- package/dist/agents.mjs +47 -88
- package/dist/args.mjs +26 -31
- package/dist/base-checks.mjs +37 -0
- package/dist/budget.mjs +16 -0
- package/dist/check-poll.mjs +73 -0
- package/dist/checks.mjs +67 -0
- package/dist/children.mjs +52 -0
- package/dist/cli.mjs +25 -15
- package/dist/command-records.mjs +15 -0
- package/dist/config-fields.mjs +32 -0
- package/dist/config.mjs +36 -71
- package/dist/conflict.mjs +43 -42
- package/dist/durations.mjs +37 -0
- package/dist/enter-review.mjs +13 -0
- package/dist/errors.mjs +20 -0
- package/dist/exec.mjs +71 -107
- package/dist/files.mjs +28 -0
- package/dist/fix.mjs +61 -50
- package/dist/gate.mjs +31 -43
- package/dist/gh.mjs +55 -0
- package/dist/git-commit.mjs +36 -0
- package/dist/git-land.mjs +30 -0
- package/dist/git-merge.mjs +42 -0
- package/dist/git.mjs +42 -166
- package/dist/github.mjs +32 -386
- package/dist/gitignore.mjs +13 -0
- package/dist/hand-over-comments.mjs +81 -0
- package/dist/handover.mjs +62 -103
- package/dist/implement.mjs +32 -0
- package/dist/interrupt.mjs +6 -19
- package/dist/job.mjs +28 -17
- package/dist/labels.mjs +65 -0
- package/dist/land.mjs +66 -0
- package/dist/lock.mjs +54 -0
- package/dist/logs.mjs +84 -62
- package/dist/loop.mjs +12 -37
- package/dist/observe.mjs +24 -98
- package/dist/open-job.mjs +39 -0
- package/dist/pending.mjs +46 -0
- package/dist/pipeline.mjs +34 -217
- package/dist/poll.mjs +20 -0
- package/dist/ports.mjs +18 -3
- package/dist/progress.mjs +36 -22
- package/dist/prompts.mjs +31 -36
- package/dist/protocol.mjs +17 -11
- package/dist/prune-runs.mjs +33 -0
- package/dist/pull-requests.mjs +60 -0
- package/dist/questions.mjs +45 -0
- package/dist/records.mjs +3 -0
- package/dist/render.mjs +19 -0
- package/dist/revert.mjs +27 -0
- package/dist/review.mjs +63 -83
- package/dist/run-logs.mjs +23 -0
- package/dist/run.mjs +59 -58
- package/dist/serve.mjs +46 -214
- package/dist/setup.mjs +2 -5
- package/dist/skip.mjs +54 -0
- package/dist/state.mjs +27 -39
- package/dist/summary.mjs +11 -7
- package/dist/text.mjs +15 -0
- package/dist/usage.mjs +45 -0
- package/dist/verdict.mjs +20 -13
- package/dist/watch.mjs +38 -0
- package/dist/worker-paths.mjs +16 -0
- package/dist/worker-run.mjs +56 -0
- package/dist/worker-server.mjs +56 -0
- package/dist/worktree-setup.mjs +25 -0
- package/dist/worktree.mjs +58 -0
- package/package.json +1 -1
- package/dist/merge.mjs +0 -75
package/dist/github.mjs
CHANGED
|
@@ -1,40 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
const READ_TRIES = 3;
|
|
5
|
-
const NETWORK_TRIES = 3;
|
|
6
|
-
const NETWORK_FAULT = /unexpected EOF|connection reset|connection refused|i\/o timeout|TLS handshake timeout|HTTP 50[234]/i;
|
|
7
|
-
const KNOWN_LABELS = new Map();
|
|
8
|
-
const NO_CHECKS = /no checks reported/i;
|
|
9
|
-
const FAILED_CONCLUSIONS = new Set(['failure', 'timed_out', 'startup_failure']);
|
|
10
|
-
function slug(repo) {
|
|
11
|
-
return `${repo.owner}/${repo.name}`;
|
|
12
|
-
}
|
|
13
|
-
async function repeatable(args, options) {
|
|
14
|
-
for (let attempt = 1;; attempt += 1) {
|
|
15
|
-
try {
|
|
16
|
-
return await must('gh', args, options);
|
|
17
|
-
}
|
|
18
|
-
catch (error) {
|
|
19
|
-
if (attempt >= NETWORK_TRIES ||
|
|
20
|
-
!(error instanceof Error) ||
|
|
21
|
-
!NETWORK_FAULT.test(error.message)) {
|
|
22
|
-
throw error;
|
|
23
|
-
}
|
|
24
|
-
await new Promise((resolve) => setTimeout(resolve, attempt * 1000));
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
async function gh(repo, args) {
|
|
29
|
-
return repeatable([...args, '--repo', slug(repo)], { cwd: repo.root });
|
|
30
|
-
}
|
|
31
|
-
async function ghOnce(repo, args, input) {
|
|
32
|
-
return must('gh', [...args, '--repo', slug(repo)], { cwd: repo.root, input });
|
|
33
|
-
}
|
|
1
|
+
import { errorMatches, parseJson } from './errors.mjs';
|
|
2
|
+
import { gh, ghApi, ghJson, ghOnce, names, repeatable, slug, viewJson, } from './gh.mjs';
|
|
3
|
+
import { lines } from './text.mjs';
|
|
34
4
|
export async function defaultBranch(repo) {
|
|
35
|
-
const raw = await repeatable(['repo', 'view', slug(repo), '--json', 'defaultBranchRef']
|
|
36
|
-
cwd: repo.root,
|
|
37
|
-
});
|
|
5
|
+
const raw = await repeatable(repo, ['repo', 'view', slug(repo), '--json', 'defaultBranchRef']);
|
|
38
6
|
return JSON.parse(raw).defaultBranchRef.name;
|
|
39
7
|
}
|
|
40
8
|
const ISSUE_FIELDS = 'number,title,body,createdAt,labels';
|
|
@@ -44,394 +12,72 @@ function toIssue(item) {
|
|
|
44
12
|
title: item.title,
|
|
45
13
|
body: item.body ?? '',
|
|
46
14
|
createdAt: item.createdAt,
|
|
47
|
-
labels: item.labels
|
|
15
|
+
labels: names(item.labels),
|
|
48
16
|
};
|
|
49
17
|
}
|
|
18
|
+
function labelFilter(labels) {
|
|
19
|
+
return labels.length === 0 ? '' : ` label:${labels.map((label) => `"${label}"`).join(',')}`;
|
|
20
|
+
}
|
|
50
21
|
export async function openIssues(repo, limit, labels) {
|
|
51
|
-
const
|
|
52
|
-
const raw = await gh(repo, [
|
|
22
|
+
const parsed = await ghJson(repo, [
|
|
53
23
|
'issue',
|
|
54
24
|
'list',
|
|
55
25
|
'--state',
|
|
56
26
|
'open',
|
|
57
27
|
'--search',
|
|
58
|
-
`sort:created-asc${
|
|
28
|
+
`sort:created-asc${labelFilter(labels)}`,
|
|
59
29
|
'--limit',
|
|
60
30
|
String(limit),
|
|
61
31
|
'--json',
|
|
62
32
|
ISSUE_FIELDS,
|
|
63
33
|
]);
|
|
64
|
-
const parsed = JSON.parse(raw);
|
|
65
34
|
return parsed.map(toIssue);
|
|
66
35
|
}
|
|
67
36
|
export async function viewIssue(repo, issue) {
|
|
68
|
-
const
|
|
69
|
-
const parsed = JSON.parse(raw);
|
|
37
|
+
const parsed = await viewJson(repo, 'issue', issue, `${ISSUE_FIELDS},state`);
|
|
70
38
|
return { ...toIssue(parsed), open: parsed.state === 'OPEN' };
|
|
71
39
|
}
|
|
72
40
|
export async function issueComments(repo, issue) {
|
|
73
|
-
const
|
|
74
|
-
const parsed = JSON.parse(raw);
|
|
41
|
+
const parsed = await viewJson(repo, 'issue', issue, 'comments');
|
|
75
42
|
return parsed.comments.map((comment) => ({ author: comment.author.login, body: comment.body }));
|
|
76
43
|
}
|
|
77
44
|
export async function threadComments(repo, number) {
|
|
78
|
-
const raw = await
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
], { cwd: repo.root });
|
|
85
|
-
return raw
|
|
86
|
-
.split('\n')
|
|
87
|
-
.filter((line) => line.length > 0)
|
|
88
|
-
.map((line) => {
|
|
89
|
-
let parsed;
|
|
90
|
-
try {
|
|
91
|
-
parsed = JSON.parse(line);
|
|
92
|
-
}
|
|
93
|
-
catch {
|
|
94
|
-
throw new Error(`gh gave a comment of #${number} that is not valid JSON: ${line}`);
|
|
95
|
-
}
|
|
96
|
-
const [author, body, url] = parsed;
|
|
97
|
-
return url === undefined ? { author, body: body ?? '' } : { author, body: body ?? '', url };
|
|
98
|
-
});
|
|
45
|
+
const raw = await ghApi(repo, `issues/${number}/comments`, '.[] | [.user.login, .body, .html_url] | @json', { paginate: true });
|
|
46
|
+
return lines(raw).map((line) => parseComment(line, number));
|
|
47
|
+
}
|
|
48
|
+
function parseComment(line, number) {
|
|
49
|
+
const [author, body, url] = parseJson(line, () => `gh gave a comment of #${number} that is not valid JSON: ${line}`);
|
|
50
|
+
return url === undefined ? { author, body: body ?? '' } : { author, body: body ?? '', url };
|
|
99
51
|
}
|
|
100
52
|
export async function canWrite(repo, login) {
|
|
101
53
|
try {
|
|
102
|
-
const permission = await
|
|
103
|
-
return ['admin', 'write'].includes(permission
|
|
54
|
+
const permission = await ghApi(repo, `collaborators/${login}/permission`, '.permission');
|
|
55
|
+
return ['admin', 'write'].includes(permission);
|
|
104
56
|
}
|
|
105
57
|
catch (error) {
|
|
106
|
-
if (error
|
|
58
|
+
if (errorMatches(error, /HTTP 404/)) {
|
|
107
59
|
return false;
|
|
108
60
|
}
|
|
109
61
|
throw error;
|
|
110
62
|
}
|
|
111
63
|
}
|
|
112
64
|
export async function openBlockers(repo, issue) {
|
|
113
|
-
const
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
`repos/${slug(repo)}/issues/${issue}/dependencies/blocked_by`,
|
|
117
|
-
'--jq',
|
|
118
|
-
'.[] | select(.state == "open") | (.repository_url | split("/") | .[-2:] | join("/")) + "#" + (.number | tostring)',
|
|
119
|
-
], { cwd: repo.root });
|
|
120
|
-
return raw
|
|
121
|
-
.split('\n')
|
|
122
|
-
.filter((line) => line.length > 0)
|
|
123
|
-
.map((ref) => (ref.startsWith(`${slug(repo)}#`) ? ref.slice(slug(repo).length) : ref));
|
|
124
|
-
}
|
|
125
|
-
async function labelsOf(repo, kind, number) {
|
|
126
|
-
const raw = await gh(repo, [kind, 'view', String(number), '--json', 'labels']);
|
|
127
|
-
const parsed = JSON.parse(raw);
|
|
128
|
-
return parsed.labels.map((label) => label.name);
|
|
129
|
-
}
|
|
130
|
-
export function labelArgs(add, remove, current) {
|
|
131
|
-
const args = ['--add-label', add];
|
|
132
|
-
for (const name of remove.filter((item) => item !== add && current.includes(item))) {
|
|
133
|
-
args.push('--remove-label', name);
|
|
134
|
-
}
|
|
135
|
-
return args;
|
|
136
|
-
}
|
|
137
|
-
export async function setLabel(repo, kind, number, add, remove) {
|
|
138
|
-
await createUnknownLabel(repo, add);
|
|
139
|
-
const current = await labelsOf(repo, kind, number);
|
|
140
|
-
const args = [kind, 'edit', String(number), ...labelArgs(add, remove, current)];
|
|
141
|
-
try {
|
|
142
|
-
await gh(repo, args);
|
|
143
|
-
}
|
|
144
|
-
catch (error) {
|
|
145
|
-
KNOWN_LABELS.delete(slug(repo));
|
|
146
|
-
const created = await createUnknownLabel(repo, add);
|
|
147
|
-
if (created === 'known') {
|
|
148
|
-
throw error;
|
|
149
|
-
}
|
|
150
|
-
if (created instanceof Error) {
|
|
151
|
-
throw created;
|
|
152
|
-
}
|
|
153
|
-
await gh(repo, args);
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
async function createUnknownLabel(repo, label) {
|
|
157
|
-
let known = KNOWN_LABELS.get(slug(repo));
|
|
158
|
-
if (known === undefined) {
|
|
159
|
-
const raw = await gh(repo, ['label', 'list', '--limit', '1000', '--json', 'name']);
|
|
160
|
-
known = new Set(JSON.parse(raw).map((item) => item.name));
|
|
161
|
-
KNOWN_LABELS.set(slug(repo), known);
|
|
162
|
-
}
|
|
163
|
-
if (known.has(label)) {
|
|
164
|
-
return 'known';
|
|
165
|
-
}
|
|
166
|
-
try {
|
|
167
|
-
await ghOnce(repo, ['label', 'create', label]);
|
|
168
|
-
}
|
|
169
|
-
catch (error) {
|
|
170
|
-
return error instanceof Error ? error : new Error(String(error));
|
|
171
|
-
}
|
|
172
|
-
known.add(label);
|
|
173
|
-
return 'created';
|
|
174
|
-
}
|
|
175
|
-
export async function findPr(repo, branch) {
|
|
176
|
-
const raw = await gh(repo, [
|
|
177
|
-
'pr',
|
|
178
|
-
'list',
|
|
179
|
-
'--head',
|
|
180
|
-
branch,
|
|
181
|
-
'--state',
|
|
182
|
-
'open',
|
|
183
|
-
'--json',
|
|
184
|
-
'number',
|
|
185
|
-
]);
|
|
186
|
-
const parsed = JSON.parse(raw);
|
|
187
|
-
return parsed[0]?.number;
|
|
188
|
-
}
|
|
189
|
-
export async function createPr(repo, branch, base, title, body, draft) {
|
|
190
|
-
const created = await ghOnce(repo, [
|
|
191
|
-
'pr',
|
|
192
|
-
'create',
|
|
193
|
-
'--head',
|
|
194
|
-
branch,
|
|
195
|
-
'--base',
|
|
196
|
-
base,
|
|
197
|
-
'--title',
|
|
198
|
-
title,
|
|
199
|
-
'--body',
|
|
200
|
-
body,
|
|
201
|
-
...(draft ? ['--draft'] : []),
|
|
202
|
-
]);
|
|
203
|
-
const number = prNumber(created) ?? (await findPr(repo, branch));
|
|
204
|
-
if (number === undefined) {
|
|
205
|
-
throw new Error(`The pull request for ${branch} was not found after creation.`);
|
|
206
|
-
}
|
|
207
|
-
return number;
|
|
208
|
-
}
|
|
209
|
-
export function prNumber(output) {
|
|
210
|
-
const match = /\/pull\/(\d+)\b/.exec(output);
|
|
211
|
-
return match === null ? undefined : Number(match[1]);
|
|
212
|
-
}
|
|
213
|
-
async function prHead(repo, pr) {
|
|
214
|
-
return gh(repo, ['pr', 'view', String(pr), '--json', 'headRefOid', '--jq', '.headRefOid']);
|
|
215
|
-
}
|
|
216
|
-
async function readChecks(repo, pr) {
|
|
217
|
-
const result = await run('gh', ['pr', 'checks', String(pr), '--json', 'name,bucket,link,description', '--repo', slug(repo)], { cwd: repo.root });
|
|
218
|
-
const output = result.stdout.trim();
|
|
219
|
-
if (output.length === 0) {
|
|
220
|
-
if (NO_CHECKS.test(result.stderr)) {
|
|
221
|
-
return 'none';
|
|
222
|
-
}
|
|
223
|
-
throw new Error(`gh pr checks ${pr} failed with code ${result.code}: ${result.stderr.trim()}`);
|
|
224
|
-
}
|
|
225
|
-
const checks = JSON.parse(output);
|
|
226
|
-
return checks.length === 0 ? 'none' : checks;
|
|
227
|
-
}
|
|
228
|
-
async function watchOnce(repo, pr, intervalSeconds, timeoutMs, show) {
|
|
229
|
-
const watch = await run('gh', [
|
|
230
|
-
'pr',
|
|
231
|
-
'checks',
|
|
232
|
-
String(pr),
|
|
233
|
-
'--watch',
|
|
234
|
-
'--fail-fast',
|
|
235
|
-
'--interval',
|
|
236
|
-
String(intervalSeconds),
|
|
237
|
-
'--repo',
|
|
238
|
-
slug(repo),
|
|
239
|
-
], { cwd: repo.root, show, timeoutMs });
|
|
240
|
-
if (watch.timedOut) {
|
|
241
|
-
return 'timeout';
|
|
242
|
-
}
|
|
243
|
-
if (watch.code === 0) {
|
|
244
|
-
return 'pass';
|
|
245
|
-
}
|
|
246
|
-
if (watch.code === CHECK_PENDING_CODE) {
|
|
247
|
-
return 'pending';
|
|
248
|
-
}
|
|
249
|
-
const checks = await readChecks(repo, pr);
|
|
250
|
-
if (checks !== 'none' && checks.some((check) => check.bucket === 'fail')) {
|
|
251
|
-
return 'fail';
|
|
252
|
-
}
|
|
253
|
-
throw new Error(`gh pr checks ${pr} --watch failed with code ${watch.code} and no check failed: ${watch.stderr.trim()}`);
|
|
254
|
-
}
|
|
255
|
-
const REAL_CLOCK = {
|
|
256
|
-
now: () => Date.now(),
|
|
257
|
-
sleep: (ms) => new Promise((resolve) => setTimeout(resolve, ms)),
|
|
258
|
-
};
|
|
259
|
-
export async function pollChecks(probe, options, clock = REAL_CLOCK) {
|
|
260
|
-
const deadline = clock.now() + options.timeoutMs;
|
|
261
|
-
const rest = () => Math.max(0, deadline - clock.now());
|
|
262
|
-
let shown;
|
|
263
|
-
let failures = 0;
|
|
264
|
-
for (;;) {
|
|
265
|
-
if (rest() === 0) {
|
|
266
|
-
return 'timeout';
|
|
267
|
-
}
|
|
268
|
-
const state = await read(probe, options.head, rest(), () => (shown ??= clock.now()));
|
|
269
|
-
if (state === 'behind' && shown !== undefined) {
|
|
270
|
-
return 'moved';
|
|
271
|
-
}
|
|
272
|
-
if (state instanceof Error) {
|
|
273
|
-
failures += 1;
|
|
274
|
-
if (failures >= READ_TRIES) {
|
|
275
|
-
throw state;
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
else {
|
|
279
|
-
failures = 0;
|
|
280
|
-
if (state === 'none' && clock.now() - (shown ?? 0) >= options.graceMs) {
|
|
281
|
-
return 'none';
|
|
282
|
-
}
|
|
283
|
-
if (state !== 'none' && state !== 'behind' && state !== 'pending') {
|
|
284
|
-
return state;
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
await clock.sleep(Math.min(options.intervalSeconds * 1000, rest()));
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
async function read(probe, head, timeoutMs, shown) {
|
|
291
|
-
try {
|
|
292
|
-
if (head !== undefined) {
|
|
293
|
-
const current = await probe.head();
|
|
294
|
-
if (current !== head) {
|
|
295
|
-
return (await probe.lags(current)) ? 'behind' : 'moved';
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
shown();
|
|
299
|
-
if ((await probe.list()) === 'none') {
|
|
300
|
-
return 'none';
|
|
301
|
-
}
|
|
302
|
-
const state = await probe.watch(timeoutMs);
|
|
303
|
-
if (head !== undefined && (await probe.head()) !== head) {
|
|
304
|
-
return 'moved';
|
|
305
|
-
}
|
|
306
|
-
return state;
|
|
307
|
-
}
|
|
308
|
-
catch (error) {
|
|
309
|
-
return error instanceof Error ? error : new Error(String(error));
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
export function waitForChecks(repo, pr, options) {
|
|
313
|
-
return pollChecks({
|
|
314
|
-
head: () => prHead(repo, pr),
|
|
315
|
-
lags: (head) => options.head === undefined
|
|
316
|
-
? Promise.resolve(true)
|
|
317
|
-
: isAncestor(repo.root, head, options.head),
|
|
318
|
-
list: async () => ((await readChecks(repo, pr)) === 'none' ? 'none' : 'some'),
|
|
319
|
-
watch: (timeoutMs) => watchOnce(repo, pr, options.intervalSeconds, timeoutMs, options.show),
|
|
320
|
-
}, options);
|
|
321
|
-
}
|
|
322
|
-
export async function failedCheckLogs(repo, pr, maxChars) {
|
|
323
|
-
const checks = await readChecks(repo, pr);
|
|
324
|
-
if (checks === 'none') {
|
|
325
|
-
return 'The checks failed, but gh reports no check for the pull request.';
|
|
326
|
-
}
|
|
327
|
-
const failed = checks
|
|
328
|
-
.filter((check) => check.bucket === 'fail')
|
|
329
|
-
.map((check) => ({
|
|
330
|
-
heading: `## ${check.name}\n${check.description}`,
|
|
331
|
-
runId: /\/actions\/runs\/(\d+)/.exec(check.link)?.[1],
|
|
332
|
-
}));
|
|
333
|
-
return runLogs(repo, failed, maxChars);
|
|
334
|
-
}
|
|
335
|
-
async function baseRuns(repo, sha) {
|
|
336
|
-
const raw = await gh(repo, [
|
|
337
|
-
'run',
|
|
338
|
-
'list',
|
|
339
|
-
'--commit',
|
|
340
|
-
sha,
|
|
341
|
-
'--json',
|
|
342
|
-
'databaseId,name,status,conclusion',
|
|
343
|
-
]);
|
|
344
|
-
return JSON.parse(raw);
|
|
345
|
-
}
|
|
346
|
-
function baseState(runs) {
|
|
347
|
-
if (runs.some((entry) => entry.status === 'completed' && FAILED_CONCLUSIONS.has(entry.conclusion))) {
|
|
348
|
-
return 'fail';
|
|
349
|
-
}
|
|
350
|
-
return runs.every((entry) => entry.status === 'completed') ? 'pass' : 'pending';
|
|
351
|
-
}
|
|
352
|
-
export function waitForBaseChecks(repo, sha, options) {
|
|
353
|
-
return pollChecks({
|
|
354
|
-
head: async () => sha,
|
|
355
|
-
lags: async () => true,
|
|
356
|
-
list: async () => ((await baseRuns(repo, sha)).length === 0 ? 'none' : 'some'),
|
|
357
|
-
watch: async () => baseState(await baseRuns(repo, sha)),
|
|
358
|
-
}, options);
|
|
359
|
-
}
|
|
360
|
-
export async function failedBaseLogs(repo, sha, maxChars) {
|
|
361
|
-
const failed = (await baseRuns(repo, sha))
|
|
362
|
-
.filter((entry) => entry.status === 'completed' && FAILED_CONCLUSIONS.has(entry.conclusion))
|
|
363
|
-
.map((entry) => ({ heading: `## ${entry.name}`, runId: String(entry.databaseId) }));
|
|
364
|
-
return runLogs(repo, failed, maxChars);
|
|
365
|
-
}
|
|
366
|
-
async function runLogs(repo, failed, maxChars) {
|
|
367
|
-
const share = Math.max(1, Math.floor(maxChars / Math.max(1, failed.length)));
|
|
368
|
-
const parts = [];
|
|
369
|
-
for (const { heading, runId } of failed) {
|
|
370
|
-
parts.push(heading);
|
|
371
|
-
if (runId === undefined) {
|
|
372
|
-
continue;
|
|
373
|
-
}
|
|
374
|
-
const logs = await run('gh', ['run', 'view', runId, '--log-failed', '--repo', slug(repo)], {
|
|
375
|
-
cwd: repo.root,
|
|
376
|
-
tailChars: share,
|
|
377
|
-
});
|
|
378
|
-
if (logs.code === 0) {
|
|
379
|
-
parts.push(logs.stdout);
|
|
380
|
-
continue;
|
|
381
|
-
}
|
|
382
|
-
const reason = [`The log read failed with code ${logs.code}.`, logs.stderr.trim()]
|
|
383
|
-
.filter((line) => line !== '')
|
|
384
|
-
.join(' ');
|
|
385
|
-
parts.push([logs.stdout.trim(), reason].filter((line) => line !== '').join('\n'));
|
|
386
|
-
}
|
|
387
|
-
return parts.join('\n\n');
|
|
388
|
-
}
|
|
389
|
-
export async function markPrReady(repo, pr) {
|
|
390
|
-
const result = await run('gh', ['pr', 'ready', String(pr), '--repo', slug(repo)], {
|
|
391
|
-
cwd: repo.root,
|
|
392
|
-
});
|
|
393
|
-
return result.code === 0;
|
|
394
|
-
}
|
|
395
|
-
export async function mergePr(repo, pr) {
|
|
396
|
-
const result = await run('gh', ['pr', 'merge', String(pr), '--squash', '--delete-branch', '--repo', slug(repo)], { cwd: repo.root });
|
|
397
|
-
return result.code === 0;
|
|
398
|
-
}
|
|
399
|
-
export async function prConflicts(repo, pr, options, clock = REAL_CLOCK) {
|
|
400
|
-
const deadline = clock.now() + options.timeoutMs;
|
|
401
|
-
for (;;) {
|
|
402
|
-
const mergeable = (await gh(repo, ['pr', 'view', String(pr), '--json', 'mergeable', '--jq', '.mergeable'])).trim();
|
|
403
|
-
const rest = deadline - clock.now();
|
|
404
|
-
if (mergeable !== 'UNKNOWN' || rest <= 0) {
|
|
405
|
-
return mergeable === 'CONFLICTING';
|
|
406
|
-
}
|
|
407
|
-
await clock.sleep(Math.min(options.intervalSeconds * 1000, rest));
|
|
408
|
-
}
|
|
409
|
-
}
|
|
410
|
-
export async function prMerged(repo, pr) {
|
|
411
|
-
return ((await gh(repo, ['pr', 'view', String(pr), '--json', 'state', '--jq', '.state'])).trim() ===
|
|
412
|
-
'MERGED');
|
|
413
|
-
}
|
|
414
|
-
export async function mergeCommit(repo, pr, options, clock = REAL_CLOCK) {
|
|
415
|
-
const deadline = clock.now() + options.timeoutMs;
|
|
416
|
-
for (;;) {
|
|
417
|
-
const raw = await gh(repo, ['pr', 'view', String(pr), '--json', 'state,mergeCommit']);
|
|
418
|
-
const view = JSON.parse(raw);
|
|
419
|
-
if (view.state === 'MERGED' && view.mergeCommit?.oid) {
|
|
420
|
-
return view.mergeCommit.oid;
|
|
421
|
-
}
|
|
422
|
-
const rest = deadline - clock.now();
|
|
423
|
-
if (view.state === 'CLOSED' || rest <= 0) {
|
|
424
|
-
return undefined;
|
|
425
|
-
}
|
|
426
|
-
await clock.sleep(Math.min(options.intervalSeconds * 1000, rest));
|
|
427
|
-
}
|
|
65
|
+
const repoSlug = slug(repo);
|
|
66
|
+
const raw = await ghApi(repo, `issues/${issue}/dependencies/blocked_by`, '.[] | select(.state == "open") | (.repository_url | split("/") | .[-2:] | join("/")) + "#" + (.number | tostring)', { paginate: true });
|
|
67
|
+
return lines(raw).map((ref) => ref.startsWith(`${repoSlug}#`) ? ref.slice(repoSlug.length) : ref);
|
|
428
68
|
}
|
|
429
69
|
export async function reopenIssue(repo, issue) {
|
|
430
70
|
await gh(repo, ['issue', 'reopen', String(issue)]);
|
|
431
71
|
}
|
|
72
|
+
export async function closeIssue(repo, issue) {
|
|
73
|
+
await gh(repo, ['issue', 'close', String(issue), '--reason', 'completed']);
|
|
74
|
+
}
|
|
75
|
+
async function comment(repo, kind, number, body) {
|
|
76
|
+
await ghOnce(repo, [kind, 'comment', String(number), '--body-file', '-'], body);
|
|
77
|
+
}
|
|
432
78
|
export async function commentOnIssue(repo, issue, body) {
|
|
433
|
-
await
|
|
79
|
+
await comment(repo, 'issue', issue, body);
|
|
434
80
|
}
|
|
435
81
|
export async function commentOnPr(repo, pr, body) {
|
|
436
|
-
await
|
|
82
|
+
await comment(repo, 'pr', pr, body);
|
|
437
83
|
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { appendFile } from 'node:fs/promises';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { readTextOrEmpty } from './files.mjs';
|
|
4
|
+
export async function ensureIgnored(root, entry) {
|
|
5
|
+
const path = join(root, '.gitignore');
|
|
6
|
+
const current = await readTextOrEmpty(path);
|
|
7
|
+
if (current.split('\n').some((line) => line.trim() === entry)) {
|
|
8
|
+
return false;
|
|
9
|
+
}
|
|
10
|
+
const gap = current.length === 0 || current.endsWith('\n') ? '' : '\n';
|
|
11
|
+
await appendFile(path, `${gap}${entry}\n`);
|
|
12
|
+
return true;
|
|
13
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { slug } from './gh.mjs';
|
|
2
|
+
import { readState } from './state.mjs';
|
|
3
|
+
export const HAND_OVER = 'next-issue needs help:';
|
|
4
|
+
const HARNESS_HEADINGS = [HAND_OVER, '### Review:', '### Fixer:'];
|
|
5
|
+
function fromHarness(comment) {
|
|
6
|
+
return HARNESS_HEADINGS.some((heading) => comment.body.startsWith(heading));
|
|
7
|
+
}
|
|
8
|
+
function writers(repo, ports) {
|
|
9
|
+
const access = new Map();
|
|
10
|
+
return (author) => {
|
|
11
|
+
if (!access.has(author)) {
|
|
12
|
+
access.set(author, ports.canWrite(repo, author));
|
|
13
|
+
}
|
|
14
|
+
return access.get(author);
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
function quote(comment) {
|
|
18
|
+
return `@${comment.author}: ${comment.body}`;
|
|
19
|
+
}
|
|
20
|
+
async function writerQuotes(comments, writer, skip = () => { }) {
|
|
21
|
+
const kept = [];
|
|
22
|
+
for (const comment of comments) {
|
|
23
|
+
if (await writer(comment.author)) {
|
|
24
|
+
kept.push(quote(comment));
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
skip(comment);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return kept;
|
|
31
|
+
}
|
|
32
|
+
async function lastHandOver(comments, writer) {
|
|
33
|
+
for (let index = comments.length - 1; index >= 0; index -= 1) {
|
|
34
|
+
const comment = comments[index];
|
|
35
|
+
if (comment.body.startsWith(HAND_OVER) && (await writer(comment.author))) {
|
|
36
|
+
return index;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return -1;
|
|
40
|
+
}
|
|
41
|
+
async function handOverThread(repo, ports, writer, thread) {
|
|
42
|
+
const comments = await ports.threadComments(repo, thread);
|
|
43
|
+
return { comments, last: await lastHandOver(comments, writer) };
|
|
44
|
+
}
|
|
45
|
+
export async function waitingQuestions(repo, config, ports) {
|
|
46
|
+
const writer = writers(repo, ports);
|
|
47
|
+
const issues = await ports.openIssues(repo, config.issueLimit, [config.labels.needsHuman]);
|
|
48
|
+
const questions = [];
|
|
49
|
+
for (const issue of issues) {
|
|
50
|
+
questions.push(await waitingQuestion(repo, config, ports, writer, issue));
|
|
51
|
+
}
|
|
52
|
+
return questions;
|
|
53
|
+
}
|
|
54
|
+
async function waitingQuestion(repo, config, ports, writer, issue) {
|
|
55
|
+
const pr = (await readState(repo, issue.number))?.pr;
|
|
56
|
+
const { comments, last } = await handOverThread(repo, ports, writer, pr ?? issue.number);
|
|
57
|
+
const comment = comments[last];
|
|
58
|
+
return {
|
|
59
|
+
issue: issue.number,
|
|
60
|
+
title: issue.title,
|
|
61
|
+
reason: comment?.body.slice(HAND_OVER.length).trim() ?? '',
|
|
62
|
+
url: comment?.url ?? '',
|
|
63
|
+
repo: slug(repo),
|
|
64
|
+
...(pr === undefined ? {} : { pr }),
|
|
65
|
+
labels: { needsHuman: config.labels.needsHuman, skip: config.labels.skip },
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
export async function writerComments(context, log, issue) {
|
|
69
|
+
const { repo, ports } = context;
|
|
70
|
+
const comments = (await ports.issueComments(repo, issue)).filter((comment) => !fromHarness(comment));
|
|
71
|
+
return writerQuotes(comments, writers(repo, ports), (comment) => log.event('comment.skip', { author: comment.author }, 'quiet'));
|
|
72
|
+
}
|
|
73
|
+
export async function handOverReply(context, issue, pr) {
|
|
74
|
+
const { repo, ports } = context;
|
|
75
|
+
const writer = writers(repo, ports);
|
|
76
|
+
const { comments, last } = await handOverThread(repo, ports, writer, pr ?? issue);
|
|
77
|
+
if (last === -1) {
|
|
78
|
+
return [];
|
|
79
|
+
}
|
|
80
|
+
return writerQuotes(comments.slice(last + 1), writer);
|
|
81
|
+
}
|