lucassabreu / comment-coverage-clover

Github Action that automatically adds a comment with a summary of coverage reports (clover)
https://github.com/marketplace/actions/coverage-report-as-comment-clover
15 stars 4 forks source link

How to handle base-file #29

Closed Floppy012 closed 1 year ago

Floppy012 commented 1 year ago

I just started using GitHub Actions and so far I love it. And I would like to use this action. But it's not really clear to me how to handle the base-file.

In my case the base-file should be the coverage report from the main branch. But how do I access that from an action running in a PR?

The only option I see would be to use a cache for that. With the only downside being that the cache will be deleted when it's not being accessed for 7 days in a row.

Or is there a better way to handle this?

jacekk commented 1 year ago

@Floppy012 I have two actions:

// .github/workflows/coverage-store.yml
name: Coverage Store

on:
    push:
        branches:
            - develop

permissions:
    actions: write
    contents: read
    packages: read

jobs:
    coverage_store:
        runs-on: ubuntu-latest
        steps:
            - uses: actions/checkout@v3
            - run: yarn install
            - run: yarn test-ci --coverage

            - name: Upload coverage for current branch
              uses: actions/upload-artifact@v3
              with:
                  name: coverage-reports-artifact
                  path: ./coverage/clover.xml

and

// .github/workflows/coverage-report.yml
name: Coverage Report

on:
    pull_request:
        branches-ignore:
            - 'dependabot/npm_and_yarn/**'

permissions:
    actions: read
    contents: read
    packages: read
    pull-requests: write

jobs:
    coverage_report:
        runs-on: ubuntu-latest
        steps:
            - uses: actions/checkout@v3
            - run: yarn install
            - run: yarn test-ci --coverage

            - name: Download coverage for DEVELOP branch
              uses: dawidd6/action-download-artifact@v2
              continue-on-error: true
              with:
                  branch: develop
                  name: coverage-reports-artifact
                  path: ./coverage/base/
                  search_artifacts: true
                  workflow: .github/workflows/coverage-store.yml
            - name: Coverage Report as Comment (Clover)
              uses: lucassabreu/comment-coverage-clover@v0.9.1
              with:
                  base-file: ./coverage/base/clover.xml
                  chart-size: 90
                  dir-prefix: ${{ github.workspace }}
                  file: ./coverage/clover.xml
                  show-percentage-change-on-table: true
                  table-coverage-change: 0.1

I hope this is helpful, as I've also had to figure this thing out :smile:

lucassabreu commented 1 year ago

hi @Floppy012 , i do use the action "dawidd6/action-download-artifact" to download from other branchs (the base branch of the PR). and use "actions/upload-artifact" on the base branchs to upload the "base-file" after the commit.

most of them look something like the yaml bellow.

the important steps are:

name: Coverage Report

on:
  pull_request:
    branches:
      - main
  push:
    branches:
      - main

jobs:
  coverage:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      # next step varies project to project
      - uses: docker://php:7.4 
        env:
          XDEBUG_ENABLED: true
        with:
          args: php ./bin/phpunit -c phpunit.xml --coverage-clover=tests/clover.xml

      - if: ${{ github.event_name == 'pull_request' }}
        name: download_base_file
        uses: dawidd6/action-download-artifact@v2.24.0
        continue-on-error: true
        with:
          workflow: .github/workflows/cover-report.yml
          branch: main
          name: clover-coverage
          path: tests/_base

      - if: ${{ github.event_name != 'pull_request' }}
        name: upload_base_file
        uses: actions/upload-artifact@v3
        with:
          name: clover-coverage
          path: tests/clover.xml

      - if: ${{ github.event_name == 'pull_request' }}
        name: Coverage Report as Comment (Clover)
        uses: lucassabreu/comment-coverage-clover@main
        with:
          file: tests/clover.xml
          base-file: tests/_base/clover.xml
lucassabreu commented 1 year ago

yours is better... less ifs makes it more clear

Floppy012 commented 1 year ago

Wow! Thanks for the quick replies.

I managed to combine both examples into a solution that works for me. However I seem to have ran into a bug. My clover file only contains a single package node. And I get a type error when running the action:

TypeError: (packages || []).reduce is not a function Stack: TypeError: (packages || []).reduce is not a function
    at fromString (/home/runner/work/_actions/lucassabreu/comment-coverage-clover/v0.9.1/bin/index.js:89431:37)
    at /home/runner/work/_actions/lucassabreu/comment-coverage-clover/v0.9.1/bin/index.js:89744:29
    at step (/home/runner/work/_actions/lucassabreu/comment-coverage-clover/v0.9.1/bin/index.js:93:23)
    at Object.next (/home/runner/work/_actions/lucassabreu/comment-coverage-clover/v0.9.1/bin/index.js:74:53)
    at fulfilled (/home/runner/work/_actions/lucassabreu/comment-coverage-clover/v0.9.1/bin/index.js:64:58)

Probably caused by this line: https://github.com/lucassabreu/comment-coverage-clover/blob/caae14061c7485620d0630fcf34f5d0b735a1204/src/clover/index.ts#L55

My clover file looks like this:

<?xml version="1.0" encoding="utf-8"?>
<coverage generated="1679408725" clover="4.3.1">
  <project timestamp="1679408725">
    <metrics complexity="0" elements="637" coveredelements="495" conditionals="0" coveredconditionals="0" statements="637" coveredstatements="495" coveredmethods="0" methods="0" classes="117" loc="4706" ncloc="637" files="117" packages="1" />
    <package name="Default">
      <metrics complexity="0" elements="637" coveredelements="495" conditionals="0" coveredconditionals="0" statements="637" coveredstatements="495" coveredmethods="0" methods="0" classes="117" loc="4706" ncloc="637" files="117" />
      <!-- files redacted  -->
    </package>
  </project>
  <testproject timestamp="1679408725">
    <metrics complexity="0" elements="0" coveredelements="0" conditionals="0" coveredconditionals="0" statements="0" coveredstatements="0" coveredmethods="0" methods="0" classes="0" loc="0" ncloc="0" files="0" packages="0" />
  </testproject>
</coverage>

(I removed the files cause I'm not allowed to share that information)

lucassabreu commented 1 year ago

@Floppy012 can you update and retry to see if the problem is fixed?

Floppy012 commented 1 year ago

@Floppy012 can you update and retry to see if the problem is fixed?

@lucassabreu Works perfectly now. Thanks for your fast work :smile: