Skip to content

Commit 8dc5223

Browse files
Update codecoverage-main
1 parent fddea9b commit 8dc5223

File tree

1 file changed

+116
-34
lines changed

1 file changed

+116
-34
lines changed

.github/workflows/codecoverage-main.yml

Lines changed: 116 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,49 +7,62 @@ on:
77
push:
88
branches:
99
- main
10+
pull_request:
11+
types: [ opened, reopened, ready_for_review, synchronize ]
12+
branches:
13+
- main
14+
workflow_dispatch:
1015

1116
jobs:
1217

13-
codecoverage-main:
18+
codecoverage:
1419
runs-on: ubuntu-latest
1520

1621
services:
1722
mysql:
18-
image: mysql:8.0
19-
env:
23+
image: mysql:5.7 # Password auth did not work on 8.0 on PHP 7.3, it did seem to work for PHP 7.4+
24+
env: # These are the same username and password as the wp-env container defaults
2025
MYSQL_ROOT_PASSWORD: password
2126
MYSQL_DATABASE: tests-wordpress
22-
ports:
27+
ports: # This mapping matches the .wp-env.json configuration
2328
- 33306:3306
2429
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
2530

2631
strategy:
27-
matrix:
28-
php: [ '7.3', '7.4', '8.0', '8.1', '8.2', '8.3' ]
32+
matrix: # Supported PHP versions
33+
php: [ '7.3', '7.4', '8.0', '8.1', '8.2', '8.3', '8.4' ]
2934

3035
steps:
3136
- name: Checkout
3237
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
33-
34-
- name: Check does gh-pages branch need to be created
35-
run: |
36-
if [[ $(git branch -l gh-pages) == "" ]]; then
37-
gh_pages_branch_needed=true
38-
else
39-
gh_pages_branch_needed=false
40-
fi
41-
echo "GH_PAGES_BRANCH_NEEDED=$gh_pages_branch_needed" >> $GITHUB_ENV;
42-
mkdir gh-pages
43-
44-
- name: Maybe create gh-pages branch
45-
if: ${{ env.GH_PAGES_BRANCH_NEEDED }}
46-
uses: peaceiris/actions-gh-pages@4f9cc6602d3f66b9c108549d475ec49e8ef4d45e # v4.0.0
4738
with:
48-
github_token: ${{ secrets.GITHUB_TOKEN }}
49-
publish_dir: ./gh-pages
50-
force_orphan: true
51-
allow_empty_commit: true
52-
commit_message: "🤖 Creating gh-pages branch"
39+
fetch-depth: 0 # attempting to get all branch names.
40+
41+
# TODO: a DEPLOY_KEY is needed to enable gh-pages for the first time (the branch can be created and pushed but
42+
# it will not be reachable as a website).
43+
# Consider @see https://github.com/peaceiris/actions-gh-pages
44+
# - name: Check does gh-pages branch need to be created
45+
# run: |
46+
# git branch -l;
47+
# if [[ $(git branch -l gh-pages) == "" ]]; then
48+
# gh_pages_branch_needed=true;
49+
# echo "gh-pages branch is needed";
50+
# else
51+
# gh_pages_branch_needed=false
52+
# echo "gh-pages branch already exists";
53+
# fi
54+
# echo "GH_PAGES_BRANCH_NEEDED=$gh_pages_branch_needed" >> $GITHUB_ENV;
55+
# mkdir gh-pages
56+
#
57+
# - name: Maybe create gh-pages branch
58+
# if: ${{ env.GH_PAGES_BRANCH_NEEDED }}
59+
# uses: peaceiris/actions-gh-pages@4f9cc6602d3f66b9c108549d475ec49e8ef4d45e # v4.0.0
60+
# with:
61+
# github_token: ${{ secrets.GITHUB_TOKEN }}
62+
# publish_dir: ./gh-pages
63+
# force_orphan: true
64+
# allow_empty_commit: true
65+
# commit_message: "🤖 Creating gh-pages branch"
5366

