Skip to content

[maintenance] Add coverage report in PR's comment #7

[maintenance] Add coverage report in PR's comment

[maintenance] Add coverage report in PR's comment #7

Workflow file for this run

name: Python Unittest Coverage
on: [push, pull_request]
permissions:
issues: write
pull-requests: write
jobs:
test:
if: false == contains(github.event.pull_request.title, 'WIP')
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest]
python-version: ['3.9']
env:
OS: ${{ matrix.os }}
PYTHON: '3.9'
timeout-minutes: 20
steps:
- uses: actions/checkout@master
- name: Setup Python ${{ matrix.python-version }}
uses: actions/setup-python@master
with:
python-version: ${{ matrix.python-version }}
- name: Install Dependencies
run: |
pip install -q -e .[full]
- name: Run tests with coverage
run: |
coverage run tests/run.py
- name: Generate coverage report
run: |
coverage report -m > coverage.txt
- name: Update or Post coverage report as PR comment
if: github.event_name == 'pull_request'
uses: actions/github-script@v3
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
const fs = require('fs');
const report = fs.readFileSync('coverage.txt', {encoding:'utf8', flag:'r'});
const commentIdentifier = '<!-- coverage-comment -->';
const commentBody = `${commentIdentifier}\n📊 **Coverage Report**:\n\`\`\`\n${report}\n\`\`\``;
const issue_number = context.issue.number;
const comments = await github.issues.listComments({
...context.repo,
issue_number
});
const coverageComment = comments.data.find(comment => comment.body.includes(commentIdentifier));
if (coverageComment) {
await github.issues.updateComment({
...context.repo,
comment_id: coverageComment.id,
body: commentBody
});
} else {
await github.issues.createComment({
...context.repo,
issue_number,
body: commentBody
});
}