@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.
Files changed (84) hide show
  1. package/CHANGELOG.md +158 -0
  2. package/README.md +3 -2
  3. package/dist/branch-store.js +73 -0
  4. package/dist/commands/change.js +245 -39
  5. package/dist/commands/epic.js +3 -3
  6. package/dist/commands/execution.js +482 -0
  7. package/dist/commands/locks.js +69 -1
  8. package/dist/commands/reaction.js +185 -0
  9. package/dist/commands/revision.js +4 -4
  10. package/dist/commands/run.js +68 -62
  11. package/dist/commands/setup.js +54 -22
  12. package/dist/commands/spec.js +1 -1
  13. package/dist/commands/status.js +437 -138
  14. package/dist/commands/trace.js +106 -29
  15. package/dist/commands/wait.js +14 -11
  16. package/dist/commands/work.js +12 -7
  17. package/dist/commands/workspace.js +16 -5
  18. package/dist/commands/worktree.js +3 -3
  19. package/dist/config.js +66 -2
  20. package/dist/epic.js +24 -5
  21. package/dist/execution.js +272 -0
  22. package/dist/feedback.js +3 -17
  23. package/dist/host/git.js +224 -0
  24. package/dist/host/github.js +145 -48
  25. package/dist/host/host.js +3 -1
  26. package/dist/host/index.js +63 -0
  27. package/dist/main.js +143 -48
  28. package/dist/reaction.js +110 -0
  29. package/dist/record.js +52 -18
  30. package/dist/runbook.js +26 -5
  31. package/dist/status.js +158 -27
  32. package/dist/templates.js +2 -1
  33. package/dist/tracker/files.js +9 -66
  34. package/dist/tracker/index.js +8 -1
  35. package/dist/tree.js +17 -0
  36. package/dist/work.js +27 -6
  37. package/dist/workspace.js +37 -12
  38. package/npm-shrinkwrap.json +2 -2
  39. package/package.json +1 -1
  40. package/skills/cupola-abort/SKILL.md +11 -5
  41. package/skills/cupola-adr/SKILL.md +23 -10
  42. package/skills/cupola-catalog/SKILL.md +1 -1
  43. package/skills/cupola-catalog-draft/SKILL.md +6 -4
  44. package/skills/cupola-complete/SKILL.md +35 -15
  45. package/skills/cupola-decide/SKILL.md +64 -25
  46. package/skills/cupola-e2e-setup/SKILL.md +1 -1
  47. package/skills/cupola-epic-accept/SKILL.md +14 -10
  48. package/skills/cupola-epic-create/SKILL.md +67 -27
  49. package/skills/cupola-epic-revise/SKILL.md +39 -18
  50. package/skills/cupola-epic-run/SKILL.md +81 -42
  51. package/skills/cupola-fix/SKILL.md +7 -5
  52. package/skills/cupola-init/SKILL.md +79 -24
  53. package/skills/cupola-intake/SKILL.md +19 -2
  54. package/skills/cupola-operate/SKILL.md +62 -45
  55. package/skills/cupola-resolve/SKILL.md +21 -14
  56. package/skills/cupola-respond/SKILL.md +73 -0
  57. package/skills/cupola-review/SKILL.md +14 -15
  58. package/skills/cupola-spec-design/SKILL.md +15 -10
  59. package/skills/cupola-spec-impl/SKILL.md +6 -4
  60. package/skills/cupola-spec-run/SKILL.md +29 -17
  61. package/skills/cupola-steering/SKILL.md +13 -6
  62. package/skills/cupola-try/SKILL.md +38 -0
  63. package/templates/install/.kiro/steering/operations.md +2 -2
  64. package/templates/install/.kiro/steering/principles.md +1 -1
  65. package/templates/install/cc-sdd/patches.json +1 -1
  66. package/templates/method-1/card.md +11 -3
  67. package/templates/method-1/case.md +2 -2
  68. package/templates/method-1/changes/catalog.md +1 -1
  69. package/templates/method-1/changes/epic.md +3 -2
  70. package/templates/method-1/changes/fix.md +1 -1
  71. package/templates/method-1/changes/release.md +1 -1
  72. package/templates/method-1/changes/spec.md +1 -1
  73. package/templates/method-1/cupola-issue-draft.md +1 -1
  74. package/templates/method-1/epic.md +17 -6
  75. package/templates/method-1/execution.md +55 -0
  76. package/templates/method-1/no-go.md +28 -0
  77. package/templates/method-1/reaction.md +17 -0
  78. package/templates/method-1/record.json +3 -3
  79. package/templates/method-1/report.md +3 -3
  80. package/templates/method-1/runbook.md +8 -2
  81. package/templates/method-1/runner-result.json +1 -1
  82. package/dist/commands/procedure.js +0 -92
  83. package/dist/procedure.js +0 -60
  84. package/templates/method-1/send-back.md +0 -23
