@kyuki3rain/cupola 0.0.0-stage → 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 +184 -0
- package/LICENSE +202 -0
- package/README.md +41 -2
- package/dist/adr.js +106 -0
- package/dist/branch-store.js +73 -0
- package/dist/catalog.js +137 -0
- package/dist/check.js +192 -0
- package/dist/cli.js +8 -0
- package/dist/code.js +216 -0
- package/dist/commands/change.js +728 -0
- package/dist/commands/epic.js +311 -0
- package/dist/commands/execution.js +482 -0
- package/dist/commands/install.js +109 -0
- package/dist/commands/locks.js +243 -0
- package/dist/commands/reaction.js +185 -0
- package/dist/commands/revision.js +139 -0
- package/dist/commands/run.js +1021 -0
- package/dist/commands/setup.js +414 -0
- package/dist/commands/spec.js +80 -0
- package/dist/commands/status.js +1457 -0
- package/dist/commands/trace.js +474 -0
- package/dist/commands/wait.js +105 -0
- package/dist/commands/work.js +126 -0
- package/dist/commands/workspace.js +137 -0
- package/dist/commands/worktree.js +422 -0
- package/dist/config.js +353 -0
- package/dist/day.js +29 -0
- package/dist/e2e-project.js +62 -0
- package/dist/e2e.js +207 -0
- package/dist/engine/cc-sdd.js +164 -0
- package/dist/epic.js +379 -0
- package/dist/execution.js +272 -0
- package/dist/feedback.js +20 -0
- package/dist/frontmatter.js +20 -0
- package/dist/git.js +365 -0
- package/dist/host/git.js +224 -0
- package/dist/host/github.js +387 -0
- package/dist/host/host.js +3 -0
- package/dist/host/index.js +63 -0
- package/dist/install.js +182 -0
- package/dist/io.js +46 -0
- package/dist/main.js +866 -0
- package/dist/method/v1.js +56 -0
- package/dist/reaction.js +110 -0
- package/dist/record.js +507 -0
- package/dist/release.js +84 -0
- package/dist/repo.js +13 -0
- package/dist/runbook.js +132 -0
- package/dist/safe-write.js +80 -0
- package/dist/spec.js +171 -0
- package/dist/status.js +321 -0
- package/dist/templates.js +50 -0
- package/dist/trace.js +275 -0
- package/dist/tracker/files.js +99 -0
- package/dist/tracker/github-issues.js +217 -0
- package/dist/tracker/index.js +34 -0
- package/dist/tracker/tracker.js +69 -0
- package/dist/tree.js +59 -0
- package/dist/trust.js +13 -0
- package/dist/version.js +5 -0
- package/dist/work.js +170 -0
- package/dist/workspace.js +324 -0
- package/npm-shrinkwrap.json +2014 -0
- package/package.json +41 -4
- package/skills/cupola-abort/SKILL.md +51 -0
- package/skills/cupola-adr/SKILL.md +54 -0
- package/skills/cupola-ask/SKILL.md +42 -0
- package/skills/cupola-catalog/SKILL.md +41 -0
- package/skills/cupola-catalog-draft/SKILL.md +116 -0
- package/skills/cupola-complete/SKILL.md +107 -0
- package/skills/cupola-decide/SKILL.md +99 -0
- package/skills/cupola-doc/SKILL.md +52 -0
- package/skills/cupola-e2e-setup/SKILL.md +78 -0
- package/skills/cupola-e2e-test/SKILL.md +115 -0
- package/skills/cupola-epic-accept/SKILL.md +166 -0
- package/skills/cupola-epic-create/SKILL.md +160 -0
- package/skills/cupola-epic-revise/SKILL.md +92 -0
- package/skills/cupola-epic-run/SKILL.md +166 -0
- package/skills/cupola-fix/SKILL.md +117 -0
- package/skills/cupola-init/SKILL.md +143 -0
- package/skills/cupola-intake/SKILL.md +95 -0
- package/skills/cupola-operate/SKILL.md +81 -0
- package/skills/cupola-resolve/SKILL.md +93 -0
- package/skills/cupola-respond/SKILL.md +73 -0
- package/skills/cupola-review/SKILL.md +131 -0
- package/skills/cupola-spec-design/SKILL.md +169 -0
- package/skills/cupola-spec-impl/SKILL.md +137 -0
- package/skills/cupola-spec-run/SKILL.md +137 -0
- package/skills/cupola-steering/SKILL.md +53 -0
- package/skills/cupola-try/SKILL.md +38 -0
- package/templates/install/.kiro/steering/e2e.md +48 -0
- package/templates/install/.kiro/steering/operations.md +109 -0
- package/templates/install/.kiro/steering/principles.md +25 -0
- package/templates/install/CLAUDE.md +19 -0
- package/templates/install/adr/TEMPLATE.md +38 -0
- package/templates/install/cc-sdd/patches.json +31 -0
- package/templates/method-1/behavior-area.md +43 -0
- package/templates/method-1/behavior-index.md +16 -0
- package/templates/method-1/card.md +49 -0
- package/templates/method-1/case-draft.md +25 -0
- package/templates/method-1/case.md +38 -0
- package/templates/method-1/catalog-spec.md +33 -0
- package/templates/method-1/changes/adr.md +4 -0
- package/templates/method-1/changes/catalog.md +29 -0
- package/templates/method-1/changes/doc.md +5 -0
- package/templates/method-1/changes/e2e.md +7 -0
- package/templates/method-1/changes/epic.md +30 -0
- package/templates/method-1/changes/fix.md +21 -0
- package/templates/method-1/changes/release.md +23 -0
- package/templates/method-1/changes/spec.md +55 -0
- package/templates/method-1/changes/system.md +13 -0
- package/templates/method-1/cupola-issue-draft.md +17 -0
- package/templates/method-1/epic.md +147 -0
- package/templates/method-1/execution.md +55 -0
- package/templates/method-1/fix-spec.md +34 -0
- package/templates/method-1/fix.md +39 -0
- package/templates/method-1/lock.json +13 -0
- package/templates/method-1/no-go.md +28 -0
- package/templates/method-1/phase-outcome.json +29 -0
- package/templates/method-1/reaction.md +17 -0
- package/templates/method-1/record.json +63 -0
- package/templates/method-1/report.md +27 -0
- package/templates/method-1/runbook.md +45 -0
- package/templates/method-1/runner-result.json +6 -0
- package/templates/methods.json +13 -0
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
|
+
}
|
|
@@ -0,0 +1,387 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
import { mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { promisify } from "node:util";
|
|
6
|
+
const exec = promisify(execFile);
|
|
7
|
+
export async function gh(cwd, args, accept) {
|
|
8
|
+
try {
|
|
9
|
+
const { stdout } = await exec("gh", args, { cwd, maxBuffer: 64 * 1024 * 1024 });
|
|
10
|
+
return stdout.trim();
|
|
11
|
+
}
|
|
12
|
+
catch (error) {
|
|
13
|
+
const e = error;
|
|
14
|
+
if (typeof e.code === "number" && accept?.(e.code, e.stderr ?? ""))
|
|
15
|
+
return (e.stdout ?? "").trim();
|
|
16
|
+
if (e.code === "ENOENT") {
|
|
17
|
+
throw new Error("the GitHub CLI (gh) is not installed; install it and run `gh auth login`");
|
|
18
|
+
}
|
|
19
|
+
const detail = (e.stderr ?? "").trim() || "gh failed without a message";
|
|
20
|
+
const auth = /auth|login|token|credential/i.test(detail) ? " (see `gh auth status`)" : "";
|
|
21
|
+
throw new Error(`GitHub: ${detail}${auth}`);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
/** Runs gh with a description passed as a file, never through a pipe. */
|
|
25
|
+
export async function withBody(body, f) {
|
|
26
|
+
const dir = await mkdtemp(join(tmpdir(), "cupola-body-"));
|
|
27
|
+
try {
|
|
28
|
+
const file = join(dir, "body.md");
|
|
29
|
+
await writeFile(file, body);
|
|
30
|
+
return await f(file);
|
|
31
|
+
}
|
|
32
|
+
finally {
|
|
33
|
+
await rm(dir, { recursive: true, force: true });
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
/** The owner in a repository URL of any form: https, ssh, scp-like, host aliases, enterprise hosts. */
|
|
37
|
+
export function ownerOf(url) {
|
|
38
|
+
return /[:/]([^/:]+)\/[^/]+?(?:\.git)?\/?$/.exec(url.trim())?.[1]?.toLowerCase();
|
|
39
|
+
}
|
|
40
|
+
/** `owner/repo` of a repository URL of any form, lowercased. */
|
|
41
|
+
export function repoOf(url) {
|
|
42
|
+
const m = /[:/]([^/:]+)\/([^/]+?)(?:\.git)?\/?$/.exec(url.trim());
|
|
43
|
+
return m ? `${m[1]}/${m[2]}`.toLowerCase() : undefined;
|
|
44
|
+
}
|
|
45
|
+
/** The owner of origin's repository, which is where work branches are pushed. */
|
|
46
|
+
async function originOwner(cwd) {
|
|
47
|
+
const { stdout } = await exec("git", ["remote", "get-url", "origin"], { cwd });
|
|
48
|
+
const owner = ownerOf(stdout);
|
|
49
|
+
if (!owner)
|
|
50
|
+
throw new Error(`cannot tell the owner of origin from ${stdout.trim()}`);
|
|
51
|
+
return owner;
|
|
52
|
+
}
|
|
53
|
+
/** Hidden in every answer Cupola writes, so that it can tell its answers from anyone else's. */
|
|
54
|
+
const ANSWER = "<!-- cupola:answer -->";
|
|
55
|
+
/** Hidden in the comment that settles a review's remark, which GitHub cannot resolve itself. */
|
|
56
|
+
const settledMark = (id) => `<!-- cupola:settled ${id} -->`;
|
|
57
|
+
/** Hidden in the comment that answers a review's remark. */
|
|
58
|
+
const onMark = (id) => `<!-- cupola:on ${id} -->`;
|
|
59
|
+
const FEEDBACK_QUERY = `query($owner: String!, $repo: String!, $number: Int!) {
|
|
60
|
+
repository(owner: $owner, name: $repo) {
|
|
61
|
+
pullRequest(number: $number) {
|
|
62
|
+
reviews(first: 100) {
|
|
63
|
+
nodes { id state author { login __typename } authorAssociation body submittedAt }
|
|
64
|
+
pageInfo { hasNextPage }
|
|
65
|
+
}
|
|
66
|
+
reviewThreads(first: 100) {
|
|
67
|
+
nodes {
|
|
68
|
+
id isResolved path line originalLine
|
|
69
|
+
comments(first: 100) {
|
|
70
|
+
nodes { author { login __typename } authorAssociation body createdAt }
|
|
71
|
+
pageInfo { hasNextPage }
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
pageInfo { hasNextPage }
|
|
75
|
+
}
|
|
76
|
+
comments(first: 100) {
|
|
77
|
+
nodes { id author { login __typename } authorAssociation body createdAt }
|
|
78
|
+
pageInfo { hasNextPage }
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}`;
|
|
83
|
+
function who(r) {
|
|
84
|
+
return {
|
|
85
|
+
author: r.author?.login ?? "",
|
|
86
|
+
association: r.authorAssociation || "NONE",
|
|
87
|
+
bot: r.author?.__typename === "Bot",
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* The review states whose remark asks for something. An approval's remark
|
|
92
|
+
* ("LGTM") sends nothing back; a dismissed or pending review says nothing yet.
|
|
93
|
+
*/
|
|
94
|
+
const ASKING = new Set(["COMMENTED", "CHANGES_REQUESTED"]);
|
|
95
|
+
function allRead(pr) {
|
|
96
|
+
const pages = [
|
|
97
|
+
pr.reviews,
|
|
98
|
+
pr.reviewThreads,
|
|
99
|
+
pr.comments,
|
|
100
|
+
...pr.reviewThreads.nodes.map((t) => t.comments),
|
|
101
|
+
];
|
|
102
|
+
if (pages.some((p) => p.pageInfo?.hasNextPage)) {
|
|
103
|
+
throw new Error("the change has more than 100 reviews, threads, or comments; Cupola cannot read them all yet");
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* GitHub's review threads and review bodies as feedback for the review phase
|
|
108
|
+
* (see `Feedback`). A mark settles or answers only when someone `trusted`
|
|
109
|
+
* wrote it.
|
|
110
|
+
*/
|
|
111
|
+
export function feedbackOf(pr, trusted = () => true) {
|
|
112
|
+
allRead(pr);
|
|
113
|
+
const said = pr.comments.nodes.filter((c) => trusted(who(c))).map((c) => c.body);
|
|
114
|
+
const has = (mark) => said.some((b) => b.includes(mark));
|
|
115
|
+
const threads = pr.reviewThreads.nodes.flatMap((t) => {
|
|
116
|
+
const [first, ...rest] = t.comments.nodes;
|
|
117
|
+
if (!first)
|
|
118
|
+
return [];
|
|
119
|
+
// The latest remark that counts: an outsider's reply neither answers nor reopens it.
|
|
120
|
+
const last = [first, ...rest].filter((c) => trusted(who(c))).at(-1);
|
|
121
|
+
return [
|
|
122
|
+
{
|
|
123
|
+
id: `thread:${t.id}`,
|
|
124
|
+
...who(first),
|
|
125
|
+
body: first.body,
|
|
126
|
+
at: first.createdAt,
|
|
127
|
+
place: { path: t.path, line: t.line ?? t.originalLine ?? null },
|
|
128
|
+
replies: rest.map((c) => ({ ...who(c), body: c.body, at: c.createdAt })),
|
|
129
|
+
open: !t.isResolved,
|
|
130
|
+
answered: last?.body.includes(ANSWER) ?? false,
|
|
131
|
+
},
|
|
132
|
+
];
|
|
133
|
+
});
|
|
134
|
+
// A review's own remark is on the whole change; GitHub keeps no thread for it.
|
|
135
|
+
// An approval still answers a request (below), but its remark is not feedback.
|
|
136
|
+
const bodies = pr.reviews.nodes
|
|
137
|
+
.filter((r) => ASKING.has(r.state) && r.body.trim() !== "" && r.submittedAt)
|
|
138
|
+
.map((r) => {
|
|
139
|
+
const id = `review:${r.id}`;
|
|
140
|
+
return {
|
|
141
|
+
id,
|
|
142
|
+
...who(r),
|
|
143
|
+
body: r.body,
|
|
144
|
+
at: r.submittedAt ?? "",
|
|
145
|
+
place: null,
|
|
146
|
+
replies: [],
|
|
147
|
+
open: !has(settledMark(id)),
|
|
148
|
+
answered: has(onMark(id)),
|
|
149
|
+
};
|
|
150
|
+
});
|
|
151
|
+
const answers = pr.reviews.nodes
|
|
152
|
+
.filter((r) => r.submittedAt)
|
|
153
|
+
.map((r) => ({ ...who(r), at: r.submittedAt ?? "" }));
|
|
154
|
+
return { feedback: [...threads, ...bodies].sort((a, b) => a.at.localeCompare(b.at)), answers };
|
|
155
|
+
}
|
|
156
|
+
/** A remark that asks for a try: `/cupola try` at its start. */
|
|
157
|
+
const TRY = /^\s*\/cupola\s+try\b/i;
|
|
158
|
+
const kindOf = (body) => (TRY.test(body) ? "try" : "comment");
|
|
159
|
+
/**
|
|
160
|
+
* GitHub's reviews, review threads, and comments as reactions: a review that
|
|
161
|
+
* requests changes is a No-Go (with or without words); a comment, a review
|
|
162
|
+
* thread, or a review that comments is a comment — a try when it starts with
|
|
163
|
+
* `/cupola try`; an approval is none (merging is going on). Cupola's own
|
|
164
|
+
* answers are none either. A reply answers one only when someone `trusted`
|
|
165
|
+
* wrote it.
|
|
166
|
+
*/
|
|
167
|
+
export function reactionsOf(pr, trusted = () => true) {
|
|
168
|
+
allRead(pr);
|
|
169
|
+
const said = pr.comments.nodes.filter((c) => trusted(who(c))).map((c) => c.body);
|
|
170
|
+
const has = (mark) => said.some((b) => b.includes(mark));
|
|
171
|
+
const threads = pr.reviewThreads.nodes.flatMap((t) => {
|
|
172
|
+
const [first, ...rest] = t.comments.nodes;
|
|
173
|
+
if (!first || first.body.includes(ANSWER))
|
|
174
|
+
return [];
|
|
175
|
+
const last = [first, ...rest].filter((c) => trusted(who(c))).at(-1);
|
|
176
|
+
return [
|
|
177
|
+
{
|
|
178
|
+
id: `thread:${t.id}`,
|
|
179
|
+
...who(first),
|
|
180
|
+
at: first.createdAt,
|
|
181
|
+
kind: kindOf(first.body),
|
|
182
|
+
body: first.body,
|
|
183
|
+
place: { path: t.path, line: t.line ?? t.originalLine ?? null },
|
|
184
|
+
// A thread someone resolved waits for nothing either.
|
|
185
|
+
answered: t.isResolved || (last?.body.includes(ANSWER) ?? false),
|
|
186
|
+
},
|
|
187
|
+
];
|
|
188
|
+
});
|
|
189
|
+
const reviews = pr.reviews.nodes.flatMap((r) => {
|
|
190
|
+
if (!r.submittedAt)
|
|
191
|
+
return [];
|
|
192
|
+
const noGo = r.state === "CHANGES_REQUESTED";
|
|
193
|
+
if (!noGo && (r.state !== "COMMENTED" || r.body.trim() === ""))
|
|
194
|
+
return [];
|
|
195
|
+
const id = `review:${r.id}`;
|
|
196
|
+
return [
|
|
197
|
+
{
|
|
198
|
+
id,
|
|
199
|
+
...who(r),
|
|
200
|
+
at: r.submittedAt,
|
|
201
|
+
kind: noGo ? "no-go" : kindOf(r.body),
|
|
202
|
+
body: r.body,
|
|
203
|
+
place: null,
|
|
204
|
+
answered: has(onMark(id)),
|
|
205
|
+
},
|
|
206
|
+
];
|
|
207
|
+
});
|
|
208
|
+
const comments = pr.comments.nodes.flatMap((c) => {
|
|
209
|
+
if (!c.id || c.body.includes(ANSWER))
|
|
210
|
+
return [];
|
|
211
|
+
const id = `comment:${c.id}`;
|
|
212
|
+
return [
|
|
213
|
+
{
|
|
214
|
+
id,
|
|
215
|
+
...who(c),
|
|
216
|
+
at: c.createdAt ?? "",
|
|
217
|
+
kind: kindOf(c.body),
|
|
218
|
+
body: c.body,
|
|
219
|
+
place: null,
|
|
220
|
+
answered: has(onMark(id)),
|
|
221
|
+
},
|
|
222
|
+
];
|
|
223
|
+
});
|
|
224
|
+
return [...threads, ...reviews, ...comments].sort((a, b) => a.at.localeCompare(b.at));
|
|
225
|
+
}
|
|
226
|
+
function toChange(pr) {
|
|
227
|
+
return {
|
|
228
|
+
number: pr.number,
|
|
229
|
+
url: pr.url,
|
|
230
|
+
...(pr.title !== undefined ? { title: pr.title } : {}),
|
|
231
|
+
state: pr.state.toLowerCase(),
|
|
232
|
+
draft: pr.isDraft,
|
|
233
|
+
...(pr.headRefOid ? { head: pr.headRefOid } : {}),
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
const FIELDS = "number,url,title,state,isDraft,headRepositoryOwner,headRefOid";
|
|
237
|
+
/** GitHub through the `gh` CLI, run in the repository so that it targets its remote. */
|
|
238
|
+
export function github(cwd) {
|
|
239
|
+
async function pullRequest(number) {
|
|
240
|
+
const out = JSON.parse(await gh(cwd, [
|
|
241
|
+
"api",
|
|
242
|
+
"graphql",
|
|
243
|
+
"-f",
|
|
244
|
+
`query=${FEEDBACK_QUERY}`,
|
|
245
|
+
"-F",
|
|
246
|
+
"owner={owner}",
|
|
247
|
+
"-F",
|
|
248
|
+
"repo={repo}",
|
|
249
|
+
"-F",
|
|
250
|
+
`number=${number}`,
|
|
251
|
+
]));
|
|
252
|
+
return out.data.repository.pullRequest;
|
|
253
|
+
}
|
|
254
|
+
/** Answers in a thread; anything else (a review, a comment) with a comment that names it. */
|
|
255
|
+
async function answer(number, id, body, settle) {
|
|
256
|
+
const [kind, node] = [id.slice(0, id.indexOf(":")), id.slice(id.indexOf(":") + 1)];
|
|
257
|
+
if (kind === "thread") {
|
|
258
|
+
await gh(cwd, [
|
|
259
|
+
"api",
|
|
260
|
+
"graphql",
|
|
261
|
+
"-f",
|
|
262
|
+
"query=mutation($id: ID!, $body: String!) { addPullRequestReviewThreadReply(input: {pullRequestReviewThreadId: $id, body: $body}) { comment { id } } }",
|
|
263
|
+
"-f",
|
|
264
|
+
`id=${node}`,
|
|
265
|
+
"-f",
|
|
266
|
+
`body=${body}\n\n${ANSWER}`,
|
|
267
|
+
]);
|
|
268
|
+
if (settle) {
|
|
269
|
+
await gh(cwd, [
|
|
270
|
+
"api",
|
|
271
|
+
"graphql",
|
|
272
|
+
"-f",
|
|
273
|
+
"query=mutation($id: ID!) { resolveReviewThread(input: {threadId: $id}) { thread { id } } }",
|
|
274
|
+
"-f",
|
|
275
|
+
`id=${node}`,
|
|
276
|
+
]);
|
|
277
|
+
}
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
if (kind !== "review" && kind !== "comment")
|
|
281
|
+
throw new Error(`no feedback ${id} on GitHub`);
|
|
282
|
+
const marks = `${ANSWER} ${onMark(id)}${settle ? ` ${settledMark(id)}` : ""}`;
|
|
283
|
+
await withBody(`${body}\n\n${marks}`, (file) => gh(cwd, ["pr", "comment", String(number), "--body-file", file]));
|
|
284
|
+
}
|
|
285
|
+
return {
|
|
286
|
+
async findChange(branch) {
|
|
287
|
+
const owner = await originOwner(cwd);
|
|
288
|
+
const out = await gh(cwd, [
|
|
289
|
+
"pr",
|
|
290
|
+
"list",
|
|
291
|
+
"--head",
|
|
292
|
+
branch,
|
|
293
|
+
"--state",
|
|
294
|
+
"all",
|
|
295
|
+
"--limit",
|
|
296
|
+
"20",
|
|
297
|
+
"--json",
|
|
298
|
+
FIELDS,
|
|
299
|
+
]);
|
|
300
|
+
// Another fork may have a branch of the same name; only origin's is this work's.
|
|
301
|
+
const pr = JSON.parse(out).find((p) => p.headRepositoryOwner?.login.toLowerCase() === owner);
|
|
302
|
+
return pr ? toChange(pr) : undefined;
|
|
303
|
+
},
|
|
304
|
+
async openChange({ branch, base, title, body, draft }) {
|
|
305
|
+
const owner = await originOwner(cwd);
|
|
306
|
+
const url = await withBody(body, (file) => {
|
|
307
|
+
const args = [
|
|
308
|
+
"pr",
|
|
309
|
+
"create",
|
|
310
|
+
"--head",
|
|
311
|
+
`${owner}:${branch}`,
|
|
312
|
+
"--base",
|
|
313
|
+
base,
|
|
314
|
+
"--title",
|
|
315
|
+
title,
|
|
316
|
+
"--body-file",
|
|
317
|
+
file,
|
|
318
|
+
];
|
|
319
|
+
if (draft)
|
|
320
|
+
args.push("--draft");
|
|
321
|
+
return gh(cwd, args);
|
|
322
|
+
});
|
|
323
|
+
const created = url.split("\n").at(-1) ?? url;
|
|
324
|
+
return toChange(JSON.parse(await gh(cwd, ["pr", "view", created, "--json", FIELDS])));
|
|
325
|
+
},
|
|
326
|
+
async body(number) {
|
|
327
|
+
return JSON.parse(await gh(cwd, ["pr", "view", String(number), "--json", "body"]))
|
|
328
|
+
.body;
|
|
329
|
+
},
|
|
330
|
+
async reach(change) {
|
|
331
|
+
return { read: change.url, merge: "merge it", reactions: true };
|
|
332
|
+
},
|
|
333
|
+
async setBody(number, body) {
|
|
334
|
+
await withBody(body, (file) => gh(cwd, ["pr", "edit", String(number), "--body-file", file]));
|
|
335
|
+
},
|
|
336
|
+
async markReady(number) {
|
|
337
|
+
await gh(cwd, ["pr", "ready", String(number)]);
|
|
338
|
+
},
|
|
339
|
+
async markDraft(number) {
|
|
340
|
+
await gh(cwd, ["pr", "ready", String(number), "--undo"]);
|
|
341
|
+
},
|
|
342
|
+
async closeChange(number, comment) {
|
|
343
|
+
// One call, so that a retry after a failure does not post the comment twice.
|
|
344
|
+
await gh(cwd, ["pr", "close", String(number), "--comment", comment]);
|
|
345
|
+
},
|
|
346
|
+
async requestReview(number, reviewer) {
|
|
347
|
+
const who = reviewer === "copilot" ? "@copilot" : reviewer;
|
|
348
|
+
await gh(cwd, ["pr", "edit", String(number), "--add-reviewer", who]);
|
|
349
|
+
},
|
|
350
|
+
async feedback(number, trusted) {
|
|
351
|
+
return feedbackOf(await pullRequest(number), trusted);
|
|
352
|
+
},
|
|
353
|
+
async reactions(number, trusted) {
|
|
354
|
+
return reactionsOf(await pullRequest(number), trusted);
|
|
355
|
+
},
|
|
356
|
+
async reply(number, id, body) {
|
|
357
|
+
await answer(number, id, body, false);
|
|
358
|
+
},
|
|
359
|
+
async answer(number, id, body, settle) {
|
|
360
|
+
await answer(number, id, body, settle);
|
|
361
|
+
},
|
|
362
|
+
async hasCi() {
|
|
363
|
+
// GitHub also lists workflows it runs itself (e.g. the Copilot reviewer, under
|
|
364
|
+
// `dynamic/`); only files in .github/workflows/ are the repository's CI.
|
|
365
|
+
const out = await gh(cwd, [
|
|
366
|
+
"api",
|
|
367
|
+
"--paginate",
|
|
368
|
+
"repos/{owner}/{repo}/actions/workflows",
|
|
369
|
+
"--jq",
|
|
370
|
+
'.workflows[] | select(.state == "active") | .path',
|
|
371
|
+
]);
|
|
372
|
+
return out.split("\n").some((path) => path.startsWith(".github/workflows/"));
|
|
373
|
+
},
|
|
374
|
+
async checks(number) {
|
|
375
|
+
// gh exits 8 while checks are pending, and 1 when one failed or when there are none;
|
|
376
|
+
// any other exit 1 (authentication, network) is a failure, not "no checks".
|
|
377
|
+
const out = await gh(cwd, ["pr", "checks", String(number), "--json", "name,bucket"], (code, stderr) => code === 8 ||
|
|
378
|
+
(code === 1 && (/no checks reported/i.test(stderr) || stderr.trim() === "")));
|
|
379
|
+
if (out === "")
|
|
380
|
+
return [];
|
|
381
|
+
return JSON.parse(out).map((c) => ({
|
|
382
|
+
name: c.name,
|
|
383
|
+
state: c.bucket,
|
|
384
|
+
}));
|
|
385
|
+
},
|
|
386
|
+
};
|
|
387
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { ConfigError, hostKindOf, loadConfig } from "../config.js";
|
|
2
|
+
import { repoRoot } from "../repo.js";
|
|
3
|
+
import { identifyRepo } from "../workspace.js";
|
|
4
|
+
import { gitHost } from "./git.js";
|
|
5
|
+
import { github } from "./github.js";
|
|
6
|
+
import { MergedOnHost } from "./host.js";
|
|
7
|
+
/** The adapter of a host kind for a repository's checkout. */
|
|
8
|
+
export function hostOf(kind, dir, slug) {
|
|
9
|
+
return kind === "git" ? gitHost(dir, slug) : github(dir);
|
|
10
|
+
}
|
|
11
|
+
/** The host of a configured repository, through its checkout. */
|
|
12
|
+
export function hostFor(config, dir, slug) {
|
|
13
|
+
return hostOf(hostKindOf(config, slug), dir, slug);
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* The code host of the repository checked out at `dir`, by its
|
|
17
|
+
* configuration: which repository it is, and that repository's host. Resolved
|
|
18
|
+
* on the first call, so that making it costs nothing. Without a configuration,
|
|
19
|
+
* GitHub.
|
|
20
|
+
*/
|
|
21
|
+
export function codeHost(dir) {
|
|
22
|
+
let resolved;
|
|
23
|
+
const host = () => {
|
|
24
|
+
resolved ??= (async () => {
|
|
25
|
+
const root = await repoRoot(dir);
|
|
26
|
+
let config;
|
|
27
|
+
try {
|
|
28
|
+
config = await loadConfig(root);
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
if (error instanceof ConfigError && error.problems.length === 0)
|
|
32
|
+
return github(dir);
|
|
33
|
+
throw error;
|
|
34
|
+
}
|
|
35
|
+
const slug = (await identifyRepo(root, config)) ?? config.hub;
|
|
36
|
+
return hostFor(config, dir, slug);
|
|
37
|
+
})();
|
|
38
|
+
return resolved;
|
|
39
|
+
};
|
|
40
|
+
return {
|
|
41
|
+
findChange: async (...a) => (await host()).findChange(...a),
|
|
42
|
+
openChange: async (...a) => (await host()).openChange(...a),
|
|
43
|
+
body: async (...a) => (await host()).body(...a),
|
|
44
|
+
reach: async (...a) => (await host()).reach(...a),
|
|
45
|
+
setBody: async (...a) => (await host()).setBody(...a),
|
|
46
|
+
markReady: async (...a) => (await host()).markReady(...a),
|
|
47
|
+
closeChange: async (...a) => (await host()).closeChange(...a),
|
|
48
|
+
markDraft: async (...a) => (await host()).markDraft(...a),
|
|
49
|
+
requestReview: async (...a) => (await host()).requestReview(...a),
|
|
50
|
+
feedback: async (...a) => (await host()).feedback(...a),
|
|
51
|
+
answer: async (...a) => (await host()).answer(...a),
|
|
52
|
+
reactions: async (...a) => (await host()).reactions(...a),
|
|
53
|
+
reply: async (...a) => (await host()).reply(...a),
|
|
54
|
+
hasCi: async () => (await host()).hasCi(),
|
|
55
|
+
checks: async (...a) => (await host()).checks(...a),
|
|
56
|
+
merge: async (number) => {
|
|
57
|
+
const h = await host();
|
|
58
|
+
if (!h.merge)
|
|
59
|
+
throw new MergedOnHost("changes on this host are merged on the host itself");
|
|
60
|
+
return h.merge(number);
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
}
|