Skip to content

Commit e1bcba2

Browse files
committed
feat(reporting): support consolidated PHP CI reports
1 parent f0049ac commit e1bcba2

10 files changed

Lines changed: 414 additions & 42 deletions

File tree

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ jobs:
4040
uses: $/.github/workflows/__test-action-test.yml
4141
permissions:
4242
contents: read
43+
pull-requests: write
44+
test-action-lint:
45+
uses: $/.github/workflows/__test-action-lint.yml
46+
permissions:
47+
contents: read
48+
pull-requests: write
4349
test-workflow-documentation:
4450
uses: $/.github/workflows/__test-workflow-documentation.yml
4551
permissions:
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
name: Internal - Test PHP lint action
2+
on:
3+
workflow_call:
4+
permissions: {}
5+
jobs:
6+
lint:
7+
runs-on: ubuntu-latest
8+
permissions:
9+
contents: read
10+
strategy:
11+
fail-fast: false
12+
matrix:
13+
scenario: [success, failure, no-reports]
14+
steps:
15+
- name: Arrange - Checkout
16+
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
17+
with:
18+
persist-credentials: false
19+
- name: Arrange - Configure native PHPStan reports
20+
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
21+
env:
22+
SCENARIO: ${{ matrix.scenario }}
23+
with:
24+
script: |
25+
const fs = require('node:fs/promises');
26+
await io.cp('tests/php-project', '.test-php-lint', {recursive: true});
27+
const manifestPath = '.test-php-lint/composer.json';
28+
const manifest = JSON.parse(await fs.readFile(manifestPath, 'utf8'));
29+
if (process.env.SCENARIO !== 'no-reports') {
30+
manifest.scripts['lint:ci'] = 'tools/vendor/bin/phpstan analyse src --level=max --no-progress --error-format=checkstyle > phpstan-checkstyle.xml';
31+
}
32+
if (process.env.SCENARIO === 'failure') {
33+
const source = '.test-php-lint/src/Sample.php';
34+
await fs.writeFile(source, (await fs.readFile(source, 'utf8')).replace('sample(): string', 'sample(): int'));
35+
}
36+
await fs.writeFile(manifestPath, JSON.stringify(manifest, null, 2) + '\n');
37+
await fs.writeFile('.test-php-lint/.private-fixture', 'Must not be uploaded');
38+
- name: Arrange - Install the Composer fixture
39+
uses: $/actions/setup-php
40+
with:
41+
working-directory: .test-php-lint
42+
coverage: none
43+
- name: Act - Lint without comment permission
44+
id: lint
45+
continue-on-error: ${{ matrix.scenario == 'failure' }}
46+
uses: $/actions/lint
47+
with:
48+
working-directory: .test-php-lint
49+
- name: Assert - Download the native lint artifact
50+
if: steps.lint.outputs.report-artifact-id != ''
51+
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
52+
with:
53+
artifact-ids: ${{ steps.lint.outputs.report-artifact-id }}
54+
path: downloaded-reports
55+
merge-multiple: true
56+
- name: Assert - Preserve failures and upload only parsed reports
57+
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
58+
env:
59+
SCENARIO: ${{ matrix.scenario }}
60+
OUTCOME: ${{ steps.lint.outcome }}
61+
ARTIFACT_ID: ${{ steps.lint.outputs.report-artifact-id }}
62+
with:
63+
script: |
64+
const assert = require('node:assert/strict');
65+
const fs = require('node:fs');
66+
assert.equal(process.env.OUTCOME, process.env.SCENARIO === 'failure' ? 'failure' : 'success');
67+
if (process.env.SCENARIO === 'no-reports') {
68+
assert.equal(process.env.ARTIFACT_ID, '');
69+
return;
70+
}
71+
assert.match(process.env.ARTIFACT_ID, /^\d+$/);
72+
const files = await (await glob.create('downloaded-reports/**/*', {matchDirectories: false})).glob();
73+
assert.equal(files.length, 1);
74+
const report = fs.readFileSync(files[0], 'utf8');
75+
assert.equal(report, fs.readFileSync('.test-php-lint/phpstan-checkstyle.xml', 'utf8'));
76+
assert.match(report, /<checkstyle\b/);
77+
assert.equal(report.includes('<error'), process.env.SCENARIO === 'failure');
78+
79+
individual-comment:
80+
runs-on: ubuntu-latest
81+
permissions:
82+
contents: read
83+
pull-requests: write
84+
steps:
85+
- name: Arrange - Checkout
86+
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
87+
with:
88+
persist-credentials: false
89+
- name: Arrange - Install the Composer fixture
90+
uses: $/actions/setup-php
91+
with:
92+
working-directory: tests/php-project
93+
coverage: none
94+
- name: Arrange - Configure PHPStan report generation
95+
working-directory: tests/php-project
96+
run: composer config scripts.lint:ci 'tools/vendor/bin/phpstan analyse src --level=max --no-progress --error-format=checkstyle > phpstan-checkstyle.xml'
97+
- name: Act - Publish an individual lint report
98+
uses: $/actions/lint
99+
with:
100+
working-directory: tests/php-project
101+
comment: "true"
102+
- name: Assert - One lint comment with collapsed details
103+
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
104+
with:
105+
script: |
106+
const assert = require('node:assert/strict');
107+
const pr = context.payload.pull_request;
108+
if (context.eventName !== 'pull_request' || pr.head.repo.full_name !== `${context.repo.owner}/${context.repo.repo}`) return;
109+
const comments = await github.paginate(github.rest.issues.listComments, {...context.repo, issue_number: pr.number, per_page: 100});
110+
const reports = comments.filter(comment => comment.user.login === 'github-actions[bot]' && comment.body.startsWith('PHP lint results\n'));
111+
assert.equal(reports.length, 1);
112+
assert.match(reports[0].body, /<details>/);
113+
assert.match(reports[0].body, /0 errors/);

