@kyuki3rain/cupola 0.1.0 → 0.2.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/CHANGELOG.md +158 -0
- package/README.md +3 -2
- package/dist/branch-store.js +73 -0
- package/dist/commands/change.js +245 -39
- package/dist/commands/epic.js +3 -3
- package/dist/commands/execution.js +482 -0
- package/dist/commands/locks.js +69 -1
- package/dist/commands/reaction.js +185 -0
- package/dist/commands/revision.js +4 -4
- package/dist/commands/run.js +68 -62
- package/dist/commands/setup.js +54 -22
- package/dist/commands/spec.js +1 -1
- package/dist/commands/status.js +437 -138
- package/dist/commands/trace.js +106 -29
- package/dist/commands/wait.js +14 -11
- package/dist/commands/work.js +12 -7
- package/dist/commands/workspace.js +16 -5
- package/dist/commands/worktree.js +3 -3
- package/dist/config.js +66 -2
- package/dist/epic.js +24 -5
- package/dist/execution.js +272 -0
- package/dist/feedback.js +3 -17
- package/dist/host/git.js +224 -0
- package/dist/host/github.js +145 -48
- package/dist/host/host.js +3 -1
- package/dist/host/index.js +63 -0
- package/dist/main.js +143 -48
- package/dist/reaction.js +110 -0
- package/dist/record.js +52 -18
- package/dist/runbook.js +26 -5
- package/dist/status.js +158 -27
- package/dist/templates.js +2 -1
- package/dist/tracker/files.js +9 -66
- package/dist/tracker/index.js +8 -1
- package/dist/tree.js +17 -0
- package/dist/work.js +27 -6
- package/dist/workspace.js +37 -12
- package/npm-shrinkwrap.json +2 -2
- package/package.json +1 -1
- package/skills/cupola-abort/SKILL.md +11 -5
- package/skills/cupola-adr/SKILL.md +23 -10
- package/skills/cupola-catalog/SKILL.md +1 -1
- package/skills/cupola-catalog-draft/SKILL.md +6 -4
- package/skills/cupola-complete/SKILL.md +35 -15
- package/skills/cupola-decide/SKILL.md +64 -25
- package/skills/cupola-e2e-setup/SKILL.md +1 -1
- package/skills/cupola-epic-accept/SKILL.md +14 -10
- package/skills/cupola-epic-create/SKILL.md +67 -27
- package/skills/cupola-epic-revise/SKILL.md +39 -18
- package/skills/cupola-epic-run/SKILL.md +81 -42
- package/skills/cupola-fix/SKILL.md +7 -5
- package/skills/cupola-init/SKILL.md +79 -24
- package/skills/cupola-intake/SKILL.md +19 -2
- package/skills/cupola-operate/SKILL.md +62 -45
- package/skills/cupola-resolve/SKILL.md +21 -14
- package/skills/cupola-respond/SKILL.md +73 -0
- package/skills/cupola-review/SKILL.md +14 -15
- package/skills/cupola-spec-design/SKILL.md +15 -10
- package/skills/cupola-spec-impl/SKILL.md +6 -4
- package/skills/cupola-spec-run/SKILL.md +29 -17
- package/skills/cupola-steering/SKILL.md +13 -6
- package/skills/cupola-try/SKILL.md +38 -0
- package/templates/install/.kiro/steering/operations.md +2 -2
- package/templates/install/.kiro/steering/principles.md +1 -1
- package/templates/install/cc-sdd/patches.json +1 -1
- package/templates/method-1/card.md +11 -3
- package/templates/method-1/case.md +2 -2
- package/templates/method-1/changes/catalog.md +1 -1
- package/templates/method-1/changes/epic.md +3 -2
- package/templates/method-1/changes/fix.md +1 -1
- package/templates/method-1/changes/release.md +1 -1
- package/templates/method-1/changes/spec.md +1 -1
- package/templates/method-1/cupola-issue-draft.md +1 -1
- package/templates/method-1/epic.md +17 -6
- package/templates/method-1/execution.md +55 -0
- package/templates/method-1/no-go.md +28 -0
- package/templates/method-1/reaction.md +17 -0
- package/templates/method-1/record.json +3 -3
- package/templates/method-1/report.md +3 -3
- package/templates/method-1/runbook.md +8 -2
- package/templates/method-1/runner-result.json +1 -1
- package/dist/commands/procedure.js +0 -92
- package/dist/procedure.js +0 -60
- package/templates/method-1/send-back.md +0 -23
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { stripComments } from "./epic.js";
|
|
3
|
+
import { parseFrontMatter } from "./frontmatter.js";
|
|
4
|
+
import { withReactions } from "./status.js";
|
|
5
|
+
/**
|
|
6
|
+
* An execution: one run of a runbook, by a human, for an epic or a case. Its
|
|
7
|
+
* record is a permanent document next to the runbook
|
|
8
|
+
* (`<paths.runbooks>/<runbook>/executions/<n>.md`), written on its own branch
|
|
9
|
+
* one step at a time and merged at acceptance to accept it. Its status is derived from
|
|
10
|
+
* that document and its change, never stored.
|
|
11
|
+
*/
|
|
12
|
+
const KEBAB = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
|
|
13
|
+
export const EXECUTION_RESULTS = ["finished", "given-up"];
|
|
14
|
+
export const ExecutionFront = z.strictObject({
|
|
15
|
+
kind: z.literal("execution"),
|
|
16
|
+
method: z.number().int(),
|
|
17
|
+
id: z.string().regex(KEBAB, "a kebab-case name"),
|
|
18
|
+
runbook: z.string().regex(KEBAB, "a kebab-case name"),
|
|
19
|
+
/** The commit of the runbook it follows. */
|
|
20
|
+
version: z.string().min(1),
|
|
21
|
+
/** The epic or case it is for. */
|
|
22
|
+
for: z.string().regex(KEBAB, "an epic or case id"),
|
|
23
|
+
params: z.record(z.string(), z.union([z.string(), z.number(), z.boolean()])).default({}),
|
|
24
|
+
started: z.union([z.string(), z.date()]),
|
|
25
|
+
result: z.enum(EXECUTION_RESULTS).nullable().default(null),
|
|
26
|
+
});
|
|
27
|
+
const STEP_ID = "(?:P|X)\\d+";
|
|
28
|
+
const ARRANGED = new RegExp(`^-\\s+(${STEP_ID})\\s+(add|change|skip)(?:\\s+(?:after\\s+(${STEP_ID})|(first)))?\\s*:\\s*(.*)$`);
|
|
29
|
+
const RECORDED = new RegExp(`^-\\s+(${STEP_ID})\\s+(done|stopped)\\b[^:]*:\\s*(.*)$`);
|
|
30
|
+
/** Where `cupola execution record` writes the next step: before this marker. */
|
|
31
|
+
export const NEXT_STEP = "<!-- cupola:next-step -->";
|
|
32
|
+
/** Where it writes the end: before this marker. */
|
|
33
|
+
export const RESULT_MARK = "<!-- cupola:result -->";
|
|
34
|
+
/** Reads an execution's front matter and its ID-led entries; headings and prose are not read. */
|
|
35
|
+
export function parseExecution(raw) {
|
|
36
|
+
const text = raw.replace(/^/, "").replace(/\r\n?/g, "\n");
|
|
37
|
+
const { data, body } = parseFrontMatter(text);
|
|
38
|
+
const offset = text.slice(0, text.length - body.length).split("\n").length - 1;
|
|
39
|
+
const arrangement = [];
|
|
40
|
+
const records = [];
|
|
41
|
+
const placeholders = [];
|
|
42
|
+
let comment = false;
|
|
43
|
+
let fence = false;
|
|
44
|
+
body.split("\n").forEach((original, i) => {
|
|
45
|
+
const n = offset + i + 1;
|
|
46
|
+
if (/^\s*(```|~~~)/.test(original)) {
|
|
47
|
+
fence = !fence;
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
if (fence)
|
|
51
|
+
return;
|
|
52
|
+
let line;
|
|
53
|
+
[line, comment] = stripComments(original, comment);
|
|
54
|
+
if (line.includes("{{"))
|
|
55
|
+
placeholders.push(n);
|
|
56
|
+
const a = ARRANGED.exec(line);
|
|
57
|
+
if (a) {
|
|
58
|
+
const after = a[4] ? "first" : a[3];
|
|
59
|
+
arrangement.push({
|
|
60
|
+
step: a[1] ?? "",
|
|
61
|
+
how: a[2],
|
|
62
|
+
...(after ? { after } : {}),
|
|
63
|
+
line: n,
|
|
64
|
+
});
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
const r = RECORDED.exec(line);
|
|
68
|
+
if (r)
|
|
69
|
+
records.push({
|
|
70
|
+
step: r[1] ?? "",
|
|
71
|
+
result: r[2],
|
|
72
|
+
text: (r[3] ?? "").trim(),
|
|
73
|
+
line: n,
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
return { front: data, arrangement, records, placeholders };
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* The steps this execution carries out, in order: the runbook's, without
|
|
80
|
+
* those it skips, with those it adds after the step each names. Problems are
|
|
81
|
+
* arrangements that name a step that is not there.
|
|
82
|
+
*/
|
|
83
|
+
export function arrangedSteps(runbook, arrangement) {
|
|
84
|
+
const problems = [];
|
|
85
|
+
const steps = [...runbook];
|
|
86
|
+
let firsts = 0;
|
|
87
|
+
const added = new Map();
|
|
88
|
+
for (const a of arrangement) {
|
|
89
|
+
if (a.how === "add") {
|
|
90
|
+
if (a.step.startsWith("P")) {
|
|
91
|
+
problems.push({ message: `${a.step}: an added step is X1, X2, …`, line: a.line });
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
if (steps.includes(a.step)) {
|
|
95
|
+
problems.push({ message: `${a.step} is added twice`, line: a.line });
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
if (!a.after) {
|
|
99
|
+
problems.push({
|
|
100
|
+
message: `${a.step}: say where it goes: \`add after <step>\` or \`add first\``,
|
|
101
|
+
line: a.line,
|
|
102
|
+
});
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
if (a.after === "first") {
|
|
106
|
+
steps.splice(firsts, 0, a.step);
|
|
107
|
+
firsts += 1;
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
const at = steps.indexOf(a.after);
|
|
111
|
+
if (at < 0) {
|
|
112
|
+
problems.push({ message: `${a.step}: no step ${a.after} to add it after`, line: a.line });
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
// Several added after the same step keep the order they are written in.
|
|
116
|
+
const n = added.get(a.after) ?? 0;
|
|
117
|
+
steps.splice(at + 1 + n, 0, a.step);
|
|
118
|
+
added.set(a.after, n + 1);
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
if (!runbook.includes(a.step))
|
|
122
|
+
problems.push({ message: `${a.step} is not a step of the runbook`, line: a.line });
|
|
123
|
+
}
|
|
124
|
+
const skipped = new Set(arrangement.filter((a) => a.how === "skip").map((a) => a.step));
|
|
125
|
+
return { steps: steps.filter((s) => !skipped.has(s)), problems };
|
|
126
|
+
}
|
|
127
|
+
/** How far an execution got, from its records. */
|
|
128
|
+
export function executionState(steps, records) {
|
|
129
|
+
const done = [...new Set(records.filter((r) => r.result === "done").map((r) => r.step))].filter((s) => steps.includes(s));
|
|
130
|
+
const last = records.at(-1);
|
|
131
|
+
const next = steps.find((s) => !done.includes(s));
|
|
132
|
+
return {
|
|
133
|
+
steps,
|
|
134
|
+
done,
|
|
135
|
+
...(last?.result === "stopped" ? { stopped: { step: last.step, text: last.text } } : {}),
|
|
136
|
+
...(next ? { next } : {}),
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
/** The status of an execution: from its record and its change only. */
|
|
140
|
+
export function deriveExecution(e) {
|
|
141
|
+
const { id, front, state, change } = e;
|
|
142
|
+
const operate = `cupola-operate ${id}`;
|
|
143
|
+
const base = {
|
|
144
|
+
work: { kind: "execution", id, component: "" },
|
|
145
|
+
change: change ?? null,
|
|
146
|
+
cards: [],
|
|
147
|
+
run: null,
|
|
148
|
+
execution: {
|
|
149
|
+
runbook: front.runbook,
|
|
150
|
+
for: front.for,
|
|
151
|
+
params: front.params,
|
|
152
|
+
result: front.result,
|
|
153
|
+
steps: state.steps,
|
|
154
|
+
done: state.done,
|
|
155
|
+
...(state.stopped ? { stopped: state.stopped.step } : {}),
|
|
156
|
+
...(state.next ? { next: state.next } : {}),
|
|
157
|
+
},
|
|
158
|
+
};
|
|
159
|
+
const ended = front.result === "given-up" ? "given up" : "finished";
|
|
160
|
+
if (change?.state === "merged") {
|
|
161
|
+
const complete = `cupola-complete ${id}`;
|
|
162
|
+
return {
|
|
163
|
+
...base,
|
|
164
|
+
status: "done",
|
|
165
|
+
turn: e.untidy ? "human" : "none",
|
|
166
|
+
gate: null,
|
|
167
|
+
next: e.untidy
|
|
168
|
+
? { kind: "run", text: `Merged: \`${complete}\` tidies up.`, command: complete }
|
|
169
|
+
: { kind: "nothing", text: `Done (${ended}).` },
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
if (change?.state === "closed") {
|
|
173
|
+
return {
|
|
174
|
+
...base,
|
|
175
|
+
status: "dropped",
|
|
176
|
+
turn: "none",
|
|
177
|
+
gate: null,
|
|
178
|
+
next: { kind: "nothing", text: "Dropped: its change was closed without merging." },
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
const operating = (text) => ({
|
|
182
|
+
...base,
|
|
183
|
+
status: "operating",
|
|
184
|
+
turn: "human",
|
|
185
|
+
gate: null,
|
|
186
|
+
next: { kind: "run", text, command: operate },
|
|
187
|
+
});
|
|
188
|
+
if (front.result) {
|
|
189
|
+
if (change?.state === "open" && !change.draft) {
|
|
190
|
+
const reach = e.reach ?? { read: change.url, merge: "merge it", reactions: true };
|
|
191
|
+
return withReactions({
|
|
192
|
+
...base,
|
|
193
|
+
status: "awaiting-acceptance",
|
|
194
|
+
turn: "human",
|
|
195
|
+
gate: "acceptance",
|
|
196
|
+
next: {
|
|
197
|
+
kind: "gate",
|
|
198
|
+
text: `Read the execution record (${ended}) in ${reach.read}, ${reach.merge} to accept it, then run the command; or comment, or give a No-Go (it goes back to a step) ${reach.reactions ? "on the change or " : ""}with \`cupola-decide ${id}\`.`,
|
|
199
|
+
command: `cupola-complete ${id}`,
|
|
200
|
+
},
|
|
201
|
+
}, id, e.gate);
|
|
202
|
+
}
|
|
203
|
+
return operating(`Recorded ${ended}, but its change is not ready: a No-Go sent it back to a step, or the run stopped before marking it; carry on.`);
|
|
204
|
+
}
|
|
205
|
+
if (e.waits && e.waits.length > 0 && state.done.length === 0 && !state.stopped) {
|
|
206
|
+
return {
|
|
207
|
+
...base,
|
|
208
|
+
status: "waiting",
|
|
209
|
+
turn: "none",
|
|
210
|
+
gate: null,
|
|
211
|
+
next: {
|
|
212
|
+
kind: "nothing",
|
|
213
|
+
text: `It waits for ${e.waits.join(", ")}, earlier in ${front.for}'s procedures; it starts once they are done.`,
|
|
214
|
+
},
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
if (state.stopped) {
|
|
218
|
+
return {
|
|
219
|
+
...base,
|
|
220
|
+
status: "blocked",
|
|
221
|
+
turn: "human",
|
|
222
|
+
gate: null,
|
|
223
|
+
next: {
|
|
224
|
+
kind: "run",
|
|
225
|
+
text: `Stopped at ${state.stopped.step}: ${state.stopped.text} Fix the cause and resume from ${state.stopped.step}, or give it up (\`${operate} --give-up\`).`,
|
|
226
|
+
command: operate,
|
|
227
|
+
},
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
if (!state.next)
|
|
231
|
+
return operating("Every step is done; record the end and mark the change ready.");
|
|
232
|
+
return operating(state.done.length > 0
|
|
233
|
+
? `${state.done.join(", ")} done; next ${state.next}.`
|
|
234
|
+
: `Start at ${state.next}.`);
|
|
235
|
+
}
|
|
236
|
+
/** What `cupola check` lints in an execution record. */
|
|
237
|
+
export function checkExecution(file, raw, runbookSteps) {
|
|
238
|
+
const problems = [];
|
|
239
|
+
const p = (message, line) => problems.push({ file, ...(line ? { line } : {}), message });
|
|
240
|
+
let doc;
|
|
241
|
+
try {
|
|
242
|
+
doc = parseExecution(raw);
|
|
243
|
+
}
|
|
244
|
+
catch (error) {
|
|
245
|
+
return [{ file, message: error.message }];
|
|
246
|
+
}
|
|
247
|
+
const front = ExecutionFront.safeParse(doc.front);
|
|
248
|
+
if (!front.success) {
|
|
249
|
+
for (const issue of front.error.issues)
|
|
250
|
+
p(`front matter ${issue.path.join(".") || "(root)"}: ${issue.message}`);
|
|
251
|
+
}
|
|
252
|
+
const parts = file.split("/");
|
|
253
|
+
const n = parts.at(-1)?.replace(/\.md$/, "");
|
|
254
|
+
const runbook = parts.at(-3);
|
|
255
|
+
if (front.success) {
|
|
256
|
+
if (front.data.runbook !== runbook)
|
|
257
|
+
p(`runbook is ${front.data.runbook}, but its folder is ${runbook}`);
|
|
258
|
+
if (front.data.id !== `${runbook}-${n}`)
|
|
259
|
+
p(`id is ${front.data.id}, expected ${runbook}-${n}`);
|
|
260
|
+
}
|
|
261
|
+
for (const line of doc.placeholders)
|
|
262
|
+
p("template placeholder left ({{…}})", line);
|
|
263
|
+
if (runbookSteps) {
|
|
264
|
+
const { steps, problems: arranged } = arrangedSteps(runbookSteps, doc.arrangement);
|
|
265
|
+
for (const a of arranged)
|
|
266
|
+
p(a.message, a.line);
|
|
267
|
+
for (const r of doc.records)
|
|
268
|
+
if (!steps.includes(r.step))
|
|
269
|
+
p(`${r.step} is not a step of this execution`, r.line);
|
|
270
|
+
}
|
|
271
|
+
return problems;
|
|
272
|
+
}
|
package/dist/feedback.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { trustedBy } from "./trust.js";
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
3
|
+
* Whose remarks on a change count: the configured reviewer's (the review
|
|
4
|
+
* phase's findings) and those of people the configuration trusts (their
|
|
5
|
+
* reactions at a gate).
|
|
6
6
|
*/
|
|
7
7
|
/** Whether a remark's author is the reviewer asked for: a login, or `copilot` (a bot account). */
|
|
8
8
|
export function byReviewer(r, reviewer) {
|
|
@@ -18,17 +18,3 @@ export function byReviewer(r, reviewer) {
|
|
|
18
18
|
export function trusted(r, reviewer, trust) {
|
|
19
19
|
return trustedBy(trust, r) || byReviewer(r, reviewer);
|
|
20
20
|
}
|
|
21
|
-
/**
|
|
22
|
-
* Open feedback a person left that Cupola has not answered yet: its latest
|
|
23
|
-
* remark that counts is by someone trusted other than the reviewer. On a
|
|
24
|
-
* change at its gate, this is the human sending it back.
|
|
25
|
-
*/
|
|
26
|
-
export function fromPeople(items, reviewer, trust) {
|
|
27
|
-
const ok = (a) => trusted(a, reviewer, trust);
|
|
28
|
-
return items.filter((f) => {
|
|
29
|
-
if (!f.open || f.answered)
|
|
30
|
-
return false;
|
|
31
|
-
const last = [f, ...f.replies].filter(ok).at(-1);
|
|
32
|
-
return last !== undefined && !byReviewer(last, reviewer);
|
|
33
|
-
});
|
|
34
|
-
}
|
package/dist/host/git.js
ADDED
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
import { branchStore, pushProblem } from "../branch-store.js";
|
|
2
|
+
import { git } from "../git.js";
|
|
3
|
+
/** The branch of each repository on a git host that holds its changes. */
|
|
4
|
+
export const CHANGES_BRANCH = "cupola/changes";
|
|
5
|
+
const DIR = "changes";
|
|
6
|
+
const TIP = "refs/cupola/changes-tip";
|
|
7
|
+
/** The trailer of the commit a change is merged as, which tells it merged whatever happens to the branch. */
|
|
8
|
+
const TRAILER = "Cupola-Change";
|
|
9
|
+
/**
|
|
10
|
+
* A code host that is nothing but the repository's origin, on any git
|
|
11
|
+
* server. A change is a record on the `cupola/changes` branch: the branch it
|
|
12
|
+
* merges, into which base, its title, description, and whether it is a draft
|
|
13
|
+
* or closed. It is merged by `cupola change merge` as one squashed commit on
|
|
14
|
+
* the base, or by any merge that brings the branch into the base. It has no
|
|
15
|
+
* reviewers, no feedback, no reactions (people react in a session), and no CI.
|
|
16
|
+
*/
|
|
17
|
+
export function gitHost(root, slug) {
|
|
18
|
+
const store = branchStore(root, CHANGES_BRANCH, TIP);
|
|
19
|
+
const path = (n) => `${DIR}/${n}.json`;
|
|
20
|
+
async function records(at) {
|
|
21
|
+
if (!at)
|
|
22
|
+
return [];
|
|
23
|
+
const out = [];
|
|
24
|
+
for (const file of await store.list(at, DIR)) {
|
|
25
|
+
if (!file.endsWith(".json"))
|
|
26
|
+
continue;
|
|
27
|
+
const text = await store.read(at, file);
|
|
28
|
+
if (text !== undefined)
|
|
29
|
+
out.push(JSON.parse(text));
|
|
30
|
+
}
|
|
31
|
+
return out.sort((a, b) => a.number - b.number);
|
|
32
|
+
}
|
|
33
|
+
async function recordOf(number) {
|
|
34
|
+
const at = await store.tip();
|
|
35
|
+
const text = at ? await store.read(at, path(number)) : undefined;
|
|
36
|
+
if (text === undefined)
|
|
37
|
+
throw new Error(`no change #${number} in ${slug}`);
|
|
38
|
+
return JSON.parse(text);
|
|
39
|
+
}
|
|
40
|
+
/** Rewrites a change's record with `edit`, which says what it becomes (undefined: as it is). */
|
|
41
|
+
async function update(number, edit, message) {
|
|
42
|
+
await store.write(async (parent) => {
|
|
43
|
+
const text = parent ? await store.read(parent, path(number)) : undefined;
|
|
44
|
+
if (text === undefined)
|
|
45
|
+
throw new Error(`no change #${number} in ${slug}`);
|
|
46
|
+
const next = edit(JSON.parse(text));
|
|
47
|
+
return next === undefined ? undefined : { path: path(number), text: json(next) };
|
|
48
|
+
}, message);
|
|
49
|
+
}
|
|
50
|
+
/** origin's tip of a branch, fetched to its remote-tracking ref; undefined when origin has no such branch. */
|
|
51
|
+
async function fetched(branch) {
|
|
52
|
+
try {
|
|
53
|
+
await git(root, ["ls-remote", "--exit-code", "--heads", "origin", `refs/heads/${branch}`]);
|
|
54
|
+
}
|
|
55
|
+
catch (error) {
|
|
56
|
+
if (error.code === 2)
|
|
57
|
+
return undefined;
|
|
58
|
+
throw new Error(`cannot reach origin of ${slug}; try again when it is reachable`);
|
|
59
|
+
}
|
|
60
|
+
await git(root, [
|
|
61
|
+
"fetch",
|
|
62
|
+
"--quiet",
|
|
63
|
+
"origin",
|
|
64
|
+
`+refs/heads/${branch}:refs/remotes/origin/${branch}`,
|
|
65
|
+
]);
|
|
66
|
+
return git(root, ["rev-parse", `refs/remotes/origin/${branch}`]);
|
|
67
|
+
}
|
|
68
|
+
async function ancestor(a, b) {
|
|
69
|
+
try {
|
|
70
|
+
await git(root, ["merge-base", "--is-ancestor", a, b]);
|
|
71
|
+
return true;
|
|
72
|
+
}
|
|
73
|
+
catch {
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
/** The record as a change, its state derived: merged once the base holds it, however it got there. */
|
|
78
|
+
async function changeOf(r) {
|
|
79
|
+
const url = `${slug}#${r.number}`;
|
|
80
|
+
const head = await fetched(r.branch);
|
|
81
|
+
const base = r.state === "open" ? await fetched(r.base) : undefined;
|
|
82
|
+
let state = r.state;
|
|
83
|
+
if (state === "open" && base) {
|
|
84
|
+
const squashed = await git(root, [
|
|
85
|
+
"log",
|
|
86
|
+
"--format=%H",
|
|
87
|
+
"-1",
|
|
88
|
+
"-E",
|
|
89
|
+
`--grep=^${TRAILER}: ${escapeRegex(url)}$`,
|
|
90
|
+
base,
|
|
91
|
+
]);
|
|
92
|
+
if (squashed !== "" || (head && head !== r.fork && (await ancestor(head, base))))
|
|
93
|
+
state = "merged";
|
|
94
|
+
}
|
|
95
|
+
return {
|
|
96
|
+
number: r.number,
|
|
97
|
+
url,
|
|
98
|
+
title: r.title,
|
|
99
|
+
state,
|
|
100
|
+
draft: state === "open" && r.draft,
|
|
101
|
+
...(head ? { head } : {}),
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
return {
|
|
105
|
+
async findChange(branch) {
|
|
106
|
+
const mine = (await records(await store.tip())).filter((r) => r.branch === branch);
|
|
107
|
+
const latest = mine.at(-1);
|
|
108
|
+
return latest ? changeOf(latest) : undefined;
|
|
109
|
+
},
|
|
110
|
+
async openChange({ branch, base, title, body, draft }) {
|
|
111
|
+
const head = await fetched(branch);
|
|
112
|
+
if (!head)
|
|
113
|
+
throw new Error(`${branch} is not on origin of ${slug}: push it first`);
|
|
114
|
+
const onto = await fetched(base);
|
|
115
|
+
if (!onto)
|
|
116
|
+
throw new Error(`${base} is not on origin of ${slug}`);
|
|
117
|
+
const fork = await git(root, ["merge-base", onto, head]);
|
|
118
|
+
let opened;
|
|
119
|
+
await store.write(async (parent, retried) => {
|
|
120
|
+
const all = await records(parent);
|
|
121
|
+
// A retry after a push that failed to report may find the change opened already.
|
|
122
|
+
const there = all.find((r) => r.branch === branch && r.state === "open");
|
|
123
|
+
if (retried && there) {
|
|
124
|
+
opened = there;
|
|
125
|
+
return undefined;
|
|
126
|
+
}
|
|
127
|
+
const number = (all.at(-1)?.number ?? 0) + 1;
|
|
128
|
+
opened = { number, branch, base, title, body, draft, state: "open", fork };
|
|
129
|
+
return { path: path(number), text: json(opened) };
|
|
130
|
+
}, `change: open ${branch}`);
|
|
131
|
+
if (!opened)
|
|
132
|
+
throw new Error(`could not open a change for ${branch} in ${slug}`);
|
|
133
|
+
return changeOf(opened);
|
|
134
|
+
},
|
|
135
|
+
async body(number) {
|
|
136
|
+
return (await recordOf(number)).body;
|
|
137
|
+
},
|
|
138
|
+
async reach(_change, work, opts) {
|
|
139
|
+
// No page to open and nothing to react on: the CLI prints and merges the change.
|
|
140
|
+
const of = `${work}${opts?.release ? " --release" : ""}`;
|
|
141
|
+
return {
|
|
142
|
+
read: `\`cupola change show ${of}\``,
|
|
143
|
+
merge: `merge it with \`cupola change merge ${of}\``,
|
|
144
|
+
reactions: false,
|
|
145
|
+
};
|
|
146
|
+
},
|
|
147
|
+
async setBody(number, body) {
|
|
148
|
+
await update(number, (r) => (r.body === body ? undefined : { ...r, body }), `change: describe #${number}`);
|
|
149
|
+
},
|
|
150
|
+
async markReady(number) {
|
|
151
|
+
await update(number, (r) => (r.draft ? { ...r, draft: false } : undefined), `change: ready #${number}`);
|
|
152
|
+
},
|
|
153
|
+
async markDraft(number) {
|
|
154
|
+
await update(number, (r) => (r.draft ? undefined : { ...r, draft: true }), `change: draft #${number}`);
|
|
155
|
+
},
|
|
156
|
+
async closeChange(number, comment) {
|
|
157
|
+
await update(number, (r) => (r.state === "closed" ? undefined : { ...r, state: "closed", closed: comment }), `change: close #${number}`);
|
|
158
|
+
},
|
|
159
|
+
async requestReview() {
|
|
160
|
+
throw new Error(`${slug} is on a git host, which has no reviewers to ask: set review.reviewer to subagent`);
|
|
161
|
+
},
|
|
162
|
+
async feedback() {
|
|
163
|
+
return { feedback: [], answers: [] };
|
|
164
|
+
},
|
|
165
|
+
async answer(_number, id) {
|
|
166
|
+
throw new Error(`no feedback ${id} on a git host`);
|
|
167
|
+
},
|
|
168
|
+
async reactions() {
|
|
169
|
+
return [];
|
|
170
|
+
},
|
|
171
|
+
async reply(_number, id) {
|
|
172
|
+
throw new Error(`no reaction ${id} on a git host: people react in a session`);
|
|
173
|
+
},
|
|
174
|
+
async hasCi() {
|
|
175
|
+
return false;
|
|
176
|
+
},
|
|
177
|
+
async checks() {
|
|
178
|
+
return [];
|
|
179
|
+
},
|
|
180
|
+
async merge(number) {
|
|
181
|
+
const r = await recordOf(number);
|
|
182
|
+
const now = await changeOf(r);
|
|
183
|
+
if (now.state === "merged")
|
|
184
|
+
return;
|
|
185
|
+
if (now.state !== "open")
|
|
186
|
+
throw new Error(`${now.url} is closed`);
|
|
187
|
+
if (r.draft)
|
|
188
|
+
throw new Error(`${now.url} is a draft: it is not at its gate yet`);
|
|
189
|
+
const head = now.head;
|
|
190
|
+
if (!head)
|
|
191
|
+
throw new Error(`${r.branch} is not on origin of ${slug}`);
|
|
192
|
+
const base = await fetched(r.base);
|
|
193
|
+
if (!base)
|
|
194
|
+
throw new Error(`${r.base} is not on origin of ${slug}`);
|
|
195
|
+
let tree;
|
|
196
|
+
try {
|
|
197
|
+
tree =
|
|
198
|
+
(await git(root, ["merge-tree", "--write-tree", "--no-messages", base, head]))
|
|
199
|
+
.split("\n")[0]
|
|
200
|
+
?.trim() ?? "";
|
|
201
|
+
}
|
|
202
|
+
catch {
|
|
203
|
+
throw new Error(`${r.branch} conflicts with ${r.base} in ${slug}: bring ${r.base} into ${r.branch} first`);
|
|
204
|
+
}
|
|
205
|
+
const message = `${r.title}\n\n${r.body.trim()}\n\n${TRAILER}: ${now.url}\n`;
|
|
206
|
+
const commit = await git(root, ["commit-tree", tree, "-p", base, "-m", message]);
|
|
207
|
+
try {
|
|
208
|
+
await git(root, ["push", "--quiet", "origin", `${commit}:refs/heads/${r.base}`]);
|
|
209
|
+
}
|
|
210
|
+
catch (error) {
|
|
211
|
+
const stderr = error.stderr ?? "";
|
|
212
|
+
throw new Error(`could not push ${r.base} of ${slug} (${pushProblem(stderr)}); if it moved on, merge again`);
|
|
213
|
+
}
|
|
214
|
+
// The trailer already tells it merged; the record says so too, and with which commit.
|
|
215
|
+
await update(number, (x) => (x.state === "merged" ? undefined : { ...x, state: "merged", merged: commit }), `change: merged #${number}`);
|
|
216
|
+
},
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
function escapeRegex(text) {
|
|
220
|
+
return text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
221
|
+
}
|
|
222
|
+
function json(r) {
|
|
223
|
+
return `${JSON.stringify(r, null, 2)}\n`;
|
|
224
|
+
}
|