DEV Community

Anuj Kapoor
Anuj Kapoor

Posted on

Open Code-Coverage Bot

My Workflow

Background:

When you try to search for some code-coverage solution that can utilize Github actions, you will definitely stumble upon two major players Coedcov and Coveralls. But both of them are proprietary and comes with an extra cost attached to it. When you are working on a personal project or an early-stage startup you don't want to spend extra bucks for the same.

Action:

So I designed this action which makes use of action artifacts to generate coverage reports and comment them on pull-requests. It also has a feature to upload reports to a file server. I used the same for managing a very data-intensive Django project. With my test-suite workflow, I used coverage.py to generate reports. I store these reports in a text file in actions-artifacts and also upload them. There is a little preprocessing done on the report to make it comment friendly.

The whole process is divided into two parts:

  • Running tests and generating the report.
  • Modifying the report to send it as an action-bot comment, and uploading it.

Expected Outcome:
example

example2

the first part was fairly simple, I used coverage.py to run standard Django tests.you can find the syntax in the Yaml file below. For the second part, I created a new action repository open-coverage-action, It takes a text file as input and comments it on a pull-request. I am also working on adding preprocessing of the report to it. Currently, it uses basic bash to extract the part which contains the final result and add some polish, after that stores it in a new file for open-coverage-action. I also use file.io to upload a full report for further reference and also upload-artifact action to upload it as a an artifact.
it pretty much does the job required for my workflow.

Submission Category:

Maintainer Must-Haves

Yaml File or Link to Code

name: pr-coverage

on: pull_request

jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      max-parallel: 4
      matrix:
        python-version: [3.6]

    steps:
      - uses: actions/checkout@v2
      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v2
        with:
          python-version: ${{ matrix.python-version }}
      - name: Cache pip
        uses: actions/cache@v2
        with:
          # This path is specific to Ubuntu
          path: ~/.cache/pip
          # Look to see if there is a cache hit for the corresponding requirements file
          key: ${{ runner.os }}-pip-${{ hashFiles('requirements.txt') }}
          restore-keys: |
            ${{ runner.os }}-pip-
            ${{ runner.os }}-
      - name: Install Python Dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -r requirements.txt
      - name: Generating and Uploading Coverage Report
        run: |
          touch local.env
          echo DEBUG_VALUE=1 > local.env
          cd data_api/
          python manage.py makemigrations
          python manage.py migrate
          coverage run manage.py test --noinput --parallel 16
          coverage report > results.txt
          # Modifying coverage report
          echo "Coverage Report: " > report.txt
          echo " Statements | Miss | Cover " >> report.txt
          tail -4 results.txt >> report.txt
          echo "   " >> report.txt
          echo "Link to full report :" >> report.txt
          # uploading report to file.io any other services can be used
          curl -F "file=@results.txt" https://file.io |     python3 -c "import sys, json; print(json.load(sys.stdin)['link'])" >> report.txt
        env:
          SECRET_KEY: ${{ secrets.SECRET_KEY }}
          ADMIN_URL_KEY: ${{ secrets.ADMIN_URL_KEY }}
          DATABASE_NAME: github_actions
          DATABASE_USER: postgres
          DATABASE_PASSWORD: postgres
          DATABASE_HOST: 127.0.0.1
          DATABASE_PORT: 5432
      - uses: actions/upload-artifact@v2
        with:
          name: report
          path: data_api/report.txt
      - uses: actions/download-artifact@v2
        with:
          name: report
      - name: Open Coverage Notifier Action
        uses: Bearbobs/open-coverage-action@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          path: data_api/report.txt
Enter fullscreen mode Exit fullscreen mode

Full file can be found in a gist @ workflow

Additional Resources / Info

This is just my first take on this idea. I am planning to reiterate on the preprocessing of the reports, to make it more polished. Currently, this workflow can be directly used by any Django project maintainers. Special thanks to file.io for free text uploading service and Github actions. please refer to the open-coverage-action repository for further reference.
Please reach out for suggestions and feedbacks. Thank You.

Links to the services used:

Open-Coverage-Action

File.io

Coverage.py

Top comments (0)