@@ -60,7 +60,7 @@ const FEEDBACK_QUERY = `query($owner: String!, $repo: String!, $number: Int!) {
60
60
  repository(owner: $owner, name: $repo) {
61
61
  pullRequest(number: $number) {
62
62
  reviews(first: 100) {
63
- nodes { id author { login __typename } authorAssociation body submittedAt }
63
+ nodes { id state author { login __typename } authorAssociation body submittedAt }
64
64
  pageInfo { hasNextPage }
65
65
  }
66
66
  reviewThreads(first: 100) {
@@ -74,7 +74,7 @@ const FEEDBACK_QUERY = `query($owner: String!, $repo: String!, $number: Int!) {
74
74
  pageInfo { hasNextPage }
75
75
  }
76
76
  comments(first: 100) {
77
- nodes { author { login __typename } authorAssociation body }
77
+ nodes { id author { login __typename } authorAssociation body createdAt }
78
78
  pageInfo { hasNextPage }
79
79
  }
80
80
  }
@@ -88,10 +88,11 @@ function who(r) {
88
88
  };
89
89
  }
90
90
  /**
91
- * GitHub's review threads and review bodies as feedback (see `Feedback`). A
92
- * mark settles or answers only when someone `trusted` wrote it.
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
93
  */
94
- export function feedbackOf(pr, trusted = () => true) {
94
+ const ASKING = new Set(["COMMENTED", "CHANGES_REQUESTED"]);
95
+ function allRead(pr) {
95
96
  const pages = [
96
97
  pr.reviews,
97
98
  pr.reviewThreads,
@@ -101,6 +102,14 @@ export function feedbackOf(pr, trusted = () => true) {
101
102
  if (pages.some((p) => p.pageInfo?.hasNextPage)) {
102
103
  throw new Error("the change has more than 100 reviews, threads, or comments; Cupola cannot read them all yet");
103
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);
104
113
  const said = pr.comments.nodes.filter((c) => trusted(who(c))).map((c) => c.body);
105
114
  const has = (mark) => said.some((b) => b.includes(mark));
106
115
  const threads = pr.reviewThreads.nodes.flatMap((t) => {
@@ -123,8 +132,9 @@ export function feedbackOf(pr, trusted = () => true) {
123
132
  ];
124
133
  });
125
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.
126
136
  const bodies = pr.reviews.nodes
127
- .filter((r) => r.body.trim() !== "" && r.submittedAt)
137
+ .filter((r) => ASKING.has(r.state) && r.body.trim() !== "" && r.submittedAt)
128
138
  .map((r) => {
129
139
  const id = `review:${r.id}`;
130
140
  return {
@@ -143,18 +153,135 @@ export function feedbackOf(pr, trusted = () => true) {
143
153
  .map((r) => ({ ...who(r), at: r.submittedAt ?? "" }));
144
154
  return { feedback: [...threads, ...bodies].sort((a, b) => a.at.localeCompare(b.at)), answers };
145
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
+ }
146
226
  function toChange(pr) {
147
227
  return {
148
228
  number: pr.number,
149
229
  url: pr.url,
230
+ ...(pr.title !== undefined ? { title: pr.title } : {}),
150
231
  state: pr.state.toLowerCase(),
151
232
  draft: pr.isDraft,
152
233
  ...(pr.headRefOid ? { head: pr.headRefOid } : {}),
153
234
  };
154
235
  }
155
- const FIELDS = "number,url,state,isDraft,headRepositoryOwner,headRefOid";
236
+ const FIELDS = "number,url,title,state,isDraft,headRepositoryOwner,headRefOid";
156
237
  /** GitHub through the `gh` CLI, run in the repository so that it targets its remote. */
157
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
+ }
158
285
  return {
159
286
  async findChange(branch) {
160
287
  const owner = await originOwner(cwd);
@@ -200,6 +327,9 @@ export function github(cwd) {
200
327
  return JSON.parse(await gh(cwd, ["pr", "view", String(number), "--json", "body"]))
201
328
  .body;
202
329
  },
330
+ async reach(change) {
331
+ return { read: change.url, merge: "merge it", reactions: true };
332
+ },
203
333
  async setBody(number, body) {
204
334
  await withBody(body, (file) => gh(cwd, ["pr", "edit", String(number), "--body-file", file]));
205
335
  },
@@ -218,49 +348,16 @@ export function github(cwd) {
218
348
  await gh(cwd, ["pr", "edit", String(number), "--add-reviewer", who]);
219
349
  },
220
350
  async feedback(number, trusted) {
221
- const out = JSON.parse(await gh(cwd, [
222
- "api",
223
- "graphql",
224
- "-f",
225
- `query=${FEEDBACK_QUERY}`,
226
- "-F",
227
- "owner={owner}",
228
- "-F",
229
- "repo={repo}",
230
- "-F",
231
- `number=${number}`,
232
- ]));
233
- return feedbackOf(out.data.repository.pullRequest, 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);
234
358
  },
235
359
  async answer(number, id, body, settle) {
236
- const [kind, node] = [id.slice(0, id.indexOf(":")), id.slice(id.indexOf(":") + 1)];
237
- if (kind === "thread") {
238
- await gh(cwd, [
239
- "api",
240
- "graphql",
241
- "-f",
242
- "query=mutation($id: ID!, $body: String!) { addPullRequestReviewThreadReply(input: {pullRequestReviewThreadId: $id, body: $body}) { comment { id } } }",
243
- "-f",
244
- `id=${node}`,
245
- "-f",
246
- `body=${body}\n\n${ANSWER}`,
247
- ]);
248
- if (settle) {
249
- await gh(cwd, [
250
- "api",
251
- "graphql",
252
- "-f",
253
- "query=mutation($id: ID!) { resolveReviewThread(input: {threadId: $id}) { thread { id } } }",
254
- "-f",
255
- `id=${node}`,
256
- ]);
257
- }
258
- return;
259
- }
260
- if (kind !== "review")
261
- throw new Error(`no feedback ${id} on GitHub`);
262
- const marks = `${ANSWER} ${onMark(id)}${settle ? ` ${settledMark(id)}` : ""}`;
263
- await withBody(`${body}\n\n${marks}`, (file) => gh(cwd, ["pr", "comment", String(number), "--body-file", file]));
360
+ await answer(number, id, body, settle);
264
361
  },
265
362
  async hasCi() {
266
363
  // GitHub also lists workflows it runs itself (e.g. the Copilot reviewer, under
package/dist/host/host.js CHANGED
@@ -1 +1,3 @@
1
- export {};
1
+ /** A change on a host where people merge on the host itself, not through `cupola change merge`. */
2
+ export class MergedOnHost extends Error {
3
+ }
@@ -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
+ }