A GiHub Action to generate a comment within Pull Requests with the latest code coverage metrics for that branch.
This action may be called from within any workflow that produces a lcov.info file. Generally, this would be created by the test engine of whatever programming language is being used. The code coverage action exposes the following inputs for customizing the report.
-
This is the permissions token passed from the calling workflow. It is used to call the GitHub API and add the coverage report to the Pull Request.
Default: The
github.tokeninherited from the calling workflow. Ensure the permissions for the job are set so that this action can leave comments in the pull request. -
This is the path to the
lcov.info(or whatever name you give it - it must be in the format of alcov.infofile) from the project's root directory.Default:
coverage/lcov.info -
This is the targeted percentage of code coverage. Any coverage percentage less than this (in a single file, across a directory, or across the entire project) will be marked with a ' ⛔ '. Any coverage within 10% of the threshold will be marked with a '
⚠️ '. Finally, any percentage greater than that will be marked with a ' ✅ '.Example:
coverage_threshold: 80- 0% - 79% is marked with ⛔
- 80% - 88% is marked with
⚠️ - 89% - 100% is marked with ✅
Default: 80
-
This is a regex pattern to denote the sorts of files that require test coverage. For example, in a Dart project, this would be all the files that have the
.dartsuffix.This value is required.
-
This is a regex pattern to denote any files that should be ignored by the coverage calculation.
Again, taking Dart as an example, all the test files end with
.dart, but we don't want those files crushing the test coverage metrics (why would we write tests to cover our tests? Then we'd need tests for those tests, and now we're creating an infinity of tests...). Because all the Dart tests live in thetest/directory, we can add a pattern here that excludes any filepath withtest/in it.Note that any file appearing in the
lcov.inforeport should never be matched by this pattern, or else the action will fail.This value is required.
name: Some workflow
on:
[your workflow triggers]
jobs:
a-job:
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
...
- name: Run tests with coverage
run: <some command that generates the lcov.info file>
- name: Generate coverage report
uses: fermi-ad/code-coverage-reporter@<release tag or commit hash>
with:
include_pattern: <some regex matching files to report coverage for>
exclude_pattern: <some regex matching files that would be included, but are not intended for code coverage reporting>
...
This is a JavaScript GitHub action, with index.js as its entry point. To ensure dependencies are correctly aligned for use within CI/CD pipelines, the "development" code must be packaged into a "production" index.js.
All source code is within the src/ directory, and the executed code is compiled to dist/index.js. If any changes are made in src/, a new dist/index.js must be built.
To package the dist/index.js, use esbuild or ncc. The steps are as follows:
This action makes use of GitHub's @actions/core and @actions/github libraries. Bring them into the development environment using
npm install @actions/core @actions/github
The next step is to install the packaging tool you'll use. Here, we'll use @vercel/ncc. Install it with
npm install -g @vercel/ncc
Generate the executable JS file with
ncc build src/index.js --out dist