@botiverse/stamp-cli 0.3.14 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/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/write.mjs
DELETED
|
@@ -1,180 +0,0 @@
|
|
|
1
|
-
import { AgentError } from "../errors.mjs";
|
|
2
|
-
import { parsePRTargetArgs, prDetailPath, takeBooleanFlag, takeNamedFlag } from "./read.mjs";
|
|
3
|
-
|
|
4
|
-
const shaRE = /^[0-9a-f]{40}$/;
|
|
5
|
-
const methods = new Set(["", "merge", "squash", "rebase"]);
|
|
6
|
-
|
|
7
|
-
// A missing flag is a usage error; a present one that is not a full commit
|
|
8
|
-
// SHA (a short SHA, a branch name) is named, before any request.
|
|
9
|
-
function requireSHA(value, flag, usageCode) {
|
|
10
|
-
if (typeof value !== "string" || value === "") throw new AgentError(usageCode);
|
|
11
|
-
const sha = value.trim().toLowerCase();
|
|
12
|
-
if (!shaRE.test(sha)) throw new AgentError("prWriteFullSHA", { flag });
|
|
13
|
-
return sha;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
function parseWriteTarget(input, usageCode) {
|
|
17
|
-
const args = [...input];
|
|
18
|
-
const head = takeNamedFlag(args, "head", undefined, usageCode);
|
|
19
|
-
const base = takeNamedFlag(args, "base", undefined, usageCode);
|
|
20
|
-
const body = takeNamedFlag(args, "body", "b", usageCode);
|
|
21
|
-
const methodRaw = takeNamedFlag(args, "method", undefined, usageCode);
|
|
22
|
-
const target = parsePRTargetArgs(args, usageCode);
|
|
23
|
-
const expectedHead = requireSHA(head, "--head", usageCode);
|
|
24
|
-
const expectedBase = requireSHA(base, "--base", usageCode);
|
|
25
|
-
const mergeMethod = methodRaw === undefined ? "" : String(methodRaw).toLowerCase();
|
|
26
|
-
if (!methods.has(mergeMethod)) throw new AgentError(usageCode);
|
|
27
|
-
if (body !== undefined && !body.trim()) throw new AgentError(usageCode);
|
|
28
|
-
return { target, expectedHead, expectedBase, body, mergeMethod };
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export function parsePRApproveArgs(input) {
|
|
32
|
-
const parsed = parseWriteTarget(input, "usagePrApprove");
|
|
33
|
-
if (parsed.mergeMethod) throw new AgentError("usagePrApprove");
|
|
34
|
-
return {
|
|
35
|
-
target: parsed.target,
|
|
36
|
-
path: `${prDetailPath(parsed.target)}/approve`,
|
|
37
|
-
method: "POST",
|
|
38
|
-
body: {
|
|
39
|
-
expectedHead: parsed.expectedHead,
|
|
40
|
-
expectedBase: parsed.expectedBase,
|
|
41
|
-
...(parsed.body !== undefined ? { body: parsed.body } : {}),
|
|
42
|
-
},
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
// An agent withdraws its own Stamp-recorded approval of this exact head. There
|
|
47
|
-
// is no body: a Stamp approval is a vote, not a review.
|
|
48
|
-
export function parsePRUnapproveArgs(input) {
|
|
49
|
-
const parsed = parseWriteTarget(input, "usagePrUnapprove");
|
|
50
|
-
if (parsed.mergeMethod || parsed.body !== undefined) throw new AgentError("usagePrUnapprove");
|
|
51
|
-
return {
|
|
52
|
-
target: parsed.target,
|
|
53
|
-
path: `${prDetailPath(parsed.target)}/unapprove`,
|
|
54
|
-
method: "POST",
|
|
55
|
-
body: { expectedHead: parsed.expectedHead, expectedBase: parsed.expectedBase },
|
|
56
|
-
};
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
export function parsePRMergeArgs(input) {
|
|
60
|
-
const parsed = parseWriteTarget(input, "usagePrMerge");
|
|
61
|
-
if (parsed.body !== undefined) throw new AgentError("usagePrMerge");
|
|
62
|
-
return {
|
|
63
|
-
target: parsed.target,
|
|
64
|
-
path: `${prDetailPath(parsed.target)}/merge`,
|
|
65
|
-
method: "PUT",
|
|
66
|
-
body: {
|
|
67
|
-
expectedHead: parsed.expectedHead,
|
|
68
|
-
expectedBase: parsed.expectedBase,
|
|
69
|
-
...(parsed.mergeMethod ? { mergeMethod: parsed.mergeMethod } : {}),
|
|
70
|
-
},
|
|
71
|
-
};
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
function requireObject(value, code) {
|
|
75
|
-
if (!value || typeof value !== "object" || Array.isArray(value)) throw new AgentError(code);
|
|
76
|
-
return value;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
// A human approval is a GitHub review (reviewerLogin); an agent approval is
|
|
80
|
-
// recorded by Stamp (recordedBy "stamp", reviewer = the agent's Raft id).
|
|
81
|
-
function validStampApproval(d, action) {
|
|
82
|
-
return d.recordedBy === "stamp" && d.action === action
|
|
83
|
-
&& typeof d.reviewer === "string" && d.reviewer.length > 0;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
export function validatePRApproveResult(result, target) {
|
|
87
|
-
const expectedRepo = `${target.owner}/${target.repo}`;
|
|
88
|
-
const d = requireObject(result, "prApproveOutcomeUnknown");
|
|
89
|
-
const valid = typeof d.repository === "string" && d.repository.toLowerCase() === expectedRepo.toLowerCase()
|
|
90
|
-
&& d.number === target.number
|
|
91
|
-
&& shaRE.test(d.approvedHead) && shaRE.test(d.approvedBase)
|
|
92
|
-
&& ((typeof d.reviewerLogin === "string" && /^[A-Za-z0-9-]+$/.test(d.reviewerLogin))
|
|
93
|
-
|| validStampApproval(d, "approve"));
|
|
94
|
-
if (!valid) throw new AgentError("prApproveOutcomeUnknown");
|
|
95
|
-
return d;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
export function validatePRUnapproveResult(result, target) {
|
|
99
|
-
const expectedRepo = `${target.owner}/${target.repo}`;
|
|
100
|
-
const d = requireObject(result, "prUnapproveOutcomeUnknown");
|
|
101
|
-
const valid = typeof d.repository === "string" && d.repository.toLowerCase() === expectedRepo.toLowerCase()
|
|
102
|
-
&& d.number === target.number
|
|
103
|
-
&& shaRE.test(d.approvedHead) && shaRE.test(d.approvedBase)
|
|
104
|
-
&& validStampApproval(d, "unapprove");
|
|
105
|
-
if (!valid) throw new AgentError("prUnapproveOutcomeUnknown");
|
|
106
|
-
return d;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
// A human merge is GitHub's, as the bound user (actorLogin); an agent merge is
|
|
110
|
-
// performed by the Stamp App (mergedBy "stamp-app", agent = the Raft id).
|
|
111
|
-
function validAppMerge(d) {
|
|
112
|
-
return d.mergedBy === "stamp-app" && typeof d.agent === "string" && d.agent.length > 0;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
export function validatePRMergeResult(result, target) {
|
|
116
|
-
const expectedRepo = `${target.owner}/${target.repo}`;
|
|
117
|
-
const d = requireObject(result, "prMergeOutcomeUnknown");
|
|
118
|
-
const valid = typeof d.repository === "string" && d.repository.toLowerCase() === expectedRepo.toLowerCase()
|
|
119
|
-
&& d.number === target.number && d.merged === true
|
|
120
|
-
&& shaRE.test(d.mergeSha) && shaRE.test(d.mergedHead) && shaRE.test(d.mergedBase)
|
|
121
|
-
&& ((typeof d.actorLogin === "string" && /^[A-Za-z0-9-]+$/.test(d.actorLogin))
|
|
122
|
-
|| validAppMerge(d));
|
|
123
|
-
if (!valid) throw new AgentError("prMergeOutcomeUnknown");
|
|
124
|
-
return d;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
// `pr merge --auto` (task #664): the author registers the PR to be merged by
|
|
128
|
-
// Stamp once someone else approves that exact head and the verdict is READY.
|
|
129
|
-
// `--auto --cancel` withdraws; `--auto --status` reads the registration.
|
|
130
|
-
// Register and withdraw pin --head and --base like every write.
|
|
131
|
-
export function isAutoMergeArgs(input) {
|
|
132
|
-
return input.includes("--auto");
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
export function parsePRAutoMergeArgs(input) {
|
|
136
|
-
const args = [...input];
|
|
137
|
-
takeBooleanFlag(args, "auto");
|
|
138
|
-
const cancel = takeBooleanFlag(args, "cancel");
|
|
139
|
-
const status = takeBooleanFlag(args, "status");
|
|
140
|
-
if (cancel && status) throw new AgentError("usagePrMerge");
|
|
141
|
-
if (status) {
|
|
142
|
-
const target = parsePRTargetArgs(args, "usagePrMerge");
|
|
143
|
-
return { kind: "status", target, path: `${prDetailPath(target)}/auto-merge`, method: "GET" };
|
|
144
|
-
}
|
|
145
|
-
const parsed = parseWriteTarget(args, "usagePrMerge");
|
|
146
|
-
if (parsed.body !== undefined || (cancel && parsed.mergeMethod)) throw new AgentError("usagePrMerge");
|
|
147
|
-
return {
|
|
148
|
-
kind: cancel ? "cancel" : "arm",
|
|
149
|
-
target: parsed.target,
|
|
150
|
-
path: `${prDetailPath(parsed.target)}/auto-merge`,
|
|
151
|
-
method: cancel ? "DELETE" : "PUT",
|
|
152
|
-
body: {
|
|
153
|
-
expectedHead: parsed.expectedHead,
|
|
154
|
-
expectedBase: parsed.expectedBase,
|
|
155
|
-
...(parsed.mergeMethod ? { mergeMethod: parsed.mergeMethod } : {}),
|
|
156
|
-
},
|
|
157
|
-
};
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
// The gateway's answer must be about the PR that was asked for. An unknown
|
|
161
|
-
// state from a newer gateway is passed through, not rejected.
|
|
162
|
-
export function validatePRAutoMergeResult(result, target) {
|
|
163
|
-
const expectedRepo = `${target.owner}/${target.repo}`;
|
|
164
|
-
const d = requireObject(result, "prMergeOutcomeUnknown");
|
|
165
|
-
const valid = typeof d.repository === "string" && d.repository.toLowerCase() === expectedRepo.toLowerCase()
|
|
166
|
-
&& d.number === target.number && typeof d.state === "string" && d.state !== ""
|
|
167
|
-
&& shaRE.test(d.currentHead)
|
|
168
|
-
&& (d.armedHead === undefined || shaRE.test(d.armedHead));
|
|
169
|
-
if (!valid) throw new AgentError("prMergeOutcomeUnknown");
|
|
170
|
-
return d;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
// One line: the state, what it means, and the pinned head.
|
|
174
|
-
export function formatAutoMerge(d, t) {
|
|
175
|
-
const key = `prAutoMerge_${d.state}`;
|
|
176
|
-
const vars = { repository: d.repository, number: d.number, head: (d.armedHead || d.currentHead).slice(0, 12), method: d.mergeMethod || "" };
|
|
177
|
-
const text = t(key, vars);
|
|
178
|
-
const line = text !== key ? text : `${d.repository}#${d.number}: ${d.state}`;
|
|
179
|
-
return d.alreadyInEffect ? `${line} ${t("prAutoMergeUnchanged")}` : line;
|
|
180
|
-
}
|
package/lib/queue/setup.mjs
DELETED
|
@@ -1,143 +0,0 @@
|
|
|
1
|
-
import { AgentError } from "../errors.mjs";
|
|
2
|
-
import { takeNamedFlag } from "../pr/read.mjs";
|
|
3
|
-
|
|
4
|
-
// `queue` sets up Stamp's merge queue for a registered target branch, as an
|
|
5
|
-
// agent: discovery (setup), save, verify, enable, status. The gateway owns
|
|
6
|
-
// every rule; the CLI parses, reads the digest it confirms, and prints.
|
|
7
|
-
|
|
8
|
-
const repoPattern = /^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/;
|
|
9
|
-
const usage = "usageQueue";
|
|
10
|
-
|
|
11
|
-
function positiveInt(value, { min = 1, max = Number.MAX_SAFE_INTEGER } = {}) {
|
|
12
|
-
if (typeof value !== "string" || !/^\d+$/.test(value)) return undefined;
|
|
13
|
-
const n = Number(value);
|
|
14
|
-
return Number.isSafeInteger(n) && n >= min && n <= max ? n : undefined;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
function takeBool(args, name) {
|
|
18
|
-
const i = args.indexOf(`--${name}`);
|
|
19
|
-
if (i === -1) return false;
|
|
20
|
-
args.splice(i, 1);
|
|
21
|
-
return true;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export function queueBasePath(repository) {
|
|
25
|
-
const [owner, repo] = repository.split("/");
|
|
26
|
-
return `/v1/repositories/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}/merge-queue`;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export function setupPath(repository, branch, workflow) {
|
|
30
|
-
const q = new URLSearchParams({ branch, mode: "native" });
|
|
31
|
-
if (workflow !== undefined) q.set("workflow", String(workflow));
|
|
32
|
-
return `${queueBasePath(repository)}/setup?${q}`;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
// parseQueueArgs parses `queue <action> OWNER/REPO [flags]` and never touches
|
|
36
|
-
// the network.
|
|
37
|
-
export function parseQueueArgs(input) {
|
|
38
|
-
const args = [...input];
|
|
39
|
-
const action = args.shift();
|
|
40
|
-
if (!["setup", "save", "verify", "enable", "status"].includes(action)) throw new AgentError(usage);
|
|
41
|
-
const out = { action };
|
|
42
|
-
if (action === "setup" || action === "save") {
|
|
43
|
-
out.branch = takeNamedFlag(args, "branch", undefined, usage);
|
|
44
|
-
const workflow = takeNamedFlag(args, "workflow", undefined, usage);
|
|
45
|
-
if (workflow !== undefined) {
|
|
46
|
-
out.workflow = positiveInt(workflow);
|
|
47
|
-
if (out.workflow === undefined) throw new AgentError("queueWorkflowInvalid");
|
|
48
|
-
}
|
|
49
|
-
if (!out.branch) throw new AgentError(usage);
|
|
50
|
-
}
|
|
51
|
-
if (action === "save") {
|
|
52
|
-
if (out.workflow === undefined) throw new AgentError(usage);
|
|
53
|
-
out.ciPolicy = takeNamedFlag(args, "ci-policy", undefined, usage);
|
|
54
|
-
const maxBatch = takeNamedFlag(args, "max-batch", undefined, usage);
|
|
55
|
-
const wait = takeNamedFlag(args, "wait", undefined, usage);
|
|
56
|
-
const timeout = takeNamedFlag(args, "timeout", undefined, usage);
|
|
57
|
-
if (!["head", "all"].includes(out.ciPolicy)) throw new AgentError("queueCIPolicyInvalid");
|
|
58
|
-
out.maxBatch = positiveInt(maxBatch, { min: 1, max: 10 });
|
|
59
|
-
out.waitSeconds = positiveInt(wait, { min: 0, max: 600 });
|
|
60
|
-
out.timeoutSeconds = positiveInt(timeout, { min: 60, max: 14400 });
|
|
61
|
-
if (out.maxBatch === undefined || out.waitSeconds === undefined || out.timeoutSeconds === undefined) throw new AgentError("queueLimitsInvalid");
|
|
62
|
-
out.confirm = takeBool(args, "confirm");
|
|
63
|
-
}
|
|
64
|
-
if (action === "verify") {
|
|
65
|
-
const pr = takeNamedFlag(args, "pr", undefined, usage);
|
|
66
|
-
out.number = positiveInt(pr);
|
|
67
|
-
if (out.number === undefined) throw new AgentError(usage);
|
|
68
|
-
}
|
|
69
|
-
const repository = args.shift();
|
|
70
|
-
if (!repository || !repoPattern.test(repository) || args.length > 0) throw new AgentError(usage);
|
|
71
|
-
out.repository = repository;
|
|
72
|
-
return out;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
// The request each action sends. save needs the discovery it confirms.
|
|
76
|
-
export function queueRequest(input, view) {
|
|
77
|
-
const base = queueBasePath(input.repository);
|
|
78
|
-
switch (input.action) {
|
|
79
|
-
case "setup":
|
|
80
|
-
return { method: "GET", path: setupPath(input.repository, input.branch, input.workflow) };
|
|
81
|
-
case "status":
|
|
82
|
-
return { method: "GET", path: base };
|
|
83
|
-
case "verify":
|
|
84
|
-
return { method: "POST", path: `${base}/verify`, body: { number: input.number } };
|
|
85
|
-
case "enable":
|
|
86
|
-
return { method: "POST", path: `${base}/enable`, body: {} };
|
|
87
|
-
case "save":
|
|
88
|
-
return {
|
|
89
|
-
method: "POST", path: `${base}/setup`,
|
|
90
|
-
body: {
|
|
91
|
-
branch: input.branch, workflow: input.workflow, mode: "native", ciPolicy: input.ciPolicy,
|
|
92
|
-
maxBatch: input.maxBatch, waitSeconds: input.waitSeconds, timeoutSeconds: input.timeoutSeconds,
|
|
93
|
-
confirm: true, confirmation: view.confirmation, revision: view.revision, source: view.sourceSha,
|
|
94
|
-
},
|
|
95
|
-
};
|
|
96
|
-
default:
|
|
97
|
-
throw new AgentError(usage);
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
export function validateQueueView(d, input) {
|
|
102
|
-
if (!d || typeof d.repository !== "string" || d.repository.toLowerCase() !== input.repository.toLowerCase()
|
|
103
|
-
|| typeof d.enabled !== "boolean" || typeof d.verified !== "boolean") throw new AgentError("queueInvalidResponse");
|
|
104
|
-
if (input.action === "setup" || input.action === "save") {
|
|
105
|
-
if (d.branch !== input.branch || !Array.isArray(d.workflows) || typeof d.canSave !== "boolean" || !Number.isInteger(d.revision)) throw new AgentError("queueInvalidResponse");
|
|
106
|
-
}
|
|
107
|
-
return d;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
const list = (xs) => (Array.isArray(xs) && xs.length ? xs.join(", ") : "—");
|
|
111
|
-
|
|
112
|
-
// formatQueueSetup is the discovery, as the lines an agent reads before it
|
|
113
|
-
// confirms anything.
|
|
114
|
-
export function formatQueueSetup(d) {
|
|
115
|
-
const lines = [`${d.repository} ${d.branch} (${d.mode}) at ${d.sourceSha || "—"}`];
|
|
116
|
-
lines.push(`workflows:`);
|
|
117
|
-
for (const w of d.workflows) lines.push(` ${w.id} ${w.name} ${w.path}${w.id === d.workflowId ? " (selected)" : ""}`);
|
|
118
|
-
if (d.workflowId) {
|
|
119
|
-
lines.push(`selected workflow: ${d.workflowId} ${d.workflowSha ? `(definition ${d.workflowSha})` : ""}`.trimEnd());
|
|
120
|
-
lines.push(`detected jobs: ${list(d.jobs)}`);
|
|
121
|
-
}
|
|
122
|
-
lines.push(`branch-required checks: ${list(d.required)}`);
|
|
123
|
-
for (const a of d.additionalWorkflows ?? []) lines.push(`additional workflow: ${a.workflowId} ${a.workflowPath} (${a.event}) jobs: ${list(a.requiredJobs)}`);
|
|
124
|
-
lines.push(`rules: ${list(d.rules)}`);
|
|
125
|
-
lines.push(d.canSave ? `confirmation: ${d.confirmation}` : "confirmation: — (select a workflow with --workflow ID to get one)");
|
|
126
|
-
lines.push(`revision: ${d.revision} enabled: ${d.enabled} verified: ${d.verified}`);
|
|
127
|
-
lines.push(`registered targets: ${list(d.registeredTargets)}`);
|
|
128
|
-
return lines;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
export function formatQueueStatus(d) {
|
|
132
|
-
const lines = [`${d.repository}: ${d.configured ? (d.enabled ? "enabled" : "not enabled") : "not configured"}; verified: ${d.verified}; waiting: ${d.waiting ?? 0}`];
|
|
133
|
-
const c = d.config;
|
|
134
|
-
if (c) {
|
|
135
|
-
lines.push(`branch ${c.branch} (${c.mode}), workflow ${c.workflowId} ${c.workflowPath}, ci-policy ${c.ciPolicy || "head"}, max-batch ${c.maxBatch}, wait ${c.waitSeconds}s, timeout ${c.timeoutSeconds}s, revision ${c.revision}`);
|
|
136
|
-
lines.push(`required jobs: ${list(c.requiredJobs)}`);
|
|
137
|
-
}
|
|
138
|
-
const v = d.lastVerification;
|
|
139
|
-
if (v) lines.push(`last verification: PR #${v.number} ${v.state}${v.passed ? " (passed)" : ""}${v.reason ? `: ${v.reason}` : ""}${v.ciUrl ? ` ${v.ciUrl}` : ""}`);
|
|
140
|
-
else lines.push("last verification: none");
|
|
141
|
-
lines.push(`registered targets: ${list(d.registeredTargets)}`);
|
|
142
|
-
return lines;
|
|
143
|
-
}
|
package/lib/receipt/list.mjs
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import { AgentError } from "../errors.mjs";
|
|
2
|
-
import { takeNamedFlag } from "../pr/read.mjs";
|
|
3
|
-
|
|
4
|
-
// `stamp receipt list` (task #580): the session agent lists only its own push
|
|
5
|
-
// receipts — never another agent's, never the same agent's on another server.
|
|
6
|
-
// The endpoint is always paged ({items, nextCursor}); --limit/--cursor opt
|
|
7
|
-
// into walking further, --repo narrows server-side and is bound into the
|
|
8
|
-
// cursor so a cursor minted under one filter cannot replay under another.
|
|
9
|
-
|
|
10
|
-
export const RECEIPT_LIST_MAX_LIMIT = 100;
|
|
11
|
-
|
|
12
|
-
export function parseReceiptListArgs(input) {
|
|
13
|
-
const args = [...input];
|
|
14
|
-
const repo = takeNamedFlag(args, "repo", "R", "usageReceiptList");
|
|
15
|
-
const limitRaw = takeNamedFlag(args, "limit", undefined, "usageReceiptList");
|
|
16
|
-
const cursor = takeNamedFlag(args, "cursor", undefined, "usageReceiptList");
|
|
17
|
-
if (args.length !== 0) throw new AgentError("usageReceiptList");
|
|
18
|
-
|
|
19
|
-
const params = new URLSearchParams();
|
|
20
|
-
if (repo !== undefined) {
|
|
21
|
-
if (!/^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/.test(repo)) throw new AgentError("usageReceiptList");
|
|
22
|
-
params.set("repo", repo);
|
|
23
|
-
}
|
|
24
|
-
if (limitRaw !== undefined) {
|
|
25
|
-
if (!/^[1-9][0-9]*$/.test(limitRaw) || Number(limitRaw) > RECEIPT_LIST_MAX_LIMIT) {
|
|
26
|
-
throw new AgentError("usageReceiptList");
|
|
27
|
-
}
|
|
28
|
-
params.set("limit", limitRaw);
|
|
29
|
-
}
|
|
30
|
-
if (cursor !== undefined) params.set("cursor", cursor);
|
|
31
|
-
const query = params.toString();
|
|
32
|
-
return { path: query === "" ? "/v1/receipts" : `/v1/receipts?${query}` };
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export function validateReceiptList(d) {
|
|
36
|
-
if (!d || !Array.isArray(d.items)) throw new AgentError("receiptListBadResponse");
|
|
37
|
-
for (const item of d.items) {
|
|
38
|
-
if (typeof item?.id !== "string" || typeof item?.status !== "string" ||
|
|
39
|
-
typeof item?.repository !== "string" || typeof item?.targetRef !== "string") {
|
|
40
|
-
throw new AgentError("receiptListBadResponse");
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
if (d.nextCursor !== null && d.nextCursor !== undefined && typeof d.nextCursor !== "string") {
|
|
44
|
-
throw new AgentError("receiptListBadResponse");
|
|
45
|
-
}
|
|
46
|
-
return { items: d.items, nextCursor: d.nextCursor ?? null };
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
export function formatReceiptLine(item) {
|
|
50
|
-
let line = `${item.id} ${item.status} ${item.repository} ${item.targetRef} published=${item.publishedSha ?? "-"}`;
|
|
51
|
-
if (item.publishAttempts) line += ` attempts=${item.publishAttempts}`;
|
|
52
|
-
if (item.nextRetryAt) line += ` nextRetry=${item.nextRetryAt}`;
|
|
53
|
-
return line;
|
|
54
|
-
}
|
package/lib/repo/clone.mjs
DELETED
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
import { spawn } from "node:child_process";
|
|
2
|
-
|
|
3
|
-
export function parseRepoCloneArgs(args) {
|
|
4
|
-
const dash = args.indexOf("--");
|
|
5
|
-
const gitFlags = dash === -1 ? [] : args.slice(dash + 1);
|
|
6
|
-
const rest = dash === -1 ? args : args.slice(0, dash);
|
|
7
|
-
const repository = rest[0];
|
|
8
|
-
const directory = rest[1];
|
|
9
|
-
if (!repository || rest.length > 2) {
|
|
10
|
-
return null;
|
|
11
|
-
}
|
|
12
|
-
return { repository, directory, gitFlags };
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export function parseRepository(input, defaultOwner) {
|
|
16
|
-
const raw = String(input ?? "").trim();
|
|
17
|
-
if (!raw) return null;
|
|
18
|
-
let value = raw.replace(/\.git$/i, "");
|
|
19
|
-
const github = value.match(
|
|
20
|
-
/^(?:https?:\/\/github\.com\/|git@github\.com:|ssh:\/\/git@github\.com\/)([^/]+)\/([^/]+)\/?$/i,
|
|
21
|
-
);
|
|
22
|
-
if (github) {
|
|
23
|
-
return { owner: github[1], name: github[2] };
|
|
24
|
-
}
|
|
25
|
-
if (!value.includes("://") && !value.includes("@") && value.includes("/")) {
|
|
26
|
-
const parts = value.split("/").filter(Boolean);
|
|
27
|
-
if (parts.length === 2) {
|
|
28
|
-
return { owner: parts[0], name: parts[1] };
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
if (!value.includes("/") && !value.includes(":") && defaultOwner) {
|
|
32
|
-
return { owner: defaultOwner, name: value };
|
|
33
|
-
}
|
|
34
|
-
return null;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export function gatewayCloneURL(host, port, owner, name) {
|
|
38
|
-
const hostname = String(host ?? "").trim();
|
|
39
|
-
const parsedPort = Number(port);
|
|
40
|
-
const sshPort = Number.isInteger(parsedPort) && parsedPort > 0 ? parsedPort : 22;
|
|
41
|
-
const authority = hostname.includes(":") ? `[${hostname}]:${sshPort}` : `${hostname}:${sshPort}`;
|
|
42
|
-
return `ssh://git@${authority}/${owner}/${name}.git`;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export function runGitClone({ url, directory, gitFlags, env, spawnImpl = spawn }) {
|
|
46
|
-
const args = ["clone", ...(gitFlags ?? []), url];
|
|
47
|
-
if (directory) args.push(directory);
|
|
48
|
-
return new Promise((resolve, reject) => {
|
|
49
|
-
const child = spawnImpl("git", args, {
|
|
50
|
-
env: env ?? process.env,
|
|
51
|
-
stdio: ["ignore", "pipe", "pipe"],
|
|
52
|
-
});
|
|
53
|
-
let stdout = "";
|
|
54
|
-
let stderr = "";
|
|
55
|
-
child.stdout?.on("data", (b) => { stdout += b; });
|
|
56
|
-
child.stderr?.on("data", (b) => { stderr += b; });
|
|
57
|
-
child.on("error", reject);
|
|
58
|
-
child.on("close", (code) => resolve({ code: code ?? 1, stdout, stderr }));
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
// runGitConfig pins the enrolled identity in the cloned repository itself, so
|
|
63
|
-
// every later `git push`/`git fetch` from that checkout uses the same key and
|
|
64
|
-
// the same pinned host key without any GIT_SSH_COMMAND export.
|
|
65
|
-
export function runGitSetSSHCommand({ directory, sshCommand, spawnImpl = spawn }) {
|
|
66
|
-
return new Promise((resolve) => {
|
|
67
|
-
const child = spawnImpl("git", ["-C", directory, "config", "--local", "core.sshCommand", sshCommand], {
|
|
68
|
-
stdio: ["ignore", "ignore", "pipe"],
|
|
69
|
-
});
|
|
70
|
-
let stderr = "";
|
|
71
|
-
child.stderr?.on("data", (b) => { stderr += b; });
|
|
72
|
-
child.on("error", () => resolve({ ok: false }));
|
|
73
|
-
child.on("close", (code) => resolve({ ok: code === 0, stderr }));
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
// runGitConfigLocal sets one key in the checkout's own config (never
|
|
78
|
-
// --global: several agents often share one machine user).
|
|
79
|
-
export function runGitConfigLocal({ directory, key, value, spawnImpl = spawn }) {
|
|
80
|
-
return new Promise((resolve) => {
|
|
81
|
-
const child = spawnImpl("git", ["-C", directory, "config", "--local", key, value], { stdio: ["ignore", "ignore", "pipe"] });
|
|
82
|
-
let stderr = "";
|
|
83
|
-
child.stderr?.on("data", (b) => { stderr += b; });
|
|
84
|
-
child.on("error", () => resolve({ ok: false }));
|
|
85
|
-
child.on("close", (code) => resolve({ ok: code === 0, stderr }));
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
// setCommitIdentity writes the agent's commit identity into the checkout
|
|
90
|
-
// (artin 09-28: the commit email is already automatic, so no step should ask
|
|
91
|
-
// for `git config user.email`). An agent's commit email comes from /v1/me;
|
|
92
|
-
// a person's checkout is left alone. It returns the address written, or "".
|
|
93
|
-
export async function setCommitIdentity(ctx, me, directory) {
|
|
94
|
-
const address = typeof me?.commitEmail?.address === "string" ? me.commitEmail.address.trim() : "";
|
|
95
|
-
if (me?.principalType !== "agent" || !address) return "";
|
|
96
|
-
const set = ctx.opts.gitConfigLocalImpl ?? runGitConfigLocal;
|
|
97
|
-
const email = await set({ directory, key: "user.email", value: address });
|
|
98
|
-
if (!email.ok) return "";
|
|
99
|
-
if (typeof me.name === "string" && me.name.trim()) await set({ directory, key: "user.name", value: me.name.trim() });
|
|
100
|
-
return address;
|
|
101
|
-
}
|
package/lib/repo/identities.mjs
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import { chmodSync, existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
2
|
-
import { homedir } from "node:os";
|
|
3
|
-
import { dirname, join } from "node:path";
|
|
4
|
-
|
|
5
|
-
// Task #514 (ka-grok): "key enroll 用过哪把钥,clone 就用那把" — the key `stamp
|
|
6
|
-
// key enroll` enrolled is the key `stamp repo clone` uses, recorded at enroll
|
|
7
|
-
// time and read back at clone time, per instance origin. The CLI never scans
|
|
8
|
-
// ~/.ssh or guesses paths: a record is written only from what the operator
|
|
9
|
-
// pointed enroll at, and a missing record falls through to the documented
|
|
10
|
-
// default path, whose absence is reported (not searched around).
|
|
11
|
-
|
|
12
|
-
export function identitiesPath(env) {
|
|
13
|
-
const fromEnv = String(env?.STAMP_IDENTITIES ?? "").trim();
|
|
14
|
-
if (fromEnv) return fromEnv;
|
|
15
|
-
const agentDir = String(env?.STAMP_AGENT_STATE_DIR ?? "").trim();
|
|
16
|
-
return agentDir ? join(agentDir, "identities.json") : join(homedir(), ".stamp", "identities.json");
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
function readAll(path) {
|
|
20
|
-
if (!existsSync(path)) return {};
|
|
21
|
-
let parsed;
|
|
22
|
-
try { parsed = JSON.parse(readFileSync(path, "utf8")); } catch { return {}; }
|
|
23
|
-
return parsed && typeof parsed === "object" && !Array.isArray(parsed) ? parsed : {};
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
// readRecordedIdentity returns the enrollment record for one origin, or null.
|
|
27
|
-
// A corrupt or foreign-shaped file is "no record", never an exception: the
|
|
28
|
-
// record is a convenience over an explicit flag, not a gate.
|
|
29
|
-
export function readRecordedIdentity(env, origin) {
|
|
30
|
-
const record = readAll(identitiesPath(env))[String(origin ?? "")];
|
|
31
|
-
if (!record || typeof record !== "object" || typeof record.identityPath !== "string" || !record.identityPath) return null;
|
|
32
|
-
return record;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
// recordIdentity merges one origin's record into the file (0600: it names a
|
|
36
|
-
// private key path and a key id, not a secret, but it is the operator's).
|
|
37
|
-
export function recordIdentity(env, origin, record) {
|
|
38
|
-
const path = identitiesPath(env);
|
|
39
|
-
const all = readAll(path);
|
|
40
|
-
all[String(origin)] = record;
|
|
41
|
-
mkdirSync(dirname(path), { recursive: true });
|
|
42
|
-
writeFileSync(path, `${JSON.stringify(all, null, 2)}\n`, { mode: 0o600 });
|
|
43
|
-
chmodSync(path, 0o600);
|
|
44
|
-
return path;
|
|
45
|
-
}
|