Skip to content

infra: Add a code coverage % report via Check Runs API #469

infra: Add a code coverage % report via Check Runs API

infra: Add a code coverage % report via Check Runs API #469

Workflow file for this run

name: Pytest
on:
push:
branches: [main]
pull_request: {}
jobs:
pytest:
environment: development
runs-on: ubuntu-latest
env:
COHERE_API_KEY: ${{ secrets.COHERE_API_KEY }}
DATABASE_URL: ${{ secrets.DATABASE_URL }}
NEXT_PUBLIC_API_HOSTNAME: ${{ secrets.NEXT_PUBLIC_API_HOSTNAME }}
steps:
- name: Checkout repo
uses: actions/checkout@v3
- name: Setup Docker
run: docker compose up backend test_db -d
- name: Run tests
run: docker compose run --name backend-container --build -e COHERE_API_KEY=$COHERE_API_KEY backend poetry run pytest --cov=src/backend --cov-report=xml:coverage.xml src/backend/tests/
- name: List running containers [remove me]
run: docker ps -a
- name: Copy coverage report to host
run: |
container_id=$(docker ps -aqf "name=backend-container")
echo "Container ID: $container_id"
docker cp $container_id:/app/coverage.xml .
- name: Parse and send coverage to PR
run: |
pip install xmltodict
coverage_xml=$(cat coverage.xml)
coverage_percentage=$(python -c "import xmltodict; report = xmltodict.parse('$coverage_xml'); totals = report['coverage']['@lines-valid']; covered = report['coverage']['@lines-covered']; print(100 * int(covered) / int(totals))")
echo "COVERAGE_PERCENTAGE=${coverage_percentage}" >> $GITHUB_ENV
- name: Send coverage to GitHub PR
uses: actions/github-script@v3
with:
script: |
const percentage = process.env.COVERAGE_PERCENTAGE;
const context = github.context;
const { owner, repo } = context.repo;
const pull_request = context.issue;
await github.rest.checks.create({
owner,
repo,
name: 'Coverage Report',
head_sha: context.payload.pull_request.head.sha,
status: 'completed',
conclusion: 'success',
output: {
title: 'Coverage Report',
summary: `Current code test coverage is ${percentage}%`,
},
});