Skip to content
This repository has been archived by the owner on Oct 4, 2023. It is now read-only.

Github action for creating coverage badges on S3

License

Notifications You must be signed in to change notification settings

Orfium/coverage-badge-creator

Repository files navigation

coverage-badge-creator

Create a coverage badge without third parties as proposed in https://itnext.io/github-actions-code-coverage-without-third-parties-f1299747064d.

Description

This actions performs the following tasks sequentially:

  • reads a file containing the coverage information
  • finds the coverage percentage using a specified regex
  • calls shields.io API in order to download a proper badge based on the parsed coverage.
  • uploads the badge in an AWS S3 bucket,

Usage

An example usage of the action is the following:

Sample Workflow

name: CI

on:
  push:
    branches:

jobs:
    unittest:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Code Repository
        uses: actions/checkout@v2
      - name: Build the Stack
        run:  docker-compose -f local.yml build
      - name: Make DB Migrations
        run:  docker-compose -f local.yml run --rm django python manage.py migrate
      - name: Run the Stack
        run:  docker-compose -f local.yml up -d
      - name: Run Django Tests
        run: docker-compose -f local.yml exec -T django coverage run --rcfile=.pre-commit/setup.cfg -m pytest --disable-pytest-warnings;
      - name: Print Coverage
        run: docker-compose -f local.yml exec -T django coverage report > coverage.txt
      - name: Tear down the Stack
        run: docker-compose -f local.yml down
      - name: Update Coverage badge
        uses: Orfium/coverage-badge-creator@master
        with:
          coverage_file: "coverage.txt"
          badge_name: "code-cov-prod.svg"
          upload_coverage_file: true
          bucket_name: "orfium-badges-bucket"
          aws_access_key: ${{ secrets.BADGES_AWS_ACCESS_KEY_ID }}
          aws_secret_key: ${{ secrets.BADGES_AWS_SECRET_ACCESS_KEY }}
          coverage_percentage_regex: ${{ secrets.COVERAGE_PERCENTAGE_REGEX }}
        if: github.ref == 'refs/heads/develop'

Input Variables

Name Description Default Required
coverage_file Path to coverage file (txt or json reports) - Yes
badge_name Name of the badge file - Yes
upload_coverage_file Upload coverage file too False No
bucket_name Name of the bucket to upload the badge to - Yes
aws_access_key AWS Access Key - Yes
aws_secret_key AWS Secret Key - Yes
coverage_percentage_regex Regex to use in order to get the coverage - No
coverage_percentage_json_path Path to use in order to get the coverage - No

At least one of the arguments coverage_percentage_regex or coverage_percentage_json_path must be set in order for the action to run based on the format of the coverage report.

Coverage Percentage Regex Examples

Python Coverage Report

Coverage Report sample (txt format):

...
---------------------------------------------------------------------------------------------------
TOTAL                                                                            3240      0   100%

Generated by running coverage report > coverage.txt

The coverage_percentage_regex should be set as shown below:

  coverage_percentage_regex: "TOTAL\s+\d+\s+\d+\s+(\d+)%$"

Coverage Report sample (json format):

{
  "totals": {
    "covered_lines": 3018,
    "num_statements": 3240,
    "percent_covered": 93.14814814814815,
    "percent_covered_display": "93",
    "missing_lines": 222,
    "excluded_lines": 34
  }
}

Generated by running coverage json

The coverage_percentage_json_path should be set as shown below:

  coverage_percentage_json_path: "totals.percent_covered_display"

or

  coverage_percentage_json_path: "totals/percent_covered_display"

JavaScript Coverage Report

Coverage Report sample:

{
  "total": {
    "lines": {
      "total": 1649,
      "covered": 1466,
      "skipped": 0,
      "pct": 88.9
    },
    "statements": {
      "total": 1692,
      "covered": 1499,
      "skipped": 0,
      "pct": 88.59
    },
    "functions": {
      "total": 698,
      "covered": 590,
      "skipped": 0,
      "pct": 84.53
    },
    "branches": {
      "total": 751,
      "covered": 568,
      "skipped": 0,
      "pct": 75.63
    }
  }
}

Generated by running yarn test --watchAll=false --coverage --coverageReporters=json-summary

The coverage_percentage_json_path for coverage of lines should be set as shown below:

  coverage_percentage_json_path: "total.lines.pct"

or

  coverage_percentage_json_path: "total/lines/pct"

Respectively, you could run the action for statements, functions and branches coverage.