‎.github/workflows/__test-action-test.yml‎

Lines changed: 69 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ jobs:
2222
coverage: ""
2323
- scenario: failing-command
2424
coverage: ""
25+
- scenario: failing-tests
26+
coverage: github
27+
- scenario: no-reports
28+
coverage: ""
2529
steps:
2630
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1
2731
with:
@@ -54,40 +58,100 @@ jobs:
5458
if (scenario === 'failing-command') {
5559
manifest.scripts['test:ci'] = ['@test --log-junit=junit.xml --no-coverage', "@php -r 'exit(7);'"];
5660
}
61+
if (scenario === 'failing-tests') {
62+
const test = path.join(directory, 'tests/SampleTest.php');
63+
await fs.writeFile(test, (await fs.readFile(test, 'utf8')).replace("'sample'", "'unexpected'"));
64+
}
65+
if (scenario === 'no-reports') manifest.scripts['test:ci'] = "@php -r 'exit(0);'";
5766
await fs.writeFile(manifestPath, JSON.stringify(manifest, null, 2) + '\n');
67+
await fs.writeFile(path.join(directory, '.private-fixture'), 'Must not be uploaded');
5868
core.setOutput('directory', directory);
5969
- uses: $/actions/setup-php
6070
with:
6171
working-directory: ${{ steps.fixture.outputs.directory }}
6272
composer-directories: '["."]'
63-
- name: Act - Run project tests
73+
- name: Act - Run project tests without comment permission
6474
id: tests
65-
continue-on-error: ${{ matrix.scenario == 'failing-command' }}
75+
continue-on-error: ${{ startsWith(matrix.scenario, 'failing-') }}
6676
uses: $/actions/test
6777
with:
6878
working-directory: ${{ steps.fixture.outputs.directory }}
6979
coverage: ${{ matrix.coverage }}
80+
- name: Assert - Download the native report artifact
81+
if: steps.tests.outputs.report-artifact-id != ''
82+
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
83+
with:
84+
artifact-ids: ${{ steps.tests.outputs.report-artifact-id }}
85+
path: downloaded-reports
86+
merge-multiple: true
7087
- name: Assert - Reports and project preparation
7188
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3
7289
env:
7390
DIRECTORY: ${{ steps.fixture.outputs.directory }}
7491
OUTCOME: ${{ steps.tests.outcome }}
7592
SCENARIO: ${{ matrix.scenario }}
93+
ARTIFACT_ID: ${{ steps.tests.outputs.report-artifact-id }}
94+
COVERAGE: ${{ matrix.coverage }}
7695
with:
7796
script: |-
7897
const assert = require('node:assert/strict');
7998
const fs = require('node:fs');
8099
const path = require('node:path');
81100
const directory = process.env.DIRECTORY;
82101
const scenario = process.env.SCENARIO;
83-
assert.equal(process.env.OUTCOME, scenario === 'failing-command' ? 'failure' : 'success');
84-
assert.match(fs.readFileSync(path.join(directory, 'junit.xml'), 'utf8'), /<testcase\b/);
102+
assert.equal(process.env.OUTCOME, scenario.startsWith('failing-') ? 'failure' : 'success');
103+
if (scenario === 'no-reports') {
104+
assert.equal(process.env.ARTIFACT_ID, '');
105+
return;
106+
}
107+
assert.match(process.env.ARTIFACT_ID, /^\d+$/);
108+
const files = await (await glob.create('downloaded-reports/**/*', {matchDirectories: false})).glob();
109+
assert.deepEqual(files.map(file => path.basename(file)).sort(), process.env.COVERAGE === 'github' ? ['cobertura.xml', 'junit.xml'] : ['junit.xml']);
110+
for (const file of files) assert.equal(fs.readFileSync(file, 'utf8'), fs.readFileSync(path.join(directory, path.basename(file)), 'utf8'));
111+
const junit = fs.readFileSync(path.join(directory, 'junit.xml'), 'utf8');
112+
assert.match(junit, /<testcase\b/);
113+
assert.equal(junit.includes('<failure'), scenario === 'failing-tests');
85114
const cobertura = path.join(directory, 'cobertura.xml');
86-
if (['reports', 'publication-disabled'].includes(scenario)) {
115+
if (['reports', 'publication-disabled', 'failing-tests'].includes(scenario)) {
87116
assert.match(fs.readFileSync(cobertura, 'utf8'), /<coverage\b[^>]*\bline-rate=/);
88117
} else {
89118
assert.equal(fs.existsSync(cobertura), false, 'Only the Composer script controls report generation.');
90119
}
91120
if (scenario === 'preparation') {
92121
assert.equal(fs.readFileSync(path.join(directory, 'prepared.txt'), 'utf8'), 'ready');
93122
}
123+
124+
individual-comment:
125+
runs-on: ubuntu-latest
126+
permissions:
127+
contents: read
128+
pull-requests: write
129+
steps:
130+
- name: Arrange - Checkout
131+
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
132+
with:
133+
persist-credentials: false
134+
- name: Arrange - Install the Composer fixture
135+
uses: $/actions/setup-php
136+
with:
137+
working-directory: tests/php-project
138+
composer-directories: '["."]'
139+
- name: Act - Publish an individual report
140+
uses: $/actions/test
141+
with:
142+
working-directory: tests/php-project
143+
comment: "true"
144+
- name: Assert - One comment with collapsed test and coverage details
145+
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
146+
with:
147+
script: |
148+
const assert = require('node:assert/strict');
149+
const pr = context.payload.pull_request;
150+
if (context.eventName !== 'pull_request' || pr.head.repo.full_name !== `${context.repo.owner}/${context.repo.repo}`) return;
151+
const comments = await github.paginate(github.rest.issues.listComments, {...context.repo, issue_number: pr.number, per_page: 100});
152+
const reports = comments.filter(comment => comment.user.login === 'github-actions[bot]' && comment.body.startsWith('PHP test results\n'));
153+
assert.equal(reports.length, 1);
154+
assert.match(reports[0].body, /<details>/);
155+
assert.match(reports[0].body, /Test Results/);
156+
assert.match(reports[0].body, /1 passed · 0 failed · 0 skipped/);
157+
assert.match(reports[0].body, /Coverage/);

‎.github/workflows/continuous-integration.md‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,40 @@ Clover reports, falling back to Cobertura. For GitHub coverage summaries, genera
9797
Cobertura. Add generated report files to `.gitignore`. Clean any retained report
9898
files in your project command if it reuses an existing workspace.
9999

100+
The lint and test actions upload only parsed native reports, including after a
101+
failed command, and retain them for seven days. Their `report-artifact-id`
102+
output identifies the artifact; it is empty when no supported reports were
103+
found. Test artifacts include coverage only with `coverage: github`.
104+
Codecov upload remains separate. Console-only lint output creates no artifact;
105+
configure a supported file report such as PHPStan Checkstyle for consolidation.
106+
107+
Individual comments are disabled by default, keeping producer jobs read-only.
108+
When calling an action directly, set `comment: true` and grant
109+
`pull-requests: write` to opt in. Human and bot actors follow the same rules;
110+
fork PRs keep summaries, annotations and artifacts without commenting.
111+
112+
For custom producer jobs named `lint` and `test`, leave individual comments
113+
disabled and add one final job using the common reporting workflow:
114+
115+
```yaml
116+
reports:
117+
needs: [lint, test]
118+
if: always()
119+
uses: hoverkraft-tech/ci-github-common/.github/workflows/ci-reports.yml@90211d735aa0289702ddbd7eb95b218020ccadad # 2.0.2
120+
permissions:
121+
contents: read
122+
pull-requests: write
123+
with:
124+
job-results: ${{ toJSON(needs) }}
125+
```
126+
127+
This updates one comment each for tests, coverage and lint, with details
128+
collapsed. Keep producer job IDs aligned with the shared reporter's contract;
129+
when wrapping producers in reusable workflows, use matching caller and inner
130+
job IDs. The full CI workflow uses inner `lint` and `test` jobs; use the
131+
individual actions when you need to compose them with this final reporter.
132+
Keep producer jobs as required checks: report publication does not replace them.
133+
100134
Preparation belongs in the Composer script, with report arguments attached only
101135
to the test command:
102136

‎.github/workflows/continuous-integration.yml‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,40 @@
8282
# Cobertura. Add generated report files to `.gitignore`. Clean any retained report
8383
# files in your project command if it reuses an existing workspace.
8484
#
85+
# The lint and test actions upload only parsed native reports, including after a
86+
# failed command, and retain them for seven days. Their `report-artifact-id`
87+
# output identifies the artifact; it is empty when no supported reports were
88+
# found. Test artifacts include coverage only with `coverage: github`.
89+
# Codecov upload remains separate. Console-only lint output creates no artifact;
90+
# configure a supported file report such as PHPStan Checkstyle for consolidation.
91+
#
92+
# Individual comments are disabled by default, keeping producer jobs read-only.
93+
# When calling an action directly, set `comment: true` and grant
94+
# `pull-requests: write` to opt in. Human and bot actors follow the same rules;
95+
# fork PRs keep summaries, annotations and artifacts without commenting.
96+
#
97+
# For custom producer jobs named `lint` and `test`, leave individual comments
98+
# disabled and add one final job using the common reporting workflow:
99+
#
100+
# ```yaml
101+
# reports:
102+
# needs: [lint, test]
103+
# if: always()
104+
# uses: hoverkraft-tech/ci-github-common/.github/workflows/ci-reports.yml@90211d735aa0289702ddbd7eb95b218020ccadad # 2.0.2
105+
# permissions:
106+
# contents: read
107+
# pull-requests: write
108+
# with:
109+
# job-results: ${{ toJSON(needs) }}
110+
# ```
111+
#
112+
# This updates one comment each for tests, coverage and lint, with details
113+
# collapsed. Keep producer job IDs aligned with the shared reporter's contract;
114+
# when wrapping producers in reusable workflows, use matching caller and inner
115+
# job IDs. The full CI workflow uses inner `lint` and `test` jobs; use the
116+
# individual actions when you need to compose them with this final reporter.
117+
# Keep producer jobs as required checks: report publication does not replace them.
118+
#
85119
# Preparation belongs in the Composer script, with report arguments attached only
86120
# to the test command:
87121
#

0 commit comments

Comments
 (0)