GithubHelp home page GithubHelp logo

fgrosse / go-coverage-report Goto Github PK

View Code? Open in Web Editor NEW
40.0 2.0 2.0 924 KB

A CLI tool and GitHub Action to post Go code coverage reports as comment to your pull requests.

License: BSD 3-Clause "New" or "Revised" License

Go 75.44% Shell 24.56%
actions code-coverage golang

go-coverage-report's Introduction

Go Coverage Report

A CLI tool and GitHub Action to post Go code coverage reports as comment to your pull requests.


go-coverage-report is a command line tool and a GitHub Action that parses code coverage results to create a tabular code coverage report. This markdown report is intended as pull request comment both to highlight the impact of the changes on the code coverage on all updated packages and to motivate the contributors to write tests for their code.

The provided tool and GitHub Action work without any third-party services which makes them fast, secure and very easy-to-setup in your own CI/CD pipeline.

Example

Example of a pull request comment created by go-coverage-report:

Example of a pull request comment created by go-coverage-report

Please note that by default, the "Coverage by file" section is collapsed so the focus is more on the overall impact on the coverage per package.

There is no indicator of the total coverage of the project because the impact by changed package is typically a better indicator of the quality of the changes. Additionally, looking at coverage changes by package is more motivating as code coverage improvements are more pronounced (i.e. typically larger percentage values) when zooming into the package level, instead of comparing changes on the project level.

The last column is an emoji "score" that is based on the coverage change of the package. The following emojis are used:

  • 🌟 - The coverage of the package increased by > 20%
  • 🎉 - The coverage of the package increased by <= 20%
  • 👍 - The coverage of the package increased by <= 10%
  • 👎 - The coverage of the package decreased by <= 10%
  • 💀 - The coverage of the package decreased by > 10%, every 10% add another skull (up to five skulls)

Usage

The go-coverage-report tool ships with a GitHub Action that you can easily include in your own Workflows:

name: CI

# This setup assumes that you run the unit tests with code coverage in the same
# workflow that will also print the coverage report as comment to the pull request. 
# Therefore, you need to trigger this workflow when a pull request is (re)opened or
# when new code is pushed to the branch of the pull request. In addition, you also
# need to trigger this workflow when new code is pushed to the main branch because 
# we need to upload the code coverage results as artifact for the main branch as
# well since it will be the baseline code coverage.
# 
# We do not want to trigger the workflow for pushes to *any* branch because this
# would trigger our jobs twice on pull requests (once from "push" event and once
# from "pull_request->synchronize")
on:
  pull_request:
    types: [opened, reopened, synchronize]
  push:
    branches:
      - 'main'

jobs:
  unit_tests:
    name: "Unit tests"
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Setup Go
        uses: actions/setup-go@v4
        with:
          go-version: ^1.22

      # When you execute your unit tests, make sure to use the "-coverprofile" flag to write a 
      # coverage profile to a file. You will need the name of the file (e.g. "coverage.txt")
      # in the next step as well as the next job.
      - name: Test
        run: go test -cover -coverprofile=coverage.txt ./...

      - name: Archive code coverage results
        uses: actions/upload-artifact@v4
        with:
          name: code-coverage
          path: coverage.txt # Make sure to use the same file name you chose for the "-coverprofile" in the "Test" step

  code_coverage:
    name: "Code coverage report"
    if: github.event_name == 'pull_request' # Do not run when workflow is triggered by push to main branch
    runs-on: ubuntu-latest
    needs: unit_tests # Depends on the artifact uploaded by the "unit_tests" job
    steps:
      - uses: fgrosse/[email protected] # Consider using a Git revision for maximum security
        with:
          coverage-artifact-name: "code-coverage" # can be omitted if you used this default value
          coverage-file-name: "coverage.txt" # can be omitted if you used this default value

Inputs

