Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 3 additions & 10 deletions .github/actions/release-prep/release-prep.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const {
pupRunner,
lastLine,
} = require('../../lib/pup.js')
const { readChangeloggerSection } = require('../../lib/changelogger.js')
const { readCheckoutFile } = require('../../lib/workspace.js')

/**
Expand Down Expand Up @@ -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 }
Expand Down
32 changes: 32 additions & 0 deletions .github/lib/changelogger.js
Original file line number Diff line number Diff line change
@@ -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 }
74 changes: 74 additions & 0 deletions .github/lib/phar.js
Original file line number Diff line number Diff line change
@@ -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<string>} 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 }
39 changes: 3 additions & 36 deletions .github/lib/pup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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+$/
Expand Down Expand Up @@ -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
Expand All @@ -264,24 +248,8 @@ function pharExists(phar) {
*
* @returns {Promise<string>} 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 })
}

/**
Expand Down Expand Up @@ -368,7 +336,6 @@ module.exports = {
resolvePupDistribution,
noPharError,
pharPath,
pharExists,
downloadPhar,
pupRunner,
setupPup,
Expand Down
18 changes: 18 additions & 0 deletions tests/lib/changelogger.test.js
Original file line number Diff line number Diff line change
@@ -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))
}
})
})
63 changes: 63 additions & 0 deletions tests/lib/phar.test.js
Original file line number Diff line number Diff line change
@@ -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)
})
})
Loading