-
Notifications
You must be signed in to change notification settings - Fork 1
362 lines (326 loc) · 11.9 KB
/
coverage.yml
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# internal coverage with PR comment and badge v0.0.5
# Note this works for public orgs but only for "internal" pull
# requests. In the case of fork PRs, there needs to be org-level
# github app with private key => ACCESS_TOKEN, with more job isolation
# and output passing in this workflow.
#
# This version has updated actions and coverage value regex, no fork isolation
# yet. Badge and comment job logic should be tuned for personal vs org use
# (fork PRs still needs the big refactor; see badge job comments below).
name: Coverage
on:
workflow_dispatch:
pull_request:
push:
branches:
- master
- develop
jobs:
pre_ci:
name: Prepare CI environment
runs-on: ubuntu-20.04
outputs:
#commit_message: ${{ steps.get_commit_message.outputs.commit_message }}
branch: ${{ steps.extract_branch.outputs.branch }}
steps:
- name: Checkout Project
uses: actions/checkout@v4
with:
# We need to fetch with a depth of 2 for pull_request so we can do HEAD^2
fetch-depth: 2
- name: Environment
run: |
bash -c set
#- name: "Get commit message"
#id: get_commit_message
#env:
#COMMIT_PUSH: ${{ github.event.head_commit.message }}
#run: |
#COMMIT_MESSAGE="${COMMIT_PUSH:-$(git log --format=%B -n 1 HEAD^2)}"
#echo "commit_message=${COMMIT_MESSAGE}" >> $GITHUB_OUTPUT
- name: Extract branch name
id: extract_branch
shell: bash
run: |
TMP_PULL_HEAD_REF="${{ github.head_ref }}"
TMP_GITHUB_REF="${GITHUB_REF#refs/heads/}"
EXPORT_VALUE=""
if [ "${TMP_PULL_HEAD_REF}" != "" ]
then
EXPORT_VALUE="${TMP_PULL_HEAD_REF}"
else
EXPORT_VALUE="${TMP_GITHUB_REF}"
fi
echo "branch=${EXPORT_VALUE}" >> $GITHUB_OUTPUT
base:
name: Base coverage
runs-on: ubuntu-20.04
outputs:
base_branch: ${{ steps.get_base.outputs.base_branch }}
base_cov: ${{ steps.get_base.outputs.base_cov }}
steps:
- uses: actions/checkout@v3
with:
ref: badges
path: badges
- name: Get base ref and coverage score
id: get_base
env:
FILE: 'test-coverage.txt'
working-directory: ./badges
shell: bash
run: |
TMP_PULL_BASE_REF="${{ github.base_ref }}"
TMP_GITHUB_REF="${GITHUB_REF#refs/heads/}"
EXPORT_VALUE=""
if [ "${TMP_PULL_BASE_REF}" != "" ]
then
EXPORT_VALUE="${TMP_PULL_BASE_REF}"
else
EXPORT_VALUE="${TMP_GITHUB_REF}"
fi
echo "base_branch=${EXPORT_VALUE}" >> $GITHUB_OUTPUT
if [ -f "${EXPORT_VALUE}/${FILE}" ]
then
echo "Base coverage found on ${EXPORT_VALUE}"
BASE_COV=$(cat "${EXPORT_VALUE}/${FILE}")
echo "Base coverage is: ${BASE_COV}"
echo "base_cov=${BASE_COV}" >> $GITHUB_OUTPUT
else
echo "Base coverage NOT found on ${EXPORT_VALUE}!!"
fi
check:
name: Pre CI check
runs-on: ubuntu-20.04
needs: [pre_ci, base]
steps:
- name: Check github variables
# NOTE base coverage env var may be empty here
env:
#COMMIT_MESSAGE: ${{ needs.pre_ci.outputs.commit_message }}
EXPORT_VALUE: ${{ needs.pre_ci.outputs.branch }}
BASE_BRANCH: ${{ needs.base.outputs.base_branch }}
BASE_COVERAGE: ${{ needs.base.outputs.base_cov }}
run: |
#echo "Commit message: ${COMMIT_MESSAGE}"
echo "Export value (head_ref): ${EXPORT_VALUE}"
echo "Base value (base_ref): ${BASE_BRANCH}"
echo "Base coverage (percent): ${{ env.BASE_COVERAGE }}"
cov_data:
name: Generate test coverage data
runs-on: ubuntu-20.04
needs: [check]
defaults:
run:
shell: bash
outputs:
coverage: ${{ steps.coverage.outputs.coverage }}
coverage-rounded-display: ${{ steps.coverage.outputs.coverage-rounded-display }}
env:
PLATFORM: ubuntu-20.04
PYTHON: '3.11'
PYTHONIOENCODING: utf-8
PIP_DOWNLOAD_CACHE: ${{ github.workspace }}/../.pip_download_cache
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: actions/setup-python@v5
with:
python-version: ${{ env.PYTHON }}
- name: Install redis (ubuntu)
run: |
sudo apt-get -qq update
sudo apt-get install -yqq redis-server
sudo systemctl stop redis
- name: Add python requirements
run: |
python -m pip install --upgrade pip
pip install tox
- name: Setup old python for test
uses: actions/setup-python@v5
with:
python-version: 3.8
- name: Generate coverage
run: |
tox -e coverage,py38,py311
- name: Code Coverage Summary Report (data)
uses: irongut/[email protected]
with:
filename: coverage.xml
output: 'both'
- uses: actions/upload-artifact@v4
with:
name: src_coverage_rpts
path: |
coverage.xml
code-coverage-results.txt
retention-days: 1
- name: Check code coverage
id: coverage
env:
VALUE: "Branch Rate"
run: |
COVERAGE=$( cat code-coverage-results.txt | grep -e ^Summary | grep -o -E "${VALUE} = .{3}" | egrep -o '([0-9]+)' )
echo "coverage=${COVERAGE}" >> $GITHUB_OUTPUT
echo "coverage-rounded-display=${COVERAGE}%" >> $GITHUB_OUTPUT
echo "Current coverage is: ${COVERAGE}%"
- name: Code Coverage Summary Report
uses: irongut/[email protected]
if: ${{ github.event_name == 'pull_request' }}
with:
filename: coverage.xml
format: 'markdown'
output: 'both'
- name: Add Coverage PR Comment
uses: marocchino/sticky-pull-request-comment@v2
if: github.event_name == 'pull_request' && (github.event.pull_request.author_association == 'MEMBER' || github.actor == github.repository_owner)
with:
header: coverage
recreate: true
path: code-coverage-results.md
test:
name: Coverage check
runs-on: ubuntu-20.04
needs: [cov_data, base]
outputs:
coverage: ${{ needs.cov_data.outputs.coverage }}
coverage-base: ${{ needs.base.outputs.base_cov }}
coverage-rounded-display: ${{ needs.cov_data.outputs.coverage-rounded-display }}
steps:
- name: Check test coverage
env:
COVERAGE: ${{ needs.cov_data.outputs.coverage }}
COVERAGE_ROUNDED: ${{ needs.cov_data.outputs.coverage-rounded-display }}
BASE_COVERAGE: ${{ needs.base.outputs.base_cov }}
MEMBER: ${{ github.event.pull_request.author_association }}
run: |
echo "Coverage: ${COVERAGE}"
echo "Coverage Rounded: ${COVERAGE_ROUNDED}"
echo "Coverage on Base Branch: ${BASE_COVERAGE}"
echo "Author assoc: ${MEMBER}"
comment_cov_change:
name: Comment on PR with coverage delta
runs-on: ubuntu-20.04
needs: [test, base]
steps:
- name: Environment
run: |
bash -c set
- name: Set whether base coverage was found
shell: bash
env:
BASE: ${{ needs.test.outputs.coverage-base }}
run: |
if [ -n "${BASE}" ]
then
BASE_RESULT="true"
else
BASE_RESULT="false"
fi
echo "HAVE_BASE_COVERAGE is ${BASE_RESULT}"
echo "HAVE_BASE_COVERAGE=${BASE_RESULT}" >> $GITHUB_ENV
echo "BASE_COVERAGE=${BASE}" >> $GITHUB_ENV
- name: Collect variables and construct comment for delta message
if: env.HAVE_BASE_COVERAGE == 'true'
shell: bash
env:
BASE_BRANCH: ${{ needs.base.outputs.base_branch }}
COVERAGE: ${{ needs.test.outputs.coverage }}
BASE_COVERAGE: ${{ needs.test.outputs.coverage-base }}
DELTA_WORD: "not change"
RATE: "Branch Rate"
run: |
if [ "${COVERAGE}" -gt "${BASE_COVERAGE}" ]
then
DELTA_WORD="increase"
elif [ "${COVERAGE}" -lt "${BASE_COVERAGE}" ]
then
DELTA_WORD="decrease"
fi
CHG=$(( COVERAGE - BASE_COVERAGE ))
CHG="${CHG/-/}"
echo "" > coverage-delta.md
echo "Hello @${{ github.actor }}! Thanks for opening this PR. We found the following information based on analysis of the coverage report:" >> coverage-delta.md
echo "" >> coverage-delta.md
echo "__Base__ ${RATE} coverage is __${BASE_COVERAGE}%__" >> coverage-delta.md
if [ "${CHG}" = "0" ]
then
echo "Merging ${{ github.sha }} into ${BASE_BRANCH} will __${DELTA_WORD}__ coverage" >> coverage-delta.md
else
echo "Merging ${{ github.sha }} into ${BASE_BRANCH} will __${DELTA_WORD}__ coverage by __${CHG}%__" >> coverage-delta.md
fi
if ! [ "${DELTA_WORD}" = "decrease" ]
then
echo "" >> coverage-delta.md
echo "Nice work, @${{ github.actor }}. Cheers! :beers:" >> coverage-delta.md
fi
- name: Comment PR with test coverage delta
uses: marocchino/sticky-pull-request-comment@v2
if: env.HAVE_BASE_COVERAGE == 'true' && (github.event.pull_request.author_association == 'MEMBER' || github.actor == github.repository_owner)
with:
header: delta
recreate: true
path: coverage-delta.md
badge:
# Only generate and publish if these conditions are met:
# - The test step ended successfully
# - One of these is met:
# - This is a push event and the push event is on branch 'main' or 'develop'
# Note: if this repo is personal (ie, not an org repo) then you can
# use the following to change the scope of the next 2 jobs
# instead of running on branch push as shown below:
# - This is a pull request event and the pull actor is the same as the repo owner
# if: ${{ ( github.event_name == 'pull_request' && github.actor == github.repository_owner ) || github.ref == 'refs/heads/main' }}
name: Generate badge image with test coverage value
runs-on: ubuntu-20.04
needs: [test, pre_ci]
if: github.event_name == 'push'
outputs:
url: ${{ steps.url.outputs.url }}
markdown: ${{ steps.url.outputs.markdown }}
steps:
- uses: actions/checkout@v4
with:
ref: badges
path: badges
# Use the output from the `coverage` step
- name: Generate the badge SVG image
uses: emibcn/[email protected]
id: badge
with:
label: 'Branch Coverage'
status: ${{ needs.test.outputs.coverage-rounded-display }}
color: ${{
needs.test.outputs.coverage > 90 && 'green' ||
needs.test.outputs.coverage > 80 && 'yellow,green' ||
needs.test.outputs.coverage > 70 && 'yellow' ||
needs.test.outputs.coverage > 60 && 'orange,yellow' ||
needs.test.outputs.coverage > 50 && 'orange' ||
needs.test.outputs.coverage > 40 && 'red,orange' ||
needs.test.outputs.coverage > 30 && 'red,red,orange' ||
needs.test.outputs.coverage > 20 && 'red,red,red,orange' ||
'red' }}
path: badges/test-coverage.svg
- name: Commit badge and data
env:
BRANCH: ${{ needs.pre_ci.outputs.branch }}
COVERAGE: ${{ needs.test.outputs.coverage }}
FILE: 'test-coverage.svg'
DATA: 'test-coverage.txt'
working-directory: ./badges
run: |
git config --local user.email "[email protected]"
git config --local user.name "GitHub Action"
mkdir -p "${BRANCH}"
mv "${FILE}" "${BRANCH}"
echo "${COVERAGE}" > "${BRANCH}/${DATA}"
git add "${BRANCH}/${FILE}" "${BRANCH}/${DATA}"
# Will give error if badge has not changed
git commit -m "Add/Update badge" || true
- name: Push badge commit
uses: ad-m/github-push-action@master
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
branch: badges
directory: badges