diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 877f1eb3..2d108f14 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -4,6 +4,8 @@ on: pull_request: types: [closed] branches: [main] + # Temporary first-release retry: PR #345 is already merged and verified. + workflow_dispatch: permissions: contents: read @@ -21,6 +23,10 @@ jobs: release: needs: checks + if: >- + ${{ !cancelled() && github.repository == 'TanStack/oracles' && + (needs.checks.result == 'success' || + (github.event_name == 'workflow_dispatch' && github.ref == 'refs/heads/main')) }} # Skipped PR closures never enter this queue. Retain eligible pending jobs. concurrency: group: release-main @@ -31,11 +37,27 @@ jobs: contents: write id-token: write steps: + - name: Verify merged version PR + id: release-pr + run: | + set -euo pipefail + merge_sha=$(gh api "repos/$GITHUB_REPOSITORY/pulls/$RELEASE_PR" | jq -er \ + --arg repo "$GITHUB_REPOSITORY" --arg merge "$EXPECTED_MERGE_SHA" ' + select(.merged == true and .base.ref == "main" and + .head.ref == "changeset-release/main" and + .head.repo.full_name == $repo and .merge_commit_sha == $merge) | + .merge_commit_sha + ') + printf 'merge_sha=%s\n' "$merge_sha" >> "$GITHUB_OUTPUT" + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + RELEASE_PR: ${{ github.event.pull_request.number || '345' }} + EXPECTED_MERGE_SHA: ${{ github.event.pull_request.merge_commit_sha || 'a1323afacd8c663ff10b624ac424b3d9443d654b' }} - name: Checkout uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 with: fetch-depth: 0 - ref: ${{ github.event.pull_request.merge_commit_sha }} + ref: ${{ steps.release-pr.outputs.merge_sha }} persist-credentials: true # Changesets pushes release tags. - name: Setup Tools uses: tanstack/config/.github/setup@e4b48f16568324f76f467aa4c2aac2f05db632c3 diff --git a/scripts/release-workflows.test.mjs b/scripts/release-workflows.test.mjs index 924c5f00..aad254c6 100644 --- a/scripts/release-workflows.test.mjs +++ b/scripts/release-workflows.test.mjs @@ -32,9 +32,38 @@ test('only guarded publication jobs enter the release queue', () => { queue: 'max', }) assert.equal(release.jobs.release.needs, 'checks') - assert.equal(release.jobs.release.if, undefined) + const eligible = new Function( + 'github', + 'needs', + 'cancelled', + `return (${release.jobs.release.if.replace(/^\$\{\{\s*|\s*\}\}$/g, '')})`, + ) + const context = { + repository: 'TanStack/oracles', + event_name: 'pull_request', + ref: 'refs/heads/main', + } + assert.equal( + eligible(context, { checks: { result: 'success' } }, () => false), + true, + ) + assert.equal( + eligible(context, { checks: { result: 'failure' } }, () => false), + false, + ) + context.event_name = 'workflow_dispatch' + assert.equal( + eligible(context, { checks: { result: 'skipped' } }, () => false), + true, + ) + context.ref = 'refs/heads/feature' + assert.equal( + eligible(context, { checks: { result: 'skipped' } }, () => false), + false, + ) assert.deepEqual(release.on, { pull_request: { types: ['closed'], branches: ['main'] }, + workflow_dispatch: null, }) const allowed = new Function('github', `return (${release.jobs.checks.if})`) const event = { @@ -89,10 +118,73 @@ test('main pushes only maintain the version PR and releases use the merge commit const checkout = release.jobs.release.steps.find((step) => step.uses?.startsWith('actions/checkout@'), ) - assert.equal( - checkout.with.ref, - '${{ github.event.pull_request.merge_commit_sha }}', + assert.equal(checkout.with.ref, '${{ steps.release-pr.outputs.merge_sha }}') +}) + +test('manual release resolves only the verified merged version PR', (t) => { + const resolver = release.jobs.release.steps.find( + (step) => step.id === 'release-pr', ) + assert.ok(resolver, 'release metadata must be verified before checkout') + const temporary = mkdtempSync(join(tmpdir(), 'release-pr-metadata-')) + t.after(() => rmSync(temporary, { recursive: true, force: true })) + const bin = join(temporary, 'bin') + mkdirSync(bin) + const gh = join(bin, 'gh') + writeFileSync(gh, '#!/bin/sh\ncat "$RELEASE_METADATA_FIXTURE"\n') + chmodSync(gh, 0o755) + const merge = 'a1323afacd8c663ff10b624ac424b3d9443d654b' + const valid = { + merged: true, + merge_commit_sha: merge, + base: { ref: 'main' }, + head: { + ref: 'changeset-release/main', + repo: { full_name: 'TanStack/oracles' }, + }, + } + const fixture = join(temporary, 'pr.json') + const output = join(temporary, 'output') + const execute = () => + execFileSync('bash', ['-euo', 'pipefail', '-c', resolver.run], { + env: { + ...process.env, + PATH: `${bin}:${process.env.PATH}`, + RELEASE_METADATA_FIXTURE: fixture, + RELEASE_PR: '345', + GITHUB_REPOSITORY: 'TanStack/oracles', + EXPECTED_MERGE_SHA: merge, + GITHUB_OUTPUT: output, + }, + stdio: 'pipe', + }) + writeFileSync(fixture, JSON.stringify(valid)) + execute() + assert.equal(readFileSync(output, 'utf8'), `merge_sha=${merge}\n`) + for (const change of [ + (pr) => { + pr.merged = false + }, + (pr) => { + pr.base.ref = 'feature' + }, + (pr) => { + pr.head.ref = 'feature' + }, + (pr) => { + pr.head.repo.full_name = 'fork/oracles' + }, + (pr) => { + pr.merge_commit_sha = 'b'.repeat(40) + }, + ]) { + const invalid = structuredClone(valid) + change(invalid) + writeFileSync(fixture, JSON.stringify(invalid)) + writeFileSync(output, '') + assert.throws(execute) + assert.equal(readFileSync(output, 'utf8'), '') + } }) const fakeCommands = `#!/usr/bin/env node