add text coverage to PR comment #610
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: Run tests | |
on: [pull_request] | |
jobs: | |
test: | |
runs-on: ubuntu-latest | |
strategy: | |
matrix: | |
postgres_version: ['14', '16'] | |
services: | |
postgres: | |
image: postgres:${{ matrix.postgres_version }} | |
env: | |
POSTGRES_DB: ndoh_hub | |
POSTGRES_USER: postgres | |
POSTGRES_PASSWORD: postgres | |
ports: | |
- 5432:5432 | |
redis: | |
image: redis:6.0 | |
ports: | |
- 6379:6379 | |
env: | |
HUB_DATABASE: postgres://postgres:postgres@localhost:5432/ndoh_hub | |
steps: | |
- uses: actions/checkout@v2 | |
- name: Install gettext | |
run: sudo apt-get install gettext | |
- uses: actions/cache@v2 | |
with: | |
path: ~/.cache/pip | |
key: ${{ hashFiles('requirements.txt', 'requirements-dev.txt') }}-pip | |
- uses: actions/setup-python@v2 | |
with: | |
python-version: 3.9 | |
- name: Install dependancies | |
run: | | |
python -m pip install --upgrade pip | |
pip install -r requirements.txt -r requirements-dev.txt | |
django-admin compilemessages | |
- name: Lint | |
run: | | |
flake8 | |
python manage.py makemigrations registrations changes eventstore \ | |
--dry-run | grep 'No changes detected' || \ | |
(echo 'There are changes which require migrations.' && exit 1) | |
black --check . | |
isort -c -rc . | |
- name: Run tests and generate coverage report | |
run: | | |
py.test --cov --cov-report=term-missing | tee coverage.txt | |
- name: Get changed lines in the PR | |
id: get_diff | |
run: | | |
git fetch origin main | |
git diff origin/main --unified=0 > diff.txt | |
grep '^@@' diff.txt | awk '{print $2}' > changed_lines.txt | |
- name: Extract untested new lines | |
id: untested_new_code | |
run: | | |
COVERAGE_FILE=coverage.txt | |
DIFF_FILE=changed_lines.txt | |
UNTTESTED_LINES="" | |
# Loop through the changed lines to find untested lines | |
while IFS= read -r LINE; do | |
LINE_NUM=$(echo "$LINE" | grep -oP '\d+') | |
UNTTESTED_LINE=$(grep -E "^ *$LINE_NUM " $COVERAGE_FILE | grep 'missed') | |
if [ -n "$UNTTESTED_LINE" ]; then | |
UNTTESTED_LINES="$UNTTESTED_LINES\n$UNTTESTED_LINE" | |
fi | |
done < $DIFF_FILE | |
echo "UNTTESTED_LINES=$UNTTESTED_LINES" >> $GITHUB_ENV | |
- name: Post coverage comment on PR | |
uses: actions/github-script@v6 | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
const untested = process.env.UNTTESTED_LINES; | |
const prNumber = context.payload.pull_request.number; | |
const commentBody = `Untested new code:\n${untested || "No untested new code found"}`; | |
await github.rest.issues.createComment({ | |
issue_number: prNumber, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
body: commentBody | |
}); |