From b980c16568fc451f5f696a380b1ad4460ec40bd0 Mon Sep 17 00:00:00 2001 From: Jacob Shufro <116244+jshufro@users.noreply.github.com> Date: Tue, 27 Aug 2024 14:46:03 -0400 Subject: [PATCH] Add coverage reports to github actions (#103) * Add comment workflow * Automatically generate and upload coverage reports for tests --- .coveragerc | 4 ++++ .github/workflows/comment.yml | 36 ++++++++++++++++++++++++++++ .github/workflows/runner.yml | 45 ++++++++++++++++++++++++++++++++++- .gitignore | 1 + 4 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 .coveragerc create mode 100644 .github/workflows/comment.yml diff --git a/.coveragerc b/.coveragerc new file mode 100644 index 00000000..33b18925 --- /dev/null +++ b/.coveragerc @@ -0,0 +1,4 @@ +[run] +relative_files = True +omit = + tests/** diff --git a/.github/workflows/comment.yml b/.github/workflows/comment.yml new file mode 100644 index 00000000..d5b5c3aa --- /dev/null +++ b/.github/workflows/comment.yml @@ -0,0 +1,36 @@ +# Safely comment on the PR - https://securitylab.github.com/resources/github-actions-preventing-pwn-requests/ +name: Comment on pull request +run-name: commenting on PR by ${{ github.actor }} +on: + workflow_run: + workflows: [ "ci-runner" ] + types: + - completed + +jobs: + comment: + runs-on: ubuntu-latest + if: > + github.event.workflow_run.event == 'pull_request' && + github.event.workflow_run.conclusion == 'success' + steps: + - name: Download artifacts + uses: actions/download-artifact@v4 + with: + name: results + run-id: ${{ github.event.workflow_run.id }} + github-token: ${{ secrets.GITHUB_TOKEN }} + + - name: 'Comment on PR' + uses: actions/github-script@v3 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + script: | + var fs = require('fs'); + var issue_number = Number(fs.readFileSync('./issue_number')); + await github.issues.createComment({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: issue_number, + body: fs.readFileSync('./coverage-text-report').toString() + }); diff --git a/.github/workflows/runner.yml b/.github/workflows/runner.yml index f2a01a25..7e2a2fbc 100644 --- a/.github/workflows/runner.yml +++ b/.github/workflows/runner.yml @@ -26,9 +26,52 @@ jobs: python -m pip install --upgrade pip pip install -r requirements.txt pip install -r requirements_test.txt + pip install coverage - name: Run tests - run: pytest tests + run: | + coverage run --data-file=.coverage.${{ matrix.os }}.${{ matrix.python-version }} -m pytest tests - name: Run type checker run: python -m mypy --config-file mypy.ini -p ethstaker_deposit - name: Run linter run: flake8 --config=flake8.ini ./ethstaker_deposit ./tests + - name: Upload coverage data + uses: actions/upload-artifact@v4 + with: + name: coverage-${{ matrix.os }}-${{ matrix.python-version }} + path: .coverage.${{ matrix.os }}.${{ matrix.python-version }} + retention-days: 1 + coverage-combine: + needs: ci-runner + if: github.event_name == 'pull_request' + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Download coverage data + uses: actions/download-artifact@v4 + - name: Merge coverage data + id: merge + run: | + python -m pip install --upgrade pip + pip install coverage + coverage combine coverage* + coverage html + - name: Upload final coverage html report + uses: actions/upload-artifact@v4 + id: upload-html + with: + name: coverage-html-report + path: htmlcov + - name: Generate text report + run: | + mkdir results + echo "Test Coverage: [Download HTML Report](${{ steps.upload-html.outputs.artifact-url }})" >> results/coverage-text-report + echo >> results/coverage-text-report + echo \`\`\` >> results/coverage-text-report + coverage report >> results/coverage-text-report + echo \`\`\` >> results/coverage-text-report + echo ${{ github.event.number }} > results/issue_number + - name: Upload final coverage text report + uses: actions/upload-artifact@v4 + with: + name: results + path: results/ diff --git a/.gitignore b/.gitignore index 740f3e6d..91998314 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ venv/ *.egg __pycache__ .DS_Store +.coverage