Skip to content

Commit

Permalink
fix(ci): calculate coverage on base pr target
Browse files Browse the repository at this point in the history
  • Loading branch information
squakez committed Jan 30, 2024
1 parent a98dec1 commit b115f1d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 13 deletions.
38 changes: 31 additions & 7 deletions .github/actions/e2e-build/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
name: e2e-build
description: 'End-to-End tests for build use-cases'

inputs:
artifact-name:
description: 'The name of the artifact to store coverage results'
required: true

runs:
using: "composite"

Expand All @@ -27,20 +32,39 @@ runs:
name: Prepare Test Environment
uses: ./.github/actions/kamel-prepare-env

- name: Test
- name: Test new branch
shell: bash
run: |
COVERAGE_OPTS="-covermode=count -coverprofile=coverage.out" make build
- name: Save coverage PR value
# Only run these on pull request events
- name: Save new coverage value
if: github.event_name == 'pull_request'
shell: bash
run: |
mkdir -p /tmp/${{ inputs.artifact-name }}
go tool cover -func=coverage.out -o=coverage.out
grep -o -P '(?<=\(statements\))(.+)(?=%)' coverage.out | xargs > /tmp/${{ inputs.artifact-name }}/coverage_new
echo ${{ github.event.number }} > /tmp/${{ inputs.artifact-name }}/id
- name: Checkout target branch code
if: github.event_name == 'pull_request'
uses: actions/checkout@v4
with:
persist-credentials: false
submodules: recursive
ref: ${{ github.event.pull_request.base.ref }}

- name: Test and save target coverage value
if: github.event_name == 'pull_request'
shell: bash
run: |
mkdir -p ./pr
COVERAGE_OPTS="-covermode=count -coverprofile=coverage.out" make build
go tool cover -func=coverage.out -o=coverage.out
grep -o -P '(?<=\(statements\))(.+)(?=%)' coverage.out | xargs > ./pr/coverage
echo ${{ github.event.number }} > ./pr/id
grep -o -P '(?<=\(statements\))(.+)(?=%)' coverage.out | xargs > /tmp/${{ inputs.artifact-name }}/coverage_old
- uses: actions/upload-artifact@v4
if: github.event_name == 'pull_request'
with:
name: pr
path: pr/
name: ${{ inputs.artifact-name }}
path: /tmp/${{ inputs.artifact-name }}/
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,5 @@ jobs:
submodules: recursive
- name: Execute Build (make)
uses: ./.github/actions/e2e-build
with:
artifact-name: pr
11 changes: 5 additions & 6 deletions .github/workflows/comment-pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,23 @@ jobs:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
var fs = require('fs');
const coverage = Number(fs.readFileSync('./coverage'));
const issue_number = Number(fs.readFileSync('./id'));
const main_coverage_result = await github.request('https://raw.githubusercontent.com/' + context.repo.owner + '/' + context.repo.repo + '/main/coverage')
const main_cov_num = Number(main_coverage_result.data)
diff = Math.round((coverage - main_cov_num + Number.EPSILON) * 100) / 100
const coverage_new = Number(fs.readFileSync('./coverage_new'));
const coverage_old = Number(fs.readFileSync('./coverage_old'));
diff = Math.round((coverage_new - coverage_old + Number.EPSILON) * 100) / 100
if(diff > 0){
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issue_number,
body: ':heavy_check_mark: Unit test coverage report - coverage increased from ' + main_cov_num + '% to ' + coverage + '% (**+' + diff + '%**)'
body: ':heavy_check_mark: Unit test coverage report - coverage increased from ' + coverage_old + '% to ' + coverage_new + '% (**+' + diff + '%**)'
})
}else if(diff < 0){
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issue_number,
body: ':warning: Unit test coverage report - coverage decreased from ' + main_cov_num + '% to ' + coverage + '% (**' + diff + '%**)'
body: ':warning: Unit test coverage report - coverage decreased from ' + coverage_old + '% to ' + coverage_new + '% (**' + diff + '%**)'
})
}

0 comments on commit b115f1d

Please sign in to comment.