Git Product home page Git Product logo

cicirello / jacoco-badge-generator Goto Github PK

View Code? Open in Web Editor NEW
96.0 4.0 41.0 683 KB

Coverage badges, and pull request coverage checks, from JaCoCo reports in GitHub Actions

Home Page: https://actions.cicirello.org/jacoco-badge-generator/

License: MIT License

Dockerfile 0.08% Python 99.92%
coverage jacoco java github-actions badges coverage-report badge-generator c0-coverage c1-coverage instructions-coverage branches-coverage testing pull-request-check

jacoco-badge-generator's Introduction

jacoco-badge-generator

cicirello/jacoco-badge-generator - Coverage badges, and pull request coverage checks, from JaCoCo reports in GitHub Actions

Check out all of our GitHub Actions: https://actions.cicirello.org/

About

GitHub Actions GitHub release (latest by date) Count of Action Users
Command-Line Utility PyPI PyPI Downloads/month PyPI Downloads/week
Build Status build CodeQL
Security Snyk security score
Source Info License GitHub top language
Support GitHub Sponsors Liberapay Ko-Fi

The jacoco-badge-generator can be used in one of two ways: as a GitHub Action or as a command-line utility (e.g., such as part of a local build script). The jacoco-badge-generator parses a jacoco.csv from a JaCoCo coverage report, computes coverage percentages from JaCoCo's Instructions and Branches counters, and generates badges for one or both of these (user configurable) to provide an easy to read visual summary of the code coverage of your test cases. The default behavior directly generates the badges internally with no external calls, but the action also provides an option to instead generate Shields JSON endpoints. It supports both the basic case of a single jacoco.csv, as well as multi-module projects in which case the action can produce coverage badges from the combination of the JaCoCo reports from all modules, provided that the individual reports are independent. It can also be configured to generate a simple JSON file containing the coverages as double-precision floating-point values, either instead of or in addition to generating the badges, which may be useful as input to other tools.

When used as a GitHub Action, the jacoco-badge-generator can also optionally be used as part of a pull-request check. Specifically, you can configure it to fail the workflow run if coverage decreased relative to prior run, and/or if coverage is below a target threshold. See the Inputs section for details of how to configure it for this purpose.

The developers of the jacoco-badge-generator are not affiliated with the developers of JaCoCo, although we are a fan and user of their excellent test coverage tool.

Table of Contents

The documentation is organized into the following sections:

  • The Coverage Metrics: Explains the JaCoCo metrics that are supported by the badge generator, such as what they measure, and why they were chosen for inclusion for the jacoco-badge-generator.
  • Badge Style and Content: Provides examples of the appearance of the badges that are generated, including a description of the color scheme used, and the formatting of the percentages.
  • GitHub Action Usage: Details on how to use the jacoco-badge-generator GitHub Action (its primary use-case).
    • Inputs: Detailed descriptions of the action inputs.
    • Outputs: Detailed descriptions of the action inputs.
    • Example Workflows: Example GitHub workflows demonstrating usage of the jacoco-badge-generator action.
    • Multi-Module Example Workflows: Example GitHub workflows demonstrating usage of the jacoco-badge-generator action with multi-module projects.
    • Examples in Other Projects: Info and a link to a template repository that we have setup to provide live runnable workflows to get you started; as well as links to a few repositories that are actively using the action, as well as direct links to the relevant workflow files.
  • Command-Line Usage: Details on how to install and run the jacoco-badge-generator as a command-line utility outside of GitHub Actions.
  • Summary of Input Defaults: A table summarizing all of the inputs, along with the defaults, for both GitHub Actions usage as well as CLI usage.
  • Built With: A list of languages, tools, etc used to develop this action.
  • Blog Posts: A selection of blog posts about the GitHub Action.
  • Support the Project: Information on various ways that you can support the project.

The Coverage Metrics

The jacoco-badge-generator currently supports generating badges for the two primary coverage metrics generated by JaCoCo: Instructions (C0 Coverage), and Branches (C1 Coverage). Here is a summary of what these compute and why they were chosen for inclusion by this badge generator.

Instructions Coverage (C0 Coverage)

The default behavior of the badge generator is to generate only the Instructions Coverage badge, which is labeled on the badge simply as "coverage". JaCoCo measures C0 Coverage from the Java bytecode instructions in the compiled .class files. One of the advantages to counting the bytecode instructions executed or missed, rather than lines of source code, is that it is independent of coding style and formatting. As a simple example, consider the sequence of Java statements to swap the values in two variables: int temp = a; a = b; b = temp;. A line counter will count this as 1 line if written on a single line, or 3 lines if each statement is written on its own line. However, JaCoCo's instructions counter treats these two cases as equivalent since they compile to the same bytecode. Consider a more complex example of calling a method while passing a simple value, such as foo(5) versus passing the result of a calculation to the method, such as foo(2.0 + bar/11.0). Line counting considers both of these as 1 line; while the second case will factor in more heavily into JaCoCo's instruction counting than will the first case. For these reasons, although JaCoCo also provides line coverage data, we do not currently support generating a badge from JaCoCo's line counter data. JaCoCo's use of bytecode instructions in its definition of C0 Coverage is a more meaningful measure of coverage than is counting lines of code.

Branches Coverage (C1 Coverage)

The badge generator also optionally supports generating a badge for Branches Coverage (or C1 Coverage), with the generated badge labeled as "branches". See the inputs section for a description of the action inputs. JaCoCo measures C1 Coverage or Branches Coverage from the Java bytecode in the compiled .class files, so the result may be a bit different than what you might expect from branch coverage. At first, you may even mistakenly guess that it is counting conditions (C2 coverage) rather than branches, but it is counting branches (in bytecode rather than in source code). Consider this example to illustrate the difference: if (a && b) foo(); else bar();. If we count branches in source code, there are 2 branches, which would require a minimum of two tests for full coverage, one where both a and b are true, and a second test where at least one of them is false. If we instead count conditions, there are 4 conditions (a==true, a==false, b==true, b==false), which can be covered with as few as two tests (e.g., a==true and b==false as test one, and b==true and a==false as test two), without actually covering both branches. JaCoCo's branches counter is neither of these. JaCoCo's branches counter counts branches in the Java bytecode. What does that mean for this example? Imagine instead that the if statement above was written as a pair of nested if statements: if (a) { if (b) { foo(); } else { bar(); } } else { bar(); }. There are a total of 4 branches in this case (and is essentially what JaCoCo would count as branches). To cover all 4 branches would require a minimum of 3 test cases: one where a and b are both true, one in which a is true and b is false, and a third where a is false and b's value doesn't matter. In this way, JaCoCo's branches counter leads to a stronger form of C1 Coverage than is usually implied by branches coverage.

Badge Style and Content

Default Color Scheme

Here are a few samples of what the badges look like if you use the default colors:

Coverage range Direct badge generation Badge generation from endpoint
Bright green for 100% coverage Coverage 100% Coverage 100%
Green for 90% through 99.9% coverage Coverage 99.9% Coverage 99.9%
Yellow green for 80% through 89.9% coverage Coverage 80% Coverage 80%
Yellow for 70% through 79.9% coverage Coverage 70% Coverage 70%
Orange for 60% through 69.9% coverage Coverage 60% Coverage 60%
Red for 0% through 59.9% coverage Coverage 59.9% Coverage 59.9%
Sample of a branch coverage badge Branches Coverage 99.9% Branches Coverage 99.9%

Customizing Colors or Coverage Intervals

The jacoco-badge-generator provides two inputs that can be used to customize the colors used for the badges. The colors input enables you to pass a list of colors to the action. The intervals input enables you to pass a list of percentages used to determine color choice. If you like the default colors, but want to start the colors at different percentages, then you can use the intervals input to accomplish that. These two inputs can be used either individually or in combination depending upon what you want to do. See the Inputs section for more details.

Displayed Percentages

The coverage displayed in the badge is the result of truncating to one decimal place. If that decimal place is 0, then it is displayed as an integer. The rationale for truncating to one decimal place, rather than rounding is to avoid displaying a just failing coverage as passing. For example, if the user of the action considers 80% to be a passing level, then we wish to avoid the case of 79.9999% being rounded to 80% (it will instead be truncated to 79.9%).