5467
- name: Checkout GitHub Pages branch for code coverage report
5568
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
@@ -58,7 +71,7 @@ jobs:
5871
path: gh-pages
5972

6073
- name: Install PHP
61-
uses: shivammathur/setup-php@9e72090525849c5e82e596468b86eb55e9cc5401 # v2.32.0
74+
uses: shivammathur/setup-php@cf4cade2721270509d5b1c766ab3549210a39a2a # v2.33.0
6275
with:
6376
php-version: ${{ matrix.php }}
6477
coverage: xdebug
@@ -75,35 +88,104 @@ jobs:
7588
run: composer install -v
7689

7790
- name: Allow writing to wp-content
91+
if: ${{ hashFiles('wp-content') != '' }} # TODO: This may be incorrect since it's a directory.
7892
run: sudo chmod -R a+w wp-content
7993

94+
# On main, there will be a previous coverage report. On PRs we create a new directory using the commit SHA.
8095
- name: Clear previous code coverage
96+
if: ${{ (matrix.php == '7.3') && (github.ref == 'refs/heads/main') }}
8197
run: |
8298
rm -rf gh-pages/phpunit || true;
8399
mkdir gh-pages/phpunit || true;
84100
85-
- name: Run tests
86-
run: XDEBUG_MODE=coverage composer test-coverage
101+
- name: Run unit tests
102+
if: ${{ hashFiles('tests/phpunit/bootstrap.php') != '' }} # Only run unit tests if they are present.
103+
run: XDEBUG_MODE=coverage vendor/bin/phpunit --bootstrap tests/phpunit/bootstrap.php --coverage-php tests/_output/unit.cov --debug
104+
105+
- name: Run wpunit tests
106+
run: XDEBUG_MODE=coverage vendor/bin/codecept run wpunit --coverage tests/_output/wpunit.cov --debug
87107

88-
- name: Merge code coverage
89-
run: vendor/bin/phpcov merge --clover gh-pages/phpunit/clover.xml --php gh-pages/phpunit/phpunit.cov --html gh-pages/phpunit/html/ tests/_output/;
108+
# For PRs, we'll generate the coverage report on each pushed commit
109+
- name: Merge code coverage for PR
110+
if: ${{ matrix.php == '7.3' && github.event_name == 'pull_request' }}
111+
run: |
112+
vendor/bin/phpcov merge --clover clover.xml tests/_output/;
113+
vendor/bin/phpcov merge --clover gh-pages/${{ github.event.pull_request.head.sha }}/phpunit/clover.xml --php gh-pages/${{ github.event.pull_request.head.sha }}/phpunit/phpunit.cov --html gh-pages/${{ github.event.pull_request.head.sha }}/phpunit/html/ tests/_output/;
114+
115+
# On main, we want it output to a different path
116+
- name: Merge code coverage for main
117+
if: ${{ (matrix.php == '7.3') && (github.ref == 'refs/heads/main') }}
118+
run: |
119+
vendor/bin/phpcov merge --clover clover.xml tests/_output/;
120+
vendor/bin/phpcov merge --clover gh-pages/phpunit/clover.xml --php gh-pages/phpunit/phpunit.cov --html gh-pages/phpunit/html/ tests/_output/;
121+
122+
# This makes the coverage percentage available in `{{ steps.coverage-percentage.outputs.coverage-rounded }}`.
123+
- name: Check test coverage
124+
if: ${{ matrix.php == '7.3' }}
125+
uses: johanvanhelden/gha-clover-test-coverage-check@2543c79a701f179bd63aa14c16c6938c509b2cec # v1
126+
id: coverage-percentage
127+
with:
128+
percentage: 25
129+
exit: false
130+
filename: clover.xml
131+
rounded-precision: "0"
90132

