@warren-dean/code-kit 0.24.0 → 0.26.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/marketplace.json +1 -1
- package/.claude-plugin/plugin.json +1 -1
- package/CHANGELOG.md +25 -0
- package/README.md +74 -60
- package/bin/code-kit.mjs +188 -3
- package/hooks/act.mjs +96 -0
- package/hooks/guard-bash.mjs +27 -1
- package/hooks/hooks.json +42 -1
- package/hooks/lib/ci.mjs +407 -0
- package/hooks/lib/config.mjs +26 -2
- package/hooks/lib/events.mjs +626 -0
- package/hooks/lib/harness.mjs +8 -0
- package/hooks/lib/instructions.mjs +1 -1
- package/hooks/lib/next.mjs +31 -0
- package/hooks/lib/plan.mjs +17 -2
- package/hooks/lib/publish.mjs +144 -0
- package/hooks/lib/queue.mjs +36 -0
- package/hooks/lib/snapshot.mjs +33 -3
- package/hooks/lib/worktrees.mjs +19 -11
- package/hooks/mod/art.mjs +0 -81
- package/hooks/mod/board.mjs +1743 -0
- package/hooks/mod/characters.mjs +516 -0
- package/hooks/mod/loop.mjs +17 -3
- package/hooks/mod/mission.mjs +2 -93
- package/hooks/mod/panes.mjs +111 -422
- package/hooks/mod/register.mjs +473 -182
- package/hooks/mod/surface/character.mjs +79 -0
- package/hooks/mod/surface/clock.mjs +37 -0
- package/hooks/mod/surface/rail.mjs +33 -0
- package/hooks/mod/view.mjs +30 -602
- package/hooks/session.mjs +14 -11
- package/package.json +2 -1
- package/skills/decide/SKILL.md +1 -1
- package/skills/dispatch/SKILL.md +1 -1
- package/skills/init/SKILL.md +4 -3
- package/skills/next/SKILL.md +1 -1
- package/skills/review/SKILL.md +1 -1
- package/templates/agent.md +1 -1
- package/templates/claude-md.md +3 -1
- package/templates/github-workflow.yml +10 -3
- package/hooks/mod/register.test.ts +0 -4932
package/hooks/lib/ci.mjs
ADDED
|
@@ -0,0 +1,407 @@
|
|
|
1
|
+
// What a project's CI costs (docs/specs/25-what-ci-costs.md). code-kit builds each story on its own branch
|
|
2
|
+
// and merges it on its own, so whatever CI runs per pull request or per merge runs once per story; the
|
|
3
|
+
// merge queue already ran the checks before each merge. `ciFindings` reads the workflows for what
|
|
4
|
+
// multiplies that cost (CIC-2), `usageSummary` sums what the project spent (CIC-4), and `releaseBranch`
|
|
5
|
+
// cuts the branch the full CI runs on instead (CIC-3).
|
|
6
|
+
import { execFileSync, spawnSync } from 'node:child_process';
|
|
7
|
+
import { existsSync, readdirSync, readFileSync } from 'node:fs';
|
|
8
|
+
import { join } from 'node:path';
|
|
9
|
+
|
|
10
|
+
export const WORKFLOWS_DIR = '.github/workflows';
|
|
11
|
+
export const RELEASE_PREFIX = 'release/';
|
|
12
|
+
/** GitHub's own limit for a job without one: six hours. */
|
|
13
|
+
const GITHUB_LIMIT_MINUTES = 360;
|
|
14
|
+
|
|
15
|
+
// --- a YAML subset ---------------------------------------------------------------------------------
|
|
16
|
+
|
|
17
|
+
const unquote = (v) => {
|
|
18
|
+
const t = v.trim();
|
|
19
|
+
if ((t.startsWith('"') && t.endsWith('"')) || (t.startsWith("'") && t.endsWith("'")))
|
|
20
|
+
return t.slice(1, -1);
|
|
21
|
+
return t;
|
|
22
|
+
};
|
|
23
|
+
const stripComment = (line) => {
|
|
24
|
+
let quote = null;
|
|
25
|
+
for (let i = 0; i < line.length; i++) {
|
|
26
|
+
const c = line[i];
|
|
27
|
+
if (quote) {
|
|
28
|
+
if (c === quote) quote = null;
|
|
29
|
+
} else if (c === '"' || c === "'") quote = c;
|
|
30
|
+
else if (c === '#' && (i === 0 || /\s/.test(line[i - 1]))) return line.slice(0, i);
|
|
31
|
+
}
|
|
32
|
+
return line;
|
|
33
|
+
};
|
|
34
|
+
const scalar = (v) => {
|
|
35
|
+
const t = v.trim();
|
|
36
|
+
if (t === '') return null;
|
|
37
|
+
if (t.startsWith('[') && t.endsWith(']'))
|
|
38
|
+
return t
|
|
39
|
+
.slice(1, -1)
|
|
40
|
+
.split(',')
|
|
41
|
+
.map((x) => unquote(x))
|
|
42
|
+
.filter(Boolean);
|
|
43
|
+
if (t.startsWith('{') && t.endsWith('}')) return {};
|
|
44
|
+
if (t === 'true') return true;
|
|
45
|
+
if (t === 'false') return false;
|
|
46
|
+
if (/^-?\d+(\.\d+)?$/.test(t)) return Number(t);
|
|
47
|
+
return unquote(t);
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* The block maps, block lists and flow lists GitHub workflows are written in, as plain objects. Block
|
|
52
|
+
* scalars (`run: |`) are kept as their text. Anchors, tags and multi-document files aren't read.
|
|
53
|
+
*/
|
|
54
|
+
export function parseYaml(text) {
|
|
55
|
+
const lines = String(text ?? '')
|
|
56
|
+
.split('\n')
|
|
57
|
+
.map((raw) => ({ raw, indent: raw.length - raw.trimStart().length }));
|
|
58
|
+
let i = 0;
|
|
59
|
+
const skipBlank = () => {
|
|
60
|
+
while (i < lines.length && !stripComment(lines[i].raw).trim()) i += 1;
|
|
61
|
+
};
|
|
62
|
+
const blockScalar = (indent) => {
|
|
63
|
+
const out = [];
|
|
64
|
+
while (i < lines.length && (!lines[i].raw.trim() || lines[i].indent > indent)) {
|
|
65
|
+
out.push(lines[i].raw.trim());
|
|
66
|
+
i += 1;
|
|
67
|
+
}
|
|
68
|
+
return out.join('\n').trim();
|
|
69
|
+
};
|
|
70
|
+
const node = (indent) => {
|
|
71
|
+
skipBlank();
|
|
72
|
+
if (i >= lines.length || lines[i].indent < indent) return null;
|
|
73
|
+
const first = stripComment(lines[i].raw).trim();
|
|
74
|
+
return first.startsWith('- ') || first === '-' ? list(lines[i].indent) : map(lines[i].indent);
|
|
75
|
+
};
|
|
76
|
+
const value = (rest, indent) => {
|
|
77
|
+
const r = rest.trim();
|
|
78
|
+
if (r === '|' || r === '>' || /^[|>][+-]?$/.test(r)) return blockScalar(indent);
|
|
79
|
+
if (r !== '') return scalar(r);
|
|
80
|
+
skipBlank();
|
|
81
|
+
if (i < lines.length && lines[i].indent > indent) return node(lines[i].indent);
|
|
82
|
+
// A list may sit at its key's own indentation.
|
|
83
|
+
if (i < lines.length && lines[i].indent === indent && /^-(\s|$)/.test(lines[i].raw.trim()))
|
|
84
|
+
return list(indent);
|
|
85
|
+
return null;
|
|
86
|
+
};
|
|
87
|
+
const map = (indent) => {
|
|
88
|
+
const out = {};
|
|
89
|
+
while (true) {
|
|
90
|
+
skipBlank();
|
|
91
|
+
if (i >= lines.length || lines[i].indent !== indent) break;
|
|
92
|
+
const line = stripComment(lines[i].raw).trim();
|
|
93
|
+
if (line.startsWith('- ')) break;
|
|
94
|
+
const m = /^("[^"]*"|'[^']*'|[^:]+?):(\s+|$)(.*)$/.exec(line);
|
|
95
|
+
i += 1;
|
|
96
|
+
if (!m) continue;
|
|
97
|
+
out[unquote(m[1])] = value(m[3], indent);
|
|
98
|
+
}
|
|
99
|
+
return out;
|
|
100
|
+
};
|
|
101
|
+
const list = (indent) => {
|
|
102
|
+
const out = [];
|
|
103
|
+
while (true) {
|
|
104
|
+
skipBlank();
|
|
105
|
+
if (i >= lines.length || lines[i].indent !== indent) break;
|
|
106
|
+
const line = stripComment(lines[i].raw).trim();
|
|
107
|
+
if (!/^-(\s|$)/.test(line)) break;
|
|
108
|
+
const rest = line.replace(/^-\s?/, '');
|
|
109
|
+
const kv = /^("[^"]*"|'[^']*'|[^:\s][^:]*?):(\s+|$)(.*)$/.exec(rest);
|
|
110
|
+
if (kv && !rest.startsWith('"') && !rest.startsWith("'")) {
|
|
111
|
+
// A map in a list: its first key is on the dash's line, the rest under it, past the dash.
|
|
112
|
+
const inner =
|
|
113
|
+
indent +
|
|
114
|
+
(lines[i].raw.slice(indent).length -
|
|
115
|
+
lines[i].raw.slice(indent).replace(/^-\s*/, '').length);
|
|
116
|
+
lines[i] = { raw: `${' '.repeat(inner)}${rest}`, indent: inner };
|
|
117
|
+
out.push(map(inner));
|
|
118
|
+
} else {
|
|
119
|
+
i += 1;
|
|
120
|
+
out.push(rest === '' ? node(indent + 1) : scalar(rest));
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return out;
|
|
124
|
+
};
|
|
125
|
+
return node(0) ?? {};
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// --- what costs most -------------------------------------------------------------------------------
|
|
129
|
+
|
|
130
|
+
/** A workflow's triggers as { event: config }, whichever way `on:` is written. */
|
|
131
|
+
export function triggersOf(doc) {
|
|
132
|
+
const on = doc?.on ?? doc?.true ?? null; // a bare `on` key can read as a boolean elsewhere
|
|
133
|
+
if (typeof on === 'string') return { [on]: null };
|
|
134
|
+
if (Array.isArray(on)) return Object.fromEntries(on.map((e) => [e, null]));
|
|
135
|
+
return on && typeof on === 'object' ? on : {};
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const listOf = (v) => (Array.isArray(v) ? v : v === null || v === undefined ? [] : [v]);
|
|
139
|
+
const globMatch = (pattern, name) =>
|
|
140
|
+
new RegExp(
|
|
141
|
+
`^${String(pattern)
|
|
142
|
+
.replace(/[.+^${}()|[\]\\]/g, '\\$&')
|
|
143
|
+
.replace(/\*\*/g, '\u0000')
|
|
144
|
+
.replace(/\*/g, '[^/]*')
|
|
145
|
+
.replace(/\u0000/g, '.*')}$`,
|
|
146
|
+
).test(name);
|
|
147
|
+
|
|
148
|
+
/** Whether a push trigger runs when `base` moves. */
|
|
149
|
+
function pushRunsOnBase(push, base) {
|
|
150
|
+
if (push === null || push === undefined) return true;
|
|
151
|
+
const branches = listOf(push.branches);
|
|
152
|
+
const ignored = listOf(push['branches-ignore']);
|
|
153
|
+
if (branches.length) return branches.some((b) => globMatch(b, base));
|
|
154
|
+
if (ignored.length) return !ignored.some((b) => globMatch(b, base));
|
|
155
|
+
// Tags (or paths) alone: a push of tags only isn't a merge.
|
|
156
|
+
return !(push.tags || push['tags-ignore']) || Boolean(push.paths);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const TESTING = /\b(test|tests|e2e|playwright|cypress|integration|pytest|vitest|jest)\b/i;
|
|
160
|
+
const runsCodeKitVerify = (step) =>
|
|
161
|
+
typeof step?.run === 'string' && /code-kit(@\S+)?\s+verify\b/.test(step.run.replace(/\n/g, ' '));
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* CIC-2: what in one workflow multiplies its cost with a story per branch: [{ file, kind, what, fix }].
|
|
165
|
+
* `base` is the branch lanes merge into.
|
|
166
|
+
*/
|
|
167
|
+
export function workflowFindings(file, text, base) {
|
|
168
|
+
let doc;
|
|
169
|
+
try {
|
|
170
|
+
doc = parseYaml(text);
|
|
171
|
+
} catch {
|
|
172
|
+
return [];
|
|
173
|
+
}
|
|
174
|
+
const on = triggersOf(doc);
|
|
175
|
+
const jobs = doc.jobs && typeof doc.jobs === 'object' ? doc.jobs : {};
|
|
176
|
+
const steps = Object.values(jobs).flatMap((j) => (Array.isArray(j?.steps) ? j.steps : []));
|
|
177
|
+
const ours = steps.some(runsCodeKitVerify);
|
|
178
|
+
const out = [];
|
|
179
|
+
const add = (kind, what, fix) => out.push({ file, kind, what, fix });
|
|
180
|
+
if ('push' in on && pushRunsOnBase(on.push, base))
|
|
181
|
+
add(
|
|
182
|
+
'every-merge',
|
|
183
|
+
`runs on every push to ${base}, so once per story merged; the merge queue already checked each one`,
|
|
184
|
+
`run it on the release branches instead: on: push: branches: ['${RELEASE_PREFIX}**']`,
|
|
185
|
+
);
|
|
186
|
+
const onPr = 'pull_request' in on || 'pull_request_target' in on;
|
|
187
|
+
const pr = on.pull_request ?? on.pull_request_target ?? null;
|
|
188
|
+
const filtered = Boolean(pr?.paths || pr?.['paths-ignore']);
|
|
189
|
+
if (
|
|
190
|
+
onPr &&
|
|
191
|
+
!ours &&
|
|
192
|
+
!filtered &&
|
|
193
|
+
steps.some((s) => TESTING.test(`${s?.name ?? ''} ${s?.run ?? ''} ${s?.uses ?? ''}`))
|
|
194
|
+
)
|
|
195
|
+
add(
|
|
196
|
+
'every-pr',
|
|
197
|
+
'runs its tests on every pull request, so once per story, after the merge queue ran them',
|
|
198
|
+
`run it on the release branches instead (on: push: branches: ['${RELEASE_PREFIX}**']), keeping a fast check on pull requests if the project wants one`,
|
|
199
|
+
);
|
|
200
|
+
const untimed = Object.entries(jobs)
|
|
201
|
+
.filter(([, j]) => j && typeof j === 'object' && !('timeout-minutes' in j) && !j.uses)
|
|
202
|
+
.map(([id]) => id);
|
|
203
|
+
if (untimed.length)
|
|
204
|
+
add(
|
|
205
|
+
'no-timeout',
|
|
206
|
+
`${untimed.length === 1 ? 'job' : 'jobs'} ${untimed.join(', ')} ${untimed.length === 1 ? 'has' : 'have'} no timeout-minutes, so a hung step runs up to GitHub's ${GITHUB_LIMIT_MINUTES / 60} hours`,
|
|
207
|
+
'give each job a timeout-minutes a little over its usual run',
|
|
208
|
+
);
|
|
209
|
+
const cancels = (c) => c && typeof c === 'object' && c['cancel-in-progress'] === true;
|
|
210
|
+
if (
|
|
211
|
+
onPr &&
|
|
212
|
+
!cancels(doc.concurrency) &&
|
|
213
|
+
!Object.values(jobs).every((j) => cancels(j?.concurrency))
|
|
214
|
+
)
|
|
215
|
+
add(
|
|
216
|
+
'no-concurrency',
|
|
217
|
+
'has no concurrency with cancel-in-progress, so every push to a pull request runs to the end',
|
|
218
|
+
'add concurrency: { group: <workflow>-${{ github.head_ref || github.ref }}, cancel-in-progress: true }',
|
|
219
|
+
);
|
|
220
|
+
if (steps.some((s) => runsCodeKitVerify(s) && !/--no-checks\b/.test(s.run)))
|
|
221
|
+
add(
|
|
222
|
+
'code-kit-checks',
|
|
223
|
+
"code-kit's verify runs the project's checks again on every pull request, after the merge queue ran them",
|
|
224
|
+
'add --no-checks to its verify step',
|
|
225
|
+
);
|
|
226
|
+
return out;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/** CIC-2: every workflow's findings, in file order. */
|
|
230
|
+
export function ciFindings(root, base) {
|
|
231
|
+
const dir = join(root, WORKFLOWS_DIR);
|
|
232
|
+
if (!existsSync(dir)) return [];
|
|
233
|
+
return readdirSync(dir)
|
|
234
|
+
.filter((f) => /\.ya?ml$/.test(f))
|
|
235
|
+
.sort()
|
|
236
|
+
.flatMap((f) =>
|
|
237
|
+
workflowFindings(`${WORKFLOWS_DIR}/${f}`, readFileSync(join(dir, f), 'utf8'), base),
|
|
238
|
+
);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// --- what it spent ---------------------------------------------------------------------------------
|
|
242
|
+
|
|
243
|
+
/** GitHub refusing to start jobs for the account's billing, from a job's annotations. */
|
|
244
|
+
export const BILLING_REFUSAL = /spending limit|payments have failed|billing/i;
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* CIC-4: runs summed by workflow and trigger: { rows: [{ workflow, event, runs, minutes, cancelled,
|
|
248
|
+
* failed }], runs, minutes }, most minutes first. A run's minutes are its wall-clock time.
|
|
249
|
+
*/
|
|
250
|
+
export function usageSummary(runs) {
|
|
251
|
+
const by = new Map();
|
|
252
|
+
let minutes = 0;
|
|
253
|
+
for (const r of runs) {
|
|
254
|
+
const start = Date.parse(r.run_started_at ?? r.created_at);
|
|
255
|
+
const end = Date.parse(r.updated_at);
|
|
256
|
+
const m =
|
|
257
|
+
Number.isFinite(start) && Number.isFinite(end) && end > start ? (end - start) / 60000 : 0;
|
|
258
|
+
const key = `${r.name}\u0000${r.event}`;
|
|
259
|
+
const row = by.get(key) ?? {
|
|
260
|
+
workflow: r.name,
|
|
261
|
+
event: r.event,
|
|
262
|
+
runs: 0,
|
|
263
|
+
minutes: 0,
|
|
264
|
+
cancelled: 0,
|
|
265
|
+
failed: 0,
|
|
266
|
+
};
|
|
267
|
+
row.runs += 1;
|
|
268
|
+
row.minutes += m;
|
|
269
|
+
if (r.conclusion === 'cancelled') row.cancelled += 1;
|
|
270
|
+
if (r.conclusion === 'failure' || r.conclusion === 'timed_out') row.failed += 1;
|
|
271
|
+
by.set(key, row);
|
|
272
|
+
minutes += m;
|
|
273
|
+
}
|
|
274
|
+
const rows = [...by.values()]
|
|
275
|
+
.map((r) => ({ ...r, minutes: Math.round(r.minutes) }))
|
|
276
|
+
.sort((a, b) => b.minutes - a.minutes || a.workflow.localeCompare(b.workflow));
|
|
277
|
+
return { rows, runs: runs.length, minutes: Math.round(minutes) };
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
const gh = (root, args) =>
|
|
281
|
+
spawnSync('gh', args, { cwd: root, encoding: 'utf8', maxBuffer: 64 * 1024 * 1024 });
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* CIC-4: the project's Actions runs over the last `days`, summed, and whether GitHub refused to start the
|
|
285
|
+
* latest run's jobs for billing: { repo, days, ...usageSummary, refused } or { error }.
|
|
286
|
+
*/
|
|
287
|
+
export function ciUsage(root, { days = 7, now = Date.now() } = {}) {
|
|
288
|
+
const repo = gh(root, ['repo', 'view', '--json', 'nameWithOwner', '-q', '.nameWithOwner']);
|
|
289
|
+
if (repo.error || repo.status !== 0)
|
|
290
|
+
return {
|
|
291
|
+
error: `GitHub's CLI couldn't say which repository this is${repo.stderr ? `: ${repo.stderr.trim().split('\n')[0]}` : ''}. Run it in a clone with a GitHub remote, signed in with gh auth login.`,
|
|
292
|
+
};
|
|
293
|
+
const name = repo.stdout.trim();
|
|
294
|
+
const since = new Date(now - days * 86_400_000).toISOString().slice(0, 10);
|
|
295
|
+
const listed = gh(root, [
|
|
296
|
+
'api',
|
|
297
|
+
'--paginate',
|
|
298
|
+
`repos/${name}/actions/runs?created=%3E%3D${since}&per_page=100`,
|
|
299
|
+
'-q',
|
|
300
|
+
'.workflow_runs[] | {name, event, conclusion, run_started_at, created_at, updated_at, id}',
|
|
301
|
+
]);
|
|
302
|
+
if (listed.status !== 0)
|
|
303
|
+
return { error: `GitHub didn't list the runs: ${listed.stderr.trim().split('\n')[0]}` };
|
|
304
|
+
const runs = listed.stdout
|
|
305
|
+
.split('\n')
|
|
306
|
+
.filter(Boolean)
|
|
307
|
+
.map((l) => JSON.parse(l));
|
|
308
|
+
let refused = null;
|
|
309
|
+
const latest = runs.find((r) => r.conclusion === 'failure');
|
|
310
|
+
if (latest && latest === runs[0]) {
|
|
311
|
+
const jobs = gh(root, [
|
|
312
|
+
'api',
|
|
313
|
+
`repos/${name}/actions/runs/${latest.id}/jobs`,
|
|
314
|
+
'-q',
|
|
315
|
+
'.jobs[].id',
|
|
316
|
+
]);
|
|
317
|
+
for (const id of jobs.stdout.split('\n').filter(Boolean).slice(0, 3)) {
|
|
318
|
+
const notes = gh(root, [
|
|
319
|
+
'api',
|
|
320
|
+
`repos/${name}/check-runs/${id}/annotations`,
|
|
321
|
+
'-q',
|
|
322
|
+
'.[].message',
|
|
323
|
+
]);
|
|
324
|
+
const hit = notes.stdout.split('\n').find((m) => BILLING_REFUSAL.test(m));
|
|
325
|
+
if (hit) {
|
|
326
|
+
refused = hit.trim();
|
|
327
|
+
break;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
return { repo: name, days, ...usageSummary(runs), refused };
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
// --- the release branch ----------------------------------------------------------------------------
|
|
335
|
+
|
|
336
|
+
const git = (root, ...args) => {
|
|
337
|
+
try {
|
|
338
|
+
return execFileSync('git', ['-C', root, ...args], {
|
|
339
|
+
encoding: 'utf8',
|
|
340
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
341
|
+
}).trim();
|
|
342
|
+
} catch {
|
|
343
|
+
return null;
|
|
344
|
+
}
|
|
345
|
+
};
|
|
346
|
+
|
|
347
|
+
/** Version-aware order of release branches: release/0.10.0 after release/0.9.0. */
|
|
348
|
+
const byVersion = (a, b) => a.localeCompare(b, undefined, { numeric: true });
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* CIC-3: cuts `release/<version>` from `base` as committed and pushes it to `remote`, so the project's
|
|
352
|
+
* full CI runs once on it: { ok, branch, commit, stories, since } or { ok: false, why }.
|
|
353
|
+
*/
|
|
354
|
+
export function releaseBranch(root, version, { base, remote = 'origin', push = true } = {}) {
|
|
355
|
+
const branch = `${RELEASE_PREFIX}${String(version).replace(/^release\//, '')}`;
|
|
356
|
+
if (!version || git(root, 'check-ref-format', '--branch', branch) === null)
|
|
357
|
+
return {
|
|
358
|
+
ok: false,
|
|
359
|
+
why: `${branch} isn't a branch name git accepts. Give a version such as 0.4.0.`,
|
|
360
|
+
};
|
|
361
|
+
const commit = git(root, 'rev-parse', '--verify', '--quiet', `${base}^{commit}`);
|
|
362
|
+
if (!commit) return { ok: false, why: `There's no ${base} here to release from.` };
|
|
363
|
+
const here = git(root, 'rev-parse', '--verify', '--quiet', `refs/heads/${branch}`);
|
|
364
|
+
const there = push ? git(root, 'ls-remote', '--heads', remote, branch) : '';
|
|
365
|
+
if (here || there)
|
|
366
|
+
return {
|
|
367
|
+
ok: false,
|
|
368
|
+
why: `${branch} already exists${here ? `, at ${here.slice(0, 7)}` : ` on ${remote}`}. A release branch is cut once; give the next version.`,
|
|
369
|
+
};
|
|
370
|
+
// The stories merged since the last release branch, by the ids their commits name.
|
|
371
|
+
const earlier = (
|
|
372
|
+
git(
|
|
373
|
+
root,
|
|
374
|
+
'for-each-ref',
|
|
375
|
+
'--format=%(refname:short)',
|
|
376
|
+
`refs/heads/${RELEASE_PREFIX}`,
|
|
377
|
+
`refs/remotes/${remote}/${RELEASE_PREFIX}`,
|
|
378
|
+
) ?? ''
|
|
379
|
+
)
|
|
380
|
+
.split('\n')
|
|
381
|
+
.filter(Boolean)
|
|
382
|
+
.sort(byVersion);
|
|
383
|
+
const since = earlier.at(-1) ?? null;
|
|
384
|
+
const log = git(root, 'log', '--format=%s%n%b', since ? `${since}..${commit}` : commit) ?? '';
|
|
385
|
+
const stories = [...new Set(log.match(/\bST-\d+\b/g) ?? [])].sort(
|
|
386
|
+
(a, b) => a.slice(3) - b.slice(3),
|
|
387
|
+
);
|
|
388
|
+
if (git(root, 'branch', branch, commit) === null)
|
|
389
|
+
return { ok: false, why: `git couldn't make ${branch} at ${commit.slice(0, 7)}.` };
|
|
390
|
+
if (push) {
|
|
391
|
+
const pushed = spawnSync(
|
|
392
|
+
'git',
|
|
393
|
+
['-C', root, 'push', remote, `refs/heads/${branch}:refs/heads/${branch}`],
|
|
394
|
+
{
|
|
395
|
+
encoding: 'utf8',
|
|
396
|
+
},
|
|
397
|
+
);
|
|
398
|
+
if (pushed.status !== 0) {
|
|
399
|
+
git(root, 'branch', '-D', branch);
|
|
400
|
+
return {
|
|
401
|
+
ok: false,
|
|
402
|
+
why: `Pushing ${branch} to ${remote} failed: ${(pushed.stderr || '').trim().split('\n').at(-1)}`,
|
|
403
|
+
};
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
return { ok: true, branch, commit, stories, since: since?.replace(`${remote}/`, '') ?? null };
|
|
407
|
+
}
|
package/hooks/lib/config.mjs
CHANGED
|
@@ -91,11 +91,15 @@ export function withDefaults(c) {
|
|
|
91
91
|
layers: c.layers ?? [],
|
|
92
92
|
importAliases: c.importAliases ?? {},
|
|
93
93
|
shell: { block: c.shell?.block ?? [], restricted: c.shell?.restricted ?? [] },
|
|
94
|
+
execution: { unrepeatable: c.execution?.unrepeatable ?? [] },
|
|
94
95
|
postEdit: c.postEdit ?? [],
|
|
95
96
|
checks: c.checks ?? [],
|
|
96
97
|
tests: c.tests ?? [],
|
|
97
98
|
regression: { prepare: c.regression?.prepare ?? null },
|
|
98
|
-
branches: {
|
|
99
|
+
branches: {
|
|
100
|
+
protected: c.branches?.protected ?? ['main', 'master'],
|
|
101
|
+
publish: c.branches?.publish ?? null,
|
|
102
|
+
},
|
|
99
103
|
approvals: {
|
|
100
104
|
lead: c.approvals?.lead === true,
|
|
101
105
|
delegate: delegateRules(c.approvals?.delegate, c.protected ?? []),
|
|
@@ -404,9 +408,29 @@ export function validate(c) {
|
|
|
404
408
|
'"approvals.delegate" must be false, or { protected?: [approval names], merge?: true|false, push?: true|false, tests?: true|false, dependencies?: { licences?, minWeeklyDownloads?, maxMonthsSinceRelease?, allowInstallScripts?: true|false|["binary","build","other"] } }',
|
|
405
409
|
);
|
|
406
410
|
need(
|
|
407
|
-
c.branches === undefined || isList(c.branches
|
|
411
|
+
c.branches === undefined || c.branches.protected === undefined || isList(c.branches.protected),
|
|
408
412
|
'"branches.protected" must be a list of branch names',
|
|
409
413
|
);
|
|
414
|
+
need(
|
|
415
|
+
c.execution === undefined ||
|
|
416
|
+
(c.execution &&
|
|
417
|
+
typeof c.execution === 'object' &&
|
|
418
|
+
(c.execution.unrepeatable === undefined ||
|
|
419
|
+
(isList(c.execution.unrepeatable) &&
|
|
420
|
+
c.execution.unrepeatable.every((r) => {
|
|
421
|
+
try {
|
|
422
|
+
new RegExp(r);
|
|
423
|
+
return true;
|
|
424
|
+
} catch {
|
|
425
|
+
return false;
|
|
426
|
+
}
|
|
427
|
+
})))),
|
|
428
|
+
'"execution.unrepeatable" must be a list of patterns (regular expressions) for commands that can\'t safely run twice',
|
|
429
|
+
);
|
|
430
|
+
need(
|
|
431
|
+
[undefined, 'push', 'pull-request'].includes(c.branches?.publish),
|
|
432
|
+
'"branches.publish" must be "push" or "pull-request": how merges reach the remote\'s base',
|
|
433
|
+
);
|
|
410
434
|
p.push(...harnessProblems(c.harness));
|
|
411
435
|
p.push(...teamProblems(c));
|
|
412
436
|
return p;
|