Skip to content

Commit 9964058

Browse files
committed
feat(test): support consolidated CI reporting
1 parent d413d0c commit 9964058

4 files changed

Lines changed: 136 additions & 6 deletions

File tree

‎.github/workflows/__shared-ci.yml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,9 @@ jobs:
9898
id-token: write
9999
issues: read
100100
security-events: write
101+
102+
test-action-test:
103+
needs: linter
104+
uses: $/.github/workflows/__test-action-test.yml
105+
permissions:
106+
contents: read
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
name: Internal - Tests for test action
2+
3+
on:
4+
workflow_call:
5+
6+
permissions: {}
7+
8+
jobs:
9+
test-report:
10+
runs-on: ubuntu-latest
11+
permissions:
12+
contents: read
13+
strategy:
14+
fail-fast: false
15+
matrix:
16+
failure: [false, true]
17+
steps:
18+
- name: Arrange - Checkout
19+
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
20+
with:
21+
persist-credentials: false
22+
- name: Arrange - Create a dependency-free native test project
23+
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
24+
with:
25+
script: |
26+
const fs = require('node:fs');
27+
await io.mkdirP('report-fixture');
28+
await io.cp('.node-version', 'report-fixture/.node-version');
29+
fs.writeFileSync('report-fixture/package.json', JSON.stringify({
30+
name: 'report-fixture', version: '1.0.0', private: true,
31+
scripts: {
32+
'pretest:ci': 'node -e "require(\'node:fs\').mkdirSync(\'.reports\', {recursive:true})"',
33+
'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',
34+
},
35+
}));
36+
fs.writeFileSync('report-fixture/source.cjs', 'module.exports = value => value;\n');
37+
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");
38+
await exec.exec('npm', ['install', '--package-lock-only', '--ignore-scripts', '--no-audit', '--no-fund'], { cwd: 'report-fixture' });
39+
- name: Act - Run tests with comments disabled and no write token
40+
id: test
41+
continue-on-error: true
42+
uses: $/actions/test
43+
env:
44+
EXPECT_FAILURE: ${{ matrix.failure }}
45+
with:
46+
working-directory: report-fixture
47+
comment: "false"
48+
report-file: |
49+
.reports/junit.xml
50+
.reports/lcov.info
51+
- name: Assert - Retrieve reports even after a failed test
52+
if: steps.test.outputs.report-artifact-id != ''
53+
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
54+
with:
55+
artifact-ids: ${{ steps.test.outputs.report-artifact-id }}
56+
path: downloaded-reports
57+
merge-multiple: true
58+
- name: Assert - Verify outcome and native report content
59+
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
60+
env:
61+
ACTUAL: ${{ steps.test.outcome }}
62+
EXPECTED: ${{ matrix.failure && 'failure' || 'success' }}
63+
ARTIFACT_ID: ${{ steps.test.outputs.report-artifact-id }}
64+
with:
65+
script: |
66+
const assert = require('node:assert/strict');
67+
const fs = require('node:fs');
68+
assert.equal(process.env.ACTUAL, process.env.EXPECTED);
69+
assert.match(process.env.ARTIFACT_ID, /^\d+$/);
70+
const globber = await glob.create('downloaded-reports/**/*', { matchDirectories: false });
71+
const files = await globber.glob();
72+
assert.equal(files.length, 2);
73+
const junit = fs.readFileSync(files.find(file => file.endsWith('junit.xml')), 'utf8');
74+
assert.equal(junit.includes('<failure'), process.env.EXPECTED === 'failure');
75+
assert.match(fs.readFileSync(files.find(file => file.endsWith('lcov.info')), 'utf8'), /SF:/);

‎actions/test/README.md‎

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# ![Icon](data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIyNCIgaGVpZ2h0PSIyNCIgdmlld0JveD0iMCAwIDI0IDI0IiBmaWxsPSJub25lIiBzdHJva2U9ImN1cnJlbnRDb2xvciIgc3Ryb2tlLXdpZHRoPSIyIiBzdHJva2UtbGluZWNhcD0icm91bmQiIHN0cm9rZS1saW5lam9pbj0icm91bmQiIGNsYXNzPSJmZWF0aGVyIGZlYXRoZXItY2hlY2stc3F1YXJlIiBjb2xvcj0iYmx1ZSI+PHBvbHlsaW5lIHBvaW50cz0iOSAxMSAxMiAxNCAyMiA0Ij48L3BvbHlsaW5lPjxwYXRoIGQ9Ik0yMSAxMnY3YTIgMiAwIDAgMS0yIDJINWEyIDIgMCAwIDEtMi0yVjVhMiAyIDAgMCAxIDItMmgxMSI+PC9wYXRoPjwvc3ZnPg==) GitHub Action: Test
44

