@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
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { branchStore } from "../branch-store.js";
|
|
2
|
+
import { parseFrontMatter } from "../frontmatter.js";
|
|
3
|
+
import { git } from "../git.js";
|
|
4
|
+
import { appendLog, CaseError, closeCase, parseCase, } from "./tracker.js";
|
|
5
|
+
/** The hub's branch that holds cases, committed and pushed to directly. */
|
|
6
|
+
export const WORK_BRANCH = "cupola/work";
|
|
7
|
+
const DIR = ".cupola/work";
|
|
8
|
+
const TIP = "refs/cupola/work-tip";
|
|
9
|
+
/**
|
|
10
|
+
* The `files` tracker: cases as Markdown under `.cupola/work/` on the hub's
|
|
11
|
+
* `cupola/work` branch. Writes go through a temporary index straight to a
|
|
12
|
+
* commit, so no checkout is touched, and are pushed at once so that a filed
|
|
13
|
+
* case is visible to everyone.
|
|
14
|
+
*/
|
|
15
|
+
export function filesTracker(root) {
|
|
16
|
+
const store = branchStore(root, WORK_BRANCH, TIP);
|
|
17
|
+
const tip = () => store.tip();
|
|
18
|
+
const read = (commit, id) => store.read(commit, `${DIR}/${id}.md`);
|
|
19
|
+
const summary = (id, text) => {
|
|
20
|
+
const { data } = parseFrontMatter(text);
|
|
21
|
+
return {
|
|
22
|
+
id,
|
|
23
|
+
title: typeof data.title === "string" ? data.title : "",
|
|
24
|
+
url: `${WORK_BRANCH}:${DIR}/${id}.md`,
|
|
25
|
+
open: !data.closed,
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Commits the case `id` on top of the tip and pushes it, retried when
|
|
30
|
+
* someone pushed first. `change` gives the new text, or undefined when it is
|
|
31
|
+
* already there — after a retry, a push that failed to report may have landed.
|
|
32
|
+
*/
|
|
33
|
+
async function write(id, change, message) {
|
|
34
|
+
await store.write(async (parent, retried) => {
|
|
35
|
+
const text = change(parent ? await read(parent, id) : undefined, retried);
|
|
36
|
+
return text === undefined ? undefined : { path: `${DIR}/${id}.md`, text };
|
|
37
|
+
}, message);
|
|
38
|
+
}
|
|
39
|
+
return {
|
|
40
|
+
async list() {
|
|
41
|
+
const at = await tip();
|
|
42
|
+
if (!at)
|
|
43
|
+
return [];
|
|
44
|
+
const names = (await git(root, ["ls-tree", "--name-only", at, `${DIR}/`]))
|
|
45
|
+
.split("\n")
|
|
46
|
+
.filter((p) => p.endsWith(".md"));
|
|
47
|
+
const cases = [];
|
|
48
|
+
for (const path of names) {
|
|
49
|
+
const id = path.slice(DIR.length + 1, -".md".length);
|
|
50
|
+
const text = await read(at, id);
|
|
51
|
+
if (text !== undefined)
|
|
52
|
+
cases.push(summary(id, text));
|
|
53
|
+
}
|
|
54
|
+
return cases;
|
|
55
|
+
},
|
|
56
|
+
async show(id) {
|
|
57
|
+
const at = await tip();
|
|
58
|
+
const text = at ? await read(at, id) : undefined;
|
|
59
|
+
if (text === undefined)
|
|
60
|
+
return undefined;
|
|
61
|
+
return { ...summary(id, text), text: `${text}\n` };
|
|
62
|
+
},
|
|
63
|
+
async file(text) {
|
|
64
|
+
const { id, title } = parseCase(text);
|
|
65
|
+
await write(id, (old, retried) => {
|
|
66
|
+
if (old === undefined)
|
|
67
|
+
return text;
|
|
68
|
+
if (retried && old.trim() === text.trim())
|
|
69
|
+
return undefined;
|
|
70
|
+
throw new CaseError(`case ${id} is already filed`);
|
|
71
|
+
}, `case: file ${id}`);
|
|
72
|
+
return { id, title, url: `${WORK_BRANCH}:${DIR}/${id}.md`, open: true };
|
|
73
|
+
},
|
|
74
|
+
async close(id, line, day) {
|
|
75
|
+
let closed = false;
|
|
76
|
+
await write(id, (old) => {
|
|
77
|
+
if (old === undefined)
|
|
78
|
+
throw new CaseError(`no case ${id}`);
|
|
79
|
+
// Closed already: by an earlier run, or by someone else while this one raced.
|
|
80
|
+
if (parseFrontMatter(old).data.closed) {
|
|
81
|
+
closed = false;
|
|
82
|
+
return undefined;
|
|
83
|
+
}
|
|
84
|
+
closed = true;
|
|
85
|
+
return appendLog(closeCase(old, day), line);
|
|
86
|
+
}, `case: close ${id}`);
|
|
87
|
+
return closed;
|
|
88
|
+
},
|
|
89
|
+
async log(id, line) {
|
|
90
|
+
await write(id, (old, retried) => {
|
|
91
|
+
if (old === undefined)
|
|
92
|
+
throw new CaseError(`no case ${id}`);
|
|
93
|
+
if (retried && old.trimEnd().endsWith(`- ${line}`))
|
|
94
|
+
return undefined;
|
|
95
|
+
return appendLog(old, line);
|
|
96
|
+
}, `case: log ${id}`);
|
|
97
|
+
},
|
|
98
|
+
};
|
|
99
|
+
}
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
import { stringify } from "yaml";
|
|
2
|
+
import { gh, withBody } from "../host/github.js";
|
|
3
|
+
import { DEFAULT_TRUST, trustedBy } from "../trust.js";
|
|
4
|
+
import { CaseError, parseCase } from "./tracker.js";
|
|
5
|
+
export const CASE_LABEL = "type:case";
|
|
6
|
+
const BLOCKS = /<!-- cupola:case (\{[^\n]*?\}) -->/g;
|
|
7
|
+
/**
|
|
8
|
+
* The case's hidden block: the last one that holds valid JSON. Text a person
|
|
9
|
+
* adds after it on the host does not lose it, and a mention of the block in
|
|
10
|
+
* prose (which holds no valid JSON) is not taken for it.
|
|
11
|
+
*/
|
|
12
|
+
function metaOf(body) {
|
|
13
|
+
let meta;
|
|
14
|
+
for (const m of body.matchAll(BLOCKS)) {
|
|
15
|
+
try {
|
|
16
|
+
const value = JSON.parse(m[1] ?? "");
|
|
17
|
+
if (value && typeof value === "object" && !Array.isArray(value))
|
|
18
|
+
meta = value;
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
// not the block
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return meta;
|
|
25
|
+
}
|
|
26
|
+
/** Text whose Cupola markers record nothing: they no longer read as markers. */
|
|
27
|
+
export function inert(text) {
|
|
28
|
+
return text.replace(/<!--(\s*)cupola:/g, "<!--$1cupola-inert:");
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* A comment as a Log line. What someone who cannot write to the repository
|
|
32
|
+
* wrote stays, named, on one line (it adds nothing to the case's structure),
|
|
33
|
+
* and records nothing.
|
|
34
|
+
*/
|
|
35
|
+
export function logLine(c, trust = DEFAULT_TRUST) {
|
|
36
|
+
return trustedBy(trust, who(c))
|
|
37
|
+
? `- ${c.body.trim()}`
|
|
38
|
+
: `- (from ${c.author?.login ?? "someone"}, not trusted by the configuration) ${inert(c.body.trim()).replace(/\s*\n\s*/g, " ")}`;
|
|
39
|
+
}
|
|
40
|
+
function who(w) {
|
|
41
|
+
return { author: w.author?.login ?? "", association: w.authorAssociation ?? "NONE" };
|
|
42
|
+
}
|
|
43
|
+
const ISSUE_QUERY = `query($owner: String!, $name: String!, $number: Int!, $endCursor: String) {
|
|
44
|
+
repository(owner: $owner, name: $name) {
|
|
45
|
+
issue(number: $number) {
|
|
46
|
+
body closedAt authorAssociation author { login }
|
|
47
|
+
comments(first: 100, after: $endCursor) {
|
|
48
|
+
nodes { body authorAssociation author { login } }
|
|
49
|
+
pageInfo { hasNextPage endCursor }
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}`;
|
|
54
|
+
/** An issue's body and every comment, with their authors: one request for up to 100 comments. */
|
|
55
|
+
async function issueView(root, hub, number) {
|
|
56
|
+
const [owner, name] = hub.split("/");
|
|
57
|
+
const pages = JSON.parse(await gh(root, [
|
|
58
|
+
"api",
|
|
59
|
+
"graphql",
|
|
60
|
+
"--paginate",
|
|
61
|
+
"--slurp",
|
|
62
|
+
"-f",
|
|
63
|
+
`query=${ISSUE_QUERY}`,
|
|
64
|
+
"-F",
|
|
65
|
+
`owner=${owner}`,
|
|
66
|
+
"-F",
|
|
67
|
+
`name=${name}`,
|
|
68
|
+
"-F",
|
|
69
|
+
`number=${number}`,
|
|
70
|
+
]));
|
|
71
|
+
const first = pages[0]?.data.repository.issue;
|
|
72
|
+
if (!first)
|
|
73
|
+
throw new Error(`${hub}#${number}: no such issue`);
|
|
74
|
+
return { ...first, comments: pages.flatMap((p) => p.data.repository.issue.comments.nodes) };
|
|
75
|
+
}
|
|
76
|
+
/** The body without its hidden blocks or the template's guidance comments, which are not for the issue's readers. */
|
|
77
|
+
function visible(body) {
|
|
78
|
+
return body
|
|
79
|
+
.replace(/<!--[\s\S]*?-->\n?/g, "")
|
|
80
|
+
.replace(/\n{3,}/g, "\n\n")
|
|
81
|
+
.trim();
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* The case's front matter other than `title` (and `kind`, `closed`, which the
|
|
85
|
+
* issue itself carries), as the trailing hidden block of the issue.
|
|
86
|
+
*/
|
|
87
|
+
export function issueBody(body, data) {
|
|
88
|
+
const { kind: _k, title: _t, closed: _c, ...meta } = data;
|
|
89
|
+
return `${visible(body)}\n\n<!-- cupola:case ${JSON.stringify(meta)} -->\n`;
|
|
90
|
+
}
|
|
91
|
+
/** The case id an issue carries in its hidden block; an issue filed by hand has only its number. */
|
|
92
|
+
export function caseIdOf(issue) {
|
|
93
|
+
const id = metaOf(issue.body)?.id;
|
|
94
|
+
return typeof id === "string" && id !== "" ? id : String(issue.number);
|
|
95
|
+
}
|
|
96
|
+
/** The `github-issues` tracker: cases are issues labeled `type:case`. */
|
|
97
|
+
export function githubIssues(root, hub, trust = DEFAULT_TRUST) {
|
|
98
|
+
const LIMIT = 5000;
|
|
99
|
+
async function issues() {
|
|
100
|
+
const out = await gh(root, [
|
|
101
|
+
"issue",
|
|
102
|
+
"list",
|
|
103
|
+
"--repo",
|
|
104
|
+
hub,
|
|
105
|
+
"--label",
|
|
106
|
+
CASE_LABEL,
|
|
107
|
+
"--state",
|
|
108
|
+
"all",
|
|
109
|
+
"--limit",
|
|
110
|
+
String(LIMIT),
|
|
111
|
+
"--json",
|
|
112
|
+
"number,title,url,state,body",
|
|
113
|
+
]);
|
|
114
|
+
const all = JSON.parse(out);
|
|
115
|
+
if (all.length >= LIMIT) {
|
|
116
|
+
throw new Error(`${hub} has ${LIMIT} or more cases; Cupola cannot read them all yet`);
|
|
117
|
+
}
|
|
118
|
+
return all;
|
|
119
|
+
}
|
|
120
|
+
const summary = (i) => ({
|
|
121
|
+
id: caseIdOf(i),
|
|
122
|
+
title: i.title,
|
|
123
|
+
url: i.url,
|
|
124
|
+
open: i.state === "OPEN",
|
|
125
|
+
});
|
|
126
|
+
async function find(id) {
|
|
127
|
+
const all = await issues();
|
|
128
|
+
const n = /^#?(\d+)$/.exec(id)?.[1];
|
|
129
|
+
return all.find((i) => caseIdOf(i) === id || (n !== undefined && i.number === Number(n)));
|
|
130
|
+
}
|
|
131
|
+
return {
|
|
132
|
+
async list() {
|
|
133
|
+
return (await issues()).map(summary);
|
|
134
|
+
},
|
|
135
|
+
async show(id) {
|
|
136
|
+
const issue = await find(id);
|
|
137
|
+
if (!issue)
|
|
138
|
+
return undefined;
|
|
139
|
+
const view = await issueView(root, hub, issue.number);
|
|
140
|
+
// The body is the case: one written by someone not trusted could be rewritten under it.
|
|
141
|
+
if (!trustedBy(trust, who(view))) {
|
|
142
|
+
throw new CaseError(`${caseIdOf(issue)} was opened by ${view.author?.login ?? "someone"}, whom the configuration does not trust: file it as a case of your own with cupola-intake, linking ${issue.url}`);
|
|
143
|
+
}
|
|
144
|
+
const meta = metaOf(view.body) ?? {};
|
|
145
|
+
const front = {
|
|
146
|
+
kind: "case",
|
|
147
|
+
...meta,
|
|
148
|
+
id: caseIdOf(issue),
|
|
149
|
+
title: issue.title,
|
|
150
|
+
};
|
|
151
|
+
// The issue's state is the case's `closed`.
|
|
152
|
+
if (issue.state === "CLOSED")
|
|
153
|
+
front.closed = view.closedAt?.slice(0, 10) ?? true;
|
|
154
|
+
const body = visible(view.body);
|
|
155
|
+
// Each comment is one Log line, dated when it was written (`cupola work log`).
|
|
156
|
+
const log = view.comments.map((c) => logLine(c, trust));
|
|
157
|
+
const text = `---\n${stringify(front).trimEnd()}\n---\n\n${body}\n${log.length > 0 && !/^## Log\s*$/m.test(body) ? "\n## Log\n" : ""}${log.length > 0 ? `\n${log.join("\n")}\n` : ""}`;
|
|
158
|
+
return { ...summary(issue), text };
|
|
159
|
+
},
|
|
160
|
+
async file(text) {
|
|
161
|
+
const { id, title, data, body } = parseCase(text);
|
|
162
|
+
if ((await issues()).some((i) => caseIdOf(i) === id))
|
|
163
|
+
throw new CaseError(`case ${id} is already filed`);
|
|
164
|
+
// Creates the label if it is missing; an existing one is left as it is.
|
|
165
|
+
await gh(root, [
|
|
166
|
+
"label",
|
|
167
|
+
"create",
|
|
168
|
+
CASE_LABEL,
|
|
169
|
+
"--repo",
|
|
170
|
+
hub,
|
|
171
|
+
"--description",
|
|
172
|
+
"A problem, filed by Cupola or by hand",
|
|
173
|
+
], (_code, stderr) => /already exists/i.test(stderr));
|
|
174
|
+
const url = await withBody(issueBody(body, data), (file) => gh(root, [
|
|
175
|
+
"issue",
|
|
176
|
+
"create",
|
|
177
|
+
"--repo",
|
|
178
|
+
hub,
|
|
179
|
+
"--title",
|
|
180
|
+
title,
|
|
181
|
+
"--body-file",
|
|
182
|
+
file,
|
|
183
|
+
"--label",
|
|
184
|
+
CASE_LABEL,
|
|
185
|
+
]));
|
|
186
|
+
return { id, title, url: url.split("\n").at(-1) ?? url, open: true };
|
|
187
|
+
},
|
|
188
|
+
async close(id, line) {
|
|
189
|
+
const issue = await find(id);
|
|
190
|
+
if (!issue)
|
|
191
|
+
throw new CaseError(`no case ${id}`);
|
|
192
|
+
if (issue.state === "CLOSED")
|
|
193
|
+
return false;
|
|
194
|
+
// The comment first, unless an earlier run that failed to close already wrote it.
|
|
195
|
+
const view = JSON.parse(await gh(root, [
|
|
196
|
+
"issue",
|
|
197
|
+
"view",
|
|
198
|
+
String(issue.number),
|
|
199
|
+
"--repo",
|
|
200
|
+
hub,
|
|
201
|
+
"--json",
|
|
202
|
+
"comments",
|
|
203
|
+
]));
|
|
204
|
+
if (view.comments.at(-1)?.body.trim() !== line.trim()) {
|
|
205
|
+
await withBody(line, (file) => gh(root, ["issue", "comment", String(issue.number), "--repo", hub, "--body-file", file]));
|
|
206
|
+
}
|
|
207
|
+
await gh(root, ["issue", "close", String(issue.number), "--repo", hub]);
|
|
208
|
+
return true;
|
|
209
|
+
},
|
|
210
|
+
async log(id, line) {
|
|
211
|
+
const issue = await find(id);
|
|
212
|
+
if (!issue)
|
|
213
|
+
throw new CaseError(`no case ${id}`);
|
|
214
|
+
await withBody(line, (file) => gh(root, ["issue", "comment", String(issue.number), "--repo", hub, "--body-file", file]));
|
|
215
|
+
},
|
|
216
|
+
};
|
|
217
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { cloneUrlOf } from "../config.js";
|
|
2
|
+
import { git } from "../git.js";
|
|
3
|
+
import { repoOf } from "../host/github.js";
|
|
4
|
+
import { sameUrl } from "../workspace.js";
|
|
5
|
+
import { filesTracker } from "./files.js";
|
|
6
|
+
import { githubIssues } from "./github-issues.js";
|
|
7
|
+
/**
|
|
8
|
+
* The tracker the configuration names. Cases live in the hub: the
|
|
9
|
+
* github-issues tracker names it on every call; the files tracker writes to
|
|
10
|
+
* origin's `cupola/work`, so it runs only in the hub's own checkout.
|
|
11
|
+
*/
|
|
12
|
+
export async function trackerFor(root, config) {
|
|
13
|
+
if (config.tracker.kind === "github-issues")
|
|
14
|
+
return githubIssues(root, config.hub, config.trust);
|
|
15
|
+
let url = "";
|
|
16
|
+
try {
|
|
17
|
+
url = await git(root, ["remote", "get-url", "origin"]);
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
url = "";
|
|
21
|
+
}
|
|
22
|
+
// A local path names no hosted repository to compare with the hub: taken as the hub.
|
|
23
|
+
const local = /^(\/|file:|[A-Za-z]:[\\/]|\.)/.test(url);
|
|
24
|
+
const origin = repoOf(url);
|
|
25
|
+
// A hub on another host names its clone URL, or was recorded as the hub by `workspace setup`.
|
|
26
|
+
const named = await git(root, ["config", "--get", "cupola.repo"]).catch(() => "");
|
|
27
|
+
const isHub = origin === config.hub.toLowerCase() ||
|
|
28
|
+
sameUrl(cloneUrlOf(config, config.hub), url) ||
|
|
29
|
+
named.toLowerCase() === config.hub.toLowerCase();
|
|
30
|
+
if (!local && !isHub) {
|
|
31
|
+
throw new Error(`the files tracker keeps cases in the hub ${config.hub}, but origin here is ${origin ?? "missing"}; run from the hub's checkout`);
|
|
32
|
+
}
|
|
33
|
+
return filesTracker(root);
|
|
34
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { stringify } from "yaml";
|
|
2
|
+
import { parseFrontMatter } from "../frontmatter.js";
|
|
3
|
+
export class CaseError extends Error {
|
|
4
|
+
}
|
|
5
|
+
/** Kebab-case with at least one letter, so that an id is never taken for an issue number. */
|
|
6
|
+
const CASE_ID = /^(?=[a-z0-9-]*[a-z])[a-z0-9]+(?:-[a-z0-9]+)*$/;
|
|
7
|
+
/** A finished case document: checked before it reaches the tracker. */
|
|
8
|
+
export function parseCase(text) {
|
|
9
|
+
const { data, body } = parseFrontMatter(text);
|
|
10
|
+
const problems = [];
|
|
11
|
+
if (data.kind !== "case")
|
|
12
|
+
problems.push("front matter kind must be case");
|
|
13
|
+
if (typeof data.method !== "number")
|
|
14
|
+
problems.push("front matter method must be a number");
|
|
15
|
+
const id = typeof data.id === "string" ? data.id : "";
|
|
16
|
+
if (!CASE_ID.test(id))
|
|
17
|
+
problems.push(`id ${id || "(missing)"} is not a kebab-case name`);
|
|
18
|
+
const title = typeof data.title === "string" ? data.title.trim() : "";
|
|
19
|
+
if (title === "")
|
|
20
|
+
problems.push("title is missing");
|
|
21
|
+
if (data.draft !== undefined)
|
|
22
|
+
problems.push("`draft` is left in the front matter");
|
|
23
|
+
if (text.includes("{{"))
|
|
24
|
+
problems.push("template placeholders are left ({{…}})");
|
|
25
|
+
for (const section of ["## Problem", "## Solved when"]) {
|
|
26
|
+
if (!body.split(/\r?\n/).some((l) => l.trim() === section))
|
|
27
|
+
problems.push(`no "${section}" section`);
|
|
28
|
+
}
|
|
29
|
+
if (problems.length > 0)
|
|
30
|
+
throw new CaseError(`not a case to file: ${problems.join("; ")}`);
|
|
31
|
+
return { id, title, data, body };
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* A case draft from a run, made into the case to file: `draft` and
|
|
35
|
+
* `duplicates-checked` are dropped, `filed` is set (case-draft).
|
|
36
|
+
*/
|
|
37
|
+
export function fromDraft(text, today) {
|
|
38
|
+
const { data, body } = parseFrontMatter(text);
|
|
39
|
+
const { draft: _draft, "duplicates-checked": _checked, ...rest } = data;
|
|
40
|
+
const ordered = {};
|
|
41
|
+
for (const key of ["kind", "method", "id", "title"])
|
|
42
|
+
if (key in rest)
|
|
43
|
+
ordered[key] = rest[key];
|
|
44
|
+
ordered.filed = today;
|
|
45
|
+
for (const [key, value] of Object.entries(rest))
|
|
46
|
+
if (!(key in ordered))
|
|
47
|
+
ordered[key] = value;
|
|
48
|
+
return `---\n${stringify(ordered).trimEnd()}\n---\n${body.startsWith("\n") ? "" : "\n"}${body}`;
|
|
49
|
+
}
|
|
50
|
+
/** The case document with `closed` set to the day, the rest left as it is. */
|
|
51
|
+
export function closeCase(text, day) {
|
|
52
|
+
const end = text.indexOf("\n---", 4);
|
|
53
|
+
if (!text.startsWith("---\n") || end < 0)
|
|
54
|
+
throw new CaseError("the case has no front matter");
|
|
55
|
+
const front = text.slice(0, end);
|
|
56
|
+
const rest = text.slice(end);
|
|
57
|
+
const closed = `closed: ${day}`;
|
|
58
|
+
return /^closed:.*$/m.test(front)
|
|
59
|
+
? front.replace(/^closed:.*$/m, closed) + rest
|
|
60
|
+
: `${front}\n${closed}${rest}`;
|
|
61
|
+
}
|
|
62
|
+
/** The body with one line appended to its Log section, which is the last one. */
|
|
63
|
+
export function appendLog(body, line) {
|
|
64
|
+
const lines = body.replace(/\s+$/, "").split("\n");
|
|
65
|
+
if (!lines.some((l) => l.trim() === "## Log"))
|
|
66
|
+
lines.push("", "## Log");
|
|
67
|
+
lines.push(`- ${line}`);
|
|
68
|
+
return `${lines.join("\n")}\n`;
|
|
69
|
+
}
|
package/dist/tree.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { readdir, readFile } from "node:fs/promises";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { git } from "./git.js";
|
|
5
|
+
export function fsTree(root) {
|
|
6
|
+
return {
|
|
7
|
+
async read(path) {
|
|
8
|
+
const file = join(root, path);
|
|
9
|
+
return existsSync(file) ? readFile(file, "utf8") : undefined;
|
|
10
|
+
},
|
|
11
|
+
async list(dir) {
|
|
12
|
+
const path = join(root, dir);
|
|
13
|
+
if (!existsSync(path))
|
|
14
|
+
return [];
|
|
15
|
+
return (await readdir(path, { withFileTypes: true }))
|
|
16
|
+
.filter((e) => e.isFile())
|
|
17
|
+
.map((e) => e.name);
|
|
18
|
+
},
|
|
19
|
+
async dirs(dir) {
|
|
20
|
+
const path = join(root, dir);
|
|
21
|
+
if (!existsSync(path))
|
|
22
|
+
return [];
|
|
23
|
+
return (await readdir(path, { withFileTypes: true }))
|
|
24
|
+
.filter((e) => e.isDirectory())
|
|
25
|
+
.map((e) => e.name);
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
/** Files at a commit, read with git without touching the working tree. */
|
|
30
|
+
export function gitTree(cwd, ref) {
|
|
31
|
+
return {
|
|
32
|
+
async read(path) {
|
|
33
|
+
try {
|
|
34
|
+
return await git(cwd, ["show", `${ref}:${path}`]);
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
async list(dir) {
|
|
41
|
+
try {
|
|
42
|
+
const out = await git(cwd, ["ls-tree", "--name-only", `${ref}:${dir}`]);
|
|
43
|
+
return out === "" ? [] : out.split("\n");
|
|
44
|
+
}
|
|
45
|
+
catch {
|
|
46
|
+
return [];
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
async dirs(dir) {
|
|
50
|
+
try {
|
|
51
|
+
const out = await git(cwd, ["ls-tree", "-d", "--name-only", `${ref}:${dir}`]);
|
|
52
|
+
return out === "" ? [] : out.split("\n");
|
|
53
|
+
}
|
|
54
|
+
catch {
|
|
55
|
+
return [];
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
}
|
package/dist/trust.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whose words may steer the work (configuration `trust`): someone whose
|
|
3
|
+
* association with the repository is listed (by default, who can write to
|
|
4
|
+
* it), or a listed login. What anyone else writes is kept as information,
|
|
5
|
+
* never acted on.
|
|
6
|
+
*/
|
|
7
|
+
export const ASSOCIATIONS = ["OWNER", "MEMBER", "COLLABORATOR", "CONTRIBUTOR"];
|
|
8
|
+
export const DEFAULT_ASSOCIATIONS = ["OWNER", "MEMBER", "COLLABORATOR"];
|
|
9
|
+
export const DEFAULT_TRUST = { associations: DEFAULT_ASSOCIATIONS, logins: [] };
|
|
10
|
+
export function trustedBy(trust, who) {
|
|
11
|
+
return (trust.associations.includes(who.association) ||
|
|
12
|
+
trust.logins.some((l) => l.toLowerCase() === who.author.toLowerCase()));
|
|
13
|
+
}
|
package/dist/version.js
ADDED
package/dist/work.js
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import { join } from "node:path";
|
|
2
|
+
import { resolveComponents } from "./config.js";
|
|
3
|
+
import { parseFrontMatter } from "./frontmatter.js";
|
|
4
|
+
import { readBlock } from "./spec.js";
|
|
5
|
+
const DOC_KINDS = new Set(["fix", "fix-spec", "catalog"]);
|
|
6
|
+
export class WorkError extends Error {
|
|
7
|
+
constructor(message) {
|
|
8
|
+
super(message);
|
|
9
|
+
this.name = "WorkError";
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
/** No document for the work item exists in the tree that was searched. */
|
|
13
|
+
export class MissingWorkError extends WorkError {
|
|
14
|
+
constructor(message) {
|
|
15
|
+
super(message);
|
|
16
|
+
this.name = "MissingWorkError";
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
const WORK_ID = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
|
|
20
|
+
export function isWorkId(id) {
|
|
21
|
+
return typeof id === "string" && WORK_ID.test(id);
|
|
22
|
+
}
|
|
23
|
+
/** Work ids are kebab-case names; anything else could leave `.cupola/`. */
|
|
24
|
+
export function assertWorkId(id) {
|
|
25
|
+
if (!isWorkId(id))
|
|
26
|
+
throw new WorkError(`${JSON.stringify(id)} is not a work id (kebab-case)`);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Finds the document of a work item in this repository: a full spec's
|
|
30
|
+
* `spec.json` with Cupola's block, or the document of a fix, fix spec, or
|
|
31
|
+
* catalog spec in `.cupola/specs/`.
|
|
32
|
+
*/
|
|
33
|
+
export async function findSpecDocument(tree, config, id,
|
|
34
|
+
/** The repository the tree is of, when known: the document's component must be one of its. */
|
|
35
|
+
repo) {
|
|
36
|
+
assertWorkId(id);
|
|
37
|
+
const components = resolveComponents(config);
|
|
38
|
+
const specJson = `.kiro/specs/${id}/spec.json`;
|
|
39
|
+
const raw = await tree.read(specJson);
|
|
40
|
+
const block = raw === undefined ? undefined : readBlock(specJson, raw);
|
|
41
|
+
if (block) {
|
|
42
|
+
if (!components.some((c) => c.name === block.component)) {
|
|
43
|
+
throw new WorkError(`${specJson}: component ${block.component} is not configured`);
|
|
44
|
+
}
|
|
45
|
+
// A follow-up spec of the same component is `-2`, `-3`, ….
|
|
46
|
+
if (id !== `${block.epic}-${block.component}` &&
|
|
47
|
+
!id.startsWith(`${block.epic}-${block.component}-`)) {
|
|
48
|
+
throw new WorkError(`${specJson}: a spec of ${block.epic} in ${block.component} is named ${block.epic}-${block.component}, not ${id}`);
|
|
49
|
+
}
|
|
50
|
+
return { kind: "spec", id, component: block.component, epic: block.epic, path: specJson };
|
|
51
|
+
}
|
|
52
|
+
const path = specDocPath(id);
|
|
53
|
+
const text = await tree.read(path);
|
|
54
|
+
if (text !== undefined) {
|
|
55
|
+
const { data } = parseFrontMatter(text);
|
|
56
|
+
if (typeof data.kind !== "string" || !DOC_KINDS.has(data.kind)) {
|
|
57
|
+
throw new WorkError(`${path}: kind must be fix, fix-spec, or catalog`);
|
|
58
|
+
}
|
|
59
|
+
if (data.id !== id)
|
|
60
|
+
throw new WorkError(`${path}: id is ${String(data.id)}, expected ${id}`);
|
|
61
|
+
const named = components.find((c) => c.name === data.component);
|
|
62
|
+
if (!named)
|
|
63
|
+
throw new WorkError(`${path}: component ${String(data.component)} is not configured`);
|
|
64
|
+
if (repo && named.repo.toLowerCase() !== repo.toLowerCase())
|
|
65
|
+
throw new WorkError(`${path}: component ${named.name} is in ${named.repo}, not ${repo}`);
|
|
66
|
+
if (data.epic !== undefined && data.epic !== null && !isWorkId(data.epic))
|
|
67
|
+
throw new WorkError(`${path}: epic ${JSON.stringify(data.epic)} is not a work id (kebab-case)`);
|
|
68
|
+
return {
|
|
69
|
+
kind: data.kind,
|
|
70
|
+
id,
|
|
71
|
+
component: named.name,
|
|
72
|
+
epic: typeof data.epic === "string" ? data.epic : null,
|
|
73
|
+
path,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
throw new MissingWorkError(`no work item ${id}: no ${path}, and no ${specJson} with a cupola block`);
|
|
77
|
+
}
|
|
78
|
+
/** The document of a fix, fix spec, or catalog spec, at the repository's root. */
|
|
79
|
+
export function specDocPath(id) {
|
|
80
|
+
return `.cupola/specs/${id}.md`;
|
|
81
|
+
}
|
|
82
|
+
export function runDir(id) {
|
|
83
|
+
assertWorkId(id);
|
|
84
|
+
return join(".cupola", "runs", id);
|
|
85
|
+
}
|
|
86
|
+
/** Reads the cards of a work item's progress record. */
|
|
87
|
+
export async function readCards(tree, id) {
|
|
88
|
+
return readCardsIn(tree, join(runDir(id), "cards"));
|
|
89
|
+
}
|
|
90
|
+
/** Reads the cards of a directory: a progress record's, or an epic's. */
|
|
91
|
+
export async function readCardsIn(tree, dir) {
|
|
92
|
+
const cards = [];
|
|
93
|
+
for (const file of (await tree.list(dir)).filter((f) => f.endsWith(".md"))) {
|
|
94
|
+
const where = join(dir, file);
|
|
95
|
+
const { data } = parseFrontMatter((await tree.read(where)) ?? "");
|
|
96
|
+
if (data.kind !== "card" || typeof data.card !== "number") {
|
|
97
|
+
throw new WorkError(`${where}: not a card`);
|
|
98
|
+
}
|
|
99
|
+
const type = String(data.type ?? "");
|
|
100
|
+
if (type === "no-go") {
|
|
101
|
+
// Answered once it says where to go back: written so by a gate that read the No-Go, or by
|
|
102
|
+
// cupola-decide when the No-Go had no content and the card asked its giver.
|
|
103
|
+
const redo = str(data.redo);
|
|
104
|
+
const effect = redo === undefined ? null : redo === "revision" ? "revise-epic" : "continue";
|
|
105
|
+
cards.push({
|
|
106
|
+
card: data.card,
|
|
107
|
+
type,
|
|
108
|
+
file,
|
|
109
|
+
effect,
|
|
110
|
+
answered: effect !== null,
|
|
111
|
+
redo,
|
|
112
|
+
take: str(data.take),
|
|
113
|
+
ask: asked(data.ask),
|
|
114
|
+
});
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
const answer = str(data.answer);
|
|
118
|
+
let effect = null;
|
|
119
|
+
if (answer === "other")
|
|
120
|
+
effect = "continue";
|
|
121
|
+
else if (answer !== undefined) {
|
|
122
|
+
const options = (data.options ?? {});
|
|
123
|
+
const chosen = options[answer];
|
|
124
|
+
if (typeof chosen !== "string")
|
|
125
|
+
throw new WorkError(`${where}: answer ${answer} is not one of the options`);
|
|
126
|
+
effect = chosen;
|
|
127
|
+
}
|
|
128
|
+
const offers = Object.values((data.options ?? {})).map(String);
|
|
129
|
+
cards.push({
|
|
130
|
+
card: data.card,
|
|
131
|
+
type,
|
|
132
|
+
file,
|
|
133
|
+
effect,
|
|
134
|
+
answered: effect !== null,
|
|
135
|
+
offers,
|
|
136
|
+
case: type === "stop-fix" ? str(data.case) : undefined,
|
|
137
|
+
recommended: str(data.recommended),
|
|
138
|
+
ask: asked(data.ask),
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
return cards.sort((a, b) => a.card - b.card);
|
|
142
|
+
}
|
|
143
|
+
/** A card's `ask:`: a list, or one value (commas separate several); free text, kept as written. */
|
|
144
|
+
function asked(value) {
|
|
145
|
+
const all = Array.isArray(value) ? value : value === null || value === undefined ? [] : [value];
|
|
146
|
+
return all
|
|
147
|
+
.flatMap((v) => String(v ?? "").split(","))
|
|
148
|
+
.map((v) => v.trim())
|
|
149
|
+
.filter((v) => v !== "");
|
|
150
|
+
}
|
|
151
|
+
function str(value) {
|
|
152
|
+
if (value === null || value === undefined || value === "")
|
|
153
|
+
return undefined;
|
|
154
|
+
return String(value);
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Whether a checkout's branch is one a work item runs on: its own branch, or,
|
|
158
|
+
* for an epic with two or more specs, its epic branch (where its own record
|
|
159
|
+
* lives).
|
|
160
|
+
*/
|
|
161
|
+
export function runsOn(branch, id) {
|
|
162
|
+
return branch === workBranchName(id) || branch === `epic/${id}`;
|
|
163
|
+
}
|
|
164
|
+
/** What every work item's branch starts with. */
|
|
165
|
+
export const WORK_BRANCH_PREFIX = "cupola/";
|
|
166
|
+
/** The branch a work item runs on; an internal convention of Cupola. */
|
|
167
|
+
export function workBranchName(id) {
|
|
168
|
+
assertWorkId(id);
|
|
169
|
+
return `${WORK_BRANCH_PREFIX}${id}`;
|
|
170
|
+
}
|