inputs:
  version:
    description: 'The exact version of the go-coverage-report tool to use.'
    required: true
    default: "v1.0.1"

  sha256sum:
    description: 'Optional SHA256 checksum of the tarball when downloading the go-coverage-report binary.'
    required: false

  coverage-artifact-name:
    description: 'The name of the artifact containing the code coverage results.'
    required: true
    default: "code-coverage"

  coverage-file-name:
    description: 'The name of the file containing the code coverage results.'
    required: true
    default: "coverage.txt"

  root-package:
    description: |
      The Go import path of the tested repository to add as a prefix to all paths of the
      changed files. This is useful to map the changed files (e.g., ["foo/my_file.go"]
      to their coverage profile which uses the full package name to identify the files
      (e.g., "github.com/fgrosse/example/foo/my_file.go"). Note that currently, 
      packages with a different name than their directory are not supported.
    required: false
    default: "github.com/${{ github.repository }}"

  trim:
    description: Trim a prefix in the "Impacted Packages" column of the markdown report.
    required: false

Outputs

This action does not provide any outputs, but it will comment on your pull request with the summary of the code coverage changes.

Limitations

  • Currently, code coverage profiles are uploaded as GitHub artifacts which automatically expire after 90 days. In a repository which receives changes only infrequently, this might lead to issues when trying to compare the code coverage of a pull request with the code coverage of the main branch (see #5).
  • Packages with a name that differs from their directory on disk are not supported yet.

Built With

Contributing

Please read CONTRIBUTING.md for details on our code of conduct and on the process for submitting pull requests to this repository.

Versioning

We use SemVer for versioning. All significant (e.g. breaking) changes are documented in the CHANGELOG.md. A list of all available versions can be found at the releases page.

Authors

  • Friedrich Große - Initial work - fgrosse

  • See also the list of contributors who participated in this project.

License

This project is licensed under the BSD-3-Clause License - see the LICENSE file for details.

go-coverage-report's People

Contributors

fgrosse avatar

Stargazers

Nathan Macnamara avatar  avatar Aleksander Piotrowski avatar Alex Truesdale avatar Tim Bart avatar Allan Shone avatar Łukasz Rżanek avatar beardo avatar Joey Sabey avatar Sven Hohlfeld avatar Ba'tiar Afas Rahmamulia avatar Jared Horvat avatar Dave Henderson avatar Radik Ilin avatar  avatar Orkan Alat avatar Nick Robinson-Wall avatar Irfan Shiddiq avatar Olivier Mengué avatar WillAbides avatar  avatar Mark Wolfe avatar Brandon Mitchell avatar  avatar Sevdalin Sabev avatar Robert Focke avatar Jorge Fuertes Alfranca avatar Joel Edström avatar Itamar Perez avatar Arne Jørgensen avatar José Solana avatar Sertaç Özercan avatar ipfans avatar Dan Wolf avatar Andrés avatar Alessandro Gambin da Silva avatar Steve Coffman avatar Joshua Smith avatar Graham Steffaniak avatar  avatar

Watchers

 avatar  avatar

go-coverage-report's Issues

Looking for artifact to fetch is too strict

Hello! Great action, but I noticed small issue that breaks my setup :D

You are doing this in action:

gh run list --status=success --branch=main '--workflow=Lint and Test' --event=push --json=databaseId --limit=1 -q '.[] | .databaseId'

However in setup I am not using I only run job on cron-schedule, so I do not have --event=push there at all. Is this option configurable maybe?

PS: I know I will lose some accuracy here because of that 😓

No coverage report posted when only _test.go files changed.

We're using this action on several private repositories and it is working great so far. Thanks.

However, I made a PR today that only changed a single _test.go file and no coverage report was added to the PR. The changed test file was noticed by the first steps of the action but seems to get filtered away sometime later. Here are the logs (somewhat redacted).

Run tj-actions/changed-files@aa08304bd477b800d468db44fe10f6c61f7f7b11 <snip>
changed-files
  Warning: The current working directory is not inside a git repository: <snip>
  Using GitHub's REST API to get changed files
  Getting changed files from GitHub API...
  Found 1 changed files from GitHub API
  All Done!
changed-files-patterns
  All Done!
Run $GITHUB_ACTION_PATH/scripts/github-action.sh <snip>
Download code coverage results from current run
Download code coverage results from target branch
Compare code coverage results
  + go-coverage-report -root=<snip> -trim= .github/outputs/old-coverage.txt .github/outputs/new-coverage.txt .github/outputs/all_changed_files.json
  Skipping report since there are no changed files
  + end_group
  Notice: No coverage report to comment

Failure to retrieve artifacts

The action is failing for me, apparently because there are no artifacts available in the target branch (not surprising because this is the first attempt to run it).

Download code coverage results from target branch
  ++ gh run list --status=success --branch=main --workflow=Build --event=push --json=databaseId --limit=1 -q '.[] | .databaseId'
  + LAST_SUCCESSFUL_RUN_ID=8953089596
  + '[' -z 8953089596 ']'
  + gh run download 8953089596 --name=code-coverage --dir=.github/outputs
  no valid artifacts found to download
  Error: Process completed with exit code 1.

Is there some set up/configuration step that I've missed?

Thanks,

Rob.

Coverage $ and Δ display 0 in action comment

As title says, when I run the provided sample workflow as part of my CI pipeline a comment is successfully generated, however, the comment does not display the correct information.
Capture

I also cloned the go-coverage-report repo (Robert-MacWha#1), removed some tests, and ran the CI workflow. This also resulted in no coverage info being displayed in the comment. Interestingly, when I logged the code coverage results, it seemed to correctly generate the oldCov and newCov structs.

+ go-coverage-report -root=github.com/Robert-MacWha/go-coverage-report -trim=github.com/fgrosse/go-coverage-report/ .github/outputs/old-coverage.txt .github/outputs/new-coverage.txt .github/outputs/all_changed_files.json
  Old Coverage: &{Files:map[github.com/fgrosse/go-coverage-report/cmd/go-coverage-report/changed_files.go:0xc00006e050 github.com/fgrosse/go-coverage-report/cmd/go-coverage-report/coverage.go:0xc00006e0a0 github.com/fgrosse/go-coverage-report/cmd/go-coverage-report/main.go:0xc00006e0f0 github.com/fgrosse/go-coverage-report/cmd/go-coverage-report/profile.go:0xc00006e140 github.com/fgrosse/go-coverage-report/cmd/go-coverage-report/report.go:0xc00006e190] TotalStmt:336 CoveredStmt:200 MissedStmt:136}
  New Coverage: &{Files:map[github.com/fgrosse/go-coverage-report/cmd/go-coverage-report/changed_files.go:0xc00006e1e0 github.com/fgrosse/go-coverage-report/cmd/go-coverage-report/coverage.go:0xc00006e230 github.com/fgrosse/go-coverage-report/cmd/go-coverage-report/main.go:0xc00006e280 github.com/fgrosse/go-coverage-report/cmd/go-coverage-report/profile.go:0xc00006e2d0 github.com/fgrosse/go-coverage-report/cmd/go-coverage-report/report.go:0xc00006e320] TotalStmt:339 CoveredStmt:85 MissedStmt:254}
  Changed Files: [github.com/Robert-MacWha/go-coverage-report/cmd/go-coverage-report/coverage_test.go github.com/Robert-MacWha/go-coverage-report/cmd/go-coverage-report/main.go github.com/Robert-MacWha/go-coverage-report/cmd/go-coverage-report/report_test.go]

I'll see if I can fix things, if so will make a quick PR.

Action does not work. Always creates comment with "Merging this branch will not change overall coverage"

image

Hello! I decided to try your Action, unfortunatelly for some reason it always shows 0 on every touched file.

This is how I am using your action. I have this 3 steps in 1 job:

      - name: generate test coverage
        run: go test -cover -coverprofile=coverage.txt ./...

      - name: Archive code coverage results
        uses: actions/upload-artifact@v4
        with:
          name: code-coverage
          path: coverage.txt
          retention-days: 2

      - uses: fgrosse/[email protected] 
        if: ${{ github.event_name == 'pull_request' }}
        with:
          coverage-artifact-name: "code-coverage"
          coverage-file-name: "coverage.txt" 

No permission to create comment

While trying out this action, I'm encountering the error: "GraphQL: Resource not accessible by integration (addComment)".

I tried passing the GITHUB_TOKEN environment variable, setting the pull-requests: write permission on the workflow rather than only the job, and adding the issues: write permission, but so far no luck. My best guess is that the gh command is running in the context of your repository instead of my own.

Here's an example run: https://github.com/regclient/regclient/actions/runs/8484759635/job/23248287252?pr=706

(Note that I have continue-on-error: true defined on the job so that it always shows as successful.)

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.