diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index f49267e1..84d5b9f0 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -42,6 +42,10 @@ jobs: pull-requests: read id-token: write attestations: write + # #1207: the fragment names the notes had missed, for the release log line + # the `stranded-log` job below posts. Empty when this tag stranded nothing. + outputs: + stranded: ${{ steps.stranded.outputs.names }} steps: # #861: this job holds `contents: write`, `packages: write`, # `id-token: write` and `attestations: write`, and the next step runs @@ -65,7 +69,7 @@ jobs: - name: Verify the changelog section env: RELEASE_TAG: ${{ inputs.tag || github.ref_name }} - run: node scripts/changelog.mjs notes "$RELEASE_TAG" > /tmp/release-notes.md + run: node scripts/changelog.mjs notes "$RELEASE_TAG" > "${RUNNER_TEMP}/release-notes.md" - name: Verify the version and tag env: @@ -187,6 +191,26 @@ jobs: ;; esac + # #1207: the notes above were assembled when the release pull request was + # built, and the fold that did it is the only reader of `changelog.d` — so + # every change merged into `main` while that pull request waited rides in + # this tag and is missing from the notes it publishes. The checkout here is + # the tag, so a fragment still in `changelog.d` is by construction one of + # those changes: it is named in the body before the body is published. + # + # This runs before the image is built on purpose. An unreadable + # `changelog.d` fails the step and the run, and no image or release is + # published from a body that would read complete without the release's own + # changes; `fragmentNames` throws rather than answering zero (#548). + - name: Name the changes that merged after the notes were assembled + id: stranded + if: ${{ steps.guard.outputs.published != 'true' }} + env: + RELEASE_TAG: ${{ inputs.tag || github.ref_name }} + run: | + set -euo pipefail + node scripts/release-stranded-notes.mjs "$RELEASE_TAG" "${RUNNER_TEMP}/release-notes.md" + # #183: stamp the image so /healthz on the published tag names the commit # it was cut from. The tag checkout is not necessarily github.sha, so the # commit comes from the checked-out tree, the same way docs/deploy.md @@ -381,7 +405,7 @@ jobs: echo "Image: ${IMAGE}:${RELEASE_TAG}@${DIGEST}" echo "SBOM: https://github.com/${GITHUB_REPOSITORY}/releases/download/${RELEASE_TAG}/sbom.spdx.json" echo "Attestation: ${DIGEST}" - } >> /tmp/release-notes.md + } >> "${RUNNER_TEMP}/release-notes.md" - name: Create the GitHub release if: ${{ steps.guard.outputs.published != 'true' }} @@ -418,7 +442,7 @@ jobs: echo "::error::${RELEASE_TAG} gained a GitHub Release while this run was publishing (${STATE}); refusing to rewrite it" exit 1 fi - gh release create "$RELEASE_TAG" --title "$RELEASE_TAG" --notes-file /tmp/release-notes.md + gh release create "$RELEASE_TAG" --title "$RELEASE_TAG" --notes-file "${RUNNER_TEMP}/release-notes.md" # #883 item 4: the SBOM is a Release asset, so it outlives the run that # made it. The body wrote its download URL before the release existed; this @@ -447,3 +471,52 @@ jobs: set -euo pipefail # `verify-release` takes the bare X.Y.Z; the tag carries the leading v. node scripts/verify-release.mjs --version "${RELEASE_TAG#v}" + + # #1207 step 3: the published notes and the release log on #80 have to agree + # about what a tag carried. The publish job reports the fragment names it + # appended; this job says them on #80. It is a separate job with `issues: + # write` alone because the publish job's token holds `contents: write`, + # `packages: write`, `id-token: write` and `attestations: write` and must not + # gain a scope it never uses. It runs only after a publish that named + # something: an ordinary night leaves it skipped, and a tag that already has a + # release never reaches the step that fills the output. + stranded-log: + name: name the stranded changes on #80 + needs: release + if: ${{ needs.release.outputs.stranded != '' }} + runs-on: ubuntu-latest + timeout-minutes: 5 + permissions: + issues: write + env: + RELEASE_TAG: ${{ inputs.tag || github.ref_name }} + STRANDED: ${{ needs.release.outputs.stranded }} + # gh resolves the repository for `gh issue comment` from GH_REPO or a git + # remote; `GITHUB_REPOSITORY` is an expression this file builds the API path + # from, not a variable gh reads, and this job has no checkout. Without this + # the comment fails after the release is already published: notes right, + # #80 line missing, run red (#1207 review). + GH_REPO: ${{ github.repository }} + steps: + - name: Comment on #80 + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + set -euo pipefail + # The marker carries the tag and the fragment set, so a re-run of one + # tag repeats nothing, and a later release with the same fragments is + # still its own line. + MARKER="" + LAST_LINE="$(gh api --paginate "repos/${GITHUB_REPOSITORY}/issues/80/comments?per_page=100" \ + | jq -s -r 'flatten | map(.body | split("\n") | first) | map(select(startswith("/, 'a re-run of one tag repeats nothing') +}) + +// The #80 job has no checkout, and `gh` resolves the repository for `gh issue +// comment` from `GH_REPO` or a git remote — `GITHUB_REPOSITORY` is not something +// gh reads; the body only uses it to build the API path. The review at +// `7557fb61` found the job failing on the one night it exists for: notes right, +// no line on #80, red run. These tests drive the step body with a stub `gh` that +// refuses an `issue comment` it cannot place, the way the client does, so the +// job's own env decides whether the line can post — a job that names no +// repository fails here instead of in production. +const COMMENT_STEP = 'Comment on #80' + +const ENV_FIXTURES: Record = { + '${{ inputs.tag || github.ref_name }}': 'v0.3.2', + '${{ needs.release.outputs.stranded }}': '999.fixed.md', + '${{ github.repository }}': 'HugoXK/fledge', + '${{ secrets.GITHUB_TOKEN }}': 'stub-token', +} + +// Env is the thing under test, so it is read out of the workflow and handed to +// the body as a runner would: job keys sit at six spaces, step keys at ten, and a +// value the test cannot stand in for is a failure, not a literal passed through. +function workflowEnv(text: string): Record { + const env: Record = {} + for (const line of text.split('\n')) { + const entry = /^ {6}(?: {4})?([A-Z][A-Z0-9_]*): (.+)$/.exec(line) + if (entry === null) continue + const value = (entry[2] ?? '').trim() + const fixture = ENV_FIXTURES[value] + assert.ok(fixture !== undefined, `the test knows what "${value}" stands for`) + env[entry[1] ?? ''] = fixture + } + return env +} + +function commentBlock(job: string): string { + const at = job.indexOf(` - name: ${COMMENT_STEP}`) + assert.notEqual(at, -1, `the stranded-log job declares the step "${COMMENT_STEP}"`) + return job.slice(at) +} + +interface CommentRun { + status: number | null + commands: string + repoSeen: string + summary: string + stderr: string +} + +function runCommentStep(apiJson: string): CommentRun { + const dir = mkdtempSync(join(tmpdir(), 'stranded-log-')) + try { + const bin = join(dir, 'bin') + mkdirSync(bin) + const commands = join(dir, 'commands') + writeFileSync(commands, '') + const repoSeen = join(dir, 'repo') + writeFileSync(repoSeen, '') + const api = join(dir, 'comments.json') + writeFileSync(api, apiJson) + // The stub answers as `gh` does: an API path needs no repository, and an + // `issue comment` without `GH_REPO`, `--repo` or a token is the failure this + // job hit in production. + const stub = [ + '#!/usr/bin/env bash', + 'printf "%s\\n" "$*" >> "$STUB_LOG"', + 'printf "%s\\n" "${GH_REPO:-}" > "$STUB_REPO"', + 'case "$1 $2" in', + ' "api --paginate") cat "$STUB_API"; exit 0 ;;', + ' "issue comment")', + ' case " $* " in *" --repo "*|*" -R "*) exit 0 ;; esac', + ' if [ -z "${GH_REPO:-}" ]; then', + ' echo "gh: could not determine base repo; set GH_REPO or --repo" >&2', + ' exit 1', + ' fi', + ' if [ -z "${GH_TOKEN:-}" ]; then', + ' echo "gh: no authentication token; set GH_TOKEN" >&2', + ' exit 4', + ' fi', + ' exit 0 ;;', + 'esac', + 'exit 0', + '', + ].join('\n') + const ghPath = join(bin, 'gh') + writeFileSync(ghPath, stub) + chmodSync(ghPath, 0o755) + const job = jobBlocks(release).get('stranded-log') + assert.ok(job !== undefined, 'release.yml carries the stranded-log job') + const block = commentBlock(job) + const summary = join(dir, 'summary.md') + writeFileSync(summary, '') + const result = spawnSync('bash', ['-euo', 'pipefail', '-c', runBody(block)], { + encoding: 'utf8', + env: { + ...process.env, + ...workflowEnv(job), + PATH: `${bin}:${process.env.PATH ?? ''}`, + GITHUB_STEP_SUMMARY: summary, + GITHUB_REPOSITORY: 'HugoXK/fledge', + STUB_LOG: commands, + STUB_REPO: repoSeen, + STUB_API: api, + }, + }) + return { + status: result.status, + commands: readFileSync(commands, 'utf8'), + repoSeen: readFileSync(repoSeen, 'utf8'), + summary: readFileSync(summary, 'utf8'), + stderr: result.stderr, + } + } finally { + rmSync(dir, { recursive: true, force: true }) + } +} + +test('the #80 job hands gh a repository, so its line can post after the release', () => { + // The behaviour first: the body is driven with the job's own env, so a job + // that names no repository fails here — on `gh: could not determine base repo` + // — instead of on the text assertion below. + const run = runCommentStep('[]') + assert.equal(run.status, 0, `the step body could not post with the job's own env: ${run.stderr}`) + assert.match(run.commands, /^issue comment 80 --body /m, 'the line is posted') + assert.equal( + run.repoSeen.trim(), + 'HugoXK/fledge', + 'gh received GH_REPO — the API path carries the repository in its URL, the comment call does not', + ) + assert.match(run.summary, /^commented /m, 'the summary names what landed') + const job = jobBlocks(release).get('stranded-log') + assert.ok(job !== undefined, 'release.yml carries the stranded-log job') + assert.match(job, /^ GH_REPO: \$\{\{ github\.repository \}\}$/m, 'the job names the repository gh must use') +}) + +test('a re-run of one tag posts no second line on #80', () => { + const run = runCommentStep('[{"body":"\\n\\nearlier"}]') + assert.equal(run.status, 0, `the step body failed on an existing marker: ${run.stderr}`) + assert.doesNotMatch(run.commands, /^issue comment /m, 'no comment when the marker is the last line on the issue') + assert.match(run.summary, /already commented/, 'the summary says why nothing was posted') +})