Coder Social home page Coder Social logo

coverage-badge-creator's Introduction

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.

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.