|
| 1 | +# |
| 2 | +# Copyright 2022 ABSA Group Limited |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# |
| 16 | + |
| 17 | +name: JaCoCo Report |
| 18 | + |
| 19 | +on: |
| 20 | + pull_request: |
| 21 | + branches: [ master ] |
| 22 | + types: [ opened, edited, synchronize, reopened ] |
| 23 | + |
| 24 | +env: |
| 25 | + scalaLong: 2.13.11 |
| 26 | + scalaShort: 2.13 |
| 27 | + coverage-overall: 57.0 |
| 28 | + coverage-changed-files: 80.0 |
| 29 | + check-overall-coverages: true |
| 30 | + |
| 31 | +jobs: |
| 32 | + build-test-and-measure: |
| 33 | + name: Build, Test and Measure |
| 34 | + runs-on: ubuntu-latest |
| 35 | + |
| 36 | + services: |
| 37 | + postgres: |
| 38 | + image: postgres:15 |
| 39 | + env: |
| 40 | + POSTGRES_PASSWORD: postgres |
| 41 | + POSTGRES_DB: movies |
| 42 | + options: >- |
| 43 | + --health-cmd pg_isready |
| 44 | + --health-interval 10s |
| 45 | + --health-timeout 5s |
| 46 | + --health-retries 5 |
| 47 | + ports: |
| 48 | + - 5432:5432 |
| 49 | + |
| 50 | + steps: |
| 51 | + - name: Checkout code |
| 52 | + uses: actions/checkout@v4 |
| 53 | + - name: Setup Scala |
| 54 | + uses: olafurpg/setup-scala@v14 |
| 55 | + with: |
| 56 | + |
| 57 | + - name: Prepare testing database |
| 58 | + run: sbt flywayMigrate |
| 59 | + - name: Build and run tests |
| 60 | + continue-on-error: true |
| 61 | + id: jacocorun |
| 62 | + run: sbt ++${{env.scalaLong}} jacoco |
| 63 | + - name: Add coverage to PR (core) |
| 64 | + if: steps.jacocorun.outcome == 'success' |
| 65 | + id: jacoco-core |
| 66 | + |
| 67 | + with: |
| 68 | + paths: ${{ github.workspace }}/core/target/scala-${{ env.scalaShort }}/jacoco/report/jacoco.xml |
| 69 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 70 | + min-coverage-overall: ${{ env.coverage-overall }} |
| 71 | + min-coverage-changed-files: ${{ env.coverage-changed-files }} |
| 72 | + title: JaCoCo `model` module code coverage report - scala ${{ env.scalaLong }} |
| 73 | + update-comment: true |
| 74 | + - name: Add coverage to PR (doobie) |
| 75 | + if: steps.jacocorun.outcome == 'success' |
| 76 | + id: jacoco-doobie |
| 77 | + |
| 78 | + with: |
| 79 | + paths: ${{ github.workspace }}/doobie/target/scala-${{ env.scalaShort }}/jacoco/report/jacoco.xml |
| 80 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 81 | + min-coverage-overall: ${{ env.coverage-overall }} |
| 82 | + min-coverage-changed-files: ${{ env.coverage-changed-files }} |
| 83 | + title: JaCoCo `agent` module code coverage report - scala ${{ env.scalaLong }} |
| 84 | + update-comment: true |
| 85 | + - name: Add coverage to PR (slick) |
| 86 | + if: steps.jacocorun.outcome == 'success' |
| 87 | + id: jacoco-slick |
| 88 | + |
| 89 | + with: |
| 90 | + paths: ${{ github.workspace }}/slick/target/scala-${{ env.scalaShort }}/jacoco/report/jacoco.xml |
| 91 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 92 | + min-coverage-overall: ${{env.coverage-overall }} |
| 93 | + min-coverage-changed-files: ${{ env.coverage-changed-files }} |
| 94 | + title: JaCoCo `slick` module code coverage report - scala ${{ env.scalaLong }} |
| 95 | + update-comment: true |
| 96 | + - name: Get the Coverage info |
| 97 | + if: steps.jacocorun.outcome == 'success' |
| 98 | + run: | |
| 99 | + echo "Total `core` module coverage ${{ steps.jacoco-core.outputs.coverage-overall }}" |
| 100 | + echo "Changed Files coverage ${{ steps.jacoco-core.outputs.coverage-changed-files }}" |
| 101 | + echo "Total `doobie` module coverage ${{ steps.jacoco-doobie.outputs.coverage-overall }}" |
| 102 | + echo "Changed Files coverage ${{ steps.jacoco-doobie.outputs.coverage-changed-files }}" |
| 103 | + echo "Total `slick` module coverage ${{ steps.jacoco-slick.outputs.coverage-overall }}" |
| 104 | + echo "Changed Files coverage ${{ steps.jacoco-slick.outputs.coverage-changed-files }}" |
| 105 | + - name: Fail PR if changed files coverage is less than ${{ env.coverage-changed-files }}% |
| 106 | + if: steps.jacocorun.outcome == 'success' |
| 107 | + uses: actions/github-script@v6 |
| 108 | + with: |
| 109 | + script: | |
| 110 | + const coverageCheckFailed = |
| 111 | + Number('${{ steps.jacoco-core.outputs.coverage-changed-files }}') < Number('${{ env.coverage-changed-files }}') || |
| 112 | + Number('${{ steps.jacoco-doobie.outputs.coverage-changed-files }}') < Number('${{ env.coverage-changed-files }}') || |
| 113 | + Number('${{ steps.jacoco-slick.outputs.coverage-changed-files }}') < Number('${{ env.coverage-changed-files }}'); |
| 114 | + if (coverageCheckFailed) { |
| 115 | + core.setFailed('Changed files coverage is less than ${{ env.coverage-changed-files }}%!'); |
| 116 | + } |
| 117 | + - name: Fail PR if overall files coverage is less than ${{ env.coverage-overall }}% |
| 118 | + if: ${{ (steps.jacocorun.outcome == 'success') && (env.check-overall-coverages == 'true') }} |
| 119 | + uses: actions/github-script@v6 |
| 120 | + with: |
| 121 | + script: | |
| 122 | + const coverageCheckFailed = |
| 123 | + Number('${{ steps.jacoco-core.outputs.coverage-overall }}') < Number('${{ env.coverage-overall }}') || |
| 124 | + Number('${{ steps.jacoco-doobie.outputs.coverage-overall }}') < Number('${{ env.coverage-overall }}') || |
| 125 | + Number('${{ steps.jacoco-slick.outputs.coverage-overall }}') < Number('${{ env.coverage-overall }}'); |
| 126 | + if (coverageCheckFailed) { |
| 127 | + core.setFailed('Overall coverage is less than ${{ env.coverage-overall }}%!'); |
| 128 | + } |
| 129 | + - name: Edit JaCoCo comments on build failure |
| 130 | + if: steps.jacocorun.outcome != 'success' |
| 131 | + uses: actions/github-script@v6 |
| 132 | + with: |
| 133 | + script: | |
| 134 | + const issue_number = context.issue.number; |
| 135 | + const owner = context.repo.owner; |
| 136 | + const repo = context.repo.repo; |
| 137 | + const jacocoReportRegExp = /^### JaCoCo .* code coverage report .*/; |
| 138 | +
|
| 139 | + const comments = await github.rest.issues.listComments({ |
| 140 | + owner, |
| 141 | + repo, |
| 142 | + issue_number, |
| 143 | + }); |
| 144 | +
|
| 145 | + for (const comment of comments.data) { |
| 146 | + const lines = comment.body.split('\n'); |
| 147 | + if (lines.length > 0 && jacocoReportRegExp.test(lines[0])) { |
| 148 | + await github.rest.issues.updateComment({ |
| 149 | + owner, |
| 150 | + repo, |
| 151 | + comment_id: comment.id, |
| 152 | + body: lines[0] + "\n\n### Build Failed", |
| 153 | + }); |
| 154 | + } |
| 155 | + } |
| 156 | +
|
| 157 | + core.setFailed('JaCoCo test coverage report generation failed, and related PR comments were updated.'); |
0 commit comments