From 55d2c1e12d1f7272d7796ba330a07ffcd94ff61c Mon Sep 17 00:00:00 2001 From: Victor Larodiel Date: Mon, 16 Nov 2026 14:16:53 -0300 Subject: [PATCH] Shared lib: the changelogger section reader and the phar download --- .github/actions/release-prep/release-prep.js | 13 +--- .github/lib/changelogger.js | 32 +++++++++ .github/lib/phar.js | 74 ++++++++++++++++++++ .github/lib/pup.js | 39 +---------- tests/lib/changelogger.test.js | 18 +++++ tests/lib/phar.test.js | 63 +++++++++++++++++ 6 files changed, 193 insertions(+), 46 deletions(-) create mode 100644 .github/lib/changelogger.js create mode 100644 .github/lib/phar.js create mode 100644 tests/lib/changelogger.test.js create mode 100644 tests/lib/phar.test.js diff --git a/.github/actions/release-prep/release-prep.js b/.github/actions/release-prep/release-prep.js index d9fb958..2755bd0 100644 --- a/.github/actions/release-prep/release-prep.js +++ b/.github/actions/release-prep/release-prep.js @@ -29,6 +29,7 @@ const { pupRunner, lastLine, } = require('../../lib/pup.js') +const { readChangeloggerSection } = require('../../lib/changelogger.js') const { readCheckoutFile } = require('../../lib/workspace.js') /** @@ -297,21 +298,13 @@ function readPuprc(text) { * `changelogger` section at all. */ function readChangeloggerStrategy(text) { - let section = null - - if (text !== null) { - try { - section = JSON.parse(text)?.changelogger ?? null - } catch { - section = null - } - } + const section = readChangeloggerSection(text) if (!section) { return { strategy: 'semver', versioning: 'semver', configured: false } } - const versioning = (typeof section === 'object' ? section.versioning : undefined) ?? 'semver' + const versioning = section.versioning ?? 'semver' if (versioning === 'stellarwp' || versioning === 'semver') { return { strategy: versioning, versioning, configured: true } diff --git a/.github/lib/changelogger.js b/.github/lib/changelogger.js new file mode 100644 index 0000000..a564881 --- /dev/null +++ b/.github/lib/changelogger.js @@ -0,0 +1,32 @@ +'use strict' + +/** + * Reads the stellarwp/changelogger configuration, shared by the toolbox actions. Loaded by a path + * relative to each action's directory, like versions.js. + */ + +/** + * The `changelogger` section of `package.json`. + * + * A file that is missing, not JSON, or without the section gives null: the changelogger itself + * then runs on its defaults, and the actions follow it. + * + * @param {string|null} text The contents of `package.json`, or null when there is none. + * + * @returns {object|null} The section, or null when there is none to read. + */ +function readChangeloggerSection(text) { + if (text === null) { + return null + } + + try { + const section = JSON.parse(text)?.changelogger + + return section !== null && typeof section === 'object' && !Array.isArray(section) ? section : null + } catch { + return null + } +} + +module.exports = { readChangeloggerSection } diff --git a/.github/lib/phar.js b/.github/lib/phar.js new file mode 100644 index 0000000..41f7222 --- /dev/null +++ b/.github/lib/phar.js @@ -0,0 +1,74 @@ +'use strict' + +/** + * Downloads a PHP tool's phar into the runner's temp directory, shared by the toolbox actions. + * Loaded by a path relative to each action's directory, like versions.js. + * + * The phar never lands in the checkout, so it is never part of a commit, and a later step of the + * same job reuses the download instead of fetching it again. + */ + +const crypto = require('node:crypto') +const fs = require('node:fs') + +const { runOrFail } = require('./exec.js') + +/** + * Whether a phar an earlier step downloaded is there to reuse. An empty file is not: it is what a + * download that was cut off leaves. + * + * @param {string} phar The path. + * + * @returns {boolean} True for a file with content. + */ +function pharExists(phar) { + try { + return fs.statSync(phar).size > 0 + } catch { + return false + } +} + +/** + * Downloads a phar to `phar`, unless an earlier step of the job already did. With `sha512`, the + * file is checked on every call, the reused one too. A download that fails or does not match + * leaves no file behind, so the next step does not run half a phar or a different one. + * + * @param {object} options + * @param {object} options.exec The @actions/exec toolkit. + * @param {string} options.phar Where the phar goes, in the runner's temp directory. + * @param {string} options.url Where it is downloaded from. + * @param {string} options.label The tool and its release, e.g. `pup 2.0.0`, for the log and the errors. + * @param {string} options.sha512 The phar's expected sha512, hex. Empty skips the check. + * @param {string} options.what What was not done, to close the checksum error. + * @param {object} options.core The @actions/core toolkit, to say when the phar is reused. + * + * @throws {Error} When the download fails, or the phar does not match `sha512`. + * + * @returns {Promise} The path of the phar. + */ +async function downloadPhar({ exec, phar, url, label, sha512 = '', what = '', core }) { + try { + if (pharExists(phar)) { + core?.info(`Reusing ${label}, downloaded by an earlier step of this job.`) + } else { + await runOrFail(exec, 'curl', ['-fsSL', '--retry', '3', '-o', phar, url], {}, `Downloading ${label}`) + } + + if (sha512 !== '') { + const actual = crypto.createHash('sha512').update(fs.readFileSync(phar)).digest('hex') + + if (actual !== sha512) { + throw new Error(`The ${label} phar does not match its sha512: expected ${sha512}, got ${actual}. ${what}`.trim()) + } + } + } catch (error) { + fs.rmSync(phar, { force: true }) + + throw error + } + + return phar +} + +module.exports = { pharExists, downloadPhar } diff --git a/.github/lib/pup.js b/.github/lib/pup.js index d499cf8..5a71992 100644 --- a/.github/lib/pup.js +++ b/.github/lib/pup.js @@ -13,11 +13,11 @@ * means a change in the actions that call it. */ -const fs = require('node:fs') const os = require('node:os') const path = require('node:path') const { runOrFail } = require('./exec.js') +const { pharExists, downloadPhar: downloadToolPhar } = require('./phar.js') /** A pup release number, which goes into the download URL. */ const PUP_VERSION_PATTERN = /^\d+\.\d+\.\d+$/ @@ -233,22 +233,6 @@ function pharPath(env, release) { return path.join(env.RUNNER_TEMP || os.tmpdir(), `pup-${release}.phar`) } -/** - * Whether a phar an earlier step downloaded is there to reuse. An empty file is not: it is what a - * download that was cut off leaves. - * - * @param {string} phar The path. - * - * @returns {boolean} True for a file with content. - */ -function pharExists(phar) { - try { - return fs.statSync(phar).size > 0 - } catch { - return false - } -} - /** * Downloads the phar of a pup release into the runner's temp directory, unless an earlier step of * the job already did. A download that fails leaves no file behind, so the next step does not @@ -264,24 +248,8 @@ function pharExists(phar) { * * @returns {Promise} The path of the phar. */ -async function downloadPhar({ exec, env, release, core }) { - const phar = pharPath(env, release) - - if (pharExists(phar)) { - core?.info(`Reusing pup ${release}, downloaded by an earlier step of this job.`) - - return phar - } - - try { - await runOrFail(exec, 'curl', ['-fsSL', '--retry', '3', '-o', phar, pupDownloadUrl(release)], {}, `Downloading pup ${release}`) - } catch (error) { - fs.rmSync(phar, { force: true }) - - throw error - } - - return phar +function downloadPhar({ exec, env, release, core }) { + return downloadToolPhar({ exec, phar: pharPath(env, release), url: pupDownloadUrl(release), label: `pup ${release}`, core }) } /** @@ -368,7 +336,6 @@ module.exports = { resolvePupDistribution, noPharError, pharPath, - pharExists, downloadPhar, pupRunner, setupPup, diff --git a/tests/lib/changelogger.test.js b/tests/lib/changelogger.test.js new file mode 100644 index 0000000..964cf4c --- /dev/null +++ b/tests/lib/changelogger.test.js @@ -0,0 +1,18 @@ +'use strict' + +const { describe, it } = require('node:test') +const assert = require('node:assert/strict') + +const { readChangeloggerSection } = require('../../.github/lib/changelogger.js') + +describe('readChangeloggerSection', () => { + it('returns the section', () => { + assert.deepEqual(readChangeloggerSection('{"changelogger":{"changesDir":"changes"}}'), { changesDir: 'changes' }) + }) + + it('returns null without a file, valid JSON or a section object, as the changelogger falls back to its defaults', () => { + for (const text of [null, 'not json', '{"name":"x"}', '{"changelogger":"x"}', '{"changelogger":[]}', 'null']) { + assert.equal(readChangeloggerSection(text), null, String(text)) + } + }) +}) diff --git a/tests/lib/phar.test.js b/tests/lib/phar.test.js new file mode 100644 index 0000000..c67745e --- /dev/null +++ b/tests/lib/phar.test.js @@ -0,0 +1,63 @@ +'use strict' + +const { describe, it } = require('node:test') +const assert = require('node:assert/strict') +const crypto = require('node:crypto') +const fs = require('node:fs') +const os = require('node:os') +const path = require('node:path') + +const { downloadPhar } = require('../../.github/lib/phar.js') + +const CONTENT = 'phar bytes' +const SHA512 = crypto.createHash('sha512').update(CONTENT).digest('hex') + +/** An exec stand-in whose curl writes `content` to the `-o` path, or fails. */ +function fakeExec({ content = CONTENT, exitCode = 0 } = {}) { + const calls = [] + + return { + calls, + async getExecOutput(command, args) { + calls.push([command, ...args]) + if (exitCode === 0) { + fs.writeFileSync(args[args.indexOf('-o') + 1], content) + } + + return { exitCode, stdout: '', stderr: exitCode === 0 ? '' : 'curl: (22) 404' } + }, + } +} + +function tempPhar() { + return path.join(fs.mkdtempSync(path.join(os.tmpdir(), 'phar-')), 'tool.phar') +} + +describe('downloadPhar', () => { + it('downloads once and reuses the file on the next call', async () => { + const phar = tempPhar() + const exec = fakeExec() + const options = { exec, phar, url: 'https://example.com/tool.phar', label: 'tool 1.0.0', sha512: SHA512 } + + assert.equal(await downloadPhar(options), phar) + assert.equal(await downloadPhar(options), phar) + assert.equal(exec.calls.length, 1) + }) + + it('deletes a phar that does not match its sha512', async () => { + const phar = tempPhar() + + await assert.rejects( + downloadPhar({ exec: fakeExec({ content: 'other' }), phar, url: 'u', label: 'tool 1.0.0', sha512: SHA512, what: 'Nothing ran.' }), + /The tool 1\.0\.0 phar does not match its sha512: .* Nothing ran\.$/ + ) + assert.equal(fs.existsSync(phar), false) + }) + + it('leaves no file behind when the download fails', async () => { + const phar = tempPhar() + + await assert.rejects(downloadPhar({ exec: fakeExec({ exitCode: 22 }), phar, url: 'u', label: 'tool 1.0.0' }), /Downloading tool 1\.0\.0/) + assert.equal(fs.existsSync(phar), false) + }) +})