From 93e7a7cdbe88ad0b8da335ed3800d2f3d0a0c08e Mon Sep 17 00:00:00 2001 From: Emilien Escalle Date: Sat, 7 Nov 2026 16:56:48 +0100 Subject: [PATCH] feat(test): support consolidated CI reporting --- .github/workflows/__shared-ci.yml | 6 ++ .github/workflows/__test-action-test.yml | 75 ++++++++++++++++++++++++ actions/test/README.md | 23 ++++++-- actions/test/action.yml | 38 +++++++++++- 4 files changed, 136 insertions(+), 6 deletions(-) create mode 100644 .github/workflows/__test-action-test.yml diff --git a/.github/workflows/__shared-ci.yml b/.github/workflows/__shared-ci.yml index 087215b6..93410f46 100644 --- a/.github/workflows/__shared-ci.yml +++ b/.github/workflows/__shared-ci.yml @@ -98,3 +98,9 @@ jobs: id-token: write issues: read security-events: write + + test-action-test: + needs: linter + uses: $/.github/workflows/__test-action-test.yml + permissions: + contents: read diff --git a/.github/workflows/__test-action-test.yml b/.github/workflows/__test-action-test.yml new file mode 100644 index 00000000..b7ea4462 --- /dev/null +++ b/.github/workflows/__test-action-test.yml @@ -0,0 +1,75 @@ +name: Internal - Tests for test action + +on: + workflow_call: + +permissions: {} + +jobs: + test-report: + runs-on: ubuntu-latest + permissions: + contents: read + strategy: + fail-fast: false + matrix: + failure: [false, true] + steps: + - name: Arrange - Checkout + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - name: Arrange - Create a dependency-free native test project + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 + with: + script: | + const fs = require('node:fs'); + await io.mkdirP('report-fixture'); + await io.cp('.node-version', 'report-fixture/.node-version'); + fs.writeFileSync('report-fixture/package.json', JSON.stringify({ + name: 'report-fixture', version: '1.0.0', private: true, + scripts: { + 'pretest:ci': 'node -e "require(\'node:fs\').mkdirSync(\'.reports\', {recursive:true})"', + 'test:ci': 'node --experimental-test-coverage --test --test-reporter=spec --test-reporter-destination=stdout --test-reporter=junit --test-reporter-destination=.reports/junit.xml --test-reporter=lcov --test-reporter-destination=.reports/lcov.info test.cjs', + }, + })); + fs.writeFileSync('report-fixture/source.cjs', 'module.exports = value => value;\n'); + fs.writeFileSync('report-fixture/test.cjs', "require('node:test').test('report fixture', () => require('node:assert/strict').equal(require('./source.cjs')(process.env.EXPECT_FAILURE), 'false'));\n"); + await exec.exec('npm', ['install', '--package-lock-only', '--ignore-scripts', '--no-audit', '--no-fund'], { cwd: 'report-fixture' }); + - name: Act - Run tests with comments disabled and no write token + id: test + continue-on-error: true + uses: $/actions/test + env: + EXPECT_FAILURE: ${{ matrix.failure }} + with: + working-directory: report-fixture + comment: "false" + report-file: | + .reports/junit.xml + .reports/lcov.info + - name: Assert - Retrieve reports even after a failed test + if: steps.test.outputs.report-artifact-id != '' + uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 + with: + artifact-ids: ${{ steps.test.outputs.report-artifact-id }} + path: downloaded-reports + merge-multiple: true + - name: Assert - Verify outcome and native report content + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 + env: + ACTUAL: ${{ steps.test.outcome }} + EXPECTED: ${{ matrix.failure && 'failure' || 'success' }} + ARTIFACT_ID: ${{ steps.test.outputs.report-artifact-id }} + with: + script: | + const assert = require('node:assert/strict'); + const fs = require('node:fs'); + assert.equal(process.env.ACTUAL, process.env.EXPECTED); + assert.match(process.env.ARTIFACT_ID, /^\d+$/); + const globber = await glob.create('downloaded-reports/**/*', { matchDirectories: false }); + const files = await globber.glob(); + assert.equal(files.length, 2); + const junit = fs.readFileSync(files.find(file => file.endsWith('junit.xml')), 'utf8'); + assert.equal(junit.includes(' file.endsWith('lcov.info')), 'utf8'), /SF:/); diff --git a/actions/test/README.md b/actions/test/README.md index 8e0d68c2..43326f17 100644 --- a/actions/test/README.md +++ b/actions/test/README.md @@ -3,7 +3,7 @@ # ![Icon](data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIyNCIgaGVpZ2h0PSIyNCIgdmlld0JveD0iMCAwIDI0IDI0IiBmaWxsPSJub25lIiBzdHJva2U9ImN1cnJlbnRDb2xvciIgc3Ryb2tlLXdpZHRoPSIyIiBzdHJva2UtbGluZWNhcD0icm91bmQiIHN0cm9rZS1saW5lam9pbj0icm91bmQiIGNsYXNzPSJmZWF0aGVyIGZlYXRoZXItY2hlY2stc3F1YXJlIiBjb2xvcj0iYmx1ZSI+PHBvbHlsaW5lIHBvaW50cz0iOSAxMSAxMiAxNCAyMiA0Ij48L3BvbHlsaW5lPjxwYXRoIGQ9Ik0yMSAxMnY3YTIgMiAwIDAgMS0yIDJINWEyIDIgMCAwIDEtMi0yVjVhMiAyIDAgMCAxIDItMmgxMSI+PC9wYXRoPjwvc3ZnPg==) GitHub Action: Test
- Test + Test
--- @@ -16,7 +16,6 @@ [![License](https://img.shields.io/github/license/hoverkraft-tech/ci-github-nodejs)](http://choosealicense.com/licenses/mit/) [![Stars](https://img.shields.io/github/stars/hoverkraft-tech/ci-github-nodejs?style=social)](https://img.shields.io/github/stars/hoverkraft-tech/ci-github-nodejs?style=social) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/hoverkraft-tech/ci-github-nodejs/blob/main/CONTRIBUTING.md) -![GitHub Verified Creator](https://img.shields.io/badge/GitHub-Verified%20Creator-4493F8?logo=data:image/svg%2bxml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAxNiAxNiIgd2lkdGg9IjE2IiBoZWlnaHQ9IjE2IiBmaWxsPSJyZ2IoNjgsIDE0NywgMjQ4KSI+CiAgPHBhdGggZD0ibTkuNTg1LjUyLjkyOS42OGMuMTUzLjExMi4zMzEuMTg2LjUxOC4yMTVsMS4xMzguMTc1YTIuNjc4IDIuNjc4IDAgMCAxIDIuMjQgMi4yNGwuMTc0IDEuMTM5Yy4wMjku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A1IDEuMzk0IDBsLjkyNy0uNjhhMi42NyAyLjY3IDAgMCAxIDEuMTgtLjQ4OWwxLjEzNy0uMTc0YTEuMTggMS4xOCAwIDAgMCAuOTg3LS45ODdsLjE3NC0xLjEzNmEyLjY3IDIuNjcgMCAwIDEgLjQ4OS0xLjE4bC42OC0uOTI4YTEuMTc2IDEuMTc2IDAgMCAwIDAtMS4zOTRsLS42OC0uOTI3YTIuNjg2IDIuNjg2IDAgMCAxLS40ODktMS4xOGwtLjE3NC0xLjEzN2ExLjE3OSAxLjE3OSAwIDAgMC0uOTg3LS45ODdsLTEuMTM2LS4xNzRhMi42NzcgMi42NzcgMCAwIDEtMS4xOC0uNDg5bC0uOTI4LS42OGExLjE3NiAxLjE3NiAwIDAgMC0xLjM5NCAwWk0xMS4yOCA2Ljc4bC0zLjc1IDMuNzVhLjc1Ljc1IDAgMCAxLTEuMDYgMEw0LjcyIDguNzhhLjc1MS43NTEgMCAwIDEgLjAxOC0xLjA0Mi43NTEuNzUxIDAgMCAxIDEuMDQyLS4wMThMNyA4Ljk0bDMuMjItMy4yMmEuNzUxLjc1MSAwIDAgMSAxLjA0Mi4wMTguNzUxLjc1MSAwIDAgMSAuMDE4IDEuMDQyWiI+PC9wYXRoPgo8L3N2Zz4K) @@ -29,6 +28,10 @@ Action to test Node.js projects with support for coverage reporting and pull request annotations +GitHub reporting retains native report artifacts for seven days, including after +test failures. Set `comment: false` and call the common `ci-reports` workflow +after test jobs to publish one test comment and one coverage comment. + Note that this action must have write permission for id-token for this to work when using Codecov coverage reporting: ```yml @@ -90,8 +93,12 @@ permissions: # See the [parse-ci-reports documentation](https://github.com/hoverkraft-tech/ci-github-common/tree/main/actions/quality/parse-ci-reports) for details. path-mapping: "" + # Publish an individual coverage PR comment. Set false when using the common ci-reports workflow; GitHub summaries and report artifacts remain enabled. + # Default: `true` + comment: "true" + # GitHub token for coverage PR comments. - # Required when coverage is set to `github`. + # Required when coverage is `github` and individual comments are enabled. # Requires permissions to create and update PR comments: # # - `issues: write` @@ -129,8 +136,9 @@ permissions: | | When omitted, the action falls back to `auto:test,auto:coverage` detection. | | | | **`path-mapping`** | Optional path mapping to adjust file paths in test and coverage reports. | **false** | - | | | See the [parse-ci-reports documentation](https://github.com/hoverkraft-tech/ci-github-common/tree/main/actions/quality/parse-ci-reports) for details. | | | +| **`comment`** | Publish an individual coverage PR comment. Set false when using the common ci-reports workflow; GitHub summaries and report artifacts remain enabled. | **false** | `true` | | **`github-token`** | GitHub token for coverage PR comments. | **false** | - | -| | Required when coverage is set to `github`. | | | +| | Required when coverage is `github` and individual comments are enabled. | | | | | Requires permissions to create and update PR comments: | | | | | | | | | | - `issues: write` | | | @@ -140,6 +148,13 @@ permissions: + +## Outputs + +| **Output** | **Description** | +| ------------------------ | -------------------------------------------------------------------------------- | +| **`report-artifact-id`** | Native test and coverage artifact ID, empty when GitHub reports are unavailable. | + diff --git a/actions/test/action.yml b/actions/test/action.yml index 6c91552e..86654b7e 100644 --- a/actions/test/action.yml +++ b/actions/test/action.yml @@ -2,6 +2,10 @@ name: "Test" description: | Action to test Node.js projects with support for coverage reporting and pull request annotations + GitHub reporting retains native report artifacts for seven days, including after + test failures. Set `comment: false` and call the common `ci-reports` workflow + after test jobs to publish one test comment and one coverage comment. + Note that this action must have write permission for id-token for this to work when using Codecov coverage reporting: ```yml @@ -64,10 +68,13 @@ inputs: See the [parse-ci-reports documentation](https://github.com/hoverkraft-tech/ci-github-common/tree/main/actions/quality/parse-ci-reports) for details. required: false default: "" + comment: + description: Publish an individual coverage PR comment. Set false when using the common ci-reports workflow; GitHub summaries and report artifacts remain enabled. + default: "true" github-token: description: | GitHub token for coverage PR comments. - Required when coverage is set to `github`. + Required when coverage is `github` and individual comments are enabled. Requires permissions to create and update PR comments: - `issues: write` @@ -75,6 +82,11 @@ inputs: required: false default: "" +outputs: + report-artifact-id: + description: Native test and coverage artifact ID, empty when GitHub reports are unavailable. + value: ${{ steps.upload-reports.outputs.artifact-id }} + runs: using: "composite" steps: @@ -150,7 +162,7 @@ runs: output-format: "summary,markdown" - name: 📊 Add coverage PR comment - if: always() && inputs.coverage == 'github' && github.event_name == 'pull_request' && steps.parse-coverage-reports.outputs.markdown + if: always() && inputs.comment == 'true' && inputs.coverage == 'github' && github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository && steps.parse-coverage-reports.outputs.markdown uses: hoverkraft-tech/ci-github-common/actions/create-or-update-comment@5fbb531db839a26ebcc35c4946be279f945f7633 # 1.0.0 with: title: "Code Coverage Report" @@ -162,3 +174,25 @@ runs: uses: hoverkraft-tech/ci-github-common/actions/quality/codecov@5fbb531db839a26ebcc35c4946be279f945f7633 # 1.0.0 with: working-directory: ${{ steps.working-directory.outputs.absolute-path }} + + - name: Prepare native report artifact + id: report-artifact + if: always() && steps.parse-coverage-reports.outputs.parsed-files != '' + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 + env: + REPORT_FILES: ${{ steps.parse-coverage-reports.outputs.parsed-files }} + with: + script: | + core.setOutput('files', JSON.parse(process.env.REPORT_FILES).join('\n')); + core.setOutput('id', require('node:crypto').randomUUID()); + + - name: Upload native test and coverage reports + id: upload-reports + if: always() && steps.report-artifact.outputs.files != '' + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: ci-reports-${{ github.run_attempt }}-${{ github.job }}-${{ steps.report-artifact.outputs.id }} + path: ${{ steps.report-artifact.outputs.files }} + include-hidden-files: true + if-no-files-found: error + retention-days: 7