Direct Badge Generation vs JSON Endpoint

The default behavior generates badges that are inspired by the style of the badges of Shields.io, and generates the badges entirely within the jacoco-badge-generator, with no external calls. However, it also supports an optional alternative to instead generate Shields JSON endpoints. Most users will likely prefer the default behavior, for a variety of reasons, such as simpler insertion of badge into README and probable faster loading. The main reason to consider generating a JSON endpoint instead is if you are trying to match the style of the coverage badges to other badges in your README that use one of Shields's alternative styles. The default internally generated badges match the default Shields style. See the Inputs section for more details on how to generate JSON endpoints instead of badges.

Adding the Badges to your README

If you generate the badges (default behavior)....

If you use the default badges directory and default badge filenames, then you can add the coverage badge to your repository's readme with the following markdown:

![Coverage](.github/badges/jacoco.svg)

And likewise for the branch coverage badge:

![Branches](.github/badges/branches.svg)

See the Inputs section for how to change the directory and filenames of the badges. You can of course also link these to the JaCoCo coverage report if you host it online, or perhaps to the workflow that generated it, such as with (just replace USERNAME and REPOSITORY with yours):

[![Coverage](.github/badges/jacoco.svg)](https://github.com/USERNAME/REPOSITORY/actions/workflows/build.yml)

The above assumes that the relevant workflow is build.yml (replace as needed). This will link the badge to the runs of that specific workflow.

If you generate JSON endpoints instead....

Inserting coverage badges into your README is more complex if you use the alternate behavior of generating JSON endpoints. It involves passing the URL of your coverage endpoint to Shields custom badge endpoint. Assuming that you use the default badge directory, you would then use the following markdown:

![Coverage](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/USERNAME/REPOSITORY/BRANCHNAME/.github/badges/jacoco.json)

In the above, replace USERNAME, REPOSITORY, and BRANCHNAME with yours. You can do something similar for the branches coverage badge, such as:

![Branches](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/USERNAME/REPOSITORY/BRANCHNAME/.github/badges/branches.json)

And of course, you can also link these to your workflow runs just as before with:

[![Coverage](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/USERNAME/REPOSITORY/BRANCHNAME/.github/badges/jacoco.json)](https://github.com/USERNAME/REPOSITORY/actions/workflows/build.yml)

If you do have reason to prefer generating endpoints over generating the badges directly, then you might consider pushing the endpoints to a GitHub Pages site instead, such as a project site served from a docs directory of your default branch, or from a gh-pages branch. To do so, in addition to configuring GitHub Pages, you would need to use the badges-directory input to change the directory where the endpoints are stored (e.g., in "docs" or in a subdirectory of "docs"). Doing so would probably speed up Shields's access to your JSON endpoint, since you'd gain the benefit of the CDN that backs GitHub Pages; whereas passing Shields the URL to the JSON file on GitHub's raw server will probably be slower. Note that the potential benefit is probably small, so if doing so would complicate your workflow, you can simply pass the URL of the endpoint from GitHub's raw server (e.g., the examples of generating badges from an endpoint in the rightmost column of the table in section Default Color Scheme were done that way, without the use of GitHub Pages).

This is not an issue if you use the default behavior of directly generating the badge, since in that case the image is served directly to the viewer from the repository whose README is being viewed.

GitHub Action Usage

The jacoco-badge-generator's primary use-case is as a GitHub Action. The subsections of this section documents how to use it as a GitHub Action.

Inputs

All inputs include default values, and are thus optional provided the defaults are relevant to your use-case.

jacoco-csv-file

This input is the full path, relative to the root of the repository, to the jacoco.csv file, including filename. It defaults to target/site/jacoco/jacoco.csv, which is the default location and filename assuming you are using the JaCoCo Maven plugin and don't change the default output location. Note that if you are using Gradle to run your build, you will definitely need to use this input, because the default location and name of the jacoco csv report is different than it is in Maven. If you use Gradle's default output directories, then you will need to set this input with something like: jacoco-csv-file: build/reports/jacoco/test/jacocoTestReport.csv.

If you have a multi-module project, you can pass the paths (including filenames) to all of the jacoco.csv files for all of the sub-projects. Separate these by spaces, and in particular see the Multi-Module Example Workflows for an example of how to do this. Multi-module support is limited to cases where each module has its own test coverage report, and where those reports don't overlap.

You can also use a glob pattern to specify the set of JaCoCo reports for your modules. For example, jacoco-csv-file: "**/jacoco.csv" will match all jacoco.csv files found across all directories within your project. Or for example jacoco-csv-file: "**/*.csv" will match all csv files found within your project, but be careful with such a pattern if your project has other csv files that are not JaCoCo reports. Or as another example, maybe all of your JaCoCo reports are in the same directory, but with names with numbers. The pattern jacoco-csv-file: "target/site/jacoco/module*.csv" will match all csv files in the directory target/site/jacoco/ whose name begins with module. Note that in all of these examples you need the quotes around the glob pattern or else GitHub Actions will give you an error that your workflow file is invalid.

The action assumes that all reports passed via this input are independent of each other. If you are using matrix testing, such that each group of tests produces a report, and where the groups overlap in what they are testing (e.g., one group tests a portion of a class or method, and another group tests another portion, etc), then the coverage computed by this action will not be correct. The csv reports don't contain enough information to properly merge such overlapping reports. If this applies to your use-case, then you will need to have JaCoCo produce a single JaCoCo report first (for example, see jacoco:report-aggregate).

badges-directory

This input is the directory for storing badges, relative to the root of the repository. The default is .github/badges. The action will create the badges directory if it doesn't already exist, although the action itself does not commit.

generate-coverage-badge

This input controls whether or not to generate the coverage badge (Instructions Coverage), and defaults to true.

coverage-badge-filename

This input is the filename for the coverage badge (Instructions or C0 Coverage). The default filename is jacoco.svg. The file format is an svg. The badge file will be created within the badges-directory directory. The action doesn't commit the badge file. You will need to have additional steps in your workflow to do that.

generate-branches-badge

This input controls whether or not to generate the branches coverage badge, and defaults to false. This defaults to false to avoid surprising users who upgrade from earlier versions with a badge they didn't know would be generated.

branches-badge-filename

This input is the filename for the branches coverage badge (C1 Coverage). The default filename is branches.svg. The file format is an svg. The badge file will be created within the badges-directory directory. The action doesn't commit the badge file. You will need to have additional steps in your workflow to do that.

generate-coverage-endpoint

This input controls whether or not to generate a JSON endpoint for coverage (Instructions Coverage), and defaults to false.

coverage-endpoint-filename

This input is the filename for the coverage endpoint (Instructions or C0 Coverage) if you have opted to generate a JSON endpoint instead of the badge. The default filename is jacoco.json, and will be created within the badges-directory directory. The action doesn't commit the JSON file. You will need to have additional steps in your workflow to do that.

generate-branches-endpoint

This input controls whether or not to generate a JSON endpoint for branches coverage, and defaults to false.

branches-endpoint-filename

This input is the filename for the branches coverage endpoint (C1 Coverage) if you have opted to generate a JSON endpoint instead of the badge. The default filename is branches.json, and will be created within the badges-directory directory. The action doesn't commit the JSON file. You will need to have additional steps in your workflow to do that.

generate-summary

This input controls whether or not to generate a simple JSON summary report of the following form:

{"branches": 77.77777777777779, "coverage": 72.72727272727273}

The default is generate-summary: false. To enable, use generate-summary: true.

summary-filename

This input is the filename for the summary report (see above). The default is summary-filename: coverage-summary.json, and will be created within the badges-directory directory. The action doesn't commit the JSON file. You will need to have additional steps in your workflow to do that.

coverage-label

This input is the text for the label on the left side of the coverage badge, which defaults to coverage.

branches-label

This input is the text for the label on the left side of the branches coverage badge, which defaults to branches.

colors

This input can be used to change the colors used for the badges. It defaults to colors: '#4c1 #97ca00 #a4a61d #dfb317 #fe7d37 #e05d44', which are the hex color codes for the colors described previously in section Default Color Scheme. Because # has special meaning to YAML (it is used for comments), you must either put quotes around the input value as shown in this example, or you can escape each #. The list of colors that you pass here can either be space separated (as shown) or comma separated. The colors in this list can be specified either with hex (as in the example above), or with any named colors that are recognized by SVG, or some combination of the two. Here is an example with named colors: colors: green yellow orange red purple blue. Notice that you don't need quotes around the input if none of the colors are specified by hex. Although the default uses six colors and six coverage intervals, you can have as many or as few as you want. For example, if you want to use green regardless of percentage, you can set colors like this: colors: green. If you pass more colors than there are intervals, then the extra colors will be ignored. If you pass an empty list of colors, then the action will simply use the default colors. The action does not do any validation of the colors that you pass.

intervals

This input enables specifying the coverage intervals for the different colors. It is a simple list of percentages. The default is intervals: 100 90 80 70 60 0, which corresponds to what is described in the section Default Color Scheme earlier. The action assumes that the percentages in this list are in decreasing order. You can space separate or comma separate the percentages. For example, intervals: 100 90 80 70 60 0 is equivalent to intervals: 100, 90, 80, 70, 60, 0. A mix of spaces and commas will also work.

If you specify too many intervals, the extras will simply be ignored. If there are C colors altogether, then only the first (C-1) percentages specified in this input are used, with the last color designated for coverages that are below that last cutoff. For example, if you use the default set of 6 colors, then intervals: 100 90 80 70 60 is equivalent to intervals: 100 90 80 70 60 0.

Although these examples have integer percentages, the action supports floating-point values. For example, you can specify something like intervals: 99.5 90 80 70 60.

If you only want to use the first three default colors (bright green, green, and yellow green), then you don't necessarily need to change the value of the colors input. You can keep the default colors, and then you can use something like intervals: 80 60, which will assign 80 and above to bright green, 60 and above to green, and less than 60 to yellow green.

If you like some of the default colors, but want to skip over some of them, then you can either use a combination of the colors input and intervals input to accomplish this, or you can leave colors at the default and exploit the action's assumption of decreasing percentages in the intervals input to skip the ones you don't like. For example, if you want to use bright green for 80 and above, yellow for 60 and above, and red for less than 60, you might do something like the following: intervals: 80 80 80 60 60 0. The 0 at the end is optional.

on-missing-report

This input controls what happens if one or more jacoco.csv files do not exist. This input accepts one of three possible values: fail, quiet, or badges. The behavior of these is defined as follows:

  • The default is on-missing-report: fail, in which case the action will return a non-zero exit code (causing the workflow run to fail) if one or more files listed in the jacoco-csv-file input do not exist, or if an empty list of files is passed to the action. We recommend that you use this default since missing coverage report files in most cases probably means that there is either a bug in your workflow (e.g., typo in path to jacoco.csv) or that something went wrong in an earlier step (e.g., unit tests failed, halting generation of the coverage report).
  • You can use on-missing-report: quiet if you would rather the workflow itself not fail, in which case the action will instead quietly exit without producing badges if any JaCoCo reports are missing.
  • Although not recommended, a third option, on-missing-report: badges, will cause the action to produce badges from the report files that do exist, simply ignoring missing report files, provided that at least one such report file exists. We do not recommend this option since such a case is likely due to an error in your workflow, and any badges produced are likely computed with missing data.

Regardless of value passed to this input, the action will log warnings for any files listed in the jacoco-csv-file input that do not exist, for your inspection in the workflow run.

fail-if-coverage-less-than

This input enables directing the action to fail the workflow run if the computed coverage is less than a minimum. The default is 0, effectively disabling the option. You can specify it as either a floating point value in the interval 0.0 to 1.0, or as a percent (with or without the percent sign). For example, all of the following are equivalent: fail-if-coverage-less-than: 0.6, fail-if-coverage-less-than: 60, or fail-if-coverage-less-than: "60%". Note that in the last case, you need the quotes due to the percent sign. Values greater than 1 are assumed percents.

fail-if-branches-less-than

This input enables directing the action to fail the workflow run if the computed branches coverage is less than a minimum. The default is 0, effectively disabling the option. You can specify it as either a floating point value in the interval 0.0 to 1.0, or as a percent (with or without the percent sign). For example, all of the following are equivalent: fail-if-branches-less-than: 0.6, fail-if-branches-less-than: 60, or fail-if-branches-less-than: "60%". Note that in the last case, you need the quotes due to the percent sign. Values greater than 1 are assumed percents.

fail-on-coverage-decrease

This input enables directing the action to fail the workflow run if the computed coverage is less than it was on the previous run as recorded in either the existing coverage badge, the existing coverage Shields endpoint, or the JSON summary report (see the generate-summary input), if one of these exists. The default is false. Use fail-on-coverage-decrease: true to enable.

Additionally, at least one of the generate-summary, generate-coverage-badge, or generate-coverage-endpoint inputs must also be true, as the action will otherwise assume that there is no existing badge or summary report from which to get the prior coverage. If more than one of these exist, this feature will use the summary report to determine if coverage decreased since it is more precise than the truncated coverage percentage stored in the badge or Shields endpoint. Therefore, when using this feature, it is recommended that you also set generate-summary: true and commit the summary report JSON file to the repository.

fail-on-branches-decrease

This input enables directing the action to fail the workflow run if the computed branches coverage is less than it was on the previous run as recorded in either the existing branches coverage badge, the existing branches coverage Shields endpoint, or the JSON summary report (see the generate-summary input), if one of these exists. The default is false. Use fail-on-branches-decrease: true to enable.

Additionally, at least one of the generate-summary, generate-branches-badge, or generate-branches-endpoint inputs must also be true, as the action will otherwise assume that there is no existing badge or summary report from which to get the prior coverage. If more than one of these exist, this feature will use the summary report to determine if branches coverage decreased since it is more precise than the truncated coverage percentage stored in the badge or Shields endpoint. Therefore, when using this feature, it is recommended that you also set generate-summary: true and commit the summary report JSON file to the repository.

generate-workflow-summary

This input controls whether or not to log the coverage percentages to the GitHub Actions workflow job summary. The default is generate-workflow-summary: true. This input is only relevant when running in GitHub Actions mode, and not when running as a CLI tool.

workflow-summary-heading

This input is used to override the heading for the GitHub Actions workflow job summary that can be found on the Actions tab with detailed workflow logs. It defaults to: workflow-summary-heading: JaCoCo Test Coverage Summary. This default should be fine in most cases. If you are using this action in a multimodule project, and generating separate badges for each module, then you might consider using this input so that the GitHub workflow job summary is clearer as to which output corresponds to which module. See the Multi-Module Example Workflows section for examples. This input is only relevant when running in GitHub Actions mode, and not when running as a CLI tool.

Outputs

The action also outputs the actual computed coverage percentages as double-precision floating-point numbers. So you can add a step to your workflow to access these if desired (these action outputs are values in the interval from 0.0 to 1.0).

coverage

This output is the actual computed coverage percentage in the interval from 0.0 to 1.0. This is coverage computed from the instructions coverage data in the JaCoCo csv report.

branches

This output is the actual computed branches coverage percentage in the interval from 0.0 to 1.0. This is the percentage of branches covered, computed from the branches data in the JaCoCo csv report.

Example Workflows

Prerequisite: Running JaCoCo

Running JaCoCo via Maven

The example workflows assume that you are using Maven to build and test a Java project, and that you have the jacoco-maven-plugin configured in your pom.xml in the test phase with something along the lines of the following:

<build>
  <plugins>
    <plugin>
      <groupId>org.jacoco</groupId>
      <artifactId>jacoco-maven-plugin</artifactId>
      <version>0.8.10</version>
      <executions>
        <execution>
          <goals>
            <goal>prepare-agent</goal>
          </goals>
        </execution>
        <execution>
          <id>generate-code-coverage-report</id>
          <phase>test</phase>
          <goals>
            <goal>report</goal>
          </goals>
        </execution>
      </executions>
      </plugin>
  </plugins>
</build>

Note that the jacoco-badge-generator action has been tested with the jacoco.csv files generated by jacoco-maven-plugin versions 0.8.6 through 0.8.10, and has not been tested with earlier versions of JaCoCo.

Running JaCoCo via Gradle

If you use gradle as your build tool, then you can configure JaCoCo in build.gradle.kts with:

plugins {
    jacoco
}

tasks.jacocoTestReport {
    reports {
        csv.required.set(true)
    }
}

Or the equivalent in build.gradle:

plugins {
    id 'jacoco'
}

jacocoTestReport {
    reports {
        csv.required = true
    }
}

Basic Action Syntax

If you use Maven as your build tool, then you will have steps in your workflow along the lines of the following (which assumes that Maven is configured to run JaCoCo during the test phase:

    - name: Build with Maven
      run: mvn -B test

    - name: Generate JaCoCo Badge
      uses: cicirello/jacoco-badge-generator@v2
      with:
        generate-branches-badge: true

The equivalent for Gradle is:

      - name: Run Tests
        run: ./gradlew test

      - name: Run Test Coverage
        run: ./gradlew jacocoTestReport

      - name: Generate JaCoCo Badge
        uses: cicirello/jacoco-badge-generator@v2
        with:
          generate-branches-badge: true
          jacoco-csv-file: build/reports/jacoco/test/jacocoTestReport.csv

You can also use a specific release with:

    - name: Generate JaCoCo Badge
      uses: cicirello/[email protected]
      with:
        generate-branches-badge: true

All Possible Action Inputs

This shows a workflow step that uses all of the possible inputs of the jacoco-badge-generator action. It simply shows all of the inputs with their default values. See the Inputs section for complete details of what these inputs do.

    - name: Generate JaCoCo Badge
      uses: cicirello/jacoco-badge-generator@v2
      with:
        jacoco-csv-file: target/site/jacoco/jacoco.csv
        badges-directory: .github/badges
        generate-coverage-badge: true
        coverage-badge-filename: jacoco.svg
        generate-branches-badge: false
        branches-badge-filename: branches.svg
        generate-coverage-endpoint: false
        coverage-endpoint-filename: jacoco.json
        generate-branches-endpoint: false
        branches-endpoint-filename: branches.json
        generate-summary: false
        summary-filename: coverage-summary.json
        coverage-label: coverage
        branches-label: branches
        colors: '#4c1 #97ca00 #a4a61d #dfb317 #fe7d37 #e05d44'
        intervals: 100 90 80 70 60 0
        on-missing-report: fail
        fail-if-coverage-less-than: 0
        fail-if-branches-less-than: 0
        fail-on-coverage-decrease: false
        fail-on-branches-decrease: false
        generate-workflow-summary: true
        workflow-summary-heading: JaCoCo Test Coverage Summary

Since the above shows all of the default values of the action inputs, it is equivalent to:

    - name: Generate JaCoCo Badge
      uses: cicirello/jacoco-badge-generator@v2

Example Workflow 1: Generate instructions (or C0) coverage badge only.

This sample workflow runs on pushes to the main branch. It first sets up Java, and runs the tests with Maven. If you have JaCoCo configured to run during the test phase, this will also produce the JaCoCo reports. The jacoco-badge-generator action is then run to parse the jacoco.csv, compute the coverage percentage, and generate the badge. The coverage percentage is then logged in the workflow so you can inspect later if necessary. The next step of the workflow checks if any changes were made to the badge, and if so, does a commit and a push (note that there are also GitHub Actions that you can use for that step). And finally, the JaCoCo coverage reports are uploaded as a workflow artifact using the actions/upload-artifact GitHub Action, so you can inspect them if necessary.

name: build

on:
  push:
    branches: [ main ]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2

    - name: Set up the Java JDK
      uses: actions/setup-java@v2
      with:
        java-version: '17'
        distribution: 'adopt'

    - name: Build with Maven
      run: mvn -B test

    - name: Generate JaCoCo Badge
      id: jacoco
      uses: cicirello/jacoco-badge-generator@v2

    - name: Log coverage percentage
      run: |
        echo "coverage = ${{ steps.jacoco.outputs.coverage }}"
        echo "branch coverage = ${{ steps.jacoco.outputs.branches }}"

    - name: Commit the badge (if it changed)
      run: |
        if [[ `git status --porcelain` ]]; then
          git config --global user.name 'YOUR NAME HERE'
          git config --global user.email '[email protected]'
          git add -A
          git commit -m "Autogenerated JaCoCo coverage badge"
          git push
        fi

    - name: Upload JaCoCo coverage report
      uses: actions/upload-artifact@v2
      with:
        name: jacoco-report
        path: target/site/jacoco/

Example Workflow 2: Generate instructions coverage and branches coverage badges.

This example workflow is just like the above example, however, it generates both badges (instructions coverage percentage and branches coverage percentage). This example also uses the EndBug/add-and-commit action to commit and push the badge, whereas the previous example did this step with shell commands.

name: build

on:
  push:
    branches: [ main ]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2

    - name: Set up the Java JDK
      uses: actions/setup-java@v2
      with:
        java-version: '17'
        distribution: 'adopt'

    - name: Build with Maven
      run: mvn -B test

    - name: Generate JaCoCo Badge
      id: jacoco
      uses: cicirello/jacoco-badge-generator@v2
      with:
        generate-branches-badge: true

    - name: Log coverage percentage
      run: |
        echo "coverage = ${{ steps.jacoco.outputs.coverage }}"
        echo "branch coverage = ${{ steps.jacoco.outputs.branches }}"

    - name: Commit and push the badge (if it changed)
      uses: EndBug/add-and-commit@v7
      with:
        default_author: github_actions
        message: 'commit badge'
        add: '*.svg'

    - name: Upload JaCoCo coverage report
      uses: actions/upload-artifact@v2
      with:
        name: jacoco-report
        path: target/site/jacoco/

Multi-Module Example Workflows

Example Workflow 3: Generate Instructions and Coverage Badges for a Multi-Module Project.

This example workflow generates both badges (instructions coverage percentage and branches coverage percentage) for a multi-module project. The badges that are generated are computed over all modules. To do so, simply pass the paths to all of the JaCoCo reports that you want to include via the jacoco-csv-file input. The > is just Yaml's way of writing a string across multiple lines. You can also just list all on a single space-separated line, but your workflow file will be easier for you to read if you put them one per line. In this example, there are three subprojects: module1, module2, and module3.

name: build

on:
  push:
    branches: [ main ]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2

    - name: Set up the Java JDK
      uses: actions/setup-java@v2
      with:
        java-version: '17'
        distribution: 'adopt'

    - name: Build with Maven
      run: mvn -B test

    - name: Generate JaCoCo Badge
      id: jacoco
      uses: cicirello/jacoco-badge-generator@v2
      with:
        generate-branches-badge: true
        jacoco-csv-file: >
          module1/target/site/jacoco/jacoco.csv
          module2/target/site/jacoco/jacoco.csv
          module3/target/site/jacoco/jacoco.csv

    - name: Log coverage percentage
      run: |
        echo "coverage = ${{ steps.jacoco.outputs.coverage }}"
        echo "branch coverage = ${{ steps.jacoco.outputs.branches }}"

    - name: Commit the badge (if it changed)
      run: |
        if [[ `git status --porcelain` ]]; then
          git config --global user.name 'YOUR NAME HERE'
          git config --global user.email '[email protected]'
          git add -A
          git commit -m "Autogenerated JaCoCo coverage badge"
          git push
        fi

Example Workflow 4: Glob Pattern to Specify Reports of a Multi-Module Project.

This example workflow uses a glob pattern to specify the reports of a multi-module project, and generates both badges (instructions coverage percentage and branches coverage percentage). The badges that are generated are computed over all modules. In this example, all of the JaCoCo reports are named jacoco.csv but reside in different directories. You can match all of them with jacoco-csv-file: "**/jacoco.csv". The quotes around the glob are required to avoid an invalid workflow error from GitHub Actions.

name: build

on:
  push:
    branches: [ main ]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2

    - name: Set up the Java JDK
      uses: actions/setup-java@v2
      with:
        java-version: '17'
        distribution: 'adopt'

    - name: Build with Maven
      run: mvn -B test

    - name: Generate JaCoCo Badge
      id: jacoco
      uses: cicirello/jacoco-badge-generator@v2
      with:
        generate-branches-badge: true
        jacoco-csv-file: "**/jacoco.csv"

    - name: Log coverage percentage
      run: |
        echo "coverage = ${{ steps.jacoco.outputs.coverage }}"
        echo "branch coverage = ${{ steps.jacoco.outputs.branches }}"

    - name: Commit the badge (if it changed)
      run: |
        if [[ `git status --porcelain` ]]; then
          git config --global user.name 'YOUR NAME HERE'
          git config --global user.email '[email protected]'
          git add -A
          git commit -m "Autogenerated JaCoCo coverage badge"
          git push
        fi

Example Workflow 5: Multi-Module Project with Separate Badges for Each Module.

If you would prefer to generate separate coverage badges for each of the modules of a multi-module project, then just include multiple steps of the jacoco-badge-generator in your workflow, such as in this example. Be sure to use the inputs to specify names for the badge files, otherwise with the defaults the subsequent steps will overwrite the previous. This example assumes that there are two modules. You also will likely want to use the coverage-label and branches-label inputs to change the text on the left side of the badges if you are displaying badges for multiple modules in the README of the same repository. This example demonstrates that as well.

name: build

on:
  push:
    branches: [ main ]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2

    - name: Set up the Java JDK
      uses: actions/setup-java@v2
      with:
        java-version: '17'
        distribution: 'adopt'

    - name: Build with Maven
      run: mvn -B test

    - name: Generate JaCoCo Badges for Module 1
      id: jacocoMod1
      uses: cicirello/jacoco-badge-generator@v2
      with:
        generate-branches-badge: true
        jacoco-csv-file: module1/target/site/jacoco/jacoco.csv
        coverage-badge-filename: jacoco1.svg
        branches-badge-filename: branches1.svg
        coverage-label: coverage (module 1)
        branches-label: branches (module 1)
        workflow-summary-heading: Module 1 JaCoCo Test Coverage Summary

    - name: Generate JaCoCo Badges for Module 2
      id: jacocoMod2
      uses: cicirello/jacoco-badge-generator@v2
      with:
        generate-branches-badge: true
        jacoco-csv-file: module2/target/site/jacoco/jacoco.csv
        coverage-badge-filename: jacoco2.svg
        branches-badge-filename: branches2.svg
        coverage-label: coverage (module 2)
        branches-label: branches (module 2)
        workflow-summary-heading: Module 2 JaCoCo Test Coverage Summary

    - name: Commit the badge (if it changed)
      run: |
        if [[ `git status --porcelain` ]]; then
          git config --global user.name 'YOUR NAME HERE'
          git config --global user.email '[email protected]'
          git add -A
          git commit -m "Autogenerated JaCoCo coverage badge"
          git push
        fi

Examples in Other Projects

Template Repository with Runnable Workflow Examples

We now have a template repository with a simple Maven Java project, using the jacoco-maven-plugin, along with several live, runnable workflows to demonstrate a variety of use-cases for the jacoco-badge-generator action. That repository, cicirello/examples-jacoco-badge-generator, is a template so that you can potentially use it as a project starter. You can of course fork it instead. Its README explains the contents of that repository, especially the details of the various workflows it contains, and includes examples inserting the badges into its README.

Live Real Examples

If you would like to see examples where the action is actively used, here are a few repositories that are actively using the jacoco-badge-generator action. The table provides a link to repositories using the action, and direct links to the relevant workflow as well as the relevant build configuration (e.g., Maven pom.xml or Gradle build.gradle.kts) so you can see how JaCoCo is configured. Note that in all of the Maven examples, JaCoCo is configured within a Maven profile within the pom.xml, which is then activated via a command line option when mvn is run by the workflow on all push/pull request events. Configuration can instead be done in the <build> section if you'd rather not use a profile.

Repository Workflow Build Configuration
Chips-n-Salsa build.yml pom.xml
JavaPermutationTools build.yml pom.xml
ρμ build.yml pom.xml
XpathQS build.yml build.gradle.kts

Command-Line Usage

The jacoco-badge-generator started its life as a GitHub Action, but due to interest can now be used as a command-line utility outside of GitHub Actions.

Installing from PyPI

The jacoco-badge-generator requires Python 3 (and has been tested with 3.8 and above).

To install from PyPi (Unix and MacOS):

python3 -m pip install jacoco-badge-generator

To install from PyPi (Windows):

py -m pip install jacoco-badge-generator

To upgrade to the latest version from PyPi (Unix and MacOS):

python3 -m pip install --upgrade jacoco-badge-generator

To upgrade to the latest version from PyPi (Windows):

py -m pip install --upgrade jacoco-badge-generator

Running for the First Time

After installing, we recommend running it once with the -h or --help flag to see the details of all of the available command-line options.

On Unix or MacOS:

python3 -m jacoco_badge_generator --help

On Windows:

py -m jacoco_badge_generator --help

CLI Examples

All GitHub Action inputs have a counterpart command-line option that can be used in CLI mode. See the Inputs section earlier for details. If the GitHub Actions input is named input-name, then in CLI mode, the corresponding command-line option is --input-name. All options are optional and provide relevant defaults for the basic use-case. The defaults are nearly identical to those of the GitHub Action, with a few exceptions.

Here are a few examples. Note that all examples assume Unix (e.g., Python command is python3). If on Windows, just change python3 to py in all of the examples below.

All Defaults

As an example, from the root of your project (assuming you've already run JaCoCo), execute the following. In this example, all of the defaults are used, which will generate only the instructions coverage badge, and will place it in a badges directory (creating it if it doesn't exist). Note that the default directory for the generated badges is one of the differences between the defaults in CLI mode vs GitHub Actions mode.

python3 -m jacoco_badge_generator

Generating Instructions Coverage and Branches Coverage Badges

python3 -m jacoco_badge_generator --generate-branches-badge true

Generating Shields JSON Endpoints Instead of Badges

If you want to generate Shields JSON endpoints instead of badges, you need to disable generating the coverage badge, and enable the JSON endpoints:

python3 -m jacoco_badge_generator --generate-coverage-badge false --generate-coverage-endpoint true --generate-branches-endpoint true

Changing Colors and Coverage Intervals

If you want to change the colors used and the coverage intervals for each color, you can use the --colors and --intervals options. In the following example, green is used if coverage is at least 90 percent, yellow if coverage is less than 90 but at least 75 percent, orange is used if coverage is less than 75 percent but at least 60 percent, and red is used if coverage is less than 60 percent.

python3 -m jacoco_badge_generator --colors green yellow orange red --intervals 90 75 60

Colors can be specified as either SVG named colors as above or as 6-digit or 3-digit hex colors (see the Inputs earlier for more detail).

Changing the Badges Directory

python3 -m jacoco_badge_generator --badges-directory put/badges/here

Gradle Location of JaCoCo Report

The utility by default assumes that the JaCoCo report is the Maven default of target/site/jacoco/jacoco.csv. If it is somewhere else, there is an option to specify its location. Here is an example with Gradle's standard location and name of the JaCoCo csv report.

python3 -m jacoco_badge_generator --jacoco-csv-file build/reports/jacoco/test/jacocoTestReport.csv

Multi-Module Example

If you have a multi-module project with multiple coverage reports that you want to combine (provided they are independent), then you can specify the locations and names of all of the report files with something like:

python3 -m jacoco_badge_generator --jacoco-csv-file reports/report1.csv reports/report2.csv

In CLI mode, glob patterns will be handled by your shell. You can accomplish the above with:

python3 -m jacoco_badge_generator --jacoco-csv-file reports/report*.csv

Or perhaps all of your reports are named jacoco.csv but in different directories, then you can match all of them with:

python3 -m jacoco_badge_generator --jacoco-csv-file **/jacoco.csv

Summary of Input Defaults

The following table summarizes the default values of all inputs for both the GitHub Actions usage as well as the CLI usage. If your use-case requires the defaults as specified below, then you do not need to include them.

GitHub Actions Default CLI Default
jacoco-csv-file: target/site/jacoco/jacoco.csv --jacoco-csv-file target/site/jacoco/jacoco.csv
or
-j target/site/jacoco/jacoco.csv
badges-directory: .github/badges --badges-directory badges
or
-d badges
generate-coverage-badge: true --generate-coverage-badge true
coverage-badge-filename: jacoco.svg --coverage-badge-filename jacoco.svg
generate-branches-badge: false --generate-branches-badge false
branches-badge-filename: branches.svg --branches-badge-filename branches.svg
generate-coverage-endpoint: false --generate-coverage-endpoint false
coverage-endpoint-filename: jacoco.json --coverage-endpoint-filename jacoco.json
generate-branches-endpoint: false --generate-branches-endpoint false
branches-endpoint-filename: branches.json --branches-endpoint-filename branches.json
generate-summary: false --generate-summary false
summary-filename: coverage-summary.json --summary-filename coverage-summary.json
coverage-label: coverage --coverage-label coverage
branches-label: branches --branches-label branches
colors: '#4c1 #97ca00 #a4a61d #dfb317 #fe7d37 #e05d44' On Windows: --colors #4c1 #97ca00 #a4a61d #dfb317 #fe7d37 #e05d44
Bash or anywhere # has special meaning: --colors '#4c1' '#97ca00' '#a4a61d' '#dfb317' '#fe7d37' '#e05d44'
intervals: 100 90 80 70 60 0 --intervals 100 90 80 70 60 0
on-missing-report: fail --on-missing-report fail
fail-if-coverage-less-than: 0 --fail-if-coverage-less-than 0
fail-if-branches-less-than: 0 --fail-if-branches-less-than 0
fail-on-coverage-decrease: false --fail-on-coverage-decrease false
fail-on-branches-decrease: false --fail-on-branches-decrease false
generate-workflow-summary: true n/a
workflow-summary-heading: JaCoCo Test Coverage Summary n/a

Built With

The jacoco-badge-generator action uses the following:

Blog Posts

Here is a selection of blog posts about the jacoco-badge-generator on DEV.to:

Support the Project

You can support the project in a number of ways:

  • Starring: If you find the jacoco-badge-generator action useful, consider starring the repository.
  • Sharing with Others: Consider sharing it with others who you feel might find it useful.
  • Reporting Issues: If you find a bug or have a suggestion for a new feature, please report it via the Issue tracker.
  • Contributing Code: If there is an open issue that you think you can help with, submit a pull request.
  • Sponsoring: You can also consider becoming a sponsor.

License

This GitHub action is released under the MIT License.

jacoco-badge-generator's People

Contributors

cicirello avatar dependabot[bot] avatar divinenickname avatar eransamo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

jacoco-badge-generator's Issues

BUG: An extra __pycache__ file with root permission will break the workflow

Describe the bug
A clear and concise description of what the bug is.

The newest version 2.6 will generate a __pycache__ folder which has a root permission and this will break the next step to remove it without sudo permission.

To Reproduce
Steps to reproduce the behavior:

  1. First, '....'
  2. Then '....'
  3. Finally '....'
  4. See error or incorrect behavior, etc.

Expected behavior
A clear and concise description of what you expected to happen.

Screenshots
If applicable, add screenshots to help explain your problem.

image

Relevant System Info (please complete the following information):

  • OS
  • Version
  • etc

Additional context
Add any other context about the problem here.

Feature request: absolute path for jacoco-csv-file

Describe the bug / To Reproduce
specifiying an absolute path for jacoco-csv-file results in:
WARNING: Report file /home/runner/work/soot-reloaded/soot-reloaded/report/target/jacoco-aggregate/jacoco.csv does not exist.
WARNING: No JaCoCo csv reports found.

The file exists (checked with stat).

Expected behavior
As with a relative path - which i am using now and it works.

Feature Req: Change coverage badge text

We have multiple workfows in a single repository creating different coverages. We'd like to set the label text on the badge to show which coverage was calculated. E.g. instead of

coverage: 70,4%

we'd like to change the text to

coverage (product.core): 70,4%

I found no possible solutions to this problem in your documentation, maybe there is none yet!

Replacement for set-output may break some self-hosted cases

Problem

Unfortunately, this is going to break usage for self-hosted GHES enterprise organizations that are not on the bleeding edge. I am still on GHES 3.5, which uses runner version 2.294.0 while this requires at least 2.297.0. I am going to have to pin to 2.7.0 if reverting is not possible.

Originally posted by @erodewald in #84 (comment)

Possible Solution

Use set-output if GITHUB_OUTPUT env variable doesn't exist.

Sample badges not showing in GitHub Marketplace listing

Summary

The sample badges in the README are visible when viewing the repository, but not when viewing the action in the GitHub Marketplace. They previously appeared there. It is likely a bug in the GitHub Marketplace for links to images relative within the repository.

BUG: Maven multi-module project can't find 'target/site/jacoco/jacoco.csv'

Describe the bug
I manage a multi-module maven project and when it comes to run cicirello/[email protected], I got the following error:

FileNotFoundError: [Errno 2] No such file or directory: 'target/site/jacoco/jacoco.csv'

Indeed, the parent artefact has no target/site/jacoco directory whereas modules have.

To Reproduce
Steps to reproduce the behavior:

  1. First, create a dummy mutli-module project like this one
  2. Then define at least one test in each sub-project
  3. Finally define GH actions including cicirello/[email protected]
  4. See error

Expected behavior
I suppose the default behavior should scan for all target directories, from parent to children to look for all CSV files.

Relevant System Info (please complete the following information):

Best regards,
CP

Feature Req: add exact coverage value, with virtually infinite precision into some hidden field of JSON and SVG for future comparisons

In a huuge projects, 0.1% might include a couple of factories implementations plus minor bugfixes. Therefore, one decimal place precision is counterproductive - someone might spend significant effort, adding coverage by 0.04%, and someone else might just add some non-covered reducing overall coverage by 0.05%. After rounding to the first decimal place, the initial value, the value after the first contributor, and reduced value after the second one are undistinguishable. That discourages the first contributor from writing any more tests.

Please, add the input parameter "rounding" to the plugin.

Feature Req: Add documentation to execute without github actions or docker

Is not an issue. Is just the requirement to generate the badge images without github actions

Describe the solution you'd like
Just a couple of lines showing how to run the python code to generate the images

Describe alternatives you've considered
I reviewed the tests and with some intuition I created this sentence:

python JacocoBadgeGenerator.py jacoco.csv /home coverage.png branch.png true true fail 50 50 true true '100, 90, 80, 70, 60, 0' "#ffffff" true true 50 50 true summary.txt

Also it would be appreciated a markdown table in the readme explaining the parameters.

Can I make you a PR to your readme?

Matrix testing Python versions for unit tests and CLI mode

Summary

Revise GitHub Actions workflow for matrix testing of Python versions supported by the CLI mode (for the unit tests and for the CLI mode of the tool itself). Note: this is not necessary for the GitHub Actions mode since as an Action it runs within a Docker container with the current version of Python.

Feature Req: Support glob to find jacoco csv files in multi module projects

Hi @cicirello,

thanks for this great action, I think it is very useful.

I have one question/suggestion related to maven multi module projects.

Is your feature request related to a problem? Please describe.

In your example, all modules are listed. This works for a small number of modules, but introduces the risk that when a new module is added to the project, it is not added here.

Describe the solution you'd like

I think it would be nice if a glob pattern could be provided here instead of the full list like **/jacoco.csv.

Describe alternatives you've considered

It is possible to use this workaround, which is a bit verbose:

      - name: Find JaCoCo reports of modules
        run: |
          find . -name jacoco.csv
          echo "jacoco_csv_files=`find . -name jacoco.csv | tr '\n' ' '`" >> "$GITHUB_OUTPUT"
        id: find_jacoco
      - name: Generate JaCoCo Badge
        id: jacoco
        uses: cicirello/jacoco-badge-generator@v2
        with:
          generate-branches-badge: true
          jacoco-csv-file: >
            ${{ steps.find_jacoco.outputs.jacoco_csv_files }}

Additional context

Just an additional thought:
Maybe it would even be nice to have a glob pattern as the default as opposed to 'target/site/jacoco/jacoco.csv', which should work for single and multi module projects. I don't see much downsides, except maybe that searching using the glob pattern will be a little slower as the hard coded default path.

Best,
Florian

Question: Badges was not generated

Describe the bug
Maybe it's a beginner question and I'm doing something wrong, but I have a workflow running your action and I can't see the badge in the read.me file.

To Reproduce
Steps to reproduce the behavior:

  1. Run the workflow build
  2. Add the read.me code like the example ![Coverage](https://github.com/pablobaldez/test-coverage-sample/actions/workflows/pr-build.yml/jacoco.svg)

Expected behavior
I was expecting to see the badge such as the build badge

Screenshots
here is how my read me is
Screenshot 2022-06-30 at 21 16 24

Additional context
here is my GitHub repository if you want to check more details https://github.com/pablobaldez/test-coverage-sample

Feature Req: Example images in `README.md`

Is your feature request related to a problem? Please describe.
I want to scan the frontpage visually, and find image examples to help me make good choices.
When using this very excellent action in my jobs, I've needed to experiment to see what the results are as I apply different configuration options.
A simple example is that the action adds a very useful list of coverage results to the Action page for each build run.

Describe the solution you'd like
Add image screenshots to the README.md for what I should expect when using this action, and what the impact is for various choices.
This will guide me to picking the best true/false configuration.

When reading through the project frontpage, I'd like to visually spot features that I want in my project. Sometimes a picture really is worth 1,000 words.

Obvious examples include:

  • Picture of the generated badge
  • Picture of the Action run page with coverage summary stats
  • Effects of action configuration that alter the generated images or markdown

Describe alternatives you've considered
I won't say "considered". Rather I'll say "hard-learned lessons".

Additional context
Add any other context or screenshots about the feature request here.

After experimentation, I found that simply adding this action generates a very helpful list for coverage to my build run pages:
image
I did nothing more than call the jacoco-badge-generator action with badge generation turned off, and voilà: my GitHub run result pages had a coverage summary!

BUG: file not found in Multi-module

I have a two-module project and I use TestNG as testing framework, which allows (like JUnit I believe) to declare groups.
I declared 8 groups, (a) some are tagged in both modules, (b) some in only one of them.
I configured this GitHub action in order to list jacoco-csv files but in (b) case only one exists.

When I run the action, it throws FileNotFoundError: [Errno 2] No such file or directory: 'solver/target/site/jacoco/jacoco.csv' which is expected. But I wonder whether it is possible to add something like:

if os.path. exists(filename):

just before JacocoBadgeGenerator.py#L90.

Getting ZeroDivisionError: division by zero

Hey, first of all thank you for this helpful plugin.

When I'm running the plugin "cicirello/[email protected]" in my workflow I'm getting this error:

Traceback (most recent call last):
File "/JacocoBadgeGenerator.py", line 189, in
cov, branches = computeCoverage(jacocoCsvFile)
File "/JacocoBadgeGenerator.py", line 96, in computeCoverage
return covered / (covered + missed), coveredBranches / (coveredBranches + missedBranches)
ZeroDivisionError: division by zero

I opened my csv file and yes I can see zeros in the data, specifically I see BRANCH_MISSED and BRANCH_COVERED in some tests are both 0. So is it the problem from jacoc for not filling those columns with data or the script can omit these tests cases if both covered and missed are 0?

Option to generate Shields.io JSON endpoints instead of SVG badges

Summary
The badges currently generated are styled in a way that is based on Shields's "flat" style, although no actual dependency on Shields (generated entirely within the action). Some users of this GitHub action may use other shields badges in their repos using one of their other styles, and may prefer all of their repo badges to have a common style. Although one potential solution would be to replicate all of the shields styles within the action. that would seem overly redundant.

Proposed Solution
Keep the default behavior as the default (generate badges directly within the action including the way they are currently styled). Add an option via action inputs to generate JSON endpoints with the fields expected by shields, either instead of or in addition to generating the badges directly.

Users who like the default style can continue to use as is, keeping the performance benefit of serving the badge directly from the repo (e.g., already generated, and one http request directly to the badge). Users who want to match other shields styles can opt-in to generating a JSON endpoint instead, using shields to generate the badge (e.g., use shields's endpoint badge, passing url to JSON to Shields to generate the badge). That would also enable those users to pass Shields's query parameters to change style, etc.

Add ability to Fail the pipeline if the code coverage was decreased

I want to implement the code coverage check as a required verification step for the PR's to the master branch.
I think that it is possible to implement it as an additional feature for the jacoco-badge-generator.
example:

      - name: Generate JaCoCo Badge
        id: jacoco
        uses: cicirello/[email protected]
        with:
          fail-on-decrese: true

Also, it will be great to have such option as:

    fail-when-coverage-lessThan: 60

Which will fail the pipeline when coverage is less than provided argument.

Workflow to publish package to PyPi during release for CLI usage

Summary

Add a workflow that runs on release events as well as workflow_dispatch events that publishes the package on PyPI. This way whenever a GitHub release is created to release the Action to the GitHub Marketplace, the command-line utility is simultaneously released to PyPI.

Feature Req: Badge for multi-module Maven project

I have a multi-module Maven project with 4 sub modules. Each of them have its own tests and produce a separate Jacoco report.
I would like to present in my README,md a badge with %coverage of all the Jacoco results together.

Instead of passing a single 'jacoco-csv-file' argument, it would be great to pass a list of files so the badge will present an average coverage weighted by the lines number of each sub module.

Since Jacoco 0.7.7, a goal of 'jacoco:report-aggregate' is supported, which create a single Jacoco report from multiple sub modules.
My problem with this alternative is that it requires an addition dummy sub module which depends on all the other sub modules, and the aggregated Jacoco report is actually the dummy sub module report.
This solution seems a bit messy to me and I want to avoid creating an additional sub module and keep my library cleaner.
Also I do want the reports themselves to be separate, as I prefer each sub module to test itself properly.

Thanks a lot of the tool, love it!

Refactor main block to extract a main function

Summary

Currently, the entrypoint of the action is an if __name__ == "__main__" block. This works fine. However, one user requested the ability to use outside of GitHub Actions as a command line utility (see #40). In order to enable such functionality, parameters will need to be processed differently than they are as a GitHub Action.

To Do

  1. Extract most of the current if __name__ == "__main__" block to a main function, except for the parameter processing.
  2. Process parameters for the GitHub Action context in the existing block, and pass to the new main function.

Future

This issue does not actually fully address the command line utility use-case. However, the proposed new main function will greatly simplify future support. This issue is a pre-requisite to #40.

Feature Req: Generate badges from JaCoCo reports in xml format

Is your feature request related to a problem? Please describe.
The Pester PowerShell testing framework allows users to output code coverage reports as JaCoCo xml files. However, this action requires input in the form of csv files.

Describe the solution you'd like
A new input option jacoco-xml-file that works for JaCoCo xml files.

Describe alternatives you've considered
I could not find any simple way to convert from csv to xml. I could also request that the Pester framework add an option to output as csv, but I think it makes more sense to start here.

Additional context
N/A

Thank you for your consideration!

No Jacoco report with Java 17

Describe the bug
After upgrading to Jdk17 from 11 the jacoco badge github action failing with the below error.

  • name: Generate JaCoCo Badge
    id: jacoco
    uses: cicirello/jacoco-badge-generator@v2
    with:
    badges-directory: badges
    generate-branches-badge: true
    generate-summary: true
    • name: Log coverage percentage
      run: |
      echo "coverage = ${{ steps.jacoco.outputs.coverage }}"
      echo "branch coverage = ${{ steps.jacoco.outputs.branches }}"
    • name: Upload JaCoCo coverage report
      uses: actions/upload-artifact@v3
      with:
      name: jacoco-report
      path: target/site/jacoco/
    • name: Upload JaCoCo coverage badge
      uses: actions/upload-artifact@v3
      with:
      name: codecoverage-badge
      path: ./badges
    • name: List files in target
      run: |
      ls -l ./badges
    • name: Comment on PR with coverage percentages
      if: ${{ github.event_name == 'pull_request' }}
      run: |
      REPORT=$(<badges/coverage-summary.json)
      COVERAGE=$(jq -r '.coverage' <<< "$REPORT")%
      BRANCHES=$(jq -r '.branches' <<< "$REPORT")%
      NEWLINE=$'\n'
      BODY="## JaCoCo Test Coverage Summary Statistics${NEWLINE}* Coverage: ${COVERAGE}${NEWLINE}* Branches: ${BRANCHES}"
      gh pr comment ${{github.event.pull_request.number}} -b "${BODY}"
      continue-on-error: true
      env:
      GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

generate-branches-badge: true
generate-summary: true
jacoco-csv-file: target/site/jacoco/jacoco.csv
coverage-badge-filename: jacoco.svg
branches-badge-filename: branches.svg
generate-coverage-badge: true
coverage-label: coverage
branches-label: branches
on-missing-report: fail
fail-if-coverage-less-than: 0
fail-if-branches-less-than: 0
fail-on-coverage-decrease: false
fail-on-branches-decrease: false
intervals: 100 90 80 70 60 0
colors: #4c1 #97ca00 #a4a61d #dfb317 #fe7d37 #e05d44
generate-coverage-endpoint: false
generate-branches-endpoint: false
coverage-endpoint-filename: jacoco.json
branches-endpoint-filename: branches.json
summary-filename: coverage-summary.json
env:
DOCKER_REGISTRY: https://packages.aa.com
DOCKER_REPOSITORY_DEV: packages.aa.com/docker-dev/dotcwebappimage
REPO_NAME: pcs-dotcras-webapp
JAVA_HOME: /opt/hostedtoolcache/Java_Temurin-Hotspot_jdk/17.0.7-7/x64
JAVA_HOME_17_X64: /opt/hostedtoolcache/Java_Temurin-Hotspot_jdk/17.0.7-7/x64
/usr/bin/docker run --name c9a4a5cf7c06edd4384493bf0d4f88f418a56b_6564cb --label c9a4a5 --workdir /github/workspace --rm -e "DOCKER_REGISTRY" -e "DOCKER_REPOSITORY_DEV" -e "REPO_NAME" -e "JAVA_HOME" -e "JAVA_HOME_17_X64" -e "INPUT_BADGES-DIRECTORY" -e "INPUT_GENERATE-BRANCHES-BADGE" -e "INPUT_GENERATE-SUMMARY" -e "INPUT_JACOCO-CSV-FILE" -e "INPUT_COVERAGE-BADGE-FILENAME" -e "INPUT_BRANCHES-BADGE-FILENAME" -e "INPUT_GENERATE-COVERAGE-BADGE" -e "INPUT_COVERAGE-LABEL" -e "INPUT_BRANCHES-LABEL" -e "INPUT_ON-MISSING-REPORT" -e "INPUT_FAIL-IF-COVERAGE-LESS-THAN" -e "INPUT_FAIL-IF-BRANCHES-LESS-THAN" -e "INPUT_FAIL-ON-COVERAGE-DECREASE" -e "INPUT_FAIL-ON-BRANCHES-DECREASE" -e "INPUT_INTERVALS" -e "INPUT_COLORS" -e "INPUT_GENERATE-COVERAGE-ENDPOINT" -e "INPUT_GENERATE-BRANCHES-ENDPOINT" -e "INPUT_COVERAGE-ENDPOINT-FILENAME" -e "INPUT_BRANCHES-ENDPOINT-FILENAME" -e "INPUT_SUMMARY-FILENAME" -e "HOME" -e "GITHUB_JOB" -e "GITHUB_REF" -e "GITHUB_SHA" -e "GITHUB_REPOSITORY" -e "GITHUB_REPOSITORY_OWNER" -e "GITHUB_REPOSITORY_OWNER_ID" -e "GITHUB_RUN_ID" -e "GITHUB_RUN_NUMBER" -e "GITHUB_RETENTION_DAYS" -e "GITHUB_RUN_ATTEMPT" -e "GITHUB_REPOSITORY_ID" -e "GITHUB_ACTOR_ID" -e "GITHUB_ACTOR" -e "GITHUB_TRIGGERING_ACTOR" -e "GITHUB_WORKFLOW" -e "GITHUB_HEAD_REF" -e "GITHUB_BASE_REF" -e "GITHUB_EVENT_NAME" -e "GITHUB_SERVER_URL" -e "GITHUB_API_URL" -e "GITHUB_GRAPHQL_URL" -e "GITHUB_REF_NAME" -e "GITHUB_REF_PROTECTED" -e "GITHUB_REF_TYPE" -e "GITHUB_WORKFLOW_REF" -e "GITHUB_WORKFLOW_SHA" -e "GITHUB_WORKSPACE" -e "GITHUB_ACTION" -e "GITHUB_EVENT_PATH" -e "GITHUB_ACTION_REPOSITORY" -e "GITHUB_ACTION_REF" -e "GITHUB_PATH" -e "GITHUB_ENV" -e "GITHUB_STEP_SUMMARY" -e "GITHUB_STATE" -e "GITHUB_OUTPUT" -e "RUNNER_OS" -e "RUNNER_ARCH" -e "RUNNER_NAME" -e "RUNNER_ENVIRONMENT" -e "RUNNER_TOOL_CACHE" -e "RUNNER_TEMP" -e "RUNNER_WORKSPACE" -e "ACTIONS_RUNTIME_URL" -e "ACTIONS_RUNTIME_TOKEN" -e "ACTIONS_CACHE_URL" -e GITHUB_ACTIONS=true -e CI=true -v "/var/run/docker.sock":"/var/run/docker.sock" -v "/home/runner/work/_temp/_github_home":"/github/home" -v "/home/runner/work/_temp/_github_workflow":"/github/workflow" -v "/home/runner/work/_temp/_runner_file_commands":"/github/file_commands" -v "/home/runner/work/pcs-dotcras-webapp/pcs-dotcras-webapp":"/github/workspace" c9a4a5:cf7c06edd4384493bf0d4f88f418a56b "target/site/jacoco/jacoco.csv" "badges" "jacoco.svg" "branches.svg" "true" "true" "fail" "0" "0" "false" "false" "100 90 80 70 60 0" "#4c1 #97ca00 #a4a61d #dfb317 #fe7d37 #e05d44" "false" "false" "jacoco.json" "branches.json" "true" "coverage-summary.json" "coverage" "branches"
WARNING: Report file target/site/jacoco/jacoco.csv does not exist or glob target/site/jacoco/jacoco.csv is empty.
WARNING: No JaCoCo csv reports found.

Feature Req: edit workflow summary values

Is your feature request related to a problem? Please describe.
Not related to a problem, more of an improvement to the summary that is generated after a workflow run.

Describe the solution you'd like
I've would like to be able to change the values of the workflow summary. See attached screenshot.

We are generating multiple badges in one workflow, by using cicirello/jacoco-badge-generator@v2 multiple times on one workflow. And when the workflow is complete it posts a workflow summary with default values, but they all look the same since there is currently no way of customize the summary.

Describe alternatives you've considered
Have considered just using separate workflows for generating each badge, but we would still like to customize the values. And we can only find the option to completely turn it off.

Additional context
Screenshot:
image

Feature Req: How to use directly without github action?

Is your feature request related to a problem? Please describe.
Is not related to a problem. Is just the request of a piece of documentation in which you show us how to use it directly without github actions

Describe the solution you'd like
Maybe on a direct usage section in readme, you could add something like this:

python3 JacocoBadgeGenerator.py \
/foo/project/target/site/jacoco/jacoco.csv \
badges \
coverage.svg \
branches.svg \
true \
true \
fail \
0.1 \
0.1 \
false \
false \
"10,30,50,70,90,100" \
'#4c1 #97ca00 #a4a61d #dfb317 #fe7d37 #e05d44' \
true \
true \
coverage.json \
branches.json \
true \
summary.json

There were many attempts to figure out the relationship between 12 and 13 args (colors and intervals). Maybe this comments could be
improved to highlight that relationship

Describe alternatives you've considered
Maybe an extra test on https://github.com/cicirello/jacoco-badge-generator/blob/main/tests/tests.py with this direct usage.

Thanks!

Replace the use of GitHub Action's deprecated set-output

Describe the bug
GitHub Actions has deprecated the set-output workflow command, which we are currently using for workflow outputs of the action. See https://github.blog/changelog/2022-10-11-github-actions-deprecating-save-state-and-set-output-commands/. That same link indicates the replacement.

To Reproduce
Steps to reproduce the behavior:

  1. Run the action.
  2. Inspect the workflow run.
  3. Notice the deprecation warning.

Expected behavior
No deprecation warning.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.