slicekit 0.3.1 → 0.3.3
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/package.json +1 -1
- package/src/build/pr.js +10 -19
- package/src/check/ci-event.js +3 -2
- package/src/check/rules/runbook.js +60 -0
- package/src/check/rules/sync-release.js +5 -4
- package/src/cli/branch.js +66 -7
- package/src/cli/index.js +2 -0
- package/src/cli/release.js +57 -0
- package/src/cli/status.js +7 -4
- package/src/cli/workflow.js +2 -0
- package/src/config.js +1 -1
- package/src/init.js +4 -1
- package/src/markdown.js +2 -1
- package/src/observability.js +203 -0
- package/src/release-version.js +221 -46
- package/src/release.js +296 -0
- package/src/runbook.js +96 -0
- package/src/workflow.js +23 -7
- package/templates/instructions/commands/intake.md +10 -0
- package/templates/instructions/commands/release.md +51 -0
package/src/runbook.js
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
// The runbook judgement (03 REQ-3.7, D-21, D-55), shared by the `runbook` check rule, `build pr` and
|
|
2
|
+
// `slicekit release`: a slice that says `runbook: yes` changes a `##` section of the runbook, or its
|
|
3
|
+
// pull request body's Runbook line begins "no operational change".
|
|
4
|
+
import { execFileSync } from 'node:child_process';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import { GitError, git } from './git.js';
|
|
7
|
+
import { headings, lines } from './markdown.js';
|
|
8
|
+
import { kitPaths } from './paths.js';
|
|
9
|
+
import { getSection } from './prbody.js';
|
|
10
|
+
|
|
11
|
+
export const NO_CHANGE = 'no operational change';
|
|
12
|
+
|
|
13
|
+
// The runbook's POSIX path relative to the root: `runbook.md` when `docsRoot` is `.`.
|
|
14
|
+
export function runbookRel(root, cfg) {
|
|
15
|
+
return path.relative(root, kitPaths(root, cfg).runbook).split(path.sep).join('/');
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const resolves = (root, ref) => {
|
|
19
|
+
try {
|
|
20
|
+
git(root, ['rev-parse', '--verify', '--quiet', ref]);
|
|
21
|
+
return true;
|
|
22
|
+
} catch (e) {
|
|
23
|
+
if (e instanceof GitError) return false;
|
|
24
|
+
throw e;
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
// The default branch as origin has it, else the local one.
|
|
29
|
+
export function defaultRef(root, cfg) {
|
|
30
|
+
const origin = `refs/remotes/origin/${cfg.defaultBranch}`;
|
|
31
|
+
return resolves(root, origin) ? origin : `refs/heads/${cfg.defaultBranch}`;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// The commit `tip` left the default branch at.
|
|
35
|
+
export function mergeBase(root, cfg, tip = 'HEAD') {
|
|
36
|
+
return git(root, ['merge-base', tip, defaultRef(root, cfg)]);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// The file at `rev`, byte for byte (git() trims, which would move every line number of a file that
|
|
40
|
+
// begins with a blank line); '' when the commit does not hold it.
|
|
41
|
+
function fileAt(root, rev, rel) {
|
|
42
|
+
try {
|
|
43
|
+
return execFileSync('git', ['show', `${rev}:${rel}`], { cwd: root, encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'], maxBuffer: 64 * 1024 * 1024 });
|
|
44
|
+
} catch {
|
|
45
|
+
return '';
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// line number (1-based) -> the text of the `##` heading whose section holds it, or null above the
|
|
50
|
+
// first one. A heading line belongs to its own section; a fenced `## ` line is not a heading.
|
|
51
|
+
function sectionFinder(text) {
|
|
52
|
+
const hs = headings(text).filter((h) => h.level === 2);
|
|
53
|
+
return (n) => {
|
|
54
|
+
let found = null;
|
|
55
|
+
for (const h of hs) {
|
|
56
|
+
if (h.line > n) break;
|
|
57
|
+
found = h.text;
|
|
58
|
+
}
|
|
59
|
+
return found;
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const HUNK = /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/;
|
|
64
|
+
|
|
65
|
+
// The `##` headings of the runbook whose section holds a line added or removed between `base` and
|
|
66
|
+
// `tip`, sorted and unique. Removed lines are placed with the base's file, added lines with the
|
|
67
|
+
// tip's. A change of trailing whitespace only is no change (D-55).
|
|
68
|
+
export function runbookSections(root, rel, base, tip) {
|
|
69
|
+
const diff = git(root, ['--literal-pathspecs', 'diff', '--no-color', '--no-ext-diff', '--no-renames', '--unified=0', '--ignore-space-at-eol', base, tip, '--', rel]);
|
|
70
|
+
if (diff === '') return [];
|
|
71
|
+
const inBase = sectionFinder(fileAt(root, base, rel));
|
|
72
|
+
const inTip = sectionFinder(fileAt(root, tip, rel));
|
|
73
|
+
const found = new Set();
|
|
74
|
+
const mark = (finder, start, count) => {
|
|
75
|
+
for (let n = start; n < start + count; n++) {
|
|
76
|
+
const s = finder(n);
|
|
77
|
+
if (s !== null) found.add(s);
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
for (const line of lines(diff)) {
|
|
81
|
+
const m = HUNK.exec(line);
|
|
82
|
+
if (!m) continue;
|
|
83
|
+
mark(inBase, Number(m[1]), m[2] === undefined ? 1 : Number(m[2]));
|
|
84
|
+
mark(inTip, Number(m[3]), m[4] === undefined ? 1 : Number(m[4]));
|
|
85
|
+
}
|
|
86
|
+
return [...found].sort();
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// null when the slice passes, else the finding's message. `body` is a pull request body, or null
|
|
90
|
+
// when there is none to read; then only the flag and the changed sections count.
|
|
91
|
+
export function runbookVerdict({ flag, sections, body, rel }) {
|
|
92
|
+
if (!flag) return null;
|
|
93
|
+
if (sections.length > 0) return null;
|
|
94
|
+
if (typeof body === 'string' && (getSection(body, 'Runbook') ?? '').trim().startsWith(NO_CHANGE)) return null;
|
|
95
|
+
return `runbook: the slice says runbook: yes, ${rel} changed no section, and the Runbook line does not begin "${NO_CHANGE}"`;
|
|
96
|
+
}
|
package/src/workflow.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// The GitHub Actions workflow init writes (REQ-3.11): a gate, a job per declared team command, a job
|
|
2
|
-
// per observability step (D-20), the ready label and the post-merge state sync (D-12), which opens a
|
|
2
|
+
// per observability step of config and of observability.md (D-20, D-56), the ready label and the post-merge state sync (D-12), which opens a
|
|
3
3
|
// [sync] pull request and starts the workflow on its branch with workflow_dispatch (D-78, D-80). The YAML is
|
|
4
4
|
// generated as text; every free-text value goes through yamlScalar so a quote, colon or hash in a
|
|
5
5
|
// team's command cannot change the document's structure.
|
|
@@ -7,8 +7,10 @@ import { execFileSync } from 'node:child_process';
|
|
|
7
7
|
import fs from 'node:fs';
|
|
8
8
|
import path from 'node:path';
|
|
9
9
|
import { fileURLToPath } from 'node:url';
|
|
10
|
-
import { DEFAULT_RUNS_ON, DEFAULT_TIMEOUT_MINUTES, GITHUB_EXPRESSION, LINE_SEPARATOR, MATRIX_OS, shardErrors, shardRunnerErrors } from './config.js';
|
|
10
|
+
import { ConfigError, DEFAULT_RUNS_ON, DEFAULT_TIMEOUT_MINUTES, GITHUB_EXPRESSION, LINE_SEPARATOR, MATRIX_OS, shardErrors, shardRunnerErrors } from './config.js';
|
|
11
11
|
import { readText } from './fs.js';
|
|
12
|
+
import { allSteps, docSteps } from './observability.js';
|
|
13
|
+
import { kitPaths } from './paths.js';
|
|
12
14
|
import { substitute } from './render/entry.js';
|
|
13
15
|
|
|
14
16
|
export const WORKFLOW_TEMPLATE = fileURLToPath(new URL('../templates/workflow/slicekit.yml', import.meta.url));
|
|
@@ -277,7 +279,9 @@ function fillFreeText(line, vars) {
|
|
|
277
279
|
return m[1] + yamlScalar(substitute(m[2], vars, FREE_TEXT_KEYS));
|
|
278
280
|
}
|
|
279
281
|
|
|
280
|
-
|
|
282
|
+
// `steps` are the observability steps to render, one obs-<n> job each in that order: config's own when
|
|
283
|
+
// the caller passes none; workflowState passes config's followed by those of observability.md (D-56).
|
|
284
|
+
export function renderWorkflow(cfg, pins = loadPins(), { template = fs.readFileSync(WORKFLOW_TEMPLATE, 'utf8'), root = null, steps = cfg.ci?.observabilitySteps ?? [] } = {}) {
|
|
281
285
|
if (/[\r\n]/.test(cfg.cli)) throw new Error('cli: must be one line to render the workflow');
|
|
282
286
|
const v = checkPins(pins);
|
|
283
287
|
const matrix = cfg.ci?.matrix === true;
|
|
@@ -294,7 +298,6 @@ export function renderWorkflow(cfg, pins = loadPins(), { template = fs.readFileS
|
|
|
294
298
|
const shards = given && (matrix ? given : { [runsOn]: Object.values(given)[0] });
|
|
295
299
|
const commands = COMMAND_ORDER.filter((name) => typeof cfg.commands[name] === 'string' && cfg.commands[name].trim() !== '');
|
|
296
300
|
const jdk = setupJavaSteps(v, cfg.ci?.java ?? DEFAULT_JAVA, javaBuildTool(root, commands.map((c) => cfg.commands[c])), root);
|
|
297
|
-
const steps = cfg.ci?.observabilitySteps ?? [];
|
|
298
301
|
const texts = [cfg.cli, ...commands.map((c) => cfg.commands[c]), ...steps.map((s) => s.run)];
|
|
299
302
|
if (texts.some((t) => GITHUB_EXPRESSION.test(t))) throw new Error('workflow: a command contains a GitHub expression; pass values through env:');
|
|
300
303
|
const blocks = {
|
|
@@ -327,12 +330,25 @@ export function renderWorkflow(cfg, pins = loadPins(), { template = fs.readFileS
|
|
|
327
330
|
return out.join('\n');
|
|
328
331
|
}
|
|
329
332
|
|
|
330
|
-
// The
|
|
333
|
+
// The observability steps of a repository (D-56): config's, then the rows of the steps table of its
|
|
334
|
+
// observability.md when the file exists. A step the workflow cannot carry, or one named in both
|
|
335
|
+
// places, throws a ConfigError naming the file and line; a row with no command is a warning.
|
|
336
|
+
export function workflowSteps(root, cfg) {
|
|
337
|
+
const file = kitPaths(root, cfg).observability;
|
|
338
|
+
const rel = path.relative(root, file).split(path.sep).join('/');
|
|
339
|
+
const all = allSteps(cfg, fs.existsSync(file) ? docSteps(readText(file), rel) : null);
|
|
340
|
+
if (all.errors.length > 0) throw new ConfigError(all.errors);
|
|
341
|
+
return { steps: all.steps, warnings: all.warnings };
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
// The rendered workflow and the repository's copy of it (null when absent), line endings normalised,
|
|
345
|
+
// with the warnings of the steps read from observability.md.
|
|
331
346
|
export function workflowState(root, cfg, pins = loadPins()) {
|
|
332
|
-
const
|
|
347
|
+
const { steps, warnings } = workflowSteps(root, cfg);
|
|
348
|
+
const expected = renderWorkflow(cfg, pins, { root, steps });
|
|
333
349
|
const file = path.join(root, WORKFLOW_PATH);
|
|
334
350
|
const current = fs.existsSync(file) ? readText(file) : null;
|
|
335
|
-
return { file, expected, current };
|
|
351
|
+
return { file, expected, current, warnings };
|
|
336
352
|
}
|
|
337
353
|
|
|
338
354
|
// D-49: a team whose test command never reads SLICEKIT_SHARD runs the whole command in every shard.
|
|
@@ -74,6 +74,16 @@ The sections of `<docsRoot>/observability.md`:
|
|
|
74
74
|
|
|
75
75
|
After each answer step, write the answer into its section of the file, followed by the D-ID in brackets, for example `One Postgres on Fly (D-07).`. Leave the single line "Not yet known" in each section the human cannot answer, and record that question with `--open`. Change nothing else in these two files.
|
|
76
76
|
|
|
77
|
+
Write the answer to `CI steps the monitoring setup requires` as a table with the two columns Step and Command, one command per row, and the D-ID on a line of its own below the table:
|
|
78
|
+
|
|
79
|
+
```
|
|
80
|
+
| Step | Command |
|
|
81
|
+
|---|---|
|
|
82
|
+
| Trace check | `npm run trace-check` |
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
Each row becomes one job of the generated workflow, named by its Step and running its Command. Write the table in exactly this form: the header cells are `Step` and `Command` in plain text with these capitals, the divider line has two cells, and every line of the table opens with a vertical bar. The table is the only place in the section where a vertical bar may stand: any other line holding one, a second table included, is refused with its file and line. A Command cell is the whole command as one code span, or text with no backtick; put nothing else in the cell, the D-ID included. Text in a code fence, in an indented code block or in an HTML comment is not read. A command is one line and holds no secret and no `${{ … }}` expression; a pipe inside a command is written `\|`, inside a code span too, and a row with a bare pipe is refused. A step that needs a secret does not go in the table: tell the human to add it to `ci.observabilitySteps` in `.slicekit/config.json` with the secret under `env`. When the monitoring setup asks nothing of CI, write `None` and no table. After the table changes, run `{{cli}} workflow --write`.
|
|
86
|
+
|
|
77
87
|
## 6. Finish
|
|
78
88
|
|
|
79
89
|
`{{cli}} intake done`
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# /slicekit release
|
|
2
|
+
|
|
3
|
+
Write the release record of a phase whose slices are all merged, set each slice released, then open
|
|
4
|
+
the release pull request (REQ-2.9). Arguments: {{args}} (the phase).
|
|
5
|
+
|
|
6
|
+
`<docsRoot>` below is the `docsRoot` in `.slicekit/config.json`. Every step that touches the
|
|
7
|
+
repository's history goes through `{{cli}}`; run no git or gh command yourself. The release records
|
|
8
|
+
what merged; it does not deploy (D-12).
|
|
9
|
+
|
|
10
|
+
## Steps
|
|
11
|
+
|
|
12
|
+
1. Run `{{cli}} release {{args}} --dry-run`. It writes nothing. When it exits 0 it prints the record
|
|
13
|
+
it would write. When it exits 1 it prints one line per reason: a slice that is not merged, a
|
|
14
|
+
slice with no recorded pull request, a slice that says `runbook: yes` and neither changed a
|
|
15
|
+
section of the runbook nor gave a Runbook line that begins "no operational change", or a record
|
|
16
|
+
that already exists. Print those lines to the human and stop: the release waits for each one.
|
|
17
|
+
2. Run `{{cli}} branch release <slug>`, the slug being the phase in lower case (`p1` for `P1`). It
|
|
18
|
+
puts the session on `release/<slug>` at the tip of the default branch as origin has it; a
|
|
19
|
+
`release/<slug>` left behind that tip with no commit of its own is moved there, and the command
|
|
20
|
+
says so. When `release/<slug>` holds commits the default branch lacks, it asks the git host for
|
|
21
|
+
an open pull request of that branch and exits 1 with one of three lines:
|
|
22
|
+
1. `release/<slug> is the branch of open pull request #<n> (<url>); the phase's release is
|
|
23
|
+
already open`: print the line to the human and stop. Take no other slug: merging or closing
|
|
24
|
+
that pull request is the human's.
|
|
25
|
+
2. A line saying the git host could not be asked: print it to the human and stop. Take no other
|
|
26
|
+
slug.
|
|
27
|
+
3. A line that names the next slug (`p1-2`): no pull request of that branch is open, so run the
|
|
28
|
+
command again with that slug.
|
|
29
|
+
3. Run `{{cli}} release {{args}}`. It prints the record's path, `<docsRoot>/releases/{{args}}.md`,
|
|
30
|
+
then one `released <slice>` line per slice, and rewrites `BUILD.md`. When it exits 1, print its
|
|
31
|
+
lines to the human and stop.
|
|
32
|
+
4. Run `{{cli}} check` and fix each finding it prints until it reports `0 findings`. Never edit the
|
|
33
|
+
record by hand. A finding in the record points at text the record quotes, and is fixed there:
|
|
34
|
+
1. Run `{{cli}} release {{args}} --undo`. It removes the record, which is not committed yet, and
|
|
35
|
+
puts the state files and `BUILD.md` back as they were, so no change is left in the session.
|
|
36
|
+
2. A pull request title or body is corrected on the host. The runbook or a decision row is
|
|
37
|
+
corrected on its own pull request, which a human merges; tell the human what to correct, and
|
|
38
|
+
stop until it has merged.
|
|
39
|
+
3. Start again at step 1, with the same slug in step 2.
|
|
40
|
+
5. Write the pull request body to a file outside the repository, with these three sections:
|
|
41
|
+
|
|
42
|
+
```
|
|
43
|
+
**Done when:** every slice of {{args}} released
|
|
44
|
+
**Proof:** <the summary line check printed>
|
|
45
|
+
**Decisions:** <the lines under "## Decisions" in the record, one per line, or "none">
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
6. Run `{{cli}} pr open --title "[release] {{args}}" --body-file <file> --commit "docs(release): {{args}}" --paths <docsRoot>/releases/{{args}}.md,.slicekit/state,BUILD.md`.
|
|
49
|
+
7. Print the pull request line `pr open` printed, and tell the human that merging the pull request
|
|
50
|
+
is the release of {{args}} in this repository, and that it deploys nothing: deployment stays the
|
|
51
|
+
team's (D-12).
|