[maintenance] Add coverage report in PR's comment #17
Workflow file for this run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Python Unittest Coverage | |
on: [push, pull_request] | |
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: | |
token: ${{secrets.CODE_COV_PAT}} | |
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 | |
}); | |
} |