forked from BrianHenryIE/strauss
-
Notifications
You must be signed in to change notification settings - Fork 0
194 lines (165 loc) · 8.73 KB
/
Copy pathcodecoverage.yml
File metadata and controls
194 lines (165 loc) · 8.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
name: Code Coverage
# Runs PHPUnit with code coverage enabled, commits the html report to
# GitHub Pages, generates a README badge with the coverage percentage.
#
# Requires a gh-pages branch already created.
#
# git checkout --orphan gh-pages
# touch index.html
# git add index.html
# git commit -m 'Set up gh-pages branch' index.html
# git push origin gh-pages
#
# @author BrianHenryIE
on:
pull_request:
types: [opened, synchronize, ready_for_review, reopened]
push:
branches:
- master
paths:
- '**.php'
- 'composer.json'
- '.github/workflows/codecoverage.yml'
workflow_dispatch:
concurrency:
# Cancel previous runs of this workflow if they are testing the same branch
group: ${{ github.workflow }}-${{ (github.event_name == 'pull_request' && github.head_ref) || github.sha }}
cancel-in-progress: true
env:
COVERAGE_PHP_VERSION: '7.4'
jobs:
tests:
runs-on: ubuntu-latest
permissions:
pull-requests: write # To add coverage comment
contents: write # To commit coverage badge & report
strategy:
matrix:
php: [ '7.4' ]
steps:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
tools: composer:v2
extensions: fileinfo, gd
coverage: ${{ matrix.php == env.COVERAGE_PHP_VERSION && 'xdebug' || 'none' }}
- name: Checkout repository
uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
with:
# Value already defaults to true, but `persist-credentials` is required to push new commits to the repository.
persist-credentials: true
- name: Checkout GitHub Pages branch for code coverage report
if: ${{ matrix.php == env.COVERAGE_PHP_VERSION }}
uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
with:
ref: gh-pages
path: gh-pages
# TODO: merge master into this branch and run tests against what master will become, not just the isolated branch.
# NB: how would that affect tj-actions/changed-files?
- name: Install PHP dependencies
run: composer update --prefer-dist --verbose
- name: Install php-codecoverage-markdown for report
run: composer require --dev brianhenryie/php-codecoverage-markdown
- name: Print composer.lock (for debug)
run: cat composer.lock
- name: Run tests without coverage
if: ${{ matrix.php != env.COVERAGE_PHP_VERSION }}
run: |
vendor/bin/phpunit ./tests/Unit/ --debug
- name: Run tests with coverage
if: ${{ matrix.php == env.COVERAGE_PHP_VERSION }} # We only need the coverage once
run: XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-text --coverage-php gh-pages/${{ github.sha }}/phpunit/coveragephp.cov --coverage-clover tests/_output/clover.xml --coverage-html gh-pages/${{ github.sha }}/phpunit/html/ ./tests/Unit/ --debug
# This makes the coverage percentage available in `{{ steps.coverage-percentage.outputs.coverage }}`.
- name: Check test coverage
if: ${{ matrix.php == env.COVERAGE_PHP_VERSION }}
uses: johanvanhelden/gha-clover-test-coverage-check@2543c79a701f179bd63aa14c16c6938c509b2cec # v1
id: coverage-percentage
with:
percentage: 25
exit: false
filename: tests/_output/clover.xml
rounded-precision: "0"
- name: Generate the badge SVG image
if: ${{ matrix.php == env.COVERAGE_PHP_VERSION }}
uses: emibcn/badge-action@v2
id: badge
with:
label: 'PHPUnit'
status: ${{ format('{0}%', steps.coverage-percentage.outputs.coverage-rounded) }}
path: .github/coverage.svg
color: ${{ steps.coverage.outputs.coverage >= 90 && 'green'
|| steps.coverage.outputs.coverage >= 80 && 'orange'
|| 'red' }}
- name: Copy badge for PR comment display
if: ${{ matrix.php == env.COVERAGE_PHP_VERSION }}
run: cp .github/coverage.svg gh-pages/${{ github.sha }}/phpunit/coverage.svg
- name: Update root gh pages to redirect to commit for master
if: ${{ matrix.php == env.COVERAGE_PHP_VERSION && github.ref == 'refs/heads/master' }}
run: |
echo '<script>window.location.href="https://${{ github.repository_owner }}.github.io/${{ github.event.repository.name }}/${{ github.sha }}/phpunit/html/index.html";</script><a href="https://${{ github.repository_owner }}.github.io/${{ github.event.repository.name }}/${{ github.sha }}/phpunit/html/index.html">Coverage at ${{ github.sha }}</a>' > gh-pages/index.html
# https://github.blog/news-insights/bypassing-jekyll-on-github-pages/
# https://docs.github.com/en/pages/setting-up-a-github-pages-site-with-jekyll/about-github-pages-and-jekyll
- name: Disable Jekyll for GitHub Pages
if: ${{ matrix.php == env.COVERAGE_PHP_VERSION }}
run: |
if [ ! -f "gh-pages/.nojekyll" ]; then
touch gh-pages/.nojekyll
fi
- name: Commit HTML code coverage + badge to gh-pages
if: ${{ matrix.php == env.COVERAGE_PHP_VERSION && (github.event.pull_request.author_association == 'COLLABORATOR' || github.event.pull_request.author_association == 'OWNER') }}
uses: stefanzweifel/git-auto-commit-action@v7
with:
repository: gh-pages
branch: gh-pages
commit_message: "🤖 Commit code coverage to gh-pages"
- name: Commit code coverage badge to master/main
if: ${{ matrix.php == env.COVERAGE_PHP_VERSION && github.ref == 'refs/heads/master' }}
uses: stefanzweifel/git-auto-commit-action@v7
with:
file_pattern: .github/coverage.svg
commit_message: "🤖 Commit code coverage badge"
- name: Get changed files
if: ${{ matrix.php == env.COVERAGE_PHP_VERSION && github.event_name == 'pull_request' }}
id: changed-files
uses: tj-actions/changed-files@v47
with:
separator: ','
files: '**/**.php'
- name: Generate markdown report
if: ${{ matrix.php == env.COVERAGE_PHP_VERSION && github.event_name == 'pull_request' }} # We only need it added once
run: |
vendor/bin/php-codecoverage-markdown \
--input-file gh-pages/${{ github.sha }}/phpunit/coveragephp.cov \
--covered-files=${{ steps.changed-files.outputs.all_changed_files }} \
--base-url ${{ format('https://{0}.github.io/{1}/{2}/phpunit/html/', github.repository_owner, github.event.repository.name, github.sha) }} \
--output-file coverage-report.md
- name: Add to the PR message
if: ${{ matrix.php == env.COVERAGE_PHP_VERSION && github.event_name == 'pull_request' }}
run: |
echo "[](https://${{ github.repository_owner }}.github.io/${{ github.event.repository.name }}/${{ github.sha }}/phpunit/html/index.html)" > coverage-comment-header.md
echo "${{ format('[project coverage report {0}%](https://{1}.github.io/{2}/{3}/phpunit/html/) @ {4}', steps.coverage-percentage.outputs.coverage-rounded, github.repository_owner, github.event.repository.name, github.sha, github.sha) }}" >> coverage-comment-header.md
echo "$(cat coverage-comment-header.md)" > coverage-comment.md
echo "" >> coverage-comment.md
echo "" >> coverage-comment.md
echo "$(cat coverage-report.md)" >> coverage-comment.md
- name: Add phpcov uncovered lines report to PR comment
if: ${{ matrix.php == env.COVERAGE_PHP_VERSION && github.event_name == 'pull_request' }}
continue-on-error: true # `phpcov` can fail if there are no uncovered lines
run: |
BRANCHED_COMMIT=$(git rev-list origin..HEAD | tail -n 1);
echo "BRANCHED_COMMIT=$BRANCHED_COMMIT"
git diff $BRANCHED_COMMIT...${{ github.event.pull_request.head.sha }} > branch.diff
cat branch.diff
OUTPUT=${vendor/bin/phpcov patch-coverage --path-prefix $(pwd) ./gh-pages/${{ github.event.pull_request.head.sha }}/phpunit/phpunit.cov branch.diff || true}
echo $OUTPUT
echo "" >> coverage-comment.md
echo "$OUTPUT" >> coverage-comment.md
- name: Comment on PR
uses: mshick/add-pr-comment@v3
if: ${{ matrix.php == env.COVERAGE_PHP_VERSION && github.event_name == 'pull_request' }} # We only need it added once
with:
message-id: coverage-report
message-path: coverage-comment.md
continue-on-error: true # When a PR is opened by a non-member, there are no write permissions (and no access to secrets), so this step will always fail.