91133
# See: https://github.blog/2009-12-29-bypassing-jekyll-on-github-pages/
92134
- name: Add `.nojekyll` file so code coverage report successfully deploys to gh-pages
93-
working-directory: gh-pages/phpunit
135+
if: ${{ (matrix.php == '7.3') }}
136+
working-directory: gh-pages
94137
run: |
95138
touch .nojekyll
96139
git add -- .nojekyll *
97140
98-
- name: Update README badge
99-
run: php-coverage-badger gh-pages/phpunit/clover.xml gh-pages/phpunit/coverage.svg
141+
- name: Update README coverage badge
142+
if: ${{ (matrix.php == '7.3') && (github.ref == 'refs/heads/main') }} # only commit on main, on the PHP version we're using in production.
143+
run: php-coverage-badger clover.xml gh-pages/phpunit/coverage.svg
144+
145+
- name: Generate PR coverage badge
146+
if: ${{ (matrix.php == '7.3') && github.event_name == 'pull_request' }}
147+
run: php-coverage-badger clover.xml gh-pages/${{ github.event.pull_request.head.sha }}/phpunit/coverage.svg
100148

101149
- name: Commit code coverage to gh-pages
150+
if: ${{ matrix.php == '7.3' }}
102151
uses: stefanzweifel/git-auto-commit-action@e348103e9026cc0eee72ae06630dbe30c8bf7a79 # v5.1.0
103152
with:
104153
repository: gh-pages
105154
branch: gh-pages
106-
commit_message: "🤖 Save code coverage report to gh-pages" # TODO: include the percentage change in the message.
155+
commit_message: ${{ format('🤖 Save code coverage report to gh-pages {0}%', steps.coverage-percentage.outputs.coverage-rounded) }}
107156
commit_options: ""
108157
env:
109158
GITHUB_TOKEN: "${{ github.token }}"
159+
160+
- name: Add coverage badge to PR comment
161+
if: ${{ matrix.php == '7.3' && github.event_name == 'pull_request' }}
162+
run: |
163+
echo "[![Code Coverage ](https://newfold-labs.github.io/wp-module-onboarding/${{ github.event.pull_request.head.sha }}/phpunit/coverage.svg)](https://newfold-labs.github.io/wp-module-onboarding/${{ github.event.pull_request.head.sha }}/phpunit/html/)" >> coverage-comment.md
164+
echo "" >> coverage-comment.md
165+
echo "" >> coverage-comment.md
166+
167+
- name: Add coverage report link to PR comment
168+
if: ${{ matrix.php == '7.3' && github.event_name == 'pull_request' }}
169+
run: |
170+
echo "${{ format('[project coverage report {0}%](https://newfold-labs.github.io/wp-module-onboarding/{1}/phpunit/html/) @ {2}', steps.coverage-percentage.outputs.coverage-rounded, github.event.pull_request.head.sha, github.event.pull_request.head.sha) }}" >> coverage-comment.md
171+
echo "" >> coverage-comment.md
172+
echo "" >> coverage-comment.md
173+
174+
- name: Add phpcov uncovered lines report to PR comment
175+
if: ${{ matrix.php == '7.3' && github.event_name == 'pull_request' }}
176+
continue-on-error: true # phpcov can fail if there are no uncovered lines
177+
run: |
178+
BRANCHED_COMMIT=$(git rev-list origin..HEAD | tail -n 1);
179+
echo "BRANCHED_COMMIT=$BRANCHED_COMMIT"
180+
git diff $BRANCHED_COMMIT...${{ github.event.pull_request.head.sha }} > branch.diff;
181+
cat branch.diff;
182+
OUTPUT=${vendor/bin/phpcov patch-coverage --path-prefix $(pwd) ./gh-pages/${{ github.event.pull_request.head.sha }}/phpunit/phpunit.cov branch.diff || true}
183+
echo $OUTPUT;
184+
echo "$OUTPUT" >> coverage-comment.md
185+
186+
- name: Add coverage PR comment
187+
uses: mshick/add-pr-comment@b8f338c590a895d50bcbfa6c5859251edc8952fc # v2.8.2
188+
if: ${{ matrix.php == '7.3' && github.event_name == 'pull_request' }}
189+
with:
190+
message-id: coverage-report
191+
message-path: coverage-comment.md

0 commit comments

Comments
 (0)