@llipe.com/dev-tasks 0.16.2 → 0.17.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/.claude/agents/developer.md +8 -3
- package/.claude/agents/github-ops.md +187 -59
- package/.claude/agents/technical-writer.md +1 -1
- package/.claude/agents/verifier.md +10 -5
- package/.claude/commands/planner.md +110 -15
- package/.claude/commands/product-engineer.md +27 -3
- package/.claude/hooks/git-guard.sh +2 -2
- package/.claude/skills/activity-init/SKILL.md +4 -0
- package/.claude/skills/activity-publish-github/SKILL.md +84 -15
- package/.claude/skills/git-ops/SKILL.md +6 -6
- package/.claude/skills/implement/SKILL.md +29 -13
- package/.claude/skills/memo-cli-usage/REFERENCE.md +1 -0
- package/.claude/skills/memo-cli-usage/SKILL.md +3 -1
- package/.claude/skills/plan/SKILL.md +6 -0
- package/.github/agents/developer.agent.md +8 -3
- package/.github/agents/github-ops.agent.md +182 -64
- package/.github/agents/planner.agent.md +123 -28
- package/.github/agents/product-engineer.agent.md +27 -3
- package/.github/agents/technical-writer.agent.md +1 -1
- package/.github/agents/verifier.agent.md +10 -5
- package/.github/instructions/implement.instructions.md +29 -13
- package/.github/instructions/plan.instructions.md +6 -0
- package/.github/skills/activity-init/SKILL.md +4 -0
- package/.github/skills/activity-publish-github/SKILL.md +84 -15
- package/.github/skills/git-ops/SKILL.md +6 -6
- package/.github/skills/memo-cli-usage/REFERENCE.md +1 -0
- package/.github/skills/memo-cli-usage/SKILL.md +3 -1
- package/.kiro/agents/developer.md +8 -3
- package/.kiro/agents/github-ops.md +182 -64
- package/.kiro/agents/planner.md +123 -28
- package/.kiro/agents/product-engineer.md +27 -3
- package/.kiro/agents/technical-writer.md +1 -1
- package/.kiro/agents/verifier.md +10 -5
- package/.kiro/skills/activity-init/SKILL.md +4 -0
- package/.kiro/skills/activity-publish-github/SKILL.md +84 -15
- package/.kiro/skills/git-ops/SKILL.md +6 -6
- package/.kiro/skills/memo-cli-usage/REFERENCE.md +1 -0
- package/.kiro/skills/memo-cli-usage/SKILL.md +3 -1
- package/.kiro/steering/implement.md +29 -13
- package/.kiro/steering/plan.md +6 -0
- package/CHANGELOG.md +42 -0
- package/README.md +79 -7
- package/TESTING.md +108 -30
- package/dist/bin/dev-tasks.d.ts +1 -1
- package/dist/bin/dev-tasks.js +126 -4
- package/dist/bin/dev-tasks.js.map +1 -1
- package/dist/bin/parse-args.d.ts +49 -1
- package/dist/bin/parse-args.d.ts.map +1 -1
- package/dist/bin/parse-args.js +132 -11
- package/dist/bin/parse-args.js.map +1 -1
- package/dist/core/checks/decision-log-format.d.ts +24 -3
- package/dist/core/checks/decision-log-format.d.ts.map +1 -1
- package/dist/core/checks/decision-log-format.js +43 -3
- package/dist/core/checks/decision-log-format.js.map +1 -1
- package/dist/core/checks/index.d.ts +2 -0
- package/dist/core/checks/index.d.ts.map +1 -1
- package/dist/core/checks/index.js +1 -0
- package/dist/core/checks/index.js.map +1 -1
- package/dist/core/checks/roadmap.d.ts +40 -0
- package/dist/core/checks/roadmap.d.ts.map +1 -0
- package/dist/core/checks/roadmap.js +78 -0
- package/dist/core/checks/roadmap.js.map +1 -0
- package/dist/core/migrate-projects/answers.d.ts +63 -0
- package/dist/core/migrate-projects/answers.d.ts.map +1 -0
- package/dist/core/migrate-projects/answers.js +79 -0
- package/dist/core/migrate-projects/answers.js.map +1 -0
- package/dist/core/migrate-projects/apply.d.ts +51 -0
- package/dist/core/migrate-projects/apply.d.ts.map +1 -0
- package/dist/core/migrate-projects/apply.js +828 -0
- package/dist/core/migrate-projects/apply.js.map +1 -0
- package/dist/core/migrate-projects/classify.d.ts +93 -0
- package/dist/core/migrate-projects/classify.d.ts.map +1 -0
- package/dist/core/migrate-projects/classify.js +170 -0
- package/dist/core/migrate-projects/classify.js.map +1 -0
- package/dist/core/migrate-projects/conventions.d.ts +118 -0
- package/dist/core/migrate-projects/conventions.d.ts.map +1 -0
- package/dist/core/migrate-projects/conventions.js +213 -0
- package/dist/core/migrate-projects/conventions.js.map +1 -0
- package/dist/core/migrate-projects/gh.d.ts +40 -0
- package/dist/core/migrate-projects/gh.d.ts.map +1 -0
- package/dist/core/migrate-projects/gh.js +115 -0
- package/dist/core/migrate-projects/gh.js.map +1 -0
- package/dist/core/migrate-projects/index.d.ts +61 -0
- package/dist/core/migrate-projects/index.d.ts.map +1 -0
- package/dist/core/migrate-projects/index.js +183 -0
- package/dist/core/migrate-projects/index.js.map +1 -0
- package/dist/core/migrate-projects/local-reader.d.ts +8 -0
- package/dist/core/migrate-projects/local-reader.d.ts.map +1 -0
- package/dist/core/migrate-projects/local-reader.js +30 -0
- package/dist/core/migrate-projects/local-reader.js.map +1 -0
- package/dist/core/migrate-projects/map.d.ts +36 -0
- package/dist/core/migrate-projects/map.d.ts.map +1 -0
- package/dist/core/migrate-projects/map.js +75 -0
- package/dist/core/migrate-projects/map.js.map +1 -0
- package/dist/core/migrate-projects/plan.d.ts +71 -0
- package/dist/core/migrate-projects/plan.d.ts.map +1 -0
- package/dist/core/migrate-projects/plan.js +638 -0
- package/dist/core/migrate-projects/plan.js.map +1 -0
- package/dist/core/migrate-projects/planfile.d.ts +149 -0
- package/dist/core/migrate-projects/planfile.d.ts.map +1 -0
- package/dist/core/migrate-projects/planfile.js +608 -0
- package/dist/core/migrate-projects/planfile.js.map +1 -0
- package/dist/core/migrate-projects/roadmap.d.ts +43 -0
- package/dist/core/migrate-projects/roadmap.d.ts.map +1 -0
- package/dist/core/migrate-projects/roadmap.js +54 -0
- package/dist/core/migrate-projects/roadmap.js.map +1 -0
- package/dist/core/migrate-projects/snapshot.d.ts +91 -0
- package/dist/core/migrate-projects/snapshot.d.ts.map +1 -0
- package/dist/core/migrate-projects/snapshot.js +285 -0
- package/dist/core/migrate-projects/snapshot.js.map +1 -0
- package/dist/core/migrate-projects/summary.d.ts +27 -0
- package/dist/core/migrate-projects/summary.d.ts.map +1 -0
- package/dist/core/migrate-projects/summary.js +35 -0
- package/dist/core/migrate-projects/summary.js.map +1 -0
- package/package.json +8 -6
|
@@ -0,0 +1,828 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `dev-tasks migrate projects --apply` (S-015; spec §6, §8.4, §13; §16a
|
|
3
|
+
* A-49 to A-51, A-64, A-67, A-80, A-82, A-93; D-22, D-27, D-79 to D-81,
|
|
4
|
+
* D-88, D-91, D-92, D-101).
|
|
5
|
+
*
|
|
6
|
+
* The only writer in the module. It runs an approved plan's batches 1 to 6
|
|
7
|
+
* in order and records each write's state in the plan file after its
|
|
8
|
+
* call, so an interruption loses at most the write in flight and a re-run
|
|
9
|
+
* resumes. It never runs batch 7 and never makes a Project call (D-81,
|
|
10
|
+
* D-22): batch 7's steps are printed for the user. It never deletes
|
|
11
|
+
* anything, never writes a title and never writes a Status (D-91, D-19,
|
|
12
|
+
* D-27). Answers in the review tables never become writes here (A-50):
|
|
13
|
+
* only batch rows do, and a fresh `--plan` turns answers into rows.
|
|
14
|
+
*
|
|
15
|
+
* Gates, in this order, each refusing with nothing written:
|
|
16
|
+
*
|
|
17
|
+
* 1. The plan file is missing, unparsable, or a row does not match its
|
|
18
|
+
* Action's grammar → `InvalidUsage` (2).
|
|
19
|
+
* 2. `gh` is missing or unauthenticated → `DependencyError` (11).
|
|
20
|
+
* 3. The header is not exactly one `Approved: yes` line → 14 (D-80).
|
|
21
|
+
* 4. `Plan digest:` no longer matches the rows → 14 (A-51).
|
|
22
|
+
* 5. The lock `workstream/.migration-projects.lock` is held → 14 (A-80).
|
|
23
|
+
* 6. While every row still reads `pending`, the snapshot hash differs →
|
|
24
|
+
* 14, "regenerate the plan" (D-101). Once any row is `done`, `failed`
|
|
25
|
+
* or `skipped`, the hash is not compared: the done writes themselves
|
|
26
|
+
* changed the repository.
|
|
27
|
+
*
|
|
28
|
+
* Every write then checks its own target first (D-101, A-49): already in
|
|
29
|
+
* the desired state → `skipped(already applied)`; in neither the state the
|
|
30
|
+
* plan expected nor the desired one → `failed(target changed)`. The state
|
|
31
|
+
* it checks is the snapshot this run read at its start, advanced by each
|
|
32
|
+
* write this run made; a status comment, the sensitive case, is re-read
|
|
33
|
+
* just before its write. So a write that landed but whose state was never
|
|
34
|
+
* recorded (a killed process, a failed write-back) is detected on the next
|
|
35
|
+
* run, never repeated, and a created issue is found again by its exact
|
|
36
|
+
* title instead of being created twice.
|
|
37
|
+
*
|
|
38
|
+
* States (CT-28): `pending`; `done`; `done(#N)` for a created issue, whose
|
|
39
|
+
* number dependent rows resolve on this or a later run; `failed(<first
|
|
40
|
+
* stderr line>)`; `skipped(already applied)`, or `skipped(already applied,
|
|
41
|
+
* #N)` for a create found by its title; `skipped(depends on W-NNN)` when a
|
|
42
|
+
* row it depends on is not applied. A re-run executes `pending`, `failed`
|
|
43
|
+
* and `skipped(depends on …)` rows (the last were never attempted), never a
|
|
44
|
+
* `done` or `skipped(already applied)` one (D-88).
|
|
45
|
+
*
|
|
46
|
+
* Argv is built only from a row's Action, Target and Detail, from the
|
|
47
|
+
* closed Action set (A-51), every value as `--flag=value` and a label name
|
|
48
|
+
* after `--` (A-93), so no value is ever read as an option. Bodies go
|
|
49
|
+
* through `--body-file` (or `--field=body=@<file>` for an edit) in a temp
|
|
50
|
+
* directory, each file removed after its call.
|
|
51
|
+
*/
|
|
52
|
+
import { closeSync, mkdirSync, mkdtempSync, openSync, rmSync, writeFileSync, writeSync, } from "node:fs";
|
|
53
|
+
import { tmpdir } from "node:os";
|
|
54
|
+
import { dirname, join } from "node:path";
|
|
55
|
+
import { ExitCode } from "../exit-codes.js";
|
|
56
|
+
import { hasLabel } from "./conventions.js";
|
|
57
|
+
import { firstLine, GhNotFoundError, paginatedItems } from "./gh.js";
|
|
58
|
+
import { TITLE_MAX } from "./plan.js";
|
|
59
|
+
import { computePlanDigest, MigrationPlanParseError, parseMigrationPlan, renderMigrationPlan, } from "./planfile.js";
|
|
60
|
+
import { AUTH_HINT, parseStatusRows, readSnapshot, snapshotHash, SnapshotError, SNAPSHOT_ERROR_EXIT, STATUS_MARKER, } from "./snapshot.js";
|
|
61
|
+
import { REVIEW_LABELS, summarizeMigrationPlan } from "./summary.js";
|
|
62
|
+
/** The lock against two concurrent runs, relative to the repository root (A-80). */
|
|
63
|
+
export const MIGRATION_APPLY_LOCK = "workstream/.migration-projects.lock";
|
|
64
|
+
/** The real lock: a file created exclusively (`wx`), removed on release. */
|
|
65
|
+
export function createLockFile(path) {
|
|
66
|
+
let held = false;
|
|
67
|
+
return {
|
|
68
|
+
acquire() {
|
|
69
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
70
|
+
let fd;
|
|
71
|
+
try {
|
|
72
|
+
fd = openSync(path, "wx");
|
|
73
|
+
}
|
|
74
|
+
catch (err) {
|
|
75
|
+
if (err.code === "EEXIST")
|
|
76
|
+
return false;
|
|
77
|
+
throw err;
|
|
78
|
+
}
|
|
79
|
+
try {
|
|
80
|
+
writeSync(fd, `pid ${process.pid}\nstarted ${new Date().toISOString()}\n`);
|
|
81
|
+
}
|
|
82
|
+
finally {
|
|
83
|
+
closeSync(fd);
|
|
84
|
+
}
|
|
85
|
+
held = true;
|
|
86
|
+
return true;
|
|
87
|
+
},
|
|
88
|
+
release() {
|
|
89
|
+
if (!held)
|
|
90
|
+
return;
|
|
91
|
+
held = false;
|
|
92
|
+
rmSync(path, { force: true });
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
// --- row grammar (the plan.ts Detail table) ---------------------------------------
|
|
97
|
+
const ISSUE = /^#(\d+)$/;
|
|
98
|
+
const WRITE_REF = /^W-\d{3,}$/;
|
|
99
|
+
const refOf = (text) => {
|
|
100
|
+
const m = text.match(ISSUE);
|
|
101
|
+
if (m)
|
|
102
|
+
return { number: Number(m[1]) };
|
|
103
|
+
return WRITE_REF.test(text) ? { id: text } : null;
|
|
104
|
+
};
|
|
105
|
+
const labelName = (target) => target.startsWith("label:") && target.length > "label:".length && !/[\n\r\t]/.test(target)
|
|
106
|
+
? target.slice("label:".length)
|
|
107
|
+
: null;
|
|
108
|
+
const validLabel = (name) => name.length > 0 && name.length <= 50 && !/[\n\r\t]/.test(name);
|
|
109
|
+
/** Parse one row by its Action's grammar, or say why it does not match. */
|
|
110
|
+
function parseRow(w) {
|
|
111
|
+
const d = w.detail;
|
|
112
|
+
switch (w.action) {
|
|
113
|
+
case "create-label": {
|
|
114
|
+
const name = labelName(w.target);
|
|
115
|
+
const m = d.match(/^color: ([0-9A-Fa-f]{6}); description: (.*)$/s);
|
|
116
|
+
if (name === null || !validLabel(name))
|
|
117
|
+
return "Target must read label:<name>";
|
|
118
|
+
if (!m)
|
|
119
|
+
return "Detail must read `color: <HEX>; description: <text>`";
|
|
120
|
+
if (m[2].length > 100 || /[\n\r]/.test(m[2]))
|
|
121
|
+
return "a description is one line of at most 100 characters";
|
|
122
|
+
return { action: w.action, name, color: m[1], description: m[2] };
|
|
123
|
+
}
|
|
124
|
+
case "edit-label": {
|
|
125
|
+
const name = labelName(w.target);
|
|
126
|
+
const m = d.match(/^description: (.*)$/s);
|
|
127
|
+
if (name === null || !validLabel(name))
|
|
128
|
+
return "Target must read label:<name>";
|
|
129
|
+
if (!m)
|
|
130
|
+
return "Detail must read `description: <text>`";
|
|
131
|
+
if (m[1].length > 100 || /[\n\r]/.test(m[1]))
|
|
132
|
+
return "a description is one line of at most 100 characters";
|
|
133
|
+
return { action: w.action, name, description: m[1] };
|
|
134
|
+
}
|
|
135
|
+
case "create-issue": {
|
|
136
|
+
const m = d.match(/^template: (prd|wave); label: (level: prd|level: wave); title: (.+)$/s);
|
|
137
|
+
if (w.target !== "new issue")
|
|
138
|
+
return "Target must read `new issue`";
|
|
139
|
+
if (!m)
|
|
140
|
+
return "Detail must read `template: prd|wave; label: <level label>; title: <title>`";
|
|
141
|
+
const template = m[1];
|
|
142
|
+
if (m[2] !== `level: ${template}`)
|
|
143
|
+
return "the label must be the template's `level:` label";
|
|
144
|
+
const form = template === "prd" ? /^\[PRD-[^\]\s]+\] \S/ : /^\[PRD-[^\]\s]+\] Wave [^\s:]+: \S/;
|
|
145
|
+
if (!form.test(m[3]) || /[\n\r]/.test(m[3]) || m[3].length > TITLE_MAX)
|
|
146
|
+
return `the title must be a ${template} tracking-issue title of at most ${TITLE_MAX} characters`;
|
|
147
|
+
return { action: w.action, template, label: m[2], title: m[3] };
|
|
148
|
+
}
|
|
149
|
+
case "add-label":
|
|
150
|
+
case "remove-label": {
|
|
151
|
+
const issue = refOf(w.target);
|
|
152
|
+
if (issue === null || (w.action === "remove-label" && !("number" in issue)))
|
|
153
|
+
return w.action === "add-label"
|
|
154
|
+
? "Target must read #<n> or W-<id>"
|
|
155
|
+
: "Target must read #<n>";
|
|
156
|
+
if (!validLabel(d))
|
|
157
|
+
return "Detail must be a label name";
|
|
158
|
+
return { action: w.action, issue, label: d };
|
|
159
|
+
}
|
|
160
|
+
case "link-sub-issue": {
|
|
161
|
+
const parent = refOf(w.target);
|
|
162
|
+
const m = d.match(/^sub-issue (#\d+|W-\d{3,})$/);
|
|
163
|
+
if (parent === null)
|
|
164
|
+
return "Target must read #<n> or W-<id>";
|
|
165
|
+
if (!m)
|
|
166
|
+
return "Detail must read `sub-issue <#n or W-id>`";
|
|
167
|
+
return { action: w.action, parent, child: refOf(m[1]) };
|
|
168
|
+
}
|
|
169
|
+
case "add-blocked-by": {
|
|
170
|
+
const t = w.target.match(ISSUE);
|
|
171
|
+
const m = d.match(/^blocked by #(\d+)$/);
|
|
172
|
+
if (!t)
|
|
173
|
+
return "Target must read #<n>";
|
|
174
|
+
if (!m)
|
|
175
|
+
return "Detail must read `blocked by #<n>`";
|
|
176
|
+
if (t[1] === m[1])
|
|
177
|
+
return "an issue cannot block itself";
|
|
178
|
+
return { action: w.action, blocked: Number(t[1]), blocker: Number(m[1]) };
|
|
179
|
+
}
|
|
180
|
+
case "status-comment": {
|
|
181
|
+
const issue = refOf(w.target);
|
|
182
|
+
const m = d.match(/^(create|add): (.+)$/s);
|
|
183
|
+
if (issue === null)
|
|
184
|
+
return "Target must read #<n> or W-<id>";
|
|
185
|
+
if (!m)
|
|
186
|
+
return "Detail must read `create: <rows>` or `add: <rows>`";
|
|
187
|
+
const rows = [];
|
|
188
|
+
for (const cell of m[2].split("; ")) {
|
|
189
|
+
const x = cell.match(/^(.+?) = (.+), (—|#\d+|W-\d{3,})$/s);
|
|
190
|
+
if (!x || /[|\n\r]/.test(cell))
|
|
191
|
+
return `status row ${JSON.stringify(cell)} must read \`<wave> = <state>, <#n, W-id or —>\``;
|
|
192
|
+
rows.push({ wave: x[1], state: x[2], issue: x[3] === "—" ? null : refOf(x[3]) });
|
|
193
|
+
}
|
|
194
|
+
return { action: w.action, issue, mode: m[1], rows };
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
/** The W- references a row's Target and Detail make. */
|
|
199
|
+
function rowRefs(row) {
|
|
200
|
+
const refs = [];
|
|
201
|
+
if (row.action === "add-label" || row.action === "remove-label")
|
|
202
|
+
refs.push(row.issue);
|
|
203
|
+
if (row.action === "link-sub-issue")
|
|
204
|
+
refs.push(row.parent, row.child);
|
|
205
|
+
if (row.action === "status-comment") {
|
|
206
|
+
refs.push(row.issue);
|
|
207
|
+
for (const r of row.rows)
|
|
208
|
+
if (r.issue !== null)
|
|
209
|
+
refs.push(r.issue);
|
|
210
|
+
}
|
|
211
|
+
return refs.flatMap((r) => ("id" in r ? [r.id] : []));
|
|
212
|
+
}
|
|
213
|
+
// --- states -------------------------------------------------------------------------
|
|
214
|
+
/** Applied: never run again (D-88). */
|
|
215
|
+
const isApplied = (state) => /^done(\(#\d+\))?$|^skipped\(already applied(, #\d+)?\)$/.test(state);
|
|
216
|
+
/** Runnable on this run: never attempted, or attempted and failed. */
|
|
217
|
+
const isRunnable = (state) => state === "pending" || state.startsWith("failed(") || state.startsWith("skipped(depends on ");
|
|
218
|
+
/** The issue number a create row recorded, if any. */
|
|
219
|
+
const createdNumber = (state) => {
|
|
220
|
+
const m = state.match(/^(?:done\(#(\d+)\)|skipped\(already applied, #(\d+)\))$/);
|
|
221
|
+
return m ? Number(m[1] ?? m[2]) : null;
|
|
222
|
+
};
|
|
223
|
+
const failedState = (stderr, exitCode) => {
|
|
224
|
+
const line = firstLine(stderr) || `exit ${exitCode}`;
|
|
225
|
+
return `failed(${line})`;
|
|
226
|
+
};
|
|
227
|
+
class Live {
|
|
228
|
+
labels = new Map();
|
|
229
|
+
issues = new Map();
|
|
230
|
+
constructor(snapshot) {
|
|
231
|
+
for (const l of snapshot.labels)
|
|
232
|
+
this.labels.set(l.name.toLowerCase(), { ...l });
|
|
233
|
+
for (const i of snapshot.openIssues) {
|
|
234
|
+
this.issues.set(i.number, {
|
|
235
|
+
number: i.number,
|
|
236
|
+
id: i.id,
|
|
237
|
+
title: i.title,
|
|
238
|
+
labels: [...i.labels],
|
|
239
|
+
parent: i.parent,
|
|
240
|
+
blocking: i.blocking === undefined ? undefined : [...i.blocking],
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
label(name) {
|
|
245
|
+
return this.labels.get(name.toLowerCase());
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
/** Paginated list items, or `null` when the output is not JSON. */
|
|
249
|
+
function readItems(text) {
|
|
250
|
+
try {
|
|
251
|
+
return paginatedItems(text);
|
|
252
|
+
}
|
|
253
|
+
catch {
|
|
254
|
+
return null;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
/** JSON from a read, or `undefined` when it is not JSON: that fails the row, never the run. */
|
|
258
|
+
function parseJson(text) {
|
|
259
|
+
try {
|
|
260
|
+
return JSON.parse(text);
|
|
261
|
+
}
|
|
262
|
+
catch {
|
|
263
|
+
return undefined;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
/** Interrupts the run: a thrown runner error or a failed write-back (A-80). */
|
|
267
|
+
class Interrupted extends Error {
|
|
268
|
+
id;
|
|
269
|
+
cause;
|
|
270
|
+
writeBack;
|
|
271
|
+
constructor(id, cause, writeBack = false) {
|
|
272
|
+
super(cause instanceof Error ? cause.message : String(cause));
|
|
273
|
+
this.id = id;
|
|
274
|
+
this.cause = cause;
|
|
275
|
+
this.writeBack = writeBack;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
// --- bodies -----------------------------------------------------------------------
|
|
279
|
+
function trackingBody(row) {
|
|
280
|
+
const credit = "created by `dev-tasks migrate projects --apply`";
|
|
281
|
+
if (row.template === "prd") {
|
|
282
|
+
const m = row.title.match(/^\[PRD-([^\]]+)\] (.+)$/s);
|
|
283
|
+
return [
|
|
284
|
+
`# ${m[2]}`,
|
|
285
|
+
"",
|
|
286
|
+
`PRD: [docs/requirements/prd-${m[1]}.md](/docs/requirements/prd-${m[1]}.md)`,
|
|
287
|
+
"Roadmap: [docs/roadmap.md](/docs/roadmap.md)",
|
|
288
|
+
"",
|
|
289
|
+
"## Waves",
|
|
290
|
+
"",
|
|
291
|
+
`The waves are this issue's sub-issues, and its status comment lists them; ${credit}.`,
|
|
292
|
+
"",
|
|
293
|
+
].join("\n");
|
|
294
|
+
}
|
|
295
|
+
const m = row.title.match(/^\[PRD-([^\]]+)\] Wave ([^\s:]+): (.+)$/s);
|
|
296
|
+
return [
|
|
297
|
+
`Scope: ${m[3]}`,
|
|
298
|
+
"Integration branch: TBD",
|
|
299
|
+
"",
|
|
300
|
+
"## Artifacts",
|
|
301
|
+
"",
|
|
302
|
+
"- Specification: pending",
|
|
303
|
+
"- Stories: pending",
|
|
304
|
+
"- Task plan: pending",
|
|
305
|
+
"",
|
|
306
|
+
"## Stories",
|
|
307
|
+
"",
|
|
308
|
+
`The stories are this issue's sub-issues; ${credit}.`,
|
|
309
|
+
"",
|
|
310
|
+
].join("\n");
|
|
311
|
+
}
|
|
312
|
+
const statusLine = (r) => `| ${r.wave} | ${r.state} | ${r.issue} |`;
|
|
313
|
+
/** A new PRD status comment: the marker first, the prefix line, the table (github-ops § Status comment). */
|
|
314
|
+
function statusCommentBody(issue, rows) {
|
|
315
|
+
return [
|
|
316
|
+
STATUS_MARKER,
|
|
317
|
+
`🔄 Update: migrated to projects-tracking by \`dev-tasks migrate projects --apply\` — Refs #${issue}`,
|
|
318
|
+
"",
|
|
319
|
+
"| Wave | State | Wave issue |",
|
|
320
|
+
"| ---- | ----- | ---------- |",
|
|
321
|
+
...rows.map(statusLine),
|
|
322
|
+
"",
|
|
323
|
+
].join("\n");
|
|
324
|
+
}
|
|
325
|
+
/** The existing comment with `rows` added after its PRD table's last row; nothing else changes (A-67). */
|
|
326
|
+
function withRows(body, rows) {
|
|
327
|
+
const eol = body.includes("\r\n") ? "\r\n" : "\n";
|
|
328
|
+
const lines = body.split(/\r?\n/);
|
|
329
|
+
const header = lines.findIndex((l) => {
|
|
330
|
+
const cells = l
|
|
331
|
+
.trim()
|
|
332
|
+
.split("|")
|
|
333
|
+
.map((c) => c.trim());
|
|
334
|
+
return l.trim().startsWith("|") && cells[1] === "Wave" && cells[3] === "Wave issue";
|
|
335
|
+
});
|
|
336
|
+
const added = rows.map(statusLine);
|
|
337
|
+
if (header === -1) {
|
|
338
|
+
const table = ["| Wave | State | Wave issue |", "| ---- | ----- | ---------- |", ...added];
|
|
339
|
+
return [...lines, "", ...table].join(eol);
|
|
340
|
+
}
|
|
341
|
+
let end = header + 1;
|
|
342
|
+
while (end < lines.length && lines[end].trim().startsWith("|"))
|
|
343
|
+
end++;
|
|
344
|
+
return [...lines.slice(0, end), ...added, ...lines.slice(end)].join(eol);
|
|
345
|
+
}
|
|
346
|
+
let bodyCount = 0;
|
|
347
|
+
async function withBody(ctx, content, use) {
|
|
348
|
+
const path = join(ctx.bodyDir(), `body-${process.pid}-${++bodyCount}.md`);
|
|
349
|
+
writeFileSync(path, content);
|
|
350
|
+
try {
|
|
351
|
+
return await use(path);
|
|
352
|
+
}
|
|
353
|
+
finally {
|
|
354
|
+
rmSync(path, { force: true });
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
/** A read the row needs; a non-zero exit fails the row with its reason. */
|
|
358
|
+
async function read(ctx, argv) {
|
|
359
|
+
const r = await ctx.runner(argv);
|
|
360
|
+
return r.exitCode === 0
|
|
361
|
+
? { ok: true, stdout: r.stdout }
|
|
362
|
+
: { ok: false, state: failedState(r.stderr, r.exitCode) };
|
|
363
|
+
}
|
|
364
|
+
async function write(ctx, id, argv) {
|
|
365
|
+
ctx.observer?.beforeWrite?.(id, argv);
|
|
366
|
+
return ctx.runner(argv);
|
|
367
|
+
}
|
|
368
|
+
/** Resolve a row's W- reference to the issue its create row made, checking it is that issue. */
|
|
369
|
+
function resolveRef(ctx, ref) {
|
|
370
|
+
if ("number" in ref)
|
|
371
|
+
return ref;
|
|
372
|
+
const create = ctx.byId.get(ref.id);
|
|
373
|
+
const n = createdNumber(create.state);
|
|
374
|
+
if (n === null)
|
|
375
|
+
return { state: `failed(${ref.id} has no recorded issue number)` };
|
|
376
|
+
// A hand-edited record must never redirect a write: #N must be the issue the row created (D-101).
|
|
377
|
+
const issue = ctx.live.issues.get(n);
|
|
378
|
+
const title = ctx.parsed.get(ref.id).title;
|
|
379
|
+
if (issue === undefined || issue.title !== title)
|
|
380
|
+
return { state: "failed(target changed)" };
|
|
381
|
+
return { number: n };
|
|
382
|
+
}
|
|
383
|
+
async function issueId(ctx, n) {
|
|
384
|
+
const issue = ctx.live.issues.get(n);
|
|
385
|
+
if (issue.id !== undefined)
|
|
386
|
+
return issue.id;
|
|
387
|
+
const r = await read(ctx, ["api", `repos/${ctx.repo}/issues/${n}`]);
|
|
388
|
+
if (!r.ok)
|
|
389
|
+
return { state: r.state };
|
|
390
|
+
const id = parseJson(r.stdout)?.id;
|
|
391
|
+
if (typeof id !== "number")
|
|
392
|
+
return { state: `failed(no id for #${n})` };
|
|
393
|
+
issue.id = id;
|
|
394
|
+
return id;
|
|
395
|
+
}
|
|
396
|
+
/** Run one row: check its target, write, and return its new state. */
|
|
397
|
+
async function runRow(ctx, w) {
|
|
398
|
+
const row = ctx.parsed.get(w.id);
|
|
399
|
+
for (const dep of [...new Set([...w.dependsOn, ...rowRefs(row)])]) {
|
|
400
|
+
const s = ctx.byId.get(dep)?.state ?? "";
|
|
401
|
+
if (!isApplied(s))
|
|
402
|
+
return `skipped(depends on ${dep})`;
|
|
403
|
+
}
|
|
404
|
+
const { live, repo } = ctx;
|
|
405
|
+
const repoFlag = `--repo=${repo}`;
|
|
406
|
+
const CHANGED = "failed(target changed)";
|
|
407
|
+
const APPLIED = "skipped(already applied)";
|
|
408
|
+
const comma = (l) => l.includes(",")
|
|
409
|
+
? "failed(label name contains a comma, which gh splits; apply it by hand)"
|
|
410
|
+
: null;
|
|
411
|
+
switch (row.action) {
|
|
412
|
+
case "create-label": {
|
|
413
|
+
const l = live.label(row.name);
|
|
414
|
+
if (l !== undefined)
|
|
415
|
+
return l.description === row.description ? APPLIED : CHANGED;
|
|
416
|
+
const r = await write(ctx, w.id, [
|
|
417
|
+
"label",
|
|
418
|
+
"create",
|
|
419
|
+
repoFlag,
|
|
420
|
+
`--color=${row.color}`,
|
|
421
|
+
`--description=${row.description}`,
|
|
422
|
+
"--",
|
|
423
|
+
row.name,
|
|
424
|
+
]);
|
|
425
|
+
if (r.exitCode !== 0)
|
|
426
|
+
return failedState(r.stderr, r.exitCode);
|
|
427
|
+
live.labels.set(row.name.toLowerCase(), { name: row.name, description: row.description });
|
|
428
|
+
return "done";
|
|
429
|
+
}
|
|
430
|
+
case "edit-label": {
|
|
431
|
+
const l = live.label(row.name);
|
|
432
|
+
if (l === undefined)
|
|
433
|
+
return CHANGED;
|
|
434
|
+
if (l.description === row.description)
|
|
435
|
+
return APPLIED;
|
|
436
|
+
const r = await write(ctx, w.id, [
|
|
437
|
+
"label",
|
|
438
|
+
"edit",
|
|
439
|
+
repoFlag,
|
|
440
|
+
`--description=${row.description}`,
|
|
441
|
+
"--",
|
|
442
|
+
row.name,
|
|
443
|
+
]);
|
|
444
|
+
if (r.exitCode !== 0)
|
|
445
|
+
return failedState(r.stderr, r.exitCode);
|
|
446
|
+
l.description = row.description;
|
|
447
|
+
return "done";
|
|
448
|
+
}
|
|
449
|
+
case "create-issue": {
|
|
450
|
+
const same = [...live.issues.values()].filter((i) => i.title === row.title);
|
|
451
|
+
if (same.length === 1 && hasLabel(same[0].labels, row.label))
|
|
452
|
+
return `skipped(already applied, #${same[0].number})`;
|
|
453
|
+
if (same.length > 0)
|
|
454
|
+
return CHANGED;
|
|
455
|
+
const r = await withBody(ctx, trackingBody(row), (path) => write(ctx, w.id, [
|
|
456
|
+
"issue",
|
|
457
|
+
"create",
|
|
458
|
+
repoFlag,
|
|
459
|
+
`--title=${row.title}`,
|
|
460
|
+
`--label=${row.label}`,
|
|
461
|
+
`--body-file=${path}`,
|
|
462
|
+
]));
|
|
463
|
+
if (r.exitCode !== 0)
|
|
464
|
+
return failedState(r.stderr, r.exitCode);
|
|
465
|
+
const m = r.stdout.match(/\/issues\/(\d+)\s*$/m);
|
|
466
|
+
if (!m)
|
|
467
|
+
return `failed(created, but gh printed no issue URL: ${firstLine(r.stdout) || "empty output"})`;
|
|
468
|
+
const n = Number(m[1]);
|
|
469
|
+
live.issues.set(n, {
|
|
470
|
+
number: n,
|
|
471
|
+
title: row.title,
|
|
472
|
+
labels: [row.label],
|
|
473
|
+
parent: null,
|
|
474
|
+
blocking: [],
|
|
475
|
+
});
|
|
476
|
+
return `done(#${n})`;
|
|
477
|
+
}
|
|
478
|
+
case "add-label":
|
|
479
|
+
case "remove-label": {
|
|
480
|
+
const ref = resolveRef(ctx, row.issue);
|
|
481
|
+
if ("state" in ref)
|
|
482
|
+
return ref.state;
|
|
483
|
+
const issue = live.issues.get(ref.number);
|
|
484
|
+
if (issue === undefined)
|
|
485
|
+
return CHANGED;
|
|
486
|
+
const present = hasLabel(issue.labels, row.label);
|
|
487
|
+
if (row.action === "add-label" ? present : !present)
|
|
488
|
+
return APPLIED;
|
|
489
|
+
const bad = comma(row.label);
|
|
490
|
+
if (bad !== null)
|
|
491
|
+
return bad;
|
|
492
|
+
const flag = row.action === "add-label" ? "--add-label" : "--remove-label";
|
|
493
|
+
const r = await write(ctx, w.id, [
|
|
494
|
+
"issue",
|
|
495
|
+
"edit",
|
|
496
|
+
String(ref.number),
|
|
497
|
+
repoFlag,
|
|
498
|
+
`${flag}=${row.label}`,
|
|
499
|
+
]);
|
|
500
|
+
if (r.exitCode !== 0)
|
|
501
|
+
return failedState(r.stderr, r.exitCode);
|
|
502
|
+
issue.labels =
|
|
503
|
+
row.action === "add-label"
|
|
504
|
+
? [...issue.labels, live.label(row.label)?.name ?? row.label]
|
|
505
|
+
: issue.labels.filter((l) => l.toLowerCase() !== row.label.toLowerCase());
|
|
506
|
+
return "done";
|
|
507
|
+
}
|
|
508
|
+
case "link-sub-issue": {
|
|
509
|
+
const parent = resolveRef(ctx, row.parent);
|
|
510
|
+
if ("state" in parent)
|
|
511
|
+
return parent.state;
|
|
512
|
+
const child = resolveRef(ctx, row.child);
|
|
513
|
+
if ("state" in child)
|
|
514
|
+
return child.state;
|
|
515
|
+
const c = live.issues.get(child.number);
|
|
516
|
+
if (c === undefined || !live.issues.has(parent.number))
|
|
517
|
+
return CHANGED;
|
|
518
|
+
if (c.parent === undefined) {
|
|
519
|
+
const r = await ctx.runner(["api", `repos/${repo}/issues/${child.number}/parent`]);
|
|
520
|
+
if (r.exitCode !== 0 && !/HTTP 404/.test(r.stderr))
|
|
521
|
+
return failedState(r.stderr, r.exitCode);
|
|
522
|
+
const parentNumber = parseJson(r.stdout)?.number;
|
|
523
|
+
if (r.exitCode === 0 && typeof parentNumber !== "number")
|
|
524
|
+
return `failed(unreadable parent of #${child.number})`;
|
|
525
|
+
c.parent = r.exitCode === 0 ? parentNumber : null;
|
|
526
|
+
}
|
|
527
|
+
if (c.parent === parent.number)
|
|
528
|
+
return APPLIED;
|
|
529
|
+
if (c.parent !== null)
|
|
530
|
+
return CHANGED;
|
|
531
|
+
const id = await issueId(ctx, child.number);
|
|
532
|
+
if (typeof id !== "number")
|
|
533
|
+
return id.state;
|
|
534
|
+
const r = await write(ctx, w.id, [
|
|
535
|
+
"api",
|
|
536
|
+
"-X",
|
|
537
|
+
"POST",
|
|
538
|
+
`repos/${repo}/issues/${parent.number}/sub_issues`,
|
|
539
|
+
`--field=sub_issue_id=${id}`,
|
|
540
|
+
]);
|
|
541
|
+
if (r.exitCode !== 0)
|
|
542
|
+
return failedState(r.stderr, r.exitCode);
|
|
543
|
+
c.parent = parent.number;
|
|
544
|
+
return "done";
|
|
545
|
+
}
|
|
546
|
+
case "add-blocked-by": {
|
|
547
|
+
const blocker = live.issues.get(row.blocker);
|
|
548
|
+
if (blocker === undefined || !live.issues.has(row.blocked))
|
|
549
|
+
return CHANGED;
|
|
550
|
+
if (blocker.blocking === undefined) {
|
|
551
|
+
const r = await read(ctx, [
|
|
552
|
+
"api",
|
|
553
|
+
"--paginate",
|
|
554
|
+
`repos/${repo}/issues/${row.blocker}/dependencies/blocking?per_page=100`,
|
|
555
|
+
]);
|
|
556
|
+
if (!r.ok)
|
|
557
|
+
return r.state;
|
|
558
|
+
const items = readItems(r.stdout);
|
|
559
|
+
if (items === null)
|
|
560
|
+
return `failed(unreadable blocking list of #${row.blocker})`;
|
|
561
|
+
blocker.blocking = items.map((i) => i.number);
|
|
562
|
+
}
|
|
563
|
+
if (blocker.blocking.includes(row.blocked))
|
|
564
|
+
return APPLIED;
|
|
565
|
+
const id = await issueId(ctx, row.blocker);
|
|
566
|
+
if (typeof id !== "number")
|
|
567
|
+
return id.state;
|
|
568
|
+
const r = await write(ctx, w.id, [
|
|
569
|
+
"api",
|
|
570
|
+
"-X",
|
|
571
|
+
"POST",
|
|
572
|
+
`repos/${repo}/issues/${row.blocked}/dependencies/blocked_by`,
|
|
573
|
+
`--field=issue_id=${id}`,
|
|
574
|
+
]);
|
|
575
|
+
if (r.exitCode !== 0)
|
|
576
|
+
return failedState(r.stderr, r.exitCode);
|
|
577
|
+
blocker.blocking.push(row.blocked);
|
|
578
|
+
return "done";
|
|
579
|
+
}
|
|
580
|
+
case "status-comment": {
|
|
581
|
+
const target = resolveRef(ctx, row.issue);
|
|
582
|
+
if ("state" in target)
|
|
583
|
+
return target.state;
|
|
584
|
+
if (!live.issues.has(target.number))
|
|
585
|
+
return CHANGED;
|
|
586
|
+
const rows = [];
|
|
587
|
+
for (const r of row.rows) {
|
|
588
|
+
if (r.issue === null)
|
|
589
|
+
rows.push({ wave: r.wave, state: r.state, issue: "—" });
|
|
590
|
+
else {
|
|
591
|
+
const n = resolveRef(ctx, r.issue);
|
|
592
|
+
if ("state" in n)
|
|
593
|
+
return n.state;
|
|
594
|
+
rows.push({ wave: r.wave, state: r.state, issue: `#${n.number}` });
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
// The marker procedure, read just before the write (github-ops § Status comment, D-92).
|
|
598
|
+
const list = await read(ctx, [
|
|
599
|
+
"api",
|
|
600
|
+
"--paginate",
|
|
601
|
+
`repos/${repo}/issues/${target.number}/comments?per_page=100`,
|
|
602
|
+
]);
|
|
603
|
+
if (!list.ok)
|
|
604
|
+
return list.state;
|
|
605
|
+
const comments = readItems(list.stdout);
|
|
606
|
+
if (comments === null)
|
|
607
|
+
return `failed(unreadable comments of #${target.number})`;
|
|
608
|
+
const marked = comments
|
|
609
|
+
.map((c) => c)
|
|
610
|
+
.filter((c) => typeof c.id === "number" &&
|
|
611
|
+
typeof c.body === "string" &&
|
|
612
|
+
c.body.startsWith(STATUS_MARKER))
|
|
613
|
+
.sort((a, b) => a.id - b.id);
|
|
614
|
+
if (marked.length === 0) {
|
|
615
|
+
if (row.mode === "add")
|
|
616
|
+
return CHANGED;
|
|
617
|
+
const r = await withBody(ctx, statusCommentBody(target.number, rows), (path) => write(ctx, w.id, [
|
|
618
|
+
"issue",
|
|
619
|
+
"comment",
|
|
620
|
+
String(target.number),
|
|
621
|
+
repoFlag,
|
|
622
|
+
`--body-file=${path}`,
|
|
623
|
+
]));
|
|
624
|
+
return r.exitCode === 0 ? "done" : failedState(r.stderr, r.exitCode);
|
|
625
|
+
}
|
|
626
|
+
const present = new Set(parseStatusRows(marked[0].body).map((r) => r.wave));
|
|
627
|
+
const missing = rows.filter((r) => !present.has(r.wave));
|
|
628
|
+
if (missing.length === 0)
|
|
629
|
+
return APPLIED;
|
|
630
|
+
if (row.mode === "create")
|
|
631
|
+
return CHANGED;
|
|
632
|
+
const r = await withBody(ctx, withRows(marked[0].body, missing), (path) => write(ctx, w.id, [
|
|
633
|
+
"api",
|
|
634
|
+
"-X",
|
|
635
|
+
"PATCH",
|
|
636
|
+
`repos/${repo}/issues/comments/${marked[0].id}`,
|
|
637
|
+
`--field=body=@${path}`,
|
|
638
|
+
]));
|
|
639
|
+
return r.exitCode === 0 ? "done" : failedState(r.stderr, r.exitCode);
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
function result(exitCode, planFile, plan, error) {
|
|
644
|
+
const summary = plan === null ? null : summarizeMigrationPlan(plan);
|
|
645
|
+
if (summary !== null) {
|
|
646
|
+
const writes = plan.batches.flatMap((b) => b.writes);
|
|
647
|
+
summary.next =
|
|
648
|
+
error !== undefined
|
|
649
|
+
? "fix the cause above, then run --apply again"
|
|
650
|
+
: writes.some((w) => w.state.startsWith("failed("))
|
|
651
|
+
? "fix what failed (the State column says why), then run --apply again"
|
|
652
|
+
: writes.some((w) => isRunnable(w.state))
|
|
653
|
+
? "run --apply again to finish the writes left"
|
|
654
|
+
: "run the Project batch (batch 7) with your own session, then set each step's State to done";
|
|
655
|
+
}
|
|
656
|
+
return {
|
|
657
|
+
exitCode,
|
|
658
|
+
repo: plan?.repo ?? null,
|
|
659
|
+
planFile,
|
|
660
|
+
summary,
|
|
661
|
+
steps: plan?.batches.flatMap((b) => b.steps.map((s) => s.step)) ?? [],
|
|
662
|
+
...(error !== undefined ? { error } : {}),
|
|
663
|
+
};
|
|
664
|
+
}
|
|
665
|
+
/** `migrate projects --apply`. Returns an exit code from `core/exit-codes.ts`; never throws for an expected failure. */
|
|
666
|
+
export async function runMigrationApply(options) {
|
|
667
|
+
const { planFile, store, runner } = options;
|
|
668
|
+
// 1. The plan file, every row by its grammar (A-51).
|
|
669
|
+
const text = store.read(planFile);
|
|
670
|
+
if (text === undefined)
|
|
671
|
+
return result(ExitCode.InvalidUsage, planFile, null, `${planFile} not found; run --plan first, review it, and approve it`);
|
|
672
|
+
let plan;
|
|
673
|
+
let digest;
|
|
674
|
+
try {
|
|
675
|
+
({ plan, planDigest: digest } = parseMigrationPlan(text));
|
|
676
|
+
}
|
|
677
|
+
catch (err) {
|
|
678
|
+
if (!(err instanceof MigrationPlanParseError))
|
|
679
|
+
throw err;
|
|
680
|
+
return result(ExitCode.InvalidUsage, planFile, null, `${planFile} is not a valid migration plan (${err.message})`);
|
|
681
|
+
}
|
|
682
|
+
const writes = plan.batches.filter((b) => b.runner === "cli").flatMap((b) => b.writes);
|
|
683
|
+
const byId = new Map(writes.map((w) => [w.id, w]));
|
|
684
|
+
const parsed = new Map();
|
|
685
|
+
for (const w of writes) {
|
|
686
|
+
const row = parseRow(w);
|
|
687
|
+
if (typeof row === "string")
|
|
688
|
+
return result(ExitCode.InvalidUsage, planFile, null, `${planFile}: ${w.id} (${w.action}): ${row}`);
|
|
689
|
+
for (const ref of rowRefs(row)) {
|
|
690
|
+
if (byId.get(ref)?.action !== "create-issue")
|
|
691
|
+
return result(ExitCode.InvalidUsage, planFile, null, `${planFile}: ${w.id} refers to ${ref}, which is not a create-issue row`);
|
|
692
|
+
}
|
|
693
|
+
parsed.set(w.id, row);
|
|
694
|
+
}
|
|
695
|
+
// 2. gh (D-96, A-88).
|
|
696
|
+
try {
|
|
697
|
+
const auth = await runner(["auth", "status", "--hostname", "github.com"]);
|
|
698
|
+
if (auth.exitCode !== 0)
|
|
699
|
+
return result(ExitCode.DependencyError, planFile, plan, `gh is not authenticated to github.com (${firstLine(auth.stderr) || `exit ${auth.exitCode}`}); ${AUTH_HINT}`);
|
|
700
|
+
}
|
|
701
|
+
catch (err) {
|
|
702
|
+
if (err instanceof GhNotFoundError)
|
|
703
|
+
return result(ExitCode.DependencyError, planFile, plan, `gh is not installed or not on PATH; install it, then ${AUTH_HINT}`);
|
|
704
|
+
throw err;
|
|
705
|
+
}
|
|
706
|
+
// 3. Approval (D-80, A-79) and 4. the digest (A-51).
|
|
707
|
+
if (!plan.approved)
|
|
708
|
+
return result(ExitCode.ReconciliationConflict, planFile, plan, `${planFile} is not approved: its header must hold exactly one line reading \`Approved: yes\`; review the plan, then approve it`);
|
|
709
|
+
if (computePlanDigest(plan.batches) !== digest)
|
|
710
|
+
return result(ExitCode.ReconciliationConflict, planFile, plan, `${planFile}'s Plan digest: no longer matches its rows (a row was edited after the plan was generated); run --plan again and approve the new plan`);
|
|
711
|
+
// 5. The lock (A-80).
|
|
712
|
+
if (!options.lock.acquire())
|
|
713
|
+
return result(ExitCode.ReconciliationConflict, planFile, plan, `another --apply holds ${MIGRATION_APPLY_LOCK}; wait for it to finish, or remove the file if no run is active`);
|
|
714
|
+
let ownDir = null;
|
|
715
|
+
try {
|
|
716
|
+
if (store.read(planFile) !== text)
|
|
717
|
+
return result(ExitCode.ReconciliationConflict, planFile, plan, `${planFile} changed while --apply started; run --apply again`);
|
|
718
|
+
if (!writes.some((w) => isRunnable(w.state)))
|
|
719
|
+
return result(ExitCode.Success, planFile, plan);
|
|
720
|
+
// 6. The snapshot: compared only while every row is pending (D-101).
|
|
721
|
+
let snapshot;
|
|
722
|
+
try {
|
|
723
|
+
snapshot = await readSnapshot({ runner, reader: options.reader, repo: plan.repo });
|
|
724
|
+
}
|
|
725
|
+
catch (err) {
|
|
726
|
+
if (err instanceof SnapshotError)
|
|
727
|
+
return result(SNAPSHOT_ERROR_EXIT[err.kind], planFile, plan, err.message);
|
|
728
|
+
throw err;
|
|
729
|
+
}
|
|
730
|
+
if (writes.every((w) => w.state === "pending") && snapshotHash(snapshot) !== plan.snapshot)
|
|
731
|
+
return result(ExitCode.ReconciliationConflict, planFile, plan, "the repository changed since the plan was generated (its snapshot hash differs); regenerate the plan with --plan and approve it again");
|
|
732
|
+
const ctx = {
|
|
733
|
+
repo: snapshot.repo,
|
|
734
|
+
runner,
|
|
735
|
+
live: new Live(snapshot),
|
|
736
|
+
byId,
|
|
737
|
+
parsed,
|
|
738
|
+
observer: options.observer,
|
|
739
|
+
bodyDir: () => {
|
|
740
|
+
if (options.tmpDir !== undefined)
|
|
741
|
+
return options.tmpDir;
|
|
742
|
+
ownDir ??= mkdtempSync(join(tmpdir(), "dev-tasks-migrate-"));
|
|
743
|
+
return ownDir;
|
|
744
|
+
},
|
|
745
|
+
};
|
|
746
|
+
for (const w of writes) {
|
|
747
|
+
if (!isRunnable(w.state))
|
|
748
|
+
continue;
|
|
749
|
+
let state;
|
|
750
|
+
try {
|
|
751
|
+
state = await runRow(ctx, w);
|
|
752
|
+
}
|
|
753
|
+
catch (err) {
|
|
754
|
+
throw new Interrupted(w.id, err);
|
|
755
|
+
}
|
|
756
|
+
if (state === w.state)
|
|
757
|
+
continue;
|
|
758
|
+
const previous = w.state;
|
|
759
|
+
w.state = state;
|
|
760
|
+
try {
|
|
761
|
+
store.write(planFile, renderMigrationPlan(plan));
|
|
762
|
+
}
|
|
763
|
+
catch (err) {
|
|
764
|
+
// The file still holds the previous state; so must the result (F-6).
|
|
765
|
+
w.state = previous;
|
|
766
|
+
throw new Interrupted(w.id, err, true);
|
|
767
|
+
}
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
catch (err) {
|
|
771
|
+
if (!(err instanceof Interrupted))
|
|
772
|
+
throw err;
|
|
773
|
+
return result(ExitCode.GeneralError, planFile, plan, err.writeBack
|
|
774
|
+
? `could not record ${err.id}'s state in ${planFile} (${err.message}); the write may have landed: run --apply again, which checks it first (if no state was recorded yet, it will ask you to regenerate the plan with --plan)`
|
|
775
|
+
: `interrupted at ${err.id} (${err.message}); the states so far are in ${planFile}: run --apply again to resume`);
|
|
776
|
+
}
|
|
777
|
+
finally {
|
|
778
|
+
options.lock.release();
|
|
779
|
+
if (ownDir !== null)
|
|
780
|
+
rmSync(ownDir, { recursive: true, force: true });
|
|
781
|
+
}
|
|
782
|
+
const failed = writes.some((w) => w.state.startsWith("failed("));
|
|
783
|
+
return result(failed ? ExitCode.GeneralError : ExitCode.Success, planFile, plan);
|
|
784
|
+
}
|
|
785
|
+
// --- output -------------------------------------------------------------------------
|
|
786
|
+
/** The one-screen summary: per-batch states, what still waits for an answer, batch 7's steps. */
|
|
787
|
+
export function formatMigrationApplySummary(r) {
|
|
788
|
+
if (r.summary === null)
|
|
789
|
+
return `migrate projects --apply: ${r.error ?? "failed"}`;
|
|
790
|
+
const s = r.summary;
|
|
791
|
+
const lines = [`Migration apply for ${r.repo}`, "", "Batches:"];
|
|
792
|
+
for (const b of s.batches) {
|
|
793
|
+
if (b.runner === "user") {
|
|
794
|
+
lines.push(` ${b.key}. ${b.name} (you run these): ${b.done} of ${b.pending + b.done} steps done`);
|
|
795
|
+
continue;
|
|
796
|
+
}
|
|
797
|
+
lines.push(` ${b.key}. ${b.name}: ${b.done} done, ${b.skipped} skipped, ${b.failed} failed, ${b.pending} pending`);
|
|
798
|
+
}
|
|
799
|
+
lines.push("", "Needs your answer (no write until answered and planned again):");
|
|
800
|
+
for (const [k, label] of Object.entries(REVIEW_LABELS))
|
|
801
|
+
lines.push(` ${label}: ${s.review[k]}`);
|
|
802
|
+
if (r.steps.length > 0) {
|
|
803
|
+
lines.push("", "Project cleanup (you run these; --apply never does):");
|
|
804
|
+
r.steps.forEach((step, i) => lines.push(` ${i + 1}. ${step}`));
|
|
805
|
+
}
|
|
806
|
+
lines.push("", `Plan file: ${r.planFile}`, `Next: ${s.next}`);
|
|
807
|
+
return lines.join("\n");
|
|
808
|
+
}
|
|
809
|
+
/** The `--json` shape (A-64), `mode: "apply"`. */
|
|
810
|
+
export function migrationApplyJson(r) {
|
|
811
|
+
return {
|
|
812
|
+
command: "migrate projects",
|
|
813
|
+
mode: "apply",
|
|
814
|
+
repo: r.repo,
|
|
815
|
+
planFile: r.planFile,
|
|
816
|
+
batches: (r.summary?.batches ?? []).map((b) => ({
|
|
817
|
+
name: b.name,
|
|
818
|
+
pending: b.pending,
|
|
819
|
+
done: b.done,
|
|
820
|
+
failed: b.failed,
|
|
821
|
+
skipped: b.skipped,
|
|
822
|
+
})),
|
|
823
|
+
review: r.summary?.review ?? null,
|
|
824
|
+
exitCode: r.exitCode,
|
|
825
|
+
...(r.error !== undefined ? { error: r.error } : {}),
|
|
826
|
+
};
|
|
827
|
+
}
|
|
828
|
+
//# sourceMappingURL=apply.js.map
|