From b115f1d1a496a589fdf192951ba92db7c101f199 Mon Sep 17 00:00:00 2001 From: Pasquale Congiusti Date: Fri, 26 Jan 2024 10:52:58 +0100 Subject: [PATCH] fix(ci): calculate coverage on base pr target --- .github/actions/e2e-build/action.yml | 38 +++++++++++++++++++++++----- .github/workflows/build.yml | 2 ++ .github/workflows/comment-pr.yaml | 11 ++++---- 3 files changed, 38 insertions(+), 13 deletions(-) diff --git a/.github/actions/e2e-build/action.yml b/.github/actions/e2e-build/action.yml index 315021a9d9..5dda564d52 100644 --- a/.github/actions/e2e-build/action.yml +++ b/.github/actions/e2e-build/action.yml @@ -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" @@ -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/ \ No newline at end of file + name: ${{ inputs.artifact-name }} + path: /tmp/${{ inputs.artifact-name }}/ \ No newline at end of file diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index b0491b68d0..4671baa0c7 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -71,3 +71,5 @@ jobs: submodules: recursive - name: Execute Build (make) uses: ./.github/actions/e2e-build + with: + artifact-name: pr diff --git a/.github/workflows/comment-pr.yaml b/.github/workflows/comment-pr.yaml index f33e0838a2..f5360bdfd0 100644 --- a/.github/workflows/comment-pr.yaml +++ b/.github/workflows/comment-pr.yaml @@ -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 + '%**)' }) }