@d-zero/release-automation 0.0.0 → 0.0.1

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.
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Adds a release to the top of a changelog, under its title. The result
3
+ * ends with exactly one newline and has one blank line between blocks, the
4
+ * shape Markdown formatters (oxfmt, Prettier) leave unchanged, so the
5
+ * format check of the Release PR passes.
6
+ * @param previous - The current changelog, or `null` when there is none
7
+ * @param release - The release
8
+ * @param release.version - The version
9
+ * @param release.date - The date, `YYYY-MM-DD`
10
+ * @param release.entries - One line per change, without the leading `- `
11
+ * @returns The new changelog
12
+ * @example
13
+ * prependRelease(null, { version: '0.0.1', date: '2026-11-26', entries: ['fix: x (abc1234)'] });
14
+ * // '# Changelog\n\n## 0.0.1 (2026-11-26)\n\n- fix: x (abc1234)\n'
15
+ */
16
+ export function prependRelease(previous, release) {
17
+ const [title = '# Changelog', ...rest] = (previous ?? '# Changelog\n').split('\n');
18
+ const body = rest.join('\n').trim();
19
+ const entry = [
20
+ `## ${release.version} (${release.date})`,
21
+ release.entries.map((line) => `- ${line}`).join('\n'),
22
+ ]
23
+ .filter((block) => block !== '')
24
+ .join('\n\n');
25
+ return `${[title, entry, body].filter((block) => block !== '').join('\n\n')}\n`;
26
+ }
package/dist/cli.js CHANGED
File without changes
@@ -53,13 +53,14 @@ export function github(tokenVariable) {
53
53
  * Classifies the repository and refuses to go on while it has errors.
54
54
  * @param git - Git access
55
55
  * @param now - The current time
56
+ * @param rev - The revision the definition is read from; `origin/dev` by default
56
57
  * @returns The lines and the review mode
57
58
  * @throws {Error} When the classification has errors
58
59
  * @example
59
60
  * const { lines } = classifiedLines(git, new Date());
60
61
  */
61
- export function classifiedLines(git, now) {
62
- const result = classify(readFacts(git, now));
62
+ export function classifiedLines(git, now, rev) {
63
+ const result = classify(readFacts(git, now, rev));
63
64
  if (result.errors.length > 0) {
64
65
  throw new Error(`The repository does not follow the rules yet:\n${result.errors.map((e) => `- ${e.message}`).join('\n')}`);
65
66
  }
@@ -1,5 +1,6 @@
1
1
  import { readFileSync, writeFileSync, existsSync } from 'node:fs';
2
2
  import path from 'node:path';
3
+ import { prependRelease } from '../changelog.js';
3
4
  import { parseCommit } from '../conventional-commit.js';
4
5
  import { runShell } from '../io/exec.js';
5
6
  import { versionAt } from '../io/repo.js';
@@ -68,13 +69,12 @@ export function generateRelease(input) {
68
69
  const parsed = parseCommit(message);
69
70
  return (parsed.breaking || ['feat', 'fix', 'perf', 'revert'].includes(parsed.type ?? ''));
70
71
  })
71
- .map(({ sha, message }) => `- ${message.split('\n')[0]} (${sha.slice(0, 7)})`);
72
- const entry = `## ${next.version} (${formatDate(input.now)})\n\n${counted.join('\n')}\n`;
73
- const previous = existsSync(changelogPath)
74
- ? readFileSync(changelogPath, 'utf8')
75
- : '# Changelog\n';
76
- const [title = '# Changelog', ...rest] = previous.split('\n');
77
- writeFileSync(changelogPath, `${title}\n\n${entry}${rest.join('\n').replace(/^\n*/, '\n')}`);
72
+ .map(({ sha, message }) => `${message.split('\n')[0]} (${sha.slice(0, 7)})`);
73
+ writeFileSync(changelogPath, prependRelease(existsSync(changelogPath) ? readFileSync(changelogPath, 'utf8') : null, {
74
+ version: next.version,
75
+ date: formatDate(input.now),
76
+ entries: counted,
77
+ }));
78
78
  }
79
79
  // The first stable release of a major on the current line graduates its
80
80
  // own prerelease (X.0.0-rc.n becomes X.0.0).
@@ -8,7 +8,8 @@ import { formatDate } from '../support-end.js';
8
8
  import { classifiedLines, localGit, summary } from './context.js';
9
9
  /**
10
10
  * Prints the classification of the repository: lines, modes, errors and the
11
- * actions the setup takes. Reads the remote refs; fetch them first.
11
+ * actions the setup takes. Reads the files of `HEAD` and the remote refs;
12
+ * fetch them first.
12
13
  * @param options - Output format
13
14
  * @param options.json - Print JSON
14
15
  * @returns The exit code: 1 when there are errors
@@ -16,7 +17,7 @@ import { classifiedLines, localGit, summary } from './context.js';
16
17
  * process.exitCode = classifyCommand({ json: false });
17
18
  */
18
19
  export function classifyCommand(options) {
19
- const result = classify(readFacts(localGit(), new Date()));
20
+ const result = classify(readFacts(localGit(), new Date(), 'HEAD'));
20
21
  if (options.json) {
21
22
  // oxlint-disable-next-line no-console -- the CLI prints its result
22
23
  console.log(JSON.stringify(result, null, 2));
@@ -40,13 +41,15 @@ export function classifyCommand(options) {
40
41
  }
41
42
  /**
42
43
  * Compares the files that name branches with the lines (see `checkLines`).
44
+ * Reads the definition of `HEAD`, so a pull request is checked against the
45
+ * lines it proposes.
43
46
  * @returns The exit code: 1 when they differ
44
47
  * @example
45
48
  * process.exitCode = await linesCheckCommand();
46
49
  */
47
50
  export async function linesCheckCommand() {
48
51
  const git = localGit();
49
- const { lines } = classifiedLines(git, new Date());
52
+ const { lines } = classifiedLines(git, new Date(), 'HEAD');
50
53
  const cwd = git.cwd;
51
54
  const read = (file) => {
52
55
  const full = path.join(cwd, file);
@@ -206,14 +206,13 @@ async function syncToDev(git, gh, sha) {
206
206
  const token = requireEnv('PUBLISH_APP_TOKEN');
207
207
  const slug = requireEnv('PUBLISH_APP_SLUG');
208
208
  const bot = await gh.rest('GET', `/users/${encodeURIComponent(`${slug}[bot]`)}`);
209
- const remote = `https://x-access-token:${token}@github.com/${gh.repository}.git`;
210
209
  const syncBranch = `sync/${sha.slice(0, 12)}`;
211
210
  await retry(async () => {
212
211
  git.raw('fetch', 'origin', 'dev');
213
212
  git.raw('checkout', '--force', '--detach', 'origin/dev');
214
213
  git.raw('-c', `user.name=${slug}[bot]`, '-c', `user.email=${bot.id}+${slug}[bot]@users.noreply.github.com`, 'merge', '--no-ff', '--no-edit', sha);
215
214
  const merged = git.sha('HEAD');
216
- git.raw('push', '--force', remote, `HEAD:refs/heads/${syncBranch}`);
215
+ git.pushAs(token, gh.repository, `HEAD:refs/heads/${syncBranch}`);
217
216
  await waitForCheck(gh, merged, CI_OK);
218
217
  await gh.rest('PATCH', '/repos/{repo}/git/refs/heads/dev', {
219
218
  sha: merged,
@@ -268,7 +268,7 @@ async function pushRelease(input) {
268
268
  git.raw('add', '--all');
269
269
  git.raw('-c', `user.name=${appSlug}[bot]`, '-c', `user.email=${bot.id}+${appSlug}[bot]@users.noreply.github.com`, 'commit', '--no-verify', '--message', generated.message);
270
270
  const token = requireEnv('RELEASE_APP_TOKEN');
271
- git.raw('push', '--force', `https://x-access-token:${token}@github.com/${input.gh.repository}.git`, `HEAD:refs/heads/${releaseBranch(input.line.branch)}`);
271
+ git.pushAs(token, input.gh.repository, `HEAD:refs/heads/${releaseBranch(input.line.branch)}`);
272
272
  return { ok: true, version: generated.version };
273
273
  }
274
274
  /**
package/dist/io/exec.js CHANGED
@@ -1,4 +1,16 @@
1
1
  import { execFileSync } from 'node:child_process';
2
+ /**
3
+ * Hides the credentials in a text: a token in a URL (`https://x-access-token:<token>@…`).
4
+ * Pushes pass the token in the remote URL, and a failed command's error
5
+ * repeats its arguments, so every error from {@link run} goes through this.
6
+ * @param text - Any text
7
+ * @returns The text with the credentials replaced by `***`
8
+ * @example
9
+ * redact('https://x-access-token:ghs_abc@github.com/o/r.git'); // 'https://x-access-token:***@github.com/o/r.git'
10
+ */
11
+ export function redact(text) {
12
+ return text.replaceAll(/(https?:\/\/[^:/@\s]+:)[^@\s]+@/g, '$1***@');
13
+ }
2
14
  /**
3
15
  * Runs a program and returns its standard output.
4
16
  * @param file - The program
@@ -7,18 +19,29 @@ import { execFileSync } from 'node:child_process';
7
19
  * @param options.cwd - The working directory
8
20
  * @param options.env - Variables added to the environment
9
21
  * @returns The standard output, without the trailing newline
10
- * @throws {Error} When the program exits with a non-zero code
22
+ * @throws {Error} When the program exits with a non-zero code; the message
23
+ * names the command and its standard error, with credentials redacted
11
24
  * @example
12
25
  * run('git', ['rev-parse', 'HEAD'], { cwd: process.cwd() });
13
26
  */
14
27
  export function run(file, args, options) {
15
- return execFileSync(file, args, {
16
- cwd: options.cwd,
17
- encoding: 'utf8',
18
- maxBuffer: 256 * 1024 * 1024,
19
- env: { ...process.env, ...options.env },
20
- stdio: ['ignore', 'pipe', 'pipe'],
21
- }).replace(/\n$/, '');
28
+ try {
29
+ return execFileSync(file, args, {
30
+ cwd: options.cwd,
31
+ encoding: 'utf8',
32
+ maxBuffer: 256 * 1024 * 1024,
33
+ env: { ...process.env, ...options.env },
34
+ stdio: ['ignore', 'pipe', 'pipe'],
35
+ }).replace(/\n$/, '');
36
+ }
37
+ catch (error) {
38
+ const stderr = typeof error === 'object' && error !== null && 'stderr' in error
39
+ ? String(error.stderr)
40
+ : '';
41
+ // The original error is not attached as `cause`: its message repeats the
42
+ // arguments unredacted.
43
+ throw new Error(redact(`${file} ${args.join(' ')} failed\n${stderr}`.trim()));
44
+ }
22
45
  }
23
46
  /**
24
47
  * Runs a program with its output passed through, for long commands such as a publish.
package/dist/io/git.js CHANGED
@@ -1,4 +1,30 @@
1
1
  import { run } from './exec.js';
2
+ /**
3
+ * The arguments of a force push to a GitHub repository as the owner of a
4
+ * token. `actions/checkout` leaves the workflow's own token in
5
+ * `http.https://github.com/.extraheader`, and git sends that header in place
6
+ * of the credentials of the URL, so the push would run as
7
+ * `github-actions[bot]` with read access. An empty value of a multi-valued
8
+ * `http.extraHeader` resets the list, and a `-c` option comes last, so the
9
+ * token of the URL is the only credential.
10
+ * @param token - The token, such as a GitHub App installation token
11
+ * @param repository - `owner/name`
12
+ * @param refspec - What to push, such as `HEAD:refs/heads/release/dev`
13
+ * @returns The git arguments
14
+ * @example
15
+ * pushArgs('ghs_x', 'o/r', 'HEAD:refs/heads/b');
16
+ * // ['-c', 'http.https://github.com/.extraheader=', 'push', '--force', 'https://x-access-token:ghs_x@github.com/o/r.git', 'HEAD:refs/heads/b']
17
+ */
18
+ export function pushArgs(token, repository, refspec) {
19
+ return [
20
+ '-c',
21
+ 'http.https://github.com/.extraheader=',
22
+ 'push',
23
+ '--force',
24
+ `https://x-access-token:${token}@github.com/${repository}.git`,
25
+ refspec,
26
+ ];
27
+ }
2
28
  /**
3
29
  * @param cwd - The root of the checkout
4
30
  * @returns Git access for it
@@ -45,5 +71,8 @@ export function createGit(cwd) {
45
71
  return { sha, message: message.trim() };
46
72
  }),
47
73
  raw: (...args) => git(...args),
74
+ pushAs: (token, repository, refspec) => {
75
+ git(...pushArgs(token, repository, refspec));
76
+ },
48
77
  };
49
78
  }
package/dist/io/repo.js CHANGED
@@ -134,16 +134,23 @@ export function publishModeAt(git, rev) {
134
134
  return root.private === true ? 'custom' : 'npm-single';
135
135
  }
136
136
  /**
137
- * Reads the facts `classify` decides on, from the remote refs and `dev`.
138
- * The caller fetches the branches and tags first.
137
+ * Reads the facts `classify` decides on: the files at a revision, and the
138
+ * branches and tags of the remote. The caller fetches the branches and tags
139
+ * first.
140
+ *
141
+ * The revision is `origin/dev` by default: `dev` holds the definition the
142
+ * automation acts on. The checks of a pull request read `HEAD` instead, so
143
+ * that the pull request that brings in or changes the definition is checked
144
+ * against what it proposes.
139
145
  * @param git - Git access
140
146
  * @param now - The current time
147
+ * @param rev - The revision the files are read from
141
148
  * @returns The facts
142
149
  * @example
143
150
  * classify(readFacts(git, new Date()));
151
+ * classify(readFacts(git, new Date(), 'HEAD'));
144
152
  */
145
- export function readFacts(git, now) {
146
- const rev = `origin/${CURRENT_BRANCH}`;
153
+ export function readFacts(git, now, rev = `origin/${CURRENT_BRANCH}`) {
147
154
  const lerna = git.show(rev, 'lerna.json');
148
155
  const root = JSON.parse(git.show(rev, 'package.json') ?? '{}');
149
156
  const codeowners = git.show(rev, '.github/CODEOWNERS');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@d-zero/release-automation",
3
- "version": "0.0.0",
3
+ "version": "0.0.1",
4
4
  "description": "Release lines, Release PRs, nightly gates and publishing for D-ZERO repositories",
5
5
  "author": "D-ZERO",
6
6
  "license": "MIT",
@@ -53,7 +53,8 @@
53
53
  "lint:oxfmt:check": "oxfmt --check \"{*,**/*}.{md,json,js,cjs,mjs,ts,mts,yml,yaml}\"",
54
54
  "lint:textlint:fix": "textlint --fix \"{*,**/*}.md\"",
55
55
  "lint:textlint:check": "textlint \"{*,**/*}.md\"",
56
- "postinstall": "husky"
56
+ "postinstall": "husky",
57
+ "release-automation": "tsc && node dist/cli.js"
57
58
  },
58
59
  "dependencies": {
59
60
  "yaml": "2.9.1"
@@ -64,19 +65,20 @@
64
65
  "@d-zero/oxlint-config": "6.0.0-alpha.3",
65
66
  "@d-zero/textlint-config": "6.0.0-alpha.3",
66
67
  "@d-zero/tsconfig": "2.0.0",
67
- "@types/node": "26.0.1",
68
+ "@types/node": "26.6.6",
68
69
  "husky": "9.1.7",
69
- "npm-run-all2": "9.0.3",
70
- "oxfmt": "0.62.0",
71
- "oxlint": "1.77.0",
72
- "oxlint-tsgolint": "7.0.2005",
73
- "textlint": "15.8.0",
74
- "typescript": "6.0.3",
75
- "vitest": "4.1.12"
70
+ "npm-run-all2": "9.1.0",
71
+ "oxfmt": "0.79.0",
72
+ "oxlint": "1.94.0",
73
+ "oxlint-tsgolint": "7.0.2008",
74
+ "textlint": "16.1.3",
75
+ "typescript": "7.0.2",
76
+ "vite": "8.3.8",
77
+ "vitest": "5.1.1"
76
78
  },
77
- "packageManager": "yarn@4.18.1",
79
+ "packageManager": "yarn@4.19.0",
78
80
  "volta": {
79
- "node": "26.7.0",
80
- "yarn": "4.18.1"
81
+ "node": "26.13.1",
82
+ "yarn": "4.19.0"
81
83
  }
82
84
  }