@botiverse/stamp-cli 0.3.14 → 0.4.1
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/bin/launch.js +124 -0
- package/bin/stamp-staging.js +3 -0
- package/bin/stamp.js +3 -0
- package/package.json +16 -13
- package/README.md +0 -225
- package/bin/stamp-staging.mjs +0 -15
- package/bin/stamp.mjs +0 -65
- package/lib/agent-login.mjs +0 -208
- package/lib/agent-refresh.mjs +0 -192
- package/lib/auth.mjs +0 -202
- package/lib/client.mjs +0 -92
- package/lib/commands/changelog.mjs +0 -58
- package/lib/commands/identity.mjs +0 -12
- package/lib/commands/key.mjs +0 -104
- package/lib/commands/knownhosts.mjs +0 -150
- package/lib/commands/pr.mjs +0 -298
- package/lib/commands/queue.mjs +0 -67
- package/lib/commands/receipt.mjs +0 -33
- package/lib/commands/repo.mjs +0 -130
- package/lib/commands/search.mjs +0 -63
- package/lib/commands/session.mjs +0 -71
- package/lib/commands/subscribe.mjs +0 -35
- package/lib/errors.mjs +0 -47
- package/lib/expiry.mjs +0 -27
- package/lib/flags.mjs +0 -26
- package/lib/locale.mjs +0 -385
- package/lib/main.mjs +0 -124
- package/lib/pr/close.mjs +0 -56
- package/lib/pr/comment.mjs +0 -45
- package/lib/pr/create.mjs +0 -165
- package/lib/pr/edit.mjs +0 -54
- package/lib/pr/queue.mjs +0 -32
- package/lib/pr/read.mjs +0 -473
- package/lib/pr/review.mjs +0 -88
- package/lib/pr/verdict.mjs +0 -66
- package/lib/pr/write.mjs +0 -180
- package/lib/queue/setup.mjs +0 -143
- package/lib/receipt/list.mjs +0 -54
- package/lib/repo/clone.mjs +0 -101
- package/lib/repo/identities.mjs +0 -45
- package/lib/repo/ssh.mjs +0 -326
- package/lib/update-check.mjs +0 -50
package/lib/pr/read.mjs
DELETED
|
@@ -1,473 +0,0 @@
|
|
|
1
|
-
import { AgentError } from "../errors.mjs";
|
|
2
|
-
import { validateVerdict } from "./verdict.mjs";
|
|
3
|
-
|
|
4
|
-
const consolidatedStatuses = new Set(["OPEN", "DRAFT", "MERGING", "MERGED", "CLOSED"]);
|
|
5
|
-
const ciStatuses = new Set(["passing", "failing", "pending", "none"]);
|
|
6
|
-
const reviewStates = new Set([
|
|
7
|
-
"HAS_ACTIVE_REVIEW",
|
|
8
|
-
"HAS_UNADDRESSED_CHANGES_REQUESTED",
|
|
9
|
-
"IS_FULLY_APPROVED",
|
|
10
|
-
"ONE_OR_MORE_APPROVALS",
|
|
11
|
-
]);
|
|
12
|
-
const terminalPassConclusions = new Set(["success", "neutral", "skipped"]);
|
|
13
|
-
const pendingCheckStates = new Set(["", "expected", "in_progress", "pending", "queued", "requested", "waiting"]);
|
|
14
|
-
|
|
15
|
-
export function takeNamedFlag(args, long, short, usageCode = "usagePrRead") {
|
|
16
|
-
const matches = [];
|
|
17
|
-
for (let i = 0; i < args.length; i++) {
|
|
18
|
-
if (args[i] === `--${long}` || (short && args[i] === `-${short}`)) {
|
|
19
|
-
matches.push({ index: i, inline: false });
|
|
20
|
-
} else if (args[i].startsWith(`--${long}=`)) {
|
|
21
|
-
matches.push({ index: i, inline: true });
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
if (matches.length === 0) return undefined;
|
|
25
|
-
if (matches.length > 1) throw new AgentError(usageCode);
|
|
26
|
-
const { index, inline } = matches[0];
|
|
27
|
-
if (inline) {
|
|
28
|
-
const value = args[index].slice(long.length + 3);
|
|
29
|
-
if (value === "") throw new AgentError(usageCode);
|
|
30
|
-
args.splice(index, 1);
|
|
31
|
-
return value;
|
|
32
|
-
}
|
|
33
|
-
const value = args[index + 1];
|
|
34
|
-
if (value === undefined || value.startsWith("-")) throw new AgentError(usageCode);
|
|
35
|
-
args.splice(index, 2);
|
|
36
|
-
return value;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export function takeBooleanFlag(args, long) {
|
|
40
|
-
const i = args.indexOf(`--${long}`);
|
|
41
|
-
if (i === -1) return false;
|
|
42
|
-
args.splice(i, 1);
|
|
43
|
-
return true;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
function strictPositiveInt(value, fallback, maximum = Number.MAX_SAFE_INTEGER) {
|
|
47
|
-
if (value === undefined) return fallback;
|
|
48
|
-
if (!/^[1-9][0-9]*$/.test(value)) return null;
|
|
49
|
-
const parsed = Number(value);
|
|
50
|
-
return Number.isSafeInteger(parsed) && parsed <= maximum ? parsed : null;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
function repositoryParts(value) {
|
|
54
|
-
const match = String(value ?? "").trim().match(/^([A-Za-z0-9_.-]+)\/([A-Za-z0-9_.-]+)$/);
|
|
55
|
-
if (!match || match[1] === "." || match[1] === ".." || match[2] === "." || match[2] === "..") return null;
|
|
56
|
-
return { owner: match[1], repo: match[2] };
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
function sameRepository(left, right) {
|
|
60
|
-
return String(left).toLowerCase() === String(right).toLowerCase();
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
function targetFromURL(value) {
|
|
64
|
-
let parsed;
|
|
65
|
-
try { parsed = new URL(value); } catch { return null; }
|
|
66
|
-
if (parsed.protocol !== "https:" || parsed.hostname.toLowerCase() !== "github.com" || parsed.port || parsed.username || parsed.password) {
|
|
67
|
-
return null;
|
|
68
|
-
}
|
|
69
|
-
const parts = parsed.pathname.split("/").filter(Boolean);
|
|
70
|
-
const marker = parts[2];
|
|
71
|
-
if (parts.length < 4 || (marker !== "pull" && marker !== "pulls")) return null;
|
|
72
|
-
const repository = repositoryParts(`${parts[0]}/${parts[1]}`);
|
|
73
|
-
const number = strictPositiveInt(parts[3]);
|
|
74
|
-
return repository && number ? { ...repository, number } : null;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* Parse `stamp pr list` args (task #397 slice 1 + #345 CLI lane).
|
|
79
|
-
*
|
|
80
|
-
* Optional, opt-in pagination — the wire adds `?limit=` and an OPAQUE cursor to
|
|
81
|
-
* the SAME `/v1/inbox/pull-requests` path (never a new path):
|
|
82
|
-
* --limit <n> 1..200 page size
|
|
83
|
-
* --cursor <s> an opaque server token, passed through VERBATIM (never parsed,
|
|
84
|
-
* never cached, never reused across identities: it is bound to
|
|
85
|
-
* (tenant, server, viewer scope, filter) server-side)
|
|
86
|
-
*
|
|
87
|
-
* With NEITHER flag the path is byte-identical to before (full set, truncated
|
|
88
|
-
* false, nextCursor null) — the server guarantees that back-compat.
|
|
89
|
-
*/
|
|
90
|
-
export const PR_LIST_MAX_LIMIT = 200;
|
|
91
|
-
|
|
92
|
-
export function parsePRListArgs(input) {
|
|
93
|
-
const args = [...input];
|
|
94
|
-
const limitRaw = takeNamedFlag(args, "limit", undefined, "usagePrList");
|
|
95
|
-
const cursor = takeNamedFlag(args, "cursor", undefined, "usagePrList");
|
|
96
|
-
const sections = takeBooleanFlag(args, "sections");
|
|
97
|
-
if (args.length !== 0) throw new AgentError("usagePrList");
|
|
98
|
-
|
|
99
|
-
const params = new URLSearchParams();
|
|
100
|
-
if (limitRaw !== undefined) {
|
|
101
|
-
const limit = strictPositiveInt(limitRaw, null, PR_LIST_MAX_LIMIT);
|
|
102
|
-
if (limit === null) throw new AgentError("usagePrList");
|
|
103
|
-
params.set("limit", String(limit));
|
|
104
|
-
}
|
|
105
|
-
if (cursor !== undefined) {
|
|
106
|
-
// A cursor is only meaningful with a page size; the server 400s a cursor
|
|
107
|
-
// without a limit, so refuse it locally with the usage error instead.
|
|
108
|
-
if (!params.has("limit")) throw new AgentError("usagePrList");
|
|
109
|
-
params.set("cursor", cursor);
|
|
110
|
-
}
|
|
111
|
-
const query = params.toString();
|
|
112
|
-
// task #413: the SSOT is /v1/inbox/pull-requests; the server keeps
|
|
113
|
-
// /v1/pr-inbox as a Deprecation alias for one version cycle. The CLI targets
|
|
114
|
-
// the SSOT (one build point).
|
|
115
|
-
const base = "/v1/inbox/pull-requests";
|
|
116
|
-
const path = query === "" ? base : `${base}?${query}`;
|
|
117
|
-
return { path, sections };
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
export function parsePRTargetArgs(input, usageCode = "usagePrRead") {
|
|
121
|
-
const args = [...input];
|
|
122
|
-
const repoFlag = takeNamedFlag(args, "repo", "R", usageCode);
|
|
123
|
-
const target = args.shift();
|
|
124
|
-
if (!target || args.length !== 0) throw new AgentError(usageCode);
|
|
125
|
-
const fromURL = targetFromURL(target);
|
|
126
|
-
if (fromURL) {
|
|
127
|
-
if (repoFlag && (!repositoryParts(repoFlag) || !sameRepository(`${fromURL.owner}/${fromURL.repo}`, repoFlag))) {
|
|
128
|
-
throw new AgentError(usageCode);
|
|
129
|
-
}
|
|
130
|
-
return fromURL;
|
|
131
|
-
}
|
|
132
|
-
const repository = repositoryParts(repoFlag);
|
|
133
|
-
const number = strictPositiveInt(target);
|
|
134
|
-
if (!repository || !number) throw new AgentError(usageCode);
|
|
135
|
-
return { ...repository, number };
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
export function parsePRDiffArgs(input) {
|
|
139
|
-
const args = [...input];
|
|
140
|
-
const nameOnly = takeBooleanFlag(args, "name-only");
|
|
141
|
-
return { ...parsePRTargetArgs(args, "usagePrDiff"), nameOnly };
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
export function prDetailPath(target) {
|
|
145
|
-
return `/v1/repositories/${encodeURIComponent(target.owner)}/${encodeURIComponent(target.repo)}/pulls/${target.number}`;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
export function prDiffPath(target) {
|
|
149
|
-
return `${prDetailPath(target)}/diff`;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
function requireObject(value) {
|
|
153
|
-
if (!value || typeof value !== "object" || Array.isArray(value)) throw new AgentError("prReadInvalidResponse");
|
|
154
|
-
return value;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
function requireString(value) {
|
|
158
|
-
if (typeof value !== "string") throw new AgentError("prReadInvalidResponse");
|
|
159
|
-
return value;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
function requireNumber(value) {
|
|
163
|
-
if (!Number.isInteger(value) || value <= 0) throw new AgentError("prReadInvalidResponse");
|
|
164
|
-
return value;
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
function requireSHA(value) {
|
|
168
|
-
const sha = requireString(value);
|
|
169
|
-
if (!/^[0-9a-f]{40}$/.test(sha)) throw new AgentError("prReadInvalidResponse");
|
|
170
|
-
return sha;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
export function safeInline(value) {
|
|
174
|
-
return String(value).replace(/[\u0000-\u001f\u007f-\u009f\u202a-\u202e\u2066-\u2069]+/g, " ").replace(/\s+/g, " ").trim();
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
function safeMultiline(value) {
|
|
178
|
-
return String(value)
|
|
179
|
-
.replace(/\r\n?/g, "\n")
|
|
180
|
-
.replace(/[\u0000-\u0008\u000b\u000c\u000e-\u001f\u007f-\u009f\u202a-\u202e\u2066-\u2069]/g, "");
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
export function validatePRListResponse(value, paged = false) {
|
|
184
|
-
const response = requireObject(value);
|
|
185
|
-
if (!Array.isArray(response.items)) throw new AgentError("prReadInvalidResponse");
|
|
186
|
-
for (const raw of response.items) {
|
|
187
|
-
const item = requireObject(raw);
|
|
188
|
-
if (!repositoryParts(requireString(item.repository))) throw new AgentError("prReadInvalidResponse");
|
|
189
|
-
requireNumber(item.number);
|
|
190
|
-
requireString(item.title);
|
|
191
|
-
requireString(item.url);
|
|
192
|
-
requireString(item.raftAgentId);
|
|
193
|
-
requireSHA(item.headSha);
|
|
194
|
-
if (!Array.isArray(item.reviewStates) || item.reviewStates.some((state) => !reviewStates.has(state))) {
|
|
195
|
-
throw new AgentError("prReadInvalidResponse");
|
|
196
|
-
}
|
|
197
|
-
const status = requireString(item.status);
|
|
198
|
-
const ciStatus = requireString(item.ciStatus);
|
|
199
|
-
if (status === "UNKNOWN") {
|
|
200
|
-
if (ciStatus !== "none" || item.reviewStates.length !== 0) throw new AgentError("prReadInvalidResponse");
|
|
201
|
-
} else {
|
|
202
|
-
if (!consolidatedStatuses.has(status) || !ciStatuses.has(ciStatus)) throw new AgentError("prReadInvalidResponse");
|
|
203
|
-
}
|
|
204
|
-
requireString(item.updatedAt);
|
|
205
|
-
}
|
|
206
|
-
// Pagination (task #397 slice 1): nextCursor is a string|null, truncated a
|
|
207
|
-
// boolean. Both are surfaced to the caller; a present cursor means "there is
|
|
208
|
-
// more", never an error. When the caller did not request a page (no
|
|
209
|
-
// limit/cursor), the server still returns nextCursor=null / truncated=false
|
|
210
|
-
// — assert that back-compat contract.
|
|
211
|
-
const nextCursor = response.nextCursor;
|
|
212
|
-
if (nextCursor !== null && typeof nextCursor !== "string") throw new AgentError("prReadInvalidResponse");
|
|
213
|
-
if (typeof response.truncated !== "boolean") throw new AgentError("prReadInvalidResponse");
|
|
214
|
-
// `truncated` and `nextCursor` must AGREE: a cursor is exactly the "there is
|
|
215
|
-
// more" signal, so a page is truncated iff it carries a cursor. Accepting
|
|
216
|
-
// (truncated=false, nextCursor="x") would print "continue" while the protocol
|
|
217
|
-
// says the page is complete; (truncated=true, nextCursor=null) would report
|
|
218
|
-
// success on a page the CLI cannot continue.
|
|
219
|
-
if (paged) {
|
|
220
|
-
if (response.truncated !== (nextCursor !== null)) throw new AgentError("prReadInvalidResponse");
|
|
221
|
-
return response;
|
|
222
|
-
}
|
|
223
|
-
// Unpaged (back-compat): strictly the pre-pagination contract.
|
|
224
|
-
if (nextCursor !== null || response.truncated !== false) {
|
|
225
|
-
throw new AgentError("prReadInvalidResponse");
|
|
226
|
-
}
|
|
227
|
-
return response;
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
export function validatePRDetail(value, expected) {
|
|
231
|
-
const detail = requireObject(value);
|
|
232
|
-
requireString(detail.repository);
|
|
233
|
-
requireNumber(detail.number);
|
|
234
|
-
requireString(detail.title);
|
|
235
|
-
requireString(detail.url);
|
|
236
|
-
requireString(detail.author);
|
|
237
|
-
requireSHA(detail.headSha);
|
|
238
|
-
requireSHA(detail.baseSha);
|
|
239
|
-
requireSHA(detail.mergeBaseSha);
|
|
240
|
-
requireString(detail.headRef);
|
|
241
|
-
requireString(detail.baseRef);
|
|
242
|
-
if (!ciStatuses.has(requireString(detail.ciStatus))) throw new AgentError("prReadInvalidResponse");
|
|
243
|
-
// Optional: absent means the CI sources were actually read. Validated rather
|
|
244
|
-
// than ignored so a malformed value is caught here instead of silently
|
|
245
|
-
// deciding an exit code further down.
|
|
246
|
-
if (detail.ciUnreadable !== undefined && typeof detail.ciUnreadable !== "boolean") {
|
|
247
|
-
throw new AgentError("prReadInvalidResponse");
|
|
248
|
-
}
|
|
249
|
-
requireString(detail.reviewStatus);
|
|
250
|
-
requireString(detail.reviewFreshness);
|
|
251
|
-
detail.verdict = validateVerdict(detail.verdict);
|
|
252
|
-
if (typeof detail.stale !== "boolean" || typeof detail.invalidated !== "boolean") throw new AgentError("prReadInvalidResponse");
|
|
253
|
-
requireString(detail.updatedAt);
|
|
254
|
-
requireString(detail.body);
|
|
255
|
-
if (!Array.isArray(detail.checks)) throw new AgentError("prReadInvalidResponse");
|
|
256
|
-
for (const raw of detail.checks) {
|
|
257
|
-
const check = requireObject(raw);
|
|
258
|
-
requireString(check.name);
|
|
259
|
-
requireString(check.status);
|
|
260
|
-
requireString(check.conclusion);
|
|
261
|
-
requireString(check.url);
|
|
262
|
-
}
|
|
263
|
-
for (const links of [detail.dependencies, detail.dependents]) {
|
|
264
|
-
if (!Array.isArray(links)) throw new AgentError("prReadInvalidResponse");
|
|
265
|
-
for (const raw of links) {
|
|
266
|
-
const link = requireObject(raw);
|
|
267
|
-
requireNumber(link.number);
|
|
268
|
-
requireString(link.title);
|
|
269
|
-
requireString(link.url);
|
|
270
|
-
requireString(link.reason);
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
if (expected && (!sameRepository(detail.repository, `${expected.owner}/${expected.repo}`) || detail.number !== expected.number)) {
|
|
274
|
-
throw new AgentError("prReadCoordinateMismatch");
|
|
275
|
-
}
|
|
276
|
-
return detail;
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
export function validatePRDiff(value, expected) {
|
|
280
|
-
const response = requireObject(value);
|
|
281
|
-
requireString(response.repository);
|
|
282
|
-
requireNumber(response.number);
|
|
283
|
-
requireSHA(response.baseSha);
|
|
284
|
-
requireSHA(response.headSha);
|
|
285
|
-
if (expected && (!sameRepository(response.repository, `${expected.owner}/${expected.repo}`) || response.number !== expected.number)) {
|
|
286
|
-
throw new AgentError("prReadCoordinateMismatch");
|
|
287
|
-
}
|
|
288
|
-
if (!Array.isArray(response.files)) throw new AgentError("prReadInvalidResponse");
|
|
289
|
-
for (const raw of response.files) {
|
|
290
|
-
const file = requireObject(raw);
|
|
291
|
-
requireString(file.path);
|
|
292
|
-
requireString(file.status);
|
|
293
|
-
if (!Number.isInteger(file.additions) || file.additions < 0 || !Number.isInteger(file.deletions) || file.deletions < 0) {
|
|
294
|
-
throw new AgentError("prReadInvalidResponse");
|
|
295
|
-
}
|
|
296
|
-
if (file.patch !== null && file.patch !== undefined && typeof file.patch !== "string") {
|
|
297
|
-
throw new AgentError("prReadInvalidResponse");
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
return response;
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
/**
|
|
304
|
-
* Author-oriented Graphite-style section for an agent-view PR row (task #397
|
|
305
|
-
* slice 2), derived purely from the wire fields the server already returns
|
|
306
|
-
* (`status` + `reviewStates`). First-match order matches the server's
|
|
307
|
-
* `DefaultInboxSections` intent:
|
|
308
|
-
*
|
|
309
|
-
* drafts <- status=DRAFT
|
|
310
|
-
* changes <- HAS_UNADDRESSED_CHANGES_REQUESTED (before approved)
|
|
311
|
-
* approved <- IS_FULLY_APPROVED (authoritative reviewDecision)
|
|
312
|
-
* approved(more) <- ONE_OR_MORE_APPROVALS and NOT IS_FULLY_APPROVED
|
|
313
|
-
* waiting <- status=OPEN with none of the above
|
|
314
|
-
* history <- MERGING / MERGED / CLOSED
|
|
315
|
-
* unavailable <- status=UNKNOWN (its own bucket, never folded into waiting)
|
|
316
|
-
*
|
|
317
|
-
* Kept pure and exported so the mapping is unit-testable.
|
|
318
|
-
*/
|
|
319
|
-
export const PR_SECTIONS = Object.freeze({
|
|
320
|
-
drafts: "drafts",
|
|
321
|
-
changes: "changes",
|
|
322
|
-
approved: "approved",
|
|
323
|
-
approvals: "approvals",
|
|
324
|
-
waiting: "waiting",
|
|
325
|
-
history: "history",
|
|
326
|
-
unavailable: "unavailable",
|
|
327
|
-
});
|
|
328
|
-
|
|
329
|
-
export function prSection(item) {
|
|
330
|
-
const status = item.status;
|
|
331
|
-
const states = Array.isArray(item.reviewStates) ? item.reviewStates : [];
|
|
332
|
-
const has = (s) => states.includes(s);
|
|
333
|
-
|
|
334
|
-
if (status === "UNKNOWN") return PR_SECTIONS.unavailable;
|
|
335
|
-
if (status === "DRAFT") return PR_SECTIONS.drafts;
|
|
336
|
-
if (status === "MERGING" || status === "MERGED" || status === "CLOSED") return PR_SECTIONS.history;
|
|
337
|
-
// From here status === "OPEN".
|
|
338
|
-
if (has("HAS_UNADDRESSED_CHANGES_REQUESTED")) return PR_SECTIONS.changes;
|
|
339
|
-
if (has("IS_FULLY_APPROVED")) return PR_SECTIONS.approved;
|
|
340
|
-
if (has("ONE_OR_MORE_APPROVALS")) return PR_SECTIONS.approvals;
|
|
341
|
-
return PR_SECTIONS.waiting;
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
export function formatPRList(response, labels = {}) {
|
|
345
|
-
return response.items.map((item) => [
|
|
346
|
-
safeInline(item.repository),
|
|
347
|
-
`#${item.number}`,
|
|
348
|
-
safeInline(item.status === "UNKNOWN" ? (labels.statusUnavailable ?? item.status) : item.status),
|
|
349
|
-
`${labels.ci ?? "CI"}:${safeInline(item.ciStatus)}`,
|
|
350
|
-
`${labels.agent ?? "Agent"}:${safeInline(item.raftAgentId)}`,
|
|
351
|
-
safeInline(item.title),
|
|
352
|
-
safeInline(item.updatedAt),
|
|
353
|
-
].join("\t"));
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
const SECTION_ORDER = [
|
|
357
|
-
PR_SECTIONS.drafts,
|
|
358
|
-
PR_SECTIONS.changes,
|
|
359
|
-
PR_SECTIONS.approved,
|
|
360
|
-
PR_SECTIONS.approvals,
|
|
361
|
-
PR_SECTIONS.waiting,
|
|
362
|
-
PR_SECTIONS.history,
|
|
363
|
-
PR_SECTIONS.unavailable,
|
|
364
|
-
];
|
|
365
|
-
|
|
366
|
-
/**
|
|
367
|
-
* Group the list rows by derived section (task #397 slice 2), keeping the flat
|
|
368
|
-
* per-row format intact and emitting each section as a labelled block in the
|
|
369
|
-
* fixed first-match order. Empty sections are omitted.
|
|
370
|
-
*/
|
|
371
|
-
export function formatPRListSections(response, labels = {}) {
|
|
372
|
-
const buckets = new Map(SECTION_ORDER.map((s) => [s, []]));
|
|
373
|
-
for (const item of response.items) buckets.get(prSection(item)).push(item);
|
|
374
|
-
const lines = [];
|
|
375
|
-
for (const section of SECTION_ORDER) {
|
|
376
|
-
const items = buckets.get(section);
|
|
377
|
-
if (items.length === 0) continue;
|
|
378
|
-
lines.push(`${labels.section ?? ""}${sectionLabel(section, labels)}`.trim());
|
|
379
|
-
for (const line of formatPRList({ items }, labels)) lines.push(` ${line}`);
|
|
380
|
-
}
|
|
381
|
-
return lines;
|
|
382
|
-
}
|
|
383
|
-
|
|
384
|
-
function sectionLabel(section, labels) {
|
|
385
|
-
const key = `prSection_${section}`;
|
|
386
|
-
return labels[key] ?? section;
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
// formatApprovals names who approved the exact head, from the verdict; with
|
|
390
|
-
// no verdict (an older gateway) it falls back to GitHub's review status.
|
|
391
|
-
export function formatApprovals(verdict, reviewStatus) {
|
|
392
|
-
const approvals = Array.isArray(verdict?.approvals) ? verdict.approvals : null;
|
|
393
|
-
if (!approvals) return safeInline(reviewStatus);
|
|
394
|
-
if (approvals.length === 0) return "no approval at this head";
|
|
395
|
-
const who = approvals.map((a) => `${safeInline(a.principal)} (${a.source === "stamp_receipt" ? "Stamp" : "GitHub"})`).join(", ");
|
|
396
|
-
return `approved at head ${safeInline(approvals[0].headSha).slice(0, 12)} by ${who}`;
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
export function formatPRDetail(detail, labels) {
|
|
400
|
-
const lines = [
|
|
401
|
-
`${safeInline(detail.repository)} #${detail.number} — ${safeInline(detail.title)}`,
|
|
402
|
-
// The verdict comes second, straight under the title: it is the answer
|
|
403
|
-
// the rest of the output explains (task #607).
|
|
404
|
-
...(labels.verdictLine ? [labels.verdictLine] : []),
|
|
405
|
-
`${labels.head}: ${safeInline(detail.headSha)}`,
|
|
406
|
-
// Forks on ciUnreadable rather than printing ciStatus straight through. Left
|
|
407
|
-
// alone, `pr view` would keep saying "none" while `pr checks` already says
|
|
408
|
-
// the checks could not be read — one endpoint, two commands, two answers,
|
|
409
|
-
// which is the false trail #598 started on.
|
|
410
|
-
`${labels.checks}: ${detail.ciUnreadable ? labels.ciUnreadable : safeInline(detail.ciStatus)}`,
|
|
411
|
-
// Stamp's own approvals first (they are what the verdict counts); GitHub's
|
|
412
|
-
// review decision alone reads "unreviewed" on a PR Stamp approved.
|
|
413
|
-
`${labels.review}: ${formatApprovals(detail.verdict, detail.reviewStatus)}`,
|
|
414
|
-
// The Stamp page first, then GitHub's: the Stamp one is the link to hand
|
|
415
|
-
// a person (artin 09-27).
|
|
416
|
-
...(labels.stampPage ? [labels.stampPage] : []),
|
|
417
|
-
safeInline(detail.url),
|
|
418
|
-
];
|
|
419
|
-
if (typeof detail.body === "string" && detail.body !== "") lines.push("", safeMultiline(detail.body));
|
|
420
|
-
return lines;
|
|
421
|
-
}
|
|
422
|
-
|
|
423
|
-
export function formatPRDiff(response, nameOnly, unavailableLabel) {
|
|
424
|
-
if (nameOnly) return response.files.map((file) => safeInline(file.path));
|
|
425
|
-
return response.files.map((file) => {
|
|
426
|
-
const summary = `${safeInline(file.path)}\t${safeInline(file.status)}\t+${file.additions}\t-${file.deletions}`;
|
|
427
|
-
if (typeof file.patch === "string" && file.patch !== "") return `${summary}\n${safeMultiline(file.patch)}`;
|
|
428
|
-
return `${summary}\t${unavailableLabel}`;
|
|
429
|
-
});
|
|
430
|
-
}
|
|
431
|
-
|
|
432
|
-
export function formatPRChecks(detail) {
|
|
433
|
-
return detail.checks.map((check) => [check.name, check.status, check.conclusion || "-", check.url].map(safeInline).join("\t"));
|
|
434
|
-
}
|
|
435
|
-
|
|
436
|
-
export function prChecksExitCode(detail) {
|
|
437
|
-
// "Unconfigured" and "unavailable" used to share the line below, because
|
|
438
|
-
// nothing on the wire could tell them apart — the comment there named both.
|
|
439
|
-
// `ciUnreadable` is the first thing that can, so the unreadable case gets its
|
|
440
|
-
// own branch instead of continuing to ride on "the set came back empty".
|
|
441
|
-
//
|
|
442
|
-
// This matters beyond tidiness: someone will eventually decide that a repo
|
|
443
|
-
// with genuinely no CI should not exit non-zero, and if the two cases are
|
|
444
|
-
// still merged, that change silently takes fail-closed with it while reading
|
|
445
|
-
// as a pure ergonomics fix in review.
|
|
446
|
-
if (detail.ciUnreadable) return 1;
|
|
447
|
-
// Match `gh pr checks`: an empty check set is not evidence that CI passed.
|
|
448
|
-
// Keep it non-zero so automation cannot treat an unconfigured or unavailable
|
|
449
|
-
// check suite as a green pull request.
|
|
450
|
-
if (detail.checks.length === 0) return 1;
|
|
451
|
-
let pending = false;
|
|
452
|
-
for (const check of detail.checks) {
|
|
453
|
-
const status = check.status.toLowerCase();
|
|
454
|
-
const conclusion = check.conclusion.toLowerCase();
|
|
455
|
-
if (pendingCheckStates.has(status)) {
|
|
456
|
-
pending = true;
|
|
457
|
-
} else if (status === "completed") {
|
|
458
|
-
if (conclusion === "") pending = true;
|
|
459
|
-
else if (!terminalPassConclusions.has(conclusion)) return 1;
|
|
460
|
-
} else if (status === "success") {
|
|
461
|
-
// The server projects legacy commit statuses as the same state in both
|
|
462
|
-
// fields. An inconsistent payload must not let `status=success` mask a
|
|
463
|
-
// failing conclusion.
|
|
464
|
-
if (conclusion !== "success") return 1;
|
|
465
|
-
} else {
|
|
466
|
-
// Legacy commit statuses use status=success|failure|error instead of a
|
|
467
|
-
// check-run's completed + conclusion pair. Unknown terminal values fail
|
|
468
|
-
// closed rather than silently reporting a green PR.
|
|
469
|
-
return 1;
|
|
470
|
-
}
|
|
471
|
-
}
|
|
472
|
-
return pending ? 8 : 0;
|
|
473
|
-
}
|
package/lib/pr/review.mjs
DELETED
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
// `stamp pr review <n>`: the reviewer's preparation, in one read (task #551,
|
|
2
|
-
// 飞牛 2026-09-18). Everything a non-author exact-head review starts from —
|
|
3
|
-
// the exact head to pin, the base, the merge-base, whether the base has moved
|
|
4
|
-
// since the branch forked (the trap: diffing against the current base then
|
|
5
|
-
// shows unrelated changes), checks, review state — and the local commands
|
|
6
|
-
// that diff and patch-id against the merge-base rather than the base.
|
|
7
|
-
import { safeInline } from "./read.mjs";
|
|
8
|
-
import { formatVerdictLine } from "./verdict.mjs";
|
|
9
|
-
|
|
10
|
-
// The brief does NOT say whether the base has advanced: GitHub's `base.sha`
|
|
11
|
-
// on a pull request is the fork point and stays put while the base branch
|
|
12
|
-
// moves, so `baseSha !== mergeBaseSha` only detects a retarget or rebase and
|
|
13
|
-
// would call an advanced base "current" — the one sentence this tool exists
|
|
14
|
-
// to prevent (飞牛, review of #258). Until the detail carries the base
|
|
15
|
-
// branch's current tip, the rule is stated unconditionally.
|
|
16
|
-
export function reviewBrief(detail) {
|
|
17
|
-
const short = (sha) => sha.slice(0, 12);
|
|
18
|
-
return {
|
|
19
|
-
repository: detail.repository,
|
|
20
|
-
number: detail.number,
|
|
21
|
-
title: detail.title,
|
|
22
|
-
author: detail.author,
|
|
23
|
-
url: detail.url,
|
|
24
|
-
headSha: detail.headSha,
|
|
25
|
-
headRef: detail.headRef,
|
|
26
|
-
baseRef: detail.baseRef,
|
|
27
|
-
baseSha: detail.baseSha,
|
|
28
|
-
mergeBaseSha: detail.mergeBaseSha,
|
|
29
|
-
ciStatus: detail.ciStatus,
|
|
30
|
-
// Carried through because this brief is what a reviewer reads before
|
|
31
|
-
// pinning a verdict to an exact head. Dropping it here let `pr review`
|
|
32
|
-
// keep saying "none" after `pr view` and `pr checks` had both learned to
|
|
33
|
-
// say the checks were refused -- the same quiet falsehood, on the path
|
|
34
|
-
// where it costs the most (cross, reviewing #602).
|
|
35
|
-
ciUnreadable: detail.ciUnreadable === true,
|
|
36
|
-
reviewStatus: detail.reviewStatus,
|
|
37
|
-
// Stamp's verdict for these exact coordinates (task #607), or null from a
|
|
38
|
-
// gateway that does not compute one.
|
|
39
|
-
stampVerdict: detail.verdict ?? null,
|
|
40
|
-
reviewFreshness: detail.reviewFreshness,
|
|
41
|
-
checks: detail.checks.map((c) => ({ name: c.name, status: c.status, conclusion: c.conclusion })),
|
|
42
|
-
commands: {
|
|
43
|
-
fetch: `git fetch origin ${detail.headRef} ${detail.baseRef}`,
|
|
44
|
-
diff: `git diff ${short(detail.mergeBaseSha)}..${short(detail.headSha)}`,
|
|
45
|
-
stat: `git diff --stat ${short(detail.mergeBaseSha)}..${short(detail.headSha)}`,
|
|
46
|
-
log: `git log --oneline ${short(detail.mergeBaseSha)}..${short(detail.headSha)}`,
|
|
47
|
-
patchId: `git diff ${short(detail.mergeBaseSha)} ${short(detail.headSha)} | git patch-id --stable`,
|
|
48
|
-
},
|
|
49
|
-
verdict: `GO|CHANGES @ exact ${detail.headSha}`,
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
export function formatPRReview(brief, t) {
|
|
54
|
-
const lines = [
|
|
55
|
-
`${safeInline(brief.repository)} #${brief.number} — ${safeInline(brief.title)} (${safeInline(brief.author)})`,
|
|
56
|
-
...(brief.stampVerdict ? [formatVerdictLine(brief.stampVerdict, t)] : []),
|
|
57
|
-
`${t("prReviewExactHead")}: ${safeInline(brief.headSha)} (${safeInline(brief.headRef)})`,
|
|
58
|
-
`${t("prReviewBase")}: ${safeInline(brief.baseRef)} @ ${safeInline(brief.baseSha)}`,
|
|
59
|
-
`${t("prReviewMergeBase")}: ${safeInline(brief.mergeBaseSha)}`,
|
|
60
|
-
` ${t("prReviewMergeBaseRule")}`,
|
|
61
|
-
brief.ciUnreadable
|
|
62
|
-
? `${t("prChecksLabel")}: ${t("prCIUnreadable")}`
|
|
63
|
-
: `${t("prChecksLabel")}: ${safeInline(brief.ciStatus)}${brief.checks.length ? " — " + brief.checks.map((c) => `${safeInline(c.name)}=${safeInline(c.conclusion || c.status)}`).join(" ") : ""}`,
|
|
64
|
-
`${t("prReviewLabel")}: ${safeInline(brief.reviewStatus)} (${safeInline(brief.reviewFreshness)})`,
|
|
65
|
-
safeInline(brief.url),
|
|
66
|
-
"",
|
|
67
|
-
`${t("prReviewCommands")}:`,
|
|
68
|
-
` ${brief.commands.fetch}`,
|
|
69
|
-
` ${brief.commands.log}`,
|
|
70
|
-
` ${brief.commands.stat}`,
|
|
71
|
-
` ${brief.commands.diff}`,
|
|
72
|
-
` ${brief.commands.patchId}`,
|
|
73
|
-
"",
|
|
74
|
-
// The qualifier goes ON this line, not eight lines above it. `Checks:
|
|
75
|
-
// unreadable` already appears near the top, but this is the line a reviewer
|
|
76
|
-
// copies to record a verdict, and by the time they reach it there is a whole
|
|
77
|
-
// block of commands in between. This card's entire subject is a true
|
|
78
|
-
// statement sitting somewhere other than where the decision is made; putting
|
|
79
|
-
// it only at the top would have been a milder version of the same thing
|
|
80
|
-
// (飞牛, reviewing #282).
|
|
81
|
-
// Stamp's state rides on the same line for the same reason: a reviewer
|
|
82
|
-
// posting GO on a PR whose required CI never ran should see that here.
|
|
83
|
-
`${t("prReviewVerdict")}: ${brief.verdict}` +
|
|
84
|
-
(brief.ciUnreadable ? ` (${t("prVerdictCIUnreadable")})` : "") +
|
|
85
|
-
(brief.stampVerdict ? ` (Stamp: ${safeInline(brief.stampVerdict.state)})` : ""),
|
|
86
|
-
];
|
|
87
|
-
return lines;
|
|
88
|
-
}
|
package/lib/pr/verdict.mjs
DELETED
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
// The pull request verdict (task #607): the gateway's one-line answer to
|
|
2
|
-
// "can this land now, and if not, why not", shared by `pr view`, `pr review`
|
|
3
|
-
// and the web header. The CLI never computes it; it only validates and prints
|
|
4
|
-
// what the gateway decided, pinned to the head and base it was decided for.
|
|
5
|
-
import { AgentError } from "../errors.mjs";
|
|
6
|
-
import { safeInline } from "./read.mjs";
|
|
7
|
-
|
|
8
|
-
// Lists that name the checks behind a state.
|
|
9
|
-
const namedChecks = {
|
|
10
|
-
CI_FAILING: "requiredFailing",
|
|
11
|
-
CI_NOT_EXECUTED: "requiredSkipped",
|
|
12
|
-
CI_PENDING: "requiredPending",
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
const listFields = ["requiredPassed", "requiredFailing", "requiredPending", "requiredSkipped", "optional"];
|
|
16
|
-
|
|
17
|
-
// Absent is allowed (a gateway before task #607 does not send it). Present
|
|
18
|
-
// must be well formed. An unknown state is NOT an error: a newer gateway may
|
|
19
|
-
// add states, and an older CLI must still print what it was told.
|
|
20
|
-
export function validateVerdict(value) {
|
|
21
|
-
if (value === undefined || value === null) return undefined;
|
|
22
|
-
if (typeof value !== "object" || Array.isArray(value)) throw new AgentError("prReadInvalidResponse");
|
|
23
|
-
for (const field of ["state", "headSha", "baseSha"]) {
|
|
24
|
-
if (typeof value[field] !== "string" || value[field] === "") throw new AgentError("prReadInvalidResponse");
|
|
25
|
-
}
|
|
26
|
-
for (const field of listFields) {
|
|
27
|
-
if (!Array.isArray(value[field]) || value[field].some((name) => typeof name !== "string")) {
|
|
28
|
-
throw new AgentError("prReadInvalidResponse");
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
if (typeof value.requiredKnown !== "boolean") throw new AgentError("prReadInvalidResponse");
|
|
32
|
-
// blockedReasons: absent from a gateway before it; present, a list of
|
|
33
|
-
// {code, detail} strings.
|
|
34
|
-
if (value.blockedReasons !== undefined && value.blockedReasons !== null) {
|
|
35
|
-
if (!Array.isArray(value.blockedReasons) ||
|
|
36
|
-
value.blockedReasons.some((r) => !r || typeof r !== "object" || typeof r.code !== "string" || typeof (r.detail ?? "") !== "string")) {
|
|
37
|
-
throw new AgentError("prReadInvalidResponse");
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
return value;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
// formatBlockedReason: the code's text (or the bare code from a newer
|
|
44
|
-
// gateway), then the gateway's detail.
|
|
45
|
-
function formatBlockedReason(reason, t) {
|
|
46
|
-
const key = `blocked_${reason.code}`;
|
|
47
|
-
const text = t(key);
|
|
48
|
-
const label = text && text !== key ? text : safeInline(reason.code);
|
|
49
|
-
return reason.detail ? `${label}: ${safeInline(reason.detail)}` : label;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
// One line: state, a short explanation, the checks behind it, and the exact
|
|
53
|
-
// coordinates. `t(key)` returns the key itself when there is no message.
|
|
54
|
-
export function formatVerdictLine(verdict, t) {
|
|
55
|
-
const short = (sha) => safeInline(sha).slice(0, 12);
|
|
56
|
-
const state = safeInline(verdict.state);
|
|
57
|
-
const key = `verdict_${verdict.state}`;
|
|
58
|
-
const explained = t(key);
|
|
59
|
-
const why = explained && explained !== key ? ` — ${explained}` : "";
|
|
60
|
-
const list = namedChecks[verdict.state] ? verdict[namedChecks[verdict.state]] : [];
|
|
61
|
-
let names = list.length ? ` (${list.map(safeInline).join(", ")})` : "";
|
|
62
|
-
if (verdict.state === "BLOCKED" && Array.isArray(verdict.blockedReasons)) {
|
|
63
|
-
names = ` (${verdict.blockedReasons.length ? verdict.blockedReasons.map((r) => formatBlockedReason(r, t)).join("; ") : t("prVerdictBlockedNone")})`;
|
|
64
|
-
}
|
|
65
|
-
return `${t("prVerdictLabel")}: ${state}${why}${names} @ head ${short(verdict.headSha)} base ${short(verdict.baseSha)}`;
|
|
66
|
-
}
|