@davidbyoung/software-factory 0.0.0-stage → 0.1.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-plugin/plugin.json +5 -0
- package/LICENSE +21 -0
- package/README.md +135 -2
- package/actions/factory-agent/action.yml +218 -0
- package/agents/factory-test-reviewer.md +129 -0
- package/agents/factory-ticket-readiness.md +107 -0
- package/agents/factory-ui-reviewer.md +160 -0
- package/bin/factory.js +45 -0
- package/bin/launch.js +67 -0
- package/dist/board.d.mts +128 -0
- package/dist/board.mjs +456 -0
- package/dist/build.d.mts +18 -0
- package/dist/build.mjs +60 -0
- package/dist/cli.d.mts +17 -0
- package/dist/cli.mjs +264 -0
- package/dist/code-host/github.d.mts +118 -0
- package/dist/code-host/github.mjs +419 -0
- package/dist/code-host/index.d.mts +9 -0
- package/dist/code-host/index.mjs +18 -0
- package/dist/code-host/types.d.mts +205 -0
- package/dist/code-host/types.mjs +12 -0
- package/dist/config.d.mts +166 -0
- package/dist/config.mjs +548 -0
- package/dist/deps.d.mts +32 -0
- package/dist/deps.mjs +126 -0
- package/dist/doctor.d.mts +34 -0
- package/dist/doctor.mjs +188 -0
- package/dist/guard-commit.d.mts +41 -0
- package/dist/guard-commit.mjs +205 -0
- package/dist/guard-permission.d.mts +15 -0
- package/dist/guard-permission.mjs +52 -0
- package/dist/guard-ticket-text.d.mts +27 -0
- package/dist/guard-ticket-text.mjs +288 -0
- package/dist/guard.d.mts +4 -0
- package/dist/guard.mjs +37 -0
- package/dist/init.d.mts +58 -0
- package/dist/init.mjs +556 -0
- package/dist/lib/clock.d.mts +14 -0
- package/dist/lib/clock.mjs +36 -0
- package/dist/lib/gh.d.mts +28 -0
- package/dist/lib/gh.mjs +92 -0
- package/dist/lib/glob.d.mts +2 -0
- package/dist/lib/glob.mjs +41 -0
- package/dist/lib/pricing.d.mts +25 -0
- package/dist/lib/pricing.mjs +77 -0
- package/dist/lib/usage.d.mts +34 -0
- package/dist/lib/usage.mjs +128 -0
- package/dist/lib/work-status.d.mts +251 -0
- package/dist/lib/work-status.mjs +510 -0
- package/dist/merge-policy.d.mts +66 -0
- package/dist/merge-policy.mjs +146 -0
- package/dist/parts.d.mts +39 -0
- package/dist/parts.mjs +138 -0
- package/dist/poller.d.mts +61 -0
- package/dist/poller.mjs +247 -0
- package/dist/pr-watch.d.mts +45 -0
- package/dist/pr-watch.mjs +244 -0
- package/dist/pr.d.mts +65 -0
- package/dist/pr.mjs +571 -0
- package/dist/review-cost.d.mts +18 -0
- package/dist/review-cost.mjs +95 -0
- package/dist/review-tools.d.mts +5 -0
- package/dist/review-tools.mjs +46 -0
- package/dist/review-verdict.d.mts +50 -0
- package/dist/review-verdict.mjs +171 -0
- package/dist/reviewers.d.mts +54 -0
- package/dist/reviewers.mjs +312 -0
- package/dist/runner/github-actions.d.mts +12 -0
- package/dist/runner/github-actions.mjs +97 -0
- package/dist/runner/index.d.mts +9 -0
- package/dist/runner/index.mjs +22 -0
- package/dist/runner/types.d.mts +60 -0
- package/dist/runner/types.mjs +7 -0
- package/dist/session-start.d.mts +20 -0
- package/dist/session-start.mjs +115 -0
- package/dist/site/capture.d.mts +44 -0
- package/dist/site/capture.mjs +114 -0
- package/dist/site/measure.d.mts +83 -0
- package/dist/site/measure.mjs +225 -0
- package/dist/site/pages.d.mts +10 -0
- package/dist/site/pages.mjs +71 -0
- package/dist/site/pixels.d.mts +47 -0
- package/dist/site/pixels.mjs +157 -0
- package/dist/site/servers.d.mts +47 -0
- package/dist/site/servers.mjs +141 -0
- package/dist/site.d.mts +60 -0
- package/dist/site.mjs +341 -0
- package/dist/status.d.mts +100 -0
- package/dist/status.mjs +514 -0
- package/dist/sync.d.mts +59 -0
- package/dist/sync.mjs +292 -0
- package/dist/ticket.d.mts +61 -0
- package/dist/ticket.mjs +562 -0
- package/dist/tickets/github.d.mts +128 -0
- package/dist/tickets/github.mjs +491 -0
- package/dist/tickets/index.d.mts +15 -0
- package/dist/tickets/index.mjs +27 -0
- package/dist/tickets/transport.d.mts +42 -0
- package/dist/tickets/transport.mjs +158 -0
- package/dist/tickets/types.d.mts +191 -0
- package/dist/tickets/types.mjs +76 -0
- package/docs/builds.md +227 -0
- package/docs/cli.md +221 -0
- package/docs/configuration.md +809 -0
- package/docs/design.md +106 -0
- package/docs/developing.md +400 -0
- package/docs/existing-repo.md +138 -0
- package/docs/github/reference.md +288 -0
- package/docs/github/setup.md +312 -0
- package/docs/glossary.md +106 -0
- package/docs/limitations.md +115 -0
- package/docs/plugins.md +192 -0
- package/docs/quick-start.md +222 -0
- package/docs/removing.md +76 -0
- package/docs/skills.md +270 -0
- package/docs/updating.md +108 -0
- package/docs/workflow.md +195 -0
- package/package.json +67 -4
- package/poller.Dockerfile +21 -0
- package/review.md +170 -0
- package/rules.md +313 -0
- package/settings.schema.json +411 -0
- package/skills/factory-address-review/SKILL.md +547 -0
- package/skills/factory-address-review/patch-coverage.py +68 -0
- package/skills/factory-build/SKILL.md +412 -0
- package/skills/factory-deps/SKILL.md +185 -0
- package/skills/factory-design/SKILL.md +159 -0
- package/skills/factory-diagnose/SKILL.md +93 -0
- package/skills/factory-mutate/SKILL.md +76 -0
- package/skills/factory-refine/SKILL.md +417 -0
- package/skills/factory-resolve-conflicts/SKILL.md +235 -0
- package/workflows/factory-board-sync.yml +91 -0
- package/workflows/factory-build.yml +72 -0
- package/workflows/factory-check.yml +37 -0
- package/workflows/factory-claude-review.yml +133 -0
- package/workflows/factory-conflicts.yml +69 -0
- package/workflows/factory-deps-auto-merge.yml +43 -0
- package/workflows/factory-deps.yml +59 -0
- package/workflows/factory-merge-policy.yml +121 -0
- package/workflows/factory-review-verdict.yml +54 -0
- package/workflows/factory-review.yml +75 -0
package/dist/init.mjs
ADDED
|
@@ -0,0 +1,556 @@
|
|
|
1
|
+
// `factory init`: sets a repository up in one run. It says what it found,
|
|
2
|
+
// asks which parts to turn on, shows everything it will do, and once told to
|
|
3
|
+
// go ahead writes the settings, syncs, makes or connects the board, stores
|
|
4
|
+
// every secret through the runner, and changes the repository's settings
|
|
5
|
+
// through the code host. It commits nothing.
|
|
6
|
+
//
|
|
7
|
+
// Tokens are read in a masked prompt and go straight to the runner: none is
|
|
8
|
+
// printed, written to a file, or passed in a command's arguments.
|
|
9
|
+
import { execFileSync, spawn, spawnSync } from "node:child_process";
|
|
10
|
+
import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync, } from "node:fs";
|
|
11
|
+
import { tmpdir } from "node:os";
|
|
12
|
+
import { dirname, join } from "node:path";
|
|
13
|
+
import { isMap, parseDocument } from "yaml";
|
|
14
|
+
import { openCodeHost } from "./code-host/index.mjs";
|
|
15
|
+
import { NeedsScopeError } from "./code-host/types.mjs";
|
|
16
|
+
import { columnsOf, fieldNamesOf, parseSettings, repoRoot, SETTINGS_FILE, SettingsError, } from "./config.mjs";
|
|
17
|
+
import { checks as doctorChecks, doctorDeps, line, } from "./doctor.mjs";
|
|
18
|
+
import { AGENT_KEY, AGENT_TOKEN, neededSecrets, PART_TEXT, PARTS, partsOn, resolveParts, SIGNING_KEY, tokenNames, tokenNeeds, } from "./parts.mjs";
|
|
19
|
+
import { CONTEXT as REVIEW_VERDICT } from "./review-verdict.mjs";
|
|
20
|
+
import { openRunner } from "./runner/index.mjs";
|
|
21
|
+
import { differences, PACKAGE, plan as syncPlan } from "./sync.mjs";
|
|
22
|
+
import { githubTickets } from "./tickets/github.mjs";
|
|
23
|
+
import { ghTransport } from "./tickets/transport.mjs";
|
|
24
|
+
import { gh } from "./lib/gh.mjs";
|
|
25
|
+
import { DEFAULT_COLUMNS } from "./lib/work-status.mjs";
|
|
26
|
+
/** Today's one choice of each tool, by the name a setting gives it. */
|
|
27
|
+
const TOOLS = {
|
|
28
|
+
codeHost: { github: "GitHub" },
|
|
29
|
+
tickets: { github: "GitHub Issues" },
|
|
30
|
+
runner: { "github-actions": "GitHub Actions" },
|
|
31
|
+
agent: "Claude Code",
|
|
32
|
+
};
|
|
33
|
+
/** The coding agent's command, and the one that makes its token. */
|
|
34
|
+
const AGENT = {
|
|
35
|
+
command: "claude",
|
|
36
|
+
token: ["claude", "setup-token"],
|
|
37
|
+
};
|
|
38
|
+
/** The rule that requires the review's verdict, by name, so removing it is
|
|
39
|
+
* deleting that one rule. */
|
|
40
|
+
const RULE = "Software factory";
|
|
41
|
+
const SIGNING_TITLE = "software factory builds";
|
|
42
|
+
const AGAIN = "Run npx --no-install factory init again to add it.";
|
|
43
|
+
/** The columns and fields the board sync uses: the settings' names where
|
|
44
|
+
* they rename one, and none for one they set to none. */
|
|
45
|
+
function boardShape(s) {
|
|
46
|
+
const columns = Object.values(s ? columnsOf(s) : DEFAULT_COLUMNS).filter((c) => c !== null);
|
|
47
|
+
const names = s
|
|
48
|
+
? fieldNamesOf(s)
|
|
49
|
+
: { session: "Session", runsIn: "Runs in", cost: "Cost" };
|
|
50
|
+
const fields = [];
|
|
51
|
+
if (names.session)
|
|
52
|
+
fields.push({ name: names.session, type: "text" });
|
|
53
|
+
if (names.runsIn)
|
|
54
|
+
fields.push({
|
|
55
|
+
name: names.runsIn,
|
|
56
|
+
type: "select",
|
|
57
|
+
options: ["Local", "Actions"],
|
|
58
|
+
});
|
|
59
|
+
if (names.cost)
|
|
60
|
+
fields.push({ name: names.cost, type: "number" });
|
|
61
|
+
return { columns, fields };
|
|
62
|
+
}
|
|
63
|
+
/** The repository and code host a remote's address names, or null for one
|
|
64
|
+
* on a code host the factory does not support. */
|
|
65
|
+
export function parseRemote(url) {
|
|
66
|
+
const match = /^(?:git@github\.com:|(?:https|ssh):\/\/(?:git@)?github\.com\/)([^/]+)\/([^/]+?)(?:\.git)?\/?$/.exec(url.trim());
|
|
67
|
+
return match
|
|
68
|
+
? { codeHost: "github", repository: `${match[1]}/${match[2]}` }
|
|
69
|
+
: null;
|
|
70
|
+
}
|
|
71
|
+
const message = (e) => (e instanceof Error ? e.message : String(e));
|
|
72
|
+
/** Items as a sentence lists them: "a", "a and b", "a, b, and c". */
|
|
73
|
+
const listed = (items) => items.length < 3
|
|
74
|
+
? items.join(" and ")
|
|
75
|
+
: `${items.slice(0, -1).join(", ")}, and ${items.at(-1)}`;
|
|
76
|
+
/** What a token must be able to do, in words. */
|
|
77
|
+
const NEED_TEXT = {
|
|
78
|
+
push: "push a branch",
|
|
79
|
+
workflows: "change a workflow's file",
|
|
80
|
+
board: "write the board",
|
|
81
|
+
};
|
|
82
|
+
/** Deletes a key, then any map the deletion left empty above it. */
|
|
83
|
+
function remove(doc, path) {
|
|
84
|
+
if (!doc.hasIn(path))
|
|
85
|
+
return;
|
|
86
|
+
doc.deleteIn(path);
|
|
87
|
+
for (let i = path.length - 1; i > 0; i--) {
|
|
88
|
+
const parent = doc.getIn(path.slice(0, i));
|
|
89
|
+
if (isMap(parent) && parent.items.length === 0)
|
|
90
|
+
doc.deleteIn(path.slice(0, i));
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
export async function main(_argv, d) {
|
|
94
|
+
const at = (path) => join(d.root, path);
|
|
95
|
+
const read = (path) => existsSync(at(path)) ? readFileSync(at(path), "utf8") : null;
|
|
96
|
+
const write = (path, text) => {
|
|
97
|
+
mkdirSync(dirname(at(path)), { recursive: true });
|
|
98
|
+
writeFileSync(at(path), text);
|
|
99
|
+
};
|
|
100
|
+
const remote = d.remote();
|
|
101
|
+
const found = remote ? parseRemote(remote) : null;
|
|
102
|
+
if (!found) {
|
|
103
|
+
d.out(remote
|
|
104
|
+
? `The origin remote, ${remote}, is not on a code host the factory supports: ${Object.values(TOOLS.codeHost).join(", ")}.`
|
|
105
|
+
: "This is not a git repository with an origin remote. Run it in your repository's checkout.");
|
|
106
|
+
return 1;
|
|
107
|
+
}
|
|
108
|
+
const { repository } = found;
|
|
109
|
+
const [owner, name] = repository.split("/");
|
|
110
|
+
const settingsText = read(SETTINGS_FILE);
|
|
111
|
+
const doc = parseDocument(settingsText ?? "");
|
|
112
|
+
let settings = null;
|
|
113
|
+
if (settingsText !== null) {
|
|
114
|
+
try {
|
|
115
|
+
settings = parseSettings(doc.toJS(), (p) => existsSync(at(p)));
|
|
116
|
+
}
|
|
117
|
+
catch (e) {
|
|
118
|
+
d.out(`${SETTINGS_FILE}: ${message(e)}`);
|
|
119
|
+
d.out("Fix it, then run init again.");
|
|
120
|
+
return 1;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
const hasPackage = existsSync(at("package.json"));
|
|
124
|
+
const agentInstalled = d.hasCommand(AGENT.command);
|
|
125
|
+
d.out(`Setting up the software factory for ${repository}`);
|
|
126
|
+
d.out("");
|
|
127
|
+
d.out("Found");
|
|
128
|
+
const row = (label, text) => d.out(` ${label.padEnd(14)} ${text}`);
|
|
129
|
+
row("Code host", `${TOOLS.codeHost.github} (from your git remote)`);
|
|
130
|
+
row("Tickets", TOOLS.tickets.github);
|
|
131
|
+
row("Coding agent", agentInstalled
|
|
132
|
+
? TOOLS.agent
|
|
133
|
+
: `${TOOLS.agent}, which is not installed: sessions and its token need it`);
|
|
134
|
+
row("Cloud builds", TOOLS.runner["github-actions"]);
|
|
135
|
+
row("package.json", hasPackage ? "yes" : "none, so a private one will be created");
|
|
136
|
+
row("Settings", settingsText === null ? "none yet" : SETTINGS_FILE);
|
|
137
|
+
row("Board", settings?.tickets.board != null
|
|
138
|
+
? `${settings.tickets.board}, from your settings`
|
|
139
|
+
: "none yet");
|
|
140
|
+
d.out("");
|
|
141
|
+
const { codeHost, runner, tickets } = d.open(repository);
|
|
142
|
+
let secrets;
|
|
143
|
+
try {
|
|
144
|
+
secrets = await runner.secretNames();
|
|
145
|
+
}
|
|
146
|
+
catch (e) {
|
|
147
|
+
d.out(`Could not reach the code host: ${message(e)}`);
|
|
148
|
+
return 1;
|
|
149
|
+
}
|
|
150
|
+
const before = partsOn(settings, secrets);
|
|
151
|
+
const first = settingsText === null;
|
|
152
|
+
const chosen = await d.ask.checkbox("Which parts do you want?", PARTS.map((part) => ({
|
|
153
|
+
name: PART_TEXT[part].name,
|
|
154
|
+
description: PART_TEXT[part].gives,
|
|
155
|
+
value: part,
|
|
156
|
+
checked: first
|
|
157
|
+
? part === "review" || part === "board" || part === "cloud"
|
|
158
|
+
: before.has(part),
|
|
159
|
+
})));
|
|
160
|
+
const resolved = resolveParts(before, new Set(chosen));
|
|
161
|
+
const parts = resolved.parts;
|
|
162
|
+
for (const note of resolved.notes)
|
|
163
|
+
d.out(note);
|
|
164
|
+
// The board: the one in the settings, or one chosen now.
|
|
165
|
+
const boardOps = tickets.board;
|
|
166
|
+
let board = parts.has("board")
|
|
167
|
+
? (settings?.tickets.board ?? null)
|
|
168
|
+
: null;
|
|
169
|
+
const chooseBoard = async (offerNew) => {
|
|
170
|
+
const existing = boardOps ? await boardOps.list() : [];
|
|
171
|
+
if (!offerNew && existing.length === 0) {
|
|
172
|
+
d.out("No board to use instead: create one on the code host, then run npx --no-install factory init again.");
|
|
173
|
+
return null;
|
|
174
|
+
}
|
|
175
|
+
const choice = await d.ask.select("Which board?", [
|
|
176
|
+
...(offerNew
|
|
177
|
+
? [
|
|
178
|
+
{
|
|
179
|
+
name: `Create "${name}", with the factory's columns`,
|
|
180
|
+
value: "new",
|
|
181
|
+
},
|
|
182
|
+
]
|
|
183
|
+
: []),
|
|
184
|
+
...existing.map((b) => ({
|
|
185
|
+
name: `Use an existing one: ${b.title} (#${b.number})`,
|
|
186
|
+
value: String(b.number),
|
|
187
|
+
})),
|
|
188
|
+
]);
|
|
189
|
+
return choice === "new" ? "new" : Number(choice);
|
|
190
|
+
};
|
|
191
|
+
if (parts.has("board") && board === null)
|
|
192
|
+
board = await chooseBoard(true);
|
|
193
|
+
let allow = settings?.autoMerge.allow ?? [];
|
|
194
|
+
if (parts.has("auto-merge") && allow.length === 0)
|
|
195
|
+
allow = (await d.ask.input("Which files may merge without you? (comma-separated)", "docs/**, **/*.md"))
|
|
196
|
+
.split(",")
|
|
197
|
+
.map((x) => x.trim())
|
|
198
|
+
.filter(Boolean);
|
|
199
|
+
// The settings as they will stand, keeping every other key and comment.
|
|
200
|
+
if (!doc.has("version"))
|
|
201
|
+
doc.set("version", 1);
|
|
202
|
+
if (!doc.has("repository"))
|
|
203
|
+
doc.set("repository", repository);
|
|
204
|
+
if (parts.has("board") && typeof board === "number")
|
|
205
|
+
doc.setIn(["tickets", "board"], board);
|
|
206
|
+
if (!parts.has("board"))
|
|
207
|
+
for (const key of ["board", "columns", "fields"])
|
|
208
|
+
remove(doc, ["tickets", key]);
|
|
209
|
+
if (parts.has("from-board"))
|
|
210
|
+
doc.setIn(["builds", "trigger"], "own-server");
|
|
211
|
+
else if (before.has("from-board"))
|
|
212
|
+
remove(doc, ["builds", "trigger"]);
|
|
213
|
+
if (parts.has("auto-merge"))
|
|
214
|
+
doc.setIn(["auto-merge", "allow"], allow);
|
|
215
|
+
else
|
|
216
|
+
remove(doc, ["auto-merge", "allow"]);
|
|
217
|
+
// The review is on while its setting is written; the sync writes its
|
|
218
|
+
// workflow then, and removes it once the setting is gone.
|
|
219
|
+
if (parts.has("review") && !doc.has("ci-review"))
|
|
220
|
+
doc.setIn(["ci-review", "commands"], []);
|
|
221
|
+
if (!parts.has("review"))
|
|
222
|
+
doc.delete("ci-review");
|
|
223
|
+
// The secrets: those the parts read that are not set, or all of them when
|
|
224
|
+
// you choose to replace them.
|
|
225
|
+
const needed = neededSecrets(parts);
|
|
226
|
+
const alreadySet = [
|
|
227
|
+
...new Set(needed.flatMap((n) => n.anyOf.filter((x) => secrets.includes(x)))),
|
|
228
|
+
];
|
|
229
|
+
let replace = false;
|
|
230
|
+
if (alreadySet.length)
|
|
231
|
+
replace = await d.ask.confirm(alreadySet.length === 1
|
|
232
|
+
? "1 secret these parts read is already set. Replace it?"
|
|
233
|
+
: `${alreadySet.length} secrets these parts read are already set. Replace them?`, false);
|
|
234
|
+
const missing = (names) => replace || !names.some((n) => secrets.includes(n));
|
|
235
|
+
const wantsAgent = (parts.has("review") || parts.has("cloud")) &&
|
|
236
|
+
missing([AGENT_TOKEN, AGENT_KEY]);
|
|
237
|
+
const tokenTo = tokenNames(parts).filter((n) => missing([n]));
|
|
238
|
+
const wantsKey = parts.has("signing") && missing([SIGNING_KEY]);
|
|
239
|
+
const removed = PARTS.filter((p) => before.has(p) && !parts.has(p));
|
|
240
|
+
const stillRead = new Set(neededSecrets(parts).flatMap((n) => n.anyOf));
|
|
241
|
+
const deletable = [
|
|
242
|
+
...new Set(neededSecrets(new Set(removed))
|
|
243
|
+
.flatMap((n) => n.anyOf)
|
|
244
|
+
.filter((n) => secrets.includes(n) && !stillRead.has(n))),
|
|
245
|
+
];
|
|
246
|
+
// The plan.
|
|
247
|
+
const newText = String(doc);
|
|
248
|
+
const writes = [
|
|
249
|
+
...(newText !== settingsText ? [SETTINGS_FILE] : []),
|
|
250
|
+
...(hasPackage ? [] : ["package.json"]),
|
|
251
|
+
...(/^\/?node_modules\/?$/m.test(read(".gitignore") ?? "")
|
|
252
|
+
? []
|
|
253
|
+
: [".gitignore"]),
|
|
254
|
+
];
|
|
255
|
+
let syncs;
|
|
256
|
+
try {
|
|
257
|
+
syncs = d.syncChanges(newText);
|
|
258
|
+
}
|
|
259
|
+
catch (e) {
|
|
260
|
+
d.out(`The settings would not be valid: ${message(e)}`);
|
|
261
|
+
return 1;
|
|
262
|
+
}
|
|
263
|
+
const toStore = [
|
|
264
|
+
...(wantsAgent ? [agentInstalled ? AGENT_TOKEN : AGENT_KEY] : []),
|
|
265
|
+
...tokenTo,
|
|
266
|
+
...(wantsKey ? [SIGNING_KEY] : []),
|
|
267
|
+
];
|
|
268
|
+
d.out("");
|
|
269
|
+
d.out("This is everything setup will do:");
|
|
270
|
+
const planRow = (label, text) => d.out(` ${label.padEnd(8)} ${text}`);
|
|
271
|
+
for (const path of [...writes, ...syncs])
|
|
272
|
+
planRow("Write", path);
|
|
273
|
+
if (board === "new")
|
|
274
|
+
planRow("Create", `board "${name}" under ${owner}, linked to the repository, with the factory's columns and fields`);
|
|
275
|
+
else if (parts.has("board") && typeof board === "number")
|
|
276
|
+
planRow("Board", `give board ${board} any of the factory's columns and fields it lacks`);
|
|
277
|
+
if (toStore.length)
|
|
278
|
+
planRow("Secrets", toStore.join(", "));
|
|
279
|
+
if (parts.has("auto-merge"))
|
|
280
|
+
planRow("Settings", "allow auto-merge on the repository");
|
|
281
|
+
if (parts.has("review"))
|
|
282
|
+
planRow("Rules", `Require "${REVIEW_VERDICT}" before a merge to the default branch, as the rule "${RULE}"`);
|
|
283
|
+
for (const part of removed)
|
|
284
|
+
planRow("Turn off", PART_TEXT[part].name);
|
|
285
|
+
if (!(await d.ask.confirm("Go ahead?", true))) {
|
|
286
|
+
d.out("Nothing changed.");
|
|
287
|
+
return 0;
|
|
288
|
+
}
|
|
289
|
+
// The board first, since the settings name its number.
|
|
290
|
+
if (parts.has("board") && boardOps) {
|
|
291
|
+
while (board === "new") {
|
|
292
|
+
try {
|
|
293
|
+
board = await boardOps.create(name);
|
|
294
|
+
d.out(`Created board ${board}, "${name}".`);
|
|
295
|
+
}
|
|
296
|
+
catch (e) {
|
|
297
|
+
d.out(`Could not create a board: ${message(e)}`);
|
|
298
|
+
board = await chooseBoard(false);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
if (typeof board === "number") {
|
|
302
|
+
doc.setIn(["tickets", "board"], board);
|
|
303
|
+
try {
|
|
304
|
+
const shape = boardShape(settings);
|
|
305
|
+
const columns = await boardOps.addColumns(board, shape.columns);
|
|
306
|
+
const fields = await boardOps.addFields(board, shape.fields);
|
|
307
|
+
const added = [...columns, ...fields];
|
|
308
|
+
if (added.length)
|
|
309
|
+
d.out(`Added to board ${board}: ${added.join(", ")}.`);
|
|
310
|
+
}
|
|
311
|
+
catch (e) {
|
|
312
|
+
d.out(`Could not give board ${board} its columns and fields: ${message(e)}`);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
if (!hasPackage)
|
|
317
|
+
write("package.json", `${JSON.stringify({
|
|
318
|
+
name: name.toLowerCase().replace(/[^a-z0-9._-]/g, "-"),
|
|
319
|
+
private: true,
|
|
320
|
+
devDependencies: { [d.packageName]: `^${d.packageVersion}` },
|
|
321
|
+
}, null, 2)}\n`);
|
|
322
|
+
const ignore = read(".gitignore");
|
|
323
|
+
if (!/^\/?node_modules\/?$/m.test(ignore ?? ""))
|
|
324
|
+
write(".gitignore", `${ignore ?? ""}${ignore && !ignore.endsWith("\n") ? "\n" : ""}node_modules/\n`);
|
|
325
|
+
write(SETTINGS_FILE, String(doc));
|
|
326
|
+
const synced = await d.sync();
|
|
327
|
+
if (synced !== 0)
|
|
328
|
+
d.out("The sync failed, as above.");
|
|
329
|
+
// The coding agent's token.
|
|
330
|
+
if (wantsAgent) {
|
|
331
|
+
let value = "";
|
|
332
|
+
if (agentInstalled) {
|
|
333
|
+
d.out("");
|
|
334
|
+
d.out(`The coding agent's token: running ${AGENT.token.join(" ")}...`);
|
|
335
|
+
await d.interactive(AGENT.token);
|
|
336
|
+
value = await d.ask.secret("Paste the token it printed (leave empty to do this later):");
|
|
337
|
+
}
|
|
338
|
+
else {
|
|
339
|
+
value = await d.ask.secret("Paste an API key from the Claude Console (leave empty to do this later):");
|
|
340
|
+
}
|
|
341
|
+
const secret = agentInstalled ? AGENT_TOKEN : AGENT_KEY;
|
|
342
|
+
if (value) {
|
|
343
|
+
await runner.setSecret(secret, value);
|
|
344
|
+
d.out(`Stored as ${secret}.`);
|
|
345
|
+
}
|
|
346
|
+
else
|
|
347
|
+
d.out(`No ${secret} stored. ${AGAIN}`);
|
|
348
|
+
}
|
|
349
|
+
// The one access token.
|
|
350
|
+
let tokenAccount = null;
|
|
351
|
+
if (tokenTo.length) {
|
|
352
|
+
const scopes = codeHost.scopesFor(tokenNeeds(parts));
|
|
353
|
+
const page = codeHost.newTokenPage(scopes, `software factory: ${repository}`);
|
|
354
|
+
d.out("");
|
|
355
|
+
d.out(`An access token: opening ${page}`);
|
|
356
|
+
d.out(`Check that ${listed(scopes)} ${scopes.length === 1 ? "is" : "are"} ticked, choose an expiry, and generate it.`);
|
|
357
|
+
d.openPage(page);
|
|
358
|
+
for (;;) {
|
|
359
|
+
const value = await d.ask.secret("Paste it (leave empty to do this later):");
|
|
360
|
+
if (!value) {
|
|
361
|
+
d.out(`No token stored. ${AGAIN}`);
|
|
362
|
+
break;
|
|
363
|
+
}
|
|
364
|
+
let owned;
|
|
365
|
+
try {
|
|
366
|
+
owned = await codeHost.tokenOwner(value);
|
|
367
|
+
}
|
|
368
|
+
catch (e) {
|
|
369
|
+
d.out(`${message(e)}. Paste another.`);
|
|
370
|
+
continue;
|
|
371
|
+
}
|
|
372
|
+
const lacking = owned.scopes
|
|
373
|
+
? scopes.filter((x) => !owned.scopes.includes(x))
|
|
374
|
+
: [];
|
|
375
|
+
if (lacking.length) {
|
|
376
|
+
d.out(`That token lacks ${listed(lacking)}. Make one with every scope checked, and paste it.`);
|
|
377
|
+
continue;
|
|
378
|
+
}
|
|
379
|
+
if (owned.scopes === null)
|
|
380
|
+
d.out(`The code host does not say what this token may do: make sure it can ${listed(tokenNeeds(parts).map((n) => NEED_TEXT[n]))}.`);
|
|
381
|
+
for (const n of tokenTo)
|
|
382
|
+
await runner.setSecret(n, value);
|
|
383
|
+
tokenAccount = owned.account;
|
|
384
|
+
d.out(`It belongs to ${owned.account}. Stored as ${tokenTo.join(", ")}.`);
|
|
385
|
+
break;
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
// The signing key.
|
|
389
|
+
if (wantsKey) {
|
|
390
|
+
const key = await d.keygen();
|
|
391
|
+
await runner.setSecret(SIGNING_KEY, key.privateKey);
|
|
392
|
+
d.out("");
|
|
393
|
+
d.out(`Made a signing key and stored it as ${SIGNING_KEY}.`);
|
|
394
|
+
const signedIn = await codeHost.signedInAs().catch(() => null);
|
|
395
|
+
let added = false;
|
|
396
|
+
if (tokenAccount !== null && tokenAccount === signedIn) {
|
|
397
|
+
for (let tries = 0; tries < 2 && !added; tries++) {
|
|
398
|
+
try {
|
|
399
|
+
await codeHost.addSigningKey(SIGNING_TITLE, key.publicKey);
|
|
400
|
+
added = true;
|
|
401
|
+
d.out(`Added its public half to ${signedIn} as a signing key.`);
|
|
402
|
+
}
|
|
403
|
+
catch (e) {
|
|
404
|
+
if (!(e instanceof NeedsScopeError) || tries > 0) {
|
|
405
|
+
d.out(`Could not add the key: ${message(e)}`);
|
|
406
|
+
break;
|
|
407
|
+
}
|
|
408
|
+
const grant = await d.ask.confirm(`Adding it to ${signedIn} needs the ${e.scope} permission. Open your browser to grant it?`, true);
|
|
409
|
+
if (!grant)
|
|
410
|
+
break;
|
|
411
|
+
await d.interactive(e.fix);
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
if (!added) {
|
|
416
|
+
d.out(`Add this public key to ${tokenAccount ?? "the account whose token builds use"}, as a signing key under Settings, "SSH and GPG keys":`);
|
|
417
|
+
d.out(` ${key.publicKey}`);
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
// The repository's settings.
|
|
421
|
+
if (parts.has("auto-merge"))
|
|
422
|
+
await codeHost
|
|
423
|
+
.allowAutoMerge()
|
|
424
|
+
.catch((e) => d.out(`Could not allow auto-merge: ${message(e)}`));
|
|
425
|
+
if (parts.has("review"))
|
|
426
|
+
await codeHost
|
|
427
|
+
.requireCheck(RULE, REVIEW_VERDICT)
|
|
428
|
+
.catch((e) => d.out(`Could not require "${REVIEW_VERDICT}": ${message(e)}. Carrying on.`));
|
|
429
|
+
// What is left for you.
|
|
430
|
+
d.out("");
|
|
431
|
+
if (deletable.length)
|
|
432
|
+
d.out(`You can now delete these secrets: ${deletable.join(", ")}`);
|
|
433
|
+
const left = [];
|
|
434
|
+
const app = codeHost.reviewAppPage();
|
|
435
|
+
if (parts.has("review") && app)
|
|
436
|
+
left.push(`Install the app the review posts through: ${app}`);
|
|
437
|
+
if (parts.has("from-board") && typeof board === "number")
|
|
438
|
+
left.push([
|
|
439
|
+
"Host the poller, which starts a build when a card moves to Ready, with:",
|
|
440
|
+
` FACTORY_REPOSITORY=${repository}`,
|
|
441
|
+
` FACTORY_BOARD_OWNER=${owner}`,
|
|
442
|
+
` FACTORY_BOARD_NUMBER=${board}`,
|
|
443
|
+
" FACTORY_BOARD_TOKEN=the access token you just made",
|
|
444
|
+
].join("\n"));
|
|
445
|
+
left.push("Commit this on a branch, open a pull request, and merge it.");
|
|
446
|
+
d.out("Nothing is committed. Left to do:");
|
|
447
|
+
left.forEach((step, i) => d.out(` ${i + 1}. ${step}`));
|
|
448
|
+
d.out("");
|
|
449
|
+
d.out("Checks");
|
|
450
|
+
const found2 = await d.checks();
|
|
451
|
+
for (const c of found2)
|
|
452
|
+
d.out(` ${line(c)}`);
|
|
453
|
+
return found2.some((c) => c.state === "fail") ? 1 : 0;
|
|
454
|
+
}
|
|
455
|
+
/** The questions, asked at the terminal. */
|
|
456
|
+
async function terminalAsk() {
|
|
457
|
+
const p = await import("@inquirer/prompts");
|
|
458
|
+
return {
|
|
459
|
+
checkbox: (message, choices) => p.checkbox({ message, choices }),
|
|
460
|
+
select: (message, choices) => p.select({ message, choices }),
|
|
461
|
+
confirm: (message, fallback) => p.confirm({ message, default: fallback }),
|
|
462
|
+
secret: (message) => p.password({ message, mask: "•" }),
|
|
463
|
+
input: (message, fallback) => p.input({ message, default: fallback }),
|
|
464
|
+
};
|
|
465
|
+
}
|
|
466
|
+
export async function run(argv) {
|
|
467
|
+
let root;
|
|
468
|
+
try {
|
|
469
|
+
root = repoRoot();
|
|
470
|
+
}
|
|
471
|
+
catch {
|
|
472
|
+
console.log("This is not a git repository. Run it in your repository's checkout.");
|
|
473
|
+
return 1;
|
|
474
|
+
}
|
|
475
|
+
const manifest = JSON.parse(readFileSync(join(PACKAGE, "package.json"), "utf8"));
|
|
476
|
+
const d = {
|
|
477
|
+
root,
|
|
478
|
+
ask: await terminalAsk(),
|
|
479
|
+
out: (l) => console.log(l),
|
|
480
|
+
remote: () => {
|
|
481
|
+
try {
|
|
482
|
+
return execFileSync("git", ["remote", "get-url", "origin"], {
|
|
483
|
+
cwd: root,
|
|
484
|
+
encoding: "utf8",
|
|
485
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
486
|
+
}).trim();
|
|
487
|
+
}
|
|
488
|
+
catch {
|
|
489
|
+
return null;
|
|
490
|
+
}
|
|
491
|
+
},
|
|
492
|
+
open: (repository) => ({
|
|
493
|
+
codeHost: openCodeHost(repository),
|
|
494
|
+
runner: openRunner(repository),
|
|
495
|
+
tickets: githubTickets({
|
|
496
|
+
transport: ghTransport(gh),
|
|
497
|
+
repository,
|
|
498
|
+
board: null,
|
|
499
|
+
}),
|
|
500
|
+
}),
|
|
501
|
+
sync: async () => (await import("./sync.mjs")).run([]),
|
|
502
|
+
syncChanges: (text) => {
|
|
503
|
+
const raw = parseDocument(text).toJS();
|
|
504
|
+
const s = parseSettings(raw, (p) => existsSync(join(root, p)));
|
|
505
|
+
return differences(root, syncPlan(PACKAGE, root, s));
|
|
506
|
+
},
|
|
507
|
+
interactive: async (argv2) => spawnSync(argv2[0], argv2.slice(1), { stdio: "inherit" }).status ?? 1,
|
|
508
|
+
hasCommand: (name) => !spawnSync(name, ["--version"], { stdio: "ignore" }).error,
|
|
509
|
+
openPage: (url) => {
|
|
510
|
+
const opener = process.platform === "darwin"
|
|
511
|
+
? "open"
|
|
512
|
+
: process.platform === "win32"
|
|
513
|
+
? "explorer"
|
|
514
|
+
: "xdg-open";
|
|
515
|
+
try {
|
|
516
|
+
spawn(opener, [url], { stdio: "ignore", detached: true })
|
|
517
|
+
.on("error", () => { })
|
|
518
|
+
.unref();
|
|
519
|
+
}
|
|
520
|
+
catch {
|
|
521
|
+
// The page's address is printed too, to open by hand.
|
|
522
|
+
}
|
|
523
|
+
},
|
|
524
|
+
keygen: async () => {
|
|
525
|
+
const dir = mkdtempSync(join(tmpdir(), "factory-key-"));
|
|
526
|
+
try {
|
|
527
|
+
const file = join(dir, "key");
|
|
528
|
+
execFileSync("ssh-keygen", ["-q", "-t", "ed25519", "-N", "", "-C", SIGNING_TITLE, "-f", file], { stdio: "ignore" });
|
|
529
|
+
return {
|
|
530
|
+
privateKey: readFileSync(file, "utf8"),
|
|
531
|
+
publicKey: readFileSync(`${file}.pub`, "utf8").trim(),
|
|
532
|
+
};
|
|
533
|
+
}
|
|
534
|
+
finally {
|
|
535
|
+
rmSync(dir, { recursive: true, force: true });
|
|
536
|
+
}
|
|
537
|
+
},
|
|
538
|
+
checks: () => doctorChecks(doctorDeps(root)),
|
|
539
|
+
packageName: manifest.name,
|
|
540
|
+
packageVersion: manifest.version,
|
|
541
|
+
};
|
|
542
|
+
try {
|
|
543
|
+
return await main(argv, d);
|
|
544
|
+
}
|
|
545
|
+
catch (e) {
|
|
546
|
+
if (e instanceof Error && e.name === "ExitPromptError") {
|
|
547
|
+
console.log("Stopped. Nothing after the last step shown was changed.");
|
|
548
|
+
return 130;
|
|
549
|
+
}
|
|
550
|
+
if (e instanceof SettingsError) {
|
|
551
|
+
console.log(e.message);
|
|
552
|
+
return 1;
|
|
553
|
+
}
|
|
554
|
+
throw e;
|
|
555
|
+
}
|
|
556
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** `issue` is null for a clock a cost note started in a session that never
|
|
2
|
+
* claimed or refined an issue: its spend is counted once, but its reviewer
|
|
3
|
+
* runs belong to no issue. */
|
|
4
|
+
export type Clock = {
|
|
5
|
+
issue: number | null;
|
|
6
|
+
start: string;
|
|
7
|
+
};
|
|
8
|
+
/** The repository's shared git directory, so a session that moves to
|
|
9
|
+
* another worktree of the same clone keeps its claims and its clock. */
|
|
10
|
+
export declare function commonDir(): string;
|
|
11
|
+
export declare const clockPath: (session: string) => string;
|
|
12
|
+
/** The clock, or null when none was started or it cannot be read. */
|
|
13
|
+
export declare function readClock(session: string): Clock | null;
|
|
14
|
+
export declare function writeClock(session: string, clock: Clock): void;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// A session's cost clock: when its spend was last noted, and on which issue.
|
|
2
|
+
// `factory ticket` starts and restarts it; the reviewer-run hooks read it
|
|
3
|
+
// to know which issue a reviewer works for. It lives in the repository's
|
|
4
|
+
// shared git directory beside the `work-claim-N` files, keyed by
|
|
5
|
+
// CLAUDE_CODE_SESSION_ID. Node built-ins only: the hooks run it before
|
|
6
|
+
// npm ci.
|
|
7
|
+
import { execFileSync } from "node:child_process";
|
|
8
|
+
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
9
|
+
import { join } from "node:path";
|
|
10
|
+
/** The repository's shared git directory, so a session that moves to
|
|
11
|
+
* another worktree of the same clone keeps its claims and its clock. */
|
|
12
|
+
export function commonDir() {
|
|
13
|
+
return execFileSync("git", ["rev-parse", "--git-common-dir"], {
|
|
14
|
+
encoding: "utf8",
|
|
15
|
+
}).trim();
|
|
16
|
+
}
|
|
17
|
+
export const clockPath = (session) => join(commonDir(), `work-clock-${session}`);
|
|
18
|
+
/** The clock, or null when none was started or it cannot be read. */
|
|
19
|
+
export function readClock(session) {
|
|
20
|
+
const path = clockPath(session);
|
|
21
|
+
if (!existsSync(path))
|
|
22
|
+
return null;
|
|
23
|
+
try {
|
|
24
|
+
const clock = JSON.parse(readFileSync(path, "utf8"));
|
|
25
|
+
return (clock.issue === null || Number.isInteger(clock.issue)) &&
|
|
26
|
+
!Number.isNaN(Date.parse(clock.start))
|
|
27
|
+
? clock
|
|
28
|
+
: null;
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
export function writeClock(session, clock) {
|
|
35
|
+
writeFileSync(clockPath(session), JSON.stringify(clock));
|
|
36
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { Runner } from "../runner/types.mts";
|
|
2
|
+
import type { Identity } from "./work-status.mts";
|
|
3
|
+
export type Gh = (args: string[]) => Promise<string>;
|
|
4
|
+
/** The GitHub CLI given `input` on standard input, for a value that must
|
|
5
|
+
* never appear in a command's arguments, such as a secret. */
|
|
6
|
+
export type GhIn = (args: string[], input: string) => Promise<string>;
|
|
7
|
+
/** What GitHub said was wrong beyond its one line: the `errors` in the
|
|
8
|
+
* answer the CLI printed, such as why a pull request could not be opened.
|
|
9
|
+
* The CLI's own message for those is only "Validation Failed". */
|
|
10
|
+
export declare function refusalsIn(stdout: string): string;
|
|
11
|
+
/** A failed call's error: the call, the CLI's own message, and the reasons
|
|
12
|
+
* in the answer it printed. */
|
|
13
|
+
export declare function failureOf(args: string[], detail: string, stdout: string): string;
|
|
14
|
+
export declare const gh: Gh;
|
|
15
|
+
export declare const ghIn: GhIn;
|
|
16
|
+
export declare function ghJson<T>(run: Gh, args: string[]): Promise<T>;
|
|
17
|
+
/** Every page of a REST list. */
|
|
18
|
+
export declare function ghList<T>(run: Gh, path: string): Promise<T[]>;
|
|
19
|
+
export declare function graphql<T>(run: Gh, query: string, variables?: Record<string, string | number | boolean | null>): Promise<T>;
|
|
20
|
+
/** Who this session is, for its marker comments. A session in one of the
|
|
21
|
+
* runner's runs goes by the run: its ID, its page, and the account that
|
|
22
|
+
* started it. A local session run in the desktop app goes by the app's
|
|
23
|
+
* `local_…` ID, which its links need; one run in a terminal has only its
|
|
24
|
+
* own. `unknown` when the environment does not say. */
|
|
25
|
+
export declare function sessionIdentity(env: Record<string, string | undefined>, runner: Pick<Runner, "thisRun">): Identity;
|
|
26
|
+
/** The link that opens a session in the desktop app; null for an ID the app
|
|
27
|
+
* does not know. */
|
|
28
|
+
export declare function appSessionLink(id: string): string | null;
|