Debian Codegraph @

.github/workflows/coverage.yml

raw
1 # internal coverage with PR comment and badge v0.0.5
2 # Note this works for public orgs but only for "internal" pull
3 # requests. In the case of fork PRs, there needs to be org-level
4 # github app with private key => ACCESS_TOKEN, with more job isolation
5 # and output passing in this workflow.
6 #
7 # This version has updated actions and coverage value regex, no fork isolation
8 # yet. This version checks for both repo owner and author_association = MEMBER
9 # so PR comments *should* be more available.
10 name: Coverage
11
12 on:
13 workflow_dispatch:
14 pull_request:
15 push:
16 branches:
17 - master
18
19 jobs:
20 pre_ci:
21 name: Prepare CI environment
22 runs-on: ubuntu-20.04
23 outputs:
24 #commit_message: ${{ steps.get_commit_message.outputs.commit_message }}
25 branch: ${{ steps.extract_branch.outputs.branch }}
26
27 steps:
28 - name: Checkout Project
29 uses: actions/checkout@v3
30 with:
31 # We need to fetch with a depth of 2 for pull_request so we can do HEAD^2
32 fetch-depth: 2
33
34 - name: Environment
35 run: |
36 bash -c set
37
38 #- name: "Get commit message"
39 #id: get_commit_message
40 #env:
41 #COMMIT_PUSH: ${{ github.event.head_commit.message }}
42 #run: |
43 #COMMIT_MESSAGE="${COMMIT_PUSH:-$(git log --format=%B -n 1 HEAD^2)}"
44 #echo "commit_message=${COMMIT_MESSAGE}" >> $GITHUB_OUTPUT
45
46 - name: Extract branch name
47 id: extract_branch
48 shell: bash
49 run: |
50 TMP_PULL_HEAD_REF="${{ github.head_ref }}"
51 TMP_GITHUB_REF="${GITHUB_REF#refs/heads/}"
52 EXPORT_VALUE=""
53 if [ "${TMP_PULL_HEAD_REF}" != "" ]
54 then
55 EXPORT_VALUE="${TMP_PULL_HEAD_REF}"
56 else
57 EXPORT_VALUE="${TMP_GITHUB_REF}"
58 fi
59 echo "branch=${EXPORT_VALUE}" >> $GITHUB_OUTPUT
60
61 base:
62 name: Base coverage
63 runs-on: ubuntu-20.04
64 outputs:
65 base_branch: ${{ steps.get_base.outputs.base_branch }}
66 base_cov: ${{ steps.get_base.outputs.base_cov }}
67
68 steps:
69 - uses: actions/checkout@v3
70 with:
71 ref: badges
72 path: badges
73
74 - name: Get base ref and coverage score
75 id: get_base
76 env:
77 FILE: 'test-coverage.txt'
78 working-directory: ./badges
79 shell: bash
80 run: |
81 TMP_PULL_BASE_REF="${{ github.base_ref }}"
82 TMP_GITHUB_REF="${GITHUB_REF#refs/heads/}"
83 EXPORT_VALUE=""
84 if [ "${TMP_PULL_BASE_REF}" != "" ]
85 then
86 EXPORT_VALUE="${TMP_PULL_BASE_REF}"
87 else
88 EXPORT_VALUE="${TMP_GITHUB_REF}"
89 fi
90 echo "base_branch=${EXPORT_VALUE}" >> $GITHUB_OUTPUT
91 if [ -f "${EXPORT_VALUE}/${FILE}" ]
92 then
93 echo "Base coverage found on ${EXPORT_VALUE}"
94 BASE_COV=$(cat "${EXPORT_VALUE}/${FILE}")
95 echo "Base coverage is: ${BASE_COV}"
96 echo "base_cov=${BASE_COV}" >> $GITHUB_OUTPUT
97 else
98 echo "Base coverage NOT found on ${EXPORT_VALUE}!!"
99 fi
100
101 check:
102 name: Pre CI check
103 runs-on: ubuntu-20.04
104 needs: [pre_ci, base]
105
106 steps:
107 - name: Check github variables
108 # NOTE base coverage env var may be empty here
109 env:
110 #COMMIT_MESSAGE: ${{ needs.pre_ci.outputs.commit_message }}
111 EXPORT_VALUE: ${{ needs.pre_ci.outputs.branch }}
112 BASE_BRANCH: ${{ needs.base.outputs.base_branch }}
113 BASE_COVERAGE: ${{ needs.base.outputs.base_cov }}
114 run: |
115 #echo "Commit message: ${COMMIT_MESSAGE}"
116 echo "Export value (head_ref): ${EXPORT_VALUE}"
117 echo "Base value (base_ref): ${BASE_BRANCH}"
118 echo "Base coverage (percent): ${{ env.BASE_COVERAGE }}"
119
120 cov_data:
121 name: Generate test coverage data
122 runs-on: ubuntu-20.04
123 needs: [check]
124 defaults:
125 run:
126 shell: bash
127 outputs:
128 coverage: ${{ steps.coverage.outputs.coverage }}
129 coverage-rounded-display: ${{ steps.coverage.outputs.coverage-rounded-display }}
130 env:
131 PLATFORM: ubuntu-20.04
132 PYTHON: '3.11'
133 PYTHONIOENCODING: utf-8
134 PIP_DOWNLOAD_CACHE: ${{ github.workspace }}/../.pip_download_cache
135
136 steps:
137 - uses: actions/checkout@v3
138 with:
139 fetch-depth: 0
140
141 - uses: actions/setup-python@v4
142 with:
143 python-version: ${{ env.PYTHON }}
144
145 - name: Add python requirements
146 run: |
147 python -m pip install --upgrade pip
148 pip install tox
149
150 - name: Generate coverage and fix pkg name
151 run: |
152 tox -e py
153
154 - name: Code Coverage Summary Report (data)
155 uses: irongut/[email protected]
156 with:
157 filename: coverage.xml
158 output: 'both'
159
160 - uses: actions/upload-artifact@v4
161 with:
162 name: src_coverage_rpts
163 path: |
164 coverage.xml
165 code-coverage-results.txt
166 retention-days: 1
167
168 - name: Check code coverage
169 id: coverage
170 env:
171 VALUE: "Branch Rate"
172 run: |
173 COVERAGE=$( cat code-coverage-results.txt | grep -e ^Summary | grep -o -E "${VALUE} = .{3}" | egrep -o '([0-9]+)' )
174 echo "coverage=${COVERAGE}" >> $GITHUB_OUTPUT
175 echo "coverage-rounded-display=${COVERAGE}%" >> $GITHUB_OUTPUT
176 echo "Current coverage is: ${COVERAGE}%"
177
178 - name: Code Coverage Summary Report
179 uses: irongut/[email protected]
180 if: ${{ github.event_name == 'pull_request' }}
181 with:
182 filename: coverage.xml
183 format: 'markdown'
184 output: 'both'
185
186 - name: Add Coverage PR Comment
187 uses: marocchino/sticky-pull-request-comment@v2
188 if: github.event_name == 'pull_request' && (github.event.pull_request.author_association == 'MEMBER' || github.actor == github.repository_owner)
189 with:
190 header: coverage
191 recreate: true
192 path: code-coverage-results.md
193
194 test:
195 name: Coverage check
196 runs-on: ubuntu-20.04
197 needs: [cov_data, base]
198 outputs:
199 coverage: ${{ needs.cov_data.outputs.coverage }}
200 coverage-base: ${{ needs.base.outputs.base_cov }}
201 coverage-rounded-display: ${{ needs.cov_data.outputs.coverage-rounded-display }}
202
203 steps:
204 - name: Check test coverage
205 env:
206 COVERAGE: ${{ needs.cov_data.outputs.coverage }}
207 COVERAGE_ROUNDED: ${{ needs.cov_data.outputs.coverage-rounded-display }}
208 BASE_COVERAGE: ${{ needs.base.outputs.base_cov }}
209 MEMBER: ${{ github.event.pull_request.author_association }}
210 run: |
211 echo "Coverage: ${COVERAGE}"
212 echo "Coverage Rounded: ${COVERAGE_ROUNDED}"
213 echo "Coverage on Base Branch: ${BASE_COVERAGE}"
214 echo "Author assoc: ${MEMBER}"
215
216 comment_cov_change:
217 name: Comment on PR with coverage delta
218 runs-on: ubuntu-20.04
219 needs: [test, base]
220
221 steps:
222 - name: Environment
223 run: |
224 bash -c set
225
226 - name: Set whether base coverage was found
227 shell: bash
228 env:
229 BASE: ${{ needs.test.outputs.coverage-base }}
230 run: |
231 if [ -n "${BASE}" ]
232 then
233 BASE_RESULT="true"
234 else
235 BASE_RESULT="false"
236 fi
237 echo "HAVE_BASE_COVERAGE is ${BASE_RESULT}"
238 echo "HAVE_BASE_COVERAGE=${BASE_RESULT}" >> $GITHUB_ENV
239 echo "BASE_COVERAGE=${BASE}" >> $GITHUB_ENV
240
241 - name: Collect variables and construct comment for delta message
242 if: env.HAVE_BASE_COVERAGE == 'true'
243 shell: bash
244 env:
245 BASE_BRANCH: ${{ needs.base.outputs.base_branch }}
246 COVERAGE: ${{ needs.test.outputs.coverage }}
247 BASE_COVERAGE: ${{ needs.test.outputs.coverage-base }}
248 DELTA_WORD: "not change"
249 RATE: "Branch Rate"
250
251 run: |
252 if [ "${COVERAGE}" -gt "${BASE_COVERAGE}" ]
253 then
254 DELTA_WORD="increase"
255 elif [ "${COVERAGE}" -lt "${BASE_COVERAGE}" ]
256 then
257 DELTA_WORD="decrease"
258 fi
259 CHG=$(( COVERAGE - BASE_COVERAGE ))
260 CHG="${CHG/-/}"
261 echo "" > coverage-delta.md
262 echo "Hello @${{ github.actor }}! Thanks for opening this PR. We found the following information based on analysis of the coverage report:" >> coverage-delta.md
263 echo "" >> coverage-delta.md
264 echo "__Base__ ${RATE} coverage is __${BASE_COVERAGE}%__" >> coverage-delta.md
265 if [ "${CHG}" = "0" ]
266 then
267 echo "Merging ${{ github.sha }} into ${BASE_BRANCH} will __${DELTA_WORD}__ coverage" >> coverage-delta.md
268 else
269 echo "Merging ${{ github.sha }} into ${BASE_BRANCH} will __${DELTA_WORD}__ coverage by __${CHG}%__" >> coverage-delta.md
270 fi
271 if ! [ "${DELTA_WORD}" = "decrease" ]
272 then
273 echo "" >> coverage-delta.md
274 echo "Nice work, @${{ github.actor }}. Cheers! :beers:" >> coverage-delta.md
275 fi
276
277 - name: Comment PR with test coverage delta
278 uses: marocchino/sticky-pull-request-comment@v2
279 if: env.HAVE_BASE_COVERAGE == 'true' && (github.event.pull_request.author_association == 'MEMBER' || github.actor == github.repository_owner)
280 with:
281 header: delta
282 recreate: true
283 path: coverage-delta.md
284
285 badge:
286 # Only generate and publish if these conditions are met:
287 # - The test step ended successfully
288 # - One of these is met:
289 # - This is a push event and the push event is on branch 'master' or 'develop'
290 # Note: if this repo is personal (ie, not an org repo) then you can
291 # use the following to change the scope of the next 2 jobs
292 # instead of running on branch push as shown below:
293 # - This is a pull request event and the pull actor is the same as the repo owner
294 # if: ${{ ( github.event_name == 'pull_request' && github.actor == github.repository_owner ) || github.ref == 'refs/heads/master' }}
295 name: Generate badge image with test coverage value
296 runs-on: ubuntu-20.04
297 needs: [test, pre_ci]
298 if: github.event_name == 'push'
299 outputs:
300 url: ${{ steps.url.outputs.url }}
301 markdown: ${{ steps.url.outputs.markdown }}
302
303 steps:
304 - uses: actions/checkout@v3
305 with:
306 ref: badges
307 path: badges
308
309 # Use the output from the `coverage` step
310 - name: Generate the badge SVG image
311 uses: emibcn/[email protected]
312 id: badge
313 with:
314 label: 'Branch Coverage'
315 status: ${{ needs.test.outputs.coverage-rounded-display }}
316 color: ${{
317 needs.test.outputs.coverage > 90 && 'green' ||
318 needs.test.outputs.coverage > 80 && 'yellow,green' ||
319 needs.test.outputs.coverage > 70 && 'yellow' ||
320 needs.test.outputs.coverage > 60 && 'orange,yellow' ||
321 needs.test.outputs.coverage > 50 && 'orange' ||
322 needs.test.outputs.coverage > 40 && 'red,orange' ||
323 needs.test.outputs.coverage > 30 && 'red,red,orange' ||
324 needs.test.outputs.coverage > 20 && 'red,red,red,orange' ||
325 'red' }}
326 path: badges/test-coverage.svg
327
328 - name: Commit badge and data
329 env:
330 BRANCH: ${{ needs.pre_ci.outputs.branch }}
331 COVERAGE: ${{ needs.test.outputs.coverage }}
332 FILE: 'test-coverage.svg'
333 DATA: 'test-coverage.txt'
334 working-directory: ./badges
335 run: |
336 git config --local user.email "[email protected]"
337 git config --local user.name "GitHub Action"
338 mkdir -p "${BRANCH}"
339 mv "${FILE}" "${BRANCH}"
340 echo "${COVERAGE}" > "${BRANCH}/${DATA}"
341 git add "${BRANCH}/${FILE}" "${BRANCH}/${DATA}"
342 # Will give error if badge has not changed
343 git commit -m "Add/Update badge" || true
344
345 - name: Push badge commit
346 uses: ad-m/github-push-action@master
347 with:
348 github_token: ${{ secrets.GITHUB_TOKEN }}
349 branch: badges
350 directory: badges