55
<div align="center">
6-
<img src="https://opengraph.githubassets.com/6963b10abf5bde223e9ca9d092769a577749af0a7d6725313a6c701c6a3a767a/hoverkraft-tech/ci-github-nodejs" width="60px" align="center" alt="Test" />
6+
<img src="https://opengraph.githubassets.com/8e2f87b5b89932502d55e7b2766b6affdf76a6bfb0491c387176a6fd18c784ef/hoverkraft-tech/ci-github-nodejs" width="60px" align="center" alt="Test" />
77
</div>
88

99
---
@@ -16,7 +16,6 @@
1616
[![License](https://img.shields.io/github/license/hoverkraft-tech/ci-github-nodejs)](http://choosealicense.com/licenses/mit/)
1717
[![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)
1818
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/hoverkraft-tech/ci-github-nodejs/blob/main/CONTRIBUTING.md)
19-
![GitHub Verified Creator](https://img.shields.io/badge/GitHub-Verified%20Creator-4493F8?logo=data:image/svg%2bxml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9I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MS0uNDg5IDEuMThsLS42OC45MjhhMS4xOCAxLjE4IDAgMCAwIDAgMS4zOTRsLjY4LjkyN2MuMjU2LjM0OC40MjQuNzUzLjQ4OSAxLjE4bC4xNzQgMS4xMzdjLjA3OC41MDkuNDc4LjkwOS45ODcuOTg3bDEuMTM2LjE3NGEyLjY3IDIuNjcgMCAwIDEgMS4xOC40ODlsLjkyOC42OGMuNDE0LjMwNS45NzkuMzA1IDEuMzk0IDBsLjkyNy0uNjhhMi42NyAyLjY3IDAgMCAxIDEuMTgtLjQ4OWwxLjEzNy0uMTc0YTEuMTggMS4xOCAwIDAgMCAuOTg3LS45ODdsLjE3NC0xLjEzNmEyLjY3IDIuNjcgMCAwIDEgLjQ4OS0xLjE4bC42OC0uOTI4YTEuMTc2IDEuMTc2IDAgMCAwIDAtMS4zOTRsLS42OC0uOTI3YTIuNjg2IDIuNjg2IDAgMCAxLS40ODktMS4xOGwtLjE3NC0xLjEzN2ExLjE3OSAxLjE3OSAwIDAgMC0uOTg3LS45ODdsLTEuMTM2LS4xNzRhMi42NzcgMi42NzcgMCAwIDEtMS4xOC0uNDg5bC0uOTI4LS42OGExLjE3NiAxLjE3NiAwIDAgMC0xLjM5NCAwWk0xMS4yOCA2Ljc4bC0zLjc1IDMuNzVhLjc1Ljc1IDAgMCAxLTEuMDYgMEw0LjcyIDguNzhhLjc1MS43NTEgMCAwIDEgLjAxOC0xLjA0Mi43NTEuNzUxIDAgMCAxIDEuMDQyLS4wMThMNyA4Ljk0bDMuMjItMy4yMmEuNzUxLjc1MSAwIDAgMSAxLjA0Mi4wMTguNzUxLjc1MSAwIDAgMSAuMDE4IDEuMDQyWiI+PC9wYXRoPgo8L3N2Zz4K)
2019

2120
<!-- badges:end -->
2221

@@ -29,6 +28,10 @@
2928

3029
Action to test Node.js projects with support for coverage reporting and pull request annotations
3130

31+
GitHub reporting retains native report artifacts for seven days, including after
32+
test failures. Set `comment: false` and call the common `ci-reports` workflow
33+
after test jobs to publish one test comment and one coverage comment.
34+
3235
Note that this action must have write permission for id-token for this to work when using Codecov coverage reporting:
3336

3437
```yml
@@ -90,8 +93,12 @@ permissions:
9093
# See the [parse-ci-reports documentation](https://github.com/hoverkraft-tech/ci-github-common/tree/main/actions/quality/parse-ci-reports) for details.
9194
path-mapping: ""
9295

96+
# Publish an individual coverage PR comment. Set false when using the common ci-reports workflow; GitHub summaries and report artifacts remain enabled.
97+
# Default: `true`
98+
comment: "true"
99+
93100
# GitHub token for coverage PR comments.
94-
# Required when coverage is set to `github`.
101+
# Required when coverage is `github` and individual comments are enabled.
95102
# Requires permissions to create and update PR comments:
96103
#
97104
# - `issues: write`
@@ -129,8 +136,9 @@ permissions:
129136
| | When omitted, the action falls back to `auto:test,auto:coverage` detection. | | |
130137
| **`path-mapping`** | Optional path mapping to adjust file paths in test and coverage reports. | **false** | - |
131138
| | See the [parse-ci-reports documentation](https://github.com/hoverkraft-tech/ci-github-common/tree/main/actions/quality/parse-ci-reports) for details. | | |
139+
| **`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` |
132140
| **`github-token`** | GitHub token for coverage PR comments. | **false** | - |
133-
| | Required when coverage is set to `github`. | | |
141+
| | Required when coverage is `github` and individual comments are enabled. | | |
134142
| | Requires permissions to create and update PR comments: | | |
135143
| | | | |
136144
| | - `issues: write` | | |
@@ -140,6 +148,13 @@ permissions:
140148
<!-- secrets:start -->
141149
<!-- secrets:end -->
142150
<!-- outputs:start -->
151+
152+
## Outputs
153+
154+
| **Output** | **Description** |
155+
| ------------------------ | -------------------------------------------------------------------------------- |
156+
| **`report-artifact-id`** | Native test and coverage artifact ID, empty when GitHub reports are unavailable. |
157+
143158
<!-- outputs:end -->
144159
<!-- examples:start -->
145160
<!-- examples:end -->

‎actions/test/action.yml‎

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ name: "Test"
22
description: |
33
Action to test Node.js projects with support for coverage reporting and pull request annotations
44
5+
GitHub reporting retains native report artifacts for seven days, including after
6+
test failures. Set `comment: false` and call the common `ci-reports` workflow
7+
after test jobs to publish one test comment and one coverage comment.
8+
59
Note that this action must have write permission for id-token for this to work when using Codecov coverage reporting:
610
711
```yml
@@ -64,17 +68,25 @@ inputs:
6468
See the [parse-ci-reports documentation](https://github.com/hoverkraft-tech/ci-github-common/tree/main/actions/quality/parse-ci-reports) for details.
6569
required: false
6670
default: ""
71+
comment:
72+
description: Publish an individual coverage PR comment. Set false when using the common ci-reports workflow; GitHub summaries and report artifacts remain enabled.
73+
default: "true"
6774
github-token:
6875
description: |
6976
GitHub token for coverage PR comments.
70-
Required when coverage is set to `github`.
77+
Required when coverage is `github` and individual comments are enabled.
7178
Requires permissions to create and update PR comments:
7279
7380
- `issues: write`
7481
- `pull-requests: write`
7582
required: false
7683
default: ""
7784

85+
outputs:
86+
report-artifact-id:
87+
description: Native test and coverage artifact ID, empty when GitHub reports are unavailable.
88+
value: ${{ steps.upload-reports.outputs.artifact-id }}
89+
7890
runs:
7991
using: "composite"
8092
steps:
@@ -150,7 +162,7 @@ runs:
150162
output-format: "summary,markdown"
151163

152164
- name: 📊 Add coverage PR comment
153-
if: always() && inputs.coverage == 'github' && github.event_name == 'pull_request' && steps.parse-coverage-reports.outputs.markdown
165+
if: always() && inputs.comment == 'true' && inputs.coverage == 'github' && github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository && github.actor != 'dependabot[bot]' && steps.parse-coverage-reports.outputs.markdown
154166
uses: hoverkraft-tech/ci-github-common/actions/create-or-update-comment@5fbb531db839a26ebcc35c4946be279f945f7633 # 1.0.0
155167
with:
156168
title: "Code Coverage Report"
@@ -162,3 +174,25 @@ runs:
162174
uses: hoverkraft-tech/ci-github-common/actions/quality/codecov@5fbb531db839a26ebcc35c4946be279f945f7633 # 1.0.0
163175
with:
164176
working-directory: ${{ steps.working-directory.outputs.absolute-path }}
177+
178+
- name: Prepare native report artifact
179+
id: report-artifact
180+
if: always() && steps.parse-coverage-reports.outputs.parsed-files != ''
181+
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
182+
env:
183+
REPORT_FILES: ${{ steps.parse-coverage-reports.outputs.parsed-files }}
184+
with:
185+
script: |
186+
core.setOutput('files', JSON.parse(process.env.REPORT_FILES).join('\n'));
187+
core.setOutput('id', require('node:crypto').randomUUID());
188+
189+
- name: Upload native test and coverage reports
190+
id: upload-reports
191+
if: always() && steps.report-artifact.outputs.files != ''
192+
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
193+
with:
194+
name: ci-reports-${{ github.run_attempt }}-${{ github.job }}-${{ steps.report-artifact.outputs.id }}
195+
path: ${{ steps.report-artifact.outputs.files }}
196+
include-hidden-files: true
197+
if-no-files-found: error
198+
retention-days: 7

0 commit comments

Comments
 (0)