You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by pc...@apache.org on 2024/01/30 16:35:05 UTC

(camel-k) branch main updated: fix(ci): calculate coverage on base pr target

This is an automated email from the ASF dual-hosted git repository.

pcongiusti pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-k.git


The following commit(s) were added to refs/heads/main by this push:
     new b115f1d1a fix(ci): calculate coverage on base pr target
b115f1d1a is described below

commit b115f1d1a496a589fdf192951ba92db7c101f199
Author: Pasquale Congiusti <pa...@gmail.com>
AuthorDate: Fri Jan 26 10:52:58 2024 +0100

    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 315021a9d..5dda564d5 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 b0491b68d..4671baa0c 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 f33e0838a..f5360bdfd 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 + '%**)'
               })
             }