diff --git a/.github/workflows/release-please.yml b/.github/workflows/release-please.yml index c0682198a..f4b700c74 100644 --- a/.github/workflows/release-please.yml +++ b/.github/workflows/release-please.yml @@ -6,9 +6,10 @@ name: release-please # else a patch bump (release-please-config.json). docs/proposals/303-release-automation.md # has the whole flow and which step is whose. # -# Run by hand, mode open-pr opens or updates the release pull requests, and makes -# their changelog entries pass `node sdk/docs/changelog.mjs` -# so nobody edits a changelog. +# Run by hand, mode open-pr opens or updates the release pull requests of every package +# whose current version is tagged, and makes their changelog +# entries pass `node sdk/docs/changelog.mjs` so nobody edits +# a changelog. # Run by hand, mode dry-run prints each pull request, tag and release body it would # produce and writes nothing. It works in a fork. # Push to main that changes a release pull request merged, because only they change @@ -88,33 +89,25 @@ jobs: - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 with: persist-credentials: false - # A release on top of a version that was never tagged would skip that version - # and link its changelog entry to a compare page that does not exist. - - name: Refuse until the manifest's versions are tagged - shell: bash - env: - GH_TOKEN: ${{ github.token }} - REPOSITORY: ${{ github.repository }} - run: | - set -euo pipefail - for tag in "v$(jq -r '."."' .release-please-manifest.json)" \ - "prism-code-v$(jq -r '."sdk/npm/prism-code"' .release-please-manifest.json)"; do - if ! gh api "repos/$REPOSITORY/git/ref/tags/$tag" --silent; then - echo "::error::tag $tag does not exist: cut it first, or use mode dry-run" - exit 1 - fi - done - # GITHUB_TOKEN can open these only with "Allow GitHub Actions to create and approve - # pull requests" on, and what it opens starts no CI. RELEASE_PLEASE_TOKEN, scoped to - # this repository with contents and pull requests write, needs neither workaround. - - id: release - uses: googleapis/release-please-action@16a9c90856f42705d54a6fda1823352bdc62cf38 # v4.4.0 + - uses: actions/setup-node@249970729cb0ef3589644e2896645e5dc5ba9c38 with: - token: ${{ secrets.RELEASE_PLEASE_TOKEN || github.token }} - config-file: release-please-config.json - manifest-file: .release-please-manifest.json - target-branch: main - skip-github-release: true + node-version: '22' + - name: Install release-please at the version the action bundles + working-directory: scripts/release-please-dry-run + run: npm ci --ignore-scripts --no-audit --no-fund + # A package whose current version has no tag waits, alone: the root tag is the + # factory's cut and can lag, and prism-code versions on its own tags. The pinned + # action cannot leave one package out, so this runs its library with the config + # narrowed in memory. GITHUB_TOKEN can open these only with "Allow GitHub Actions + # to create and approve pull requests" on, and what it opens starts no CI. + # RELEASE_PLEASE_TOKEN, scoped to this repository with contents and pull requests + # write, needs neither workaround. + - id: release + name: Open release pull requests for every package whose version is tagged + env: + GITHUB_TOKEN: ${{ secrets.RELEASE_PLEASE_TOKEN || github.token }} + BRANCH: ${{ github.ref_name }} + run: node scripts/release-please-dry-run/open-pr.mjs --branch "$BRANCH" - if: steps.release.outputs.prs_created == 'true' uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 with: diff --git a/docs/proposals/303-release-automation.md b/docs/proposals/303-release-automation.md index b274102e1..267ec9c44 100644 --- a/docs/proposals/303-release-automation.md +++ b/docs/proposals/303-release-automation.md @@ -67,7 +67,7 @@ The other three npm packages get tags from this flow but have no publish workflo This merges before `v0.4.0` and `prism-code-v0.3.0` exist. Four things make that safe. - **The merge creates no release.** Adding the manifest starts the `tag` job. release-please only tags a merged pull request labelled `autorelease: pending`, and none exists. Its own release step, run against the real `main`, returned zero releases. -- **`open-pr` refuses until those two tags exist.** Otherwise a release would land on top of a version that never shipped, and its changelog entry would link to a compare page that does not exist. Mode `dry-run` still works. +- **A package waits until its own version is tagged.** `open-pr` leaves out the root package while `v` is missing, and prism-code while `prism-code-v` is missing, so no release lands on top of a version that never shipped or links to a compare page that does not exist. The others still get their pull requests: the root tag is the factory's cut and can lag for days, and it must not hold prism-code back. The pinned action cannot leave one package out, so `scripts/release-please-dry-run/open-pr.mjs` runs its library with the config narrowed in memory. Mode `dry-run` still works. - **The manifest holds what the packages declare:** `0.4.0` and `0.3.0`. A test holds it to `Cargo.toml` and each `package.json`. - **`bootstrap-sha` is the 0.4.0 preparation commit `2e44eed0`.** A package with no tag reads history only back to there, instead of up to 500 commits. Once a tag exists, the tag is used instead. diff --git a/scripts/release-please-dry-run/open-pr.mjs b/scripts/release-please-dry-run/open-pr.mjs new file mode 100644 index 000000000..a8364a2d0 --- /dev/null +++ b/scripts/release-please-dry-run/open-pr.mjs @@ -0,0 +1,90 @@ +#!/usr/bin/env node +/** + * Opens or updates the release pull requests, for every package whose current + * version is already tagged. + * + * A release on top of a version that was never tagged would skip that version and + * link its changelog entry to a compare page that does not exist, so such a package + * waits. It waits alone: the root tag vVERSION is the factory's cut (release-cut.yml) + * and can lag by days, and prism-code versions on its own tags. release-please-action + * reads its config from the branch and cannot leave one package out, so this runs the + * same release-please library (the version the pinned action bundles) with the config + * narrowed in memory. The config file itself is never changed. + * + * GITHUB_TOKEN=... node scripts/release-please-dry-run/open-pr.mjs [--repo owner/name] [--branch main] + * + * Writes `prs` and `prs_created` to $GITHUB_OUTPUT in the shape release-please-action + * gives them. Exits 1 when no package can be released. + */ +import { appendFileSync, readFileSync } from 'node:fs'; +import { dirname, join, resolve } from 'node:path'; +import { fileURLToPath } from 'node:url'; +import { parseArgs } from 'node:util'; + +const ROOT = resolve(dirname(fileURLToPath(import.meta.url)), '..', '..'); +const CONFIG = 'release-please-config.json'; +const MANIFEST = '.release-please-manifest.json'; + +/** The packages whose last version must be tagged before a release builds on it. */ +export const GUARDED = ['.', 'sdk/npm/prism-code']; + +/** The tag release-please gives a package's version under this config. */ +export function tagOf(config, path, version) { + const settings = config.packages[path] ?? {}; + const withComponent = settings['include-component-in-tag'] ?? config['include-component-in-tag'] ?? true; + const v = (settings['include-v-in-tag'] ?? config['include-v-in-tag'] ?? true) ? 'v' : ''; + return `${withComponent && settings.component ? `${settings.component}-` : ''}${v}${version}`; +} + +/** The config with each guarded package whose tag is missing left out, and what was left out. */ +export function narrow(config, manifest, tagExists) { + const waiting = GUARDED + .map((path) => ({ path, tag: tagOf(config, path, manifest[path]) })) + .filter(({ tag }) => !tagExists(tag)); + const packages = Object.fromEntries(Object.entries(config.packages).filter(([path]) => !waiting.some((w) => w.path === path))); + if (!Object.keys(packages).length) throw new Error('no package can be released: every one waits for its tag'); + return { config: { ...config, packages }, waiting }; +} + +async function main() { + const { values } = parseArgs({ options: { + repo: { type: 'string', default: process.env.GITHUB_REPOSITORY || 'UOR-Foundation/prismpm' }, + branch: { type: 'string', default: 'main' }, + } }); + const token = process.env.GITHUB_TOKEN || process.env.GH_TOKEN; + if (!token) throw new Error('set GITHUB_TOKEN or GH_TOKEN'); + const read = (path) => JSON.parse(readFileSync(join(ROOT, path), 'utf8')); + const [owner, repo] = values.repo.split('/'); + + const { GitHub, Manifest } = await import('release-please'); + const github = await GitHub.create({ owner, repo, token, defaultBranch: values.branch }); + const tags = new Set(); + for (const { tag } of GUARDED.map((path) => ({ tag: tagOf(read(CONFIG), path, read(MANIFEST)[path]) }))) { + try { + await github.octokit.git.getRef({ owner, repo, ref: `tags/${tag}` }); + tags.add(tag); + } catch (error) { + if (error.status !== 404) throw error; + } + } + const { config, waiting } = narrow(read(CONFIG), read(MANIFEST), (tag) => tags.has(tag)); + for (const { path, tag } of waiting) console.log(`::warning::${path} waits: tag ${tag} does not exist, so no release pull request builds on it`); + + // Only the config is replaced, and only in memory; every other file is read from the branch. + const real = github.getFileContentsOnBranch.bind(github); + github.getFileContentsOnBranch = async (path, branch) => { + if (path !== CONFIG) return real(path, branch); + const parsedContent = JSON.stringify(config, null, 2); + return { sha: '', mode: '100644', branch, parsedContent, content: Buffer.from(parsedContent).toString('base64') }; + }; + const manifest = await Manifest.fromManifest(github, values.branch, CONFIG, MANIFEST); + const prs = (await manifest.createPullRequests()).filter(Boolean); + for (const pr of prs) console.log(`release pull request #${pr.number} from ${pr.headBranchName}`); + if (process.env.GITHUB_OUTPUT) { + appendFileSync(process.env.GITHUB_OUTPUT, `prs_created=${prs.length > 0}\nprs=${JSON.stringify(prs)}\n`); + } +} + +if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) { + main().catch((error) => { console.error(`::error::${error.message}`); process.exitCode = 1; }); +} diff --git a/sdk/docs/test/release-config.test.mjs b/sdk/docs/test/release-config.test.mjs index ffcb71ac5..d6ea08bdb 100644 --- a/sdk/docs/test/release-config.test.mjs +++ b/sdk/docs/test/release-config.test.mjs @@ -5,6 +5,7 @@ import { join } from 'node:path'; import { HEADERS } from '../lib/changelog.mjs'; import { ROOT } from '../lib/spec.mjs'; import { releasedPackages } from '../../../scripts/release-please-notes.mjs'; +import { GUARDED, narrow, tagOf } from '../../../scripts/release-please-dry-run/open-pr.mjs'; const read = (path) => readFileSync(join(ROOT, path), 'utf8').split('\r\n').join('\n'); const config = JSON.parse(read('release-please-config.json')); @@ -86,16 +87,36 @@ test('the tag job uses GITHUB_TOKEN alone, so its tag cannot start the npm publi test('the open-pr job never tags, and leaves each pull request passing the changelog check', () => { const openPr = uncommented(job('open-pr')); - assert.match(openPr, /skip-github-release: true/); + assert.match(openPr, /npm ci --ignore-scripts/); + assert.match(openPr, /node scripts\/release-please-dry-run\/open-pr\.mjs/); + assert.doesNotMatch(read('scripts/release-please-dry-run/open-pr.mjs'), /createReleases|\.createRelease\(|\.tag\(/); assert.match(openPr, /changelog\.mjs --normalize/); assert.match(openPr, /--depth=2/); - assert.ok(openPr.indexOf('git/ref/tags/') < openPr.indexOf('release-please-action'), 'the tag check runs before release-please'); - assert.match(openPr, /"v\$\(jq -r '\."\."' \.release-please-manifest\.json\)"/); - assert.match(openPr, /"prism-code-v\$\(jq -r/); assert.match(openPr, /cargo update --workspace/); assert.match(openPr, /node sdk\/docs\/changelog\.mjs\n/); }); +test('a package whose version is untagged waits alone, and the others still get release pull requests', () => { + assert.deepEqual(GUARDED, ['.', 'sdk/npm/prism-code']); + assert.equal(tagOf(config, '.', '0.5.0'), 'v0.5.0'); + assert.equal(tagOf(config, 'sdk/npm/prism-code', '0.9.4'), 'prism-code-v0.9.4'); + const all = Object.keys(config.packages); + + const rootWaits = narrow(config, manifest, (tag) => tag !== `v${manifest['.']}`); + assert.deepEqual(rootWaits.waiting, [{ path: '.', tag: `v${manifest['.']}` }]); + assert.deepEqual(Object.keys(rootWaits.config.packages), all.filter((p) => p !== '.')); + assert.ok(rootWaits.config.packages['sdk/npm/prism-code']); + assert.equal(config.packages['.'] !== undefined, true, 'the config read from disk is never changed'); + + const bothTagged = narrow(config, manifest, () => true); + assert.deepEqual(bothTagged.waiting, []); + assert.deepEqual(Object.keys(bothTagged.config.packages), all); + + const noneTagged = narrow(config, manifest, () => false); + assert.deepEqual(noneTagged.waiting.map((w) => w.path), GUARDED); + assert.throws(() => narrow({ ...config, packages: { '.': config.packages['.'] } }, manifest, () => false), /every one waits/); +}); + test('the dry run can only read', () => { const dryRun = uncommented(job('dry-run')); assert.match(dryRun, /contents: read/);