From 45ca6dfa3cb7f5cdcb7eec74c9ec1311ac71a282 Mon Sep 17 00:00:00 2001 From: Mathieu Lamiot Date: Tue, 27 Aug 2024 15:16:23 +0200 Subject: [PATCH] add diff-cover to CI --- .github/workflows/ci-on_pr_main_bash.yml | 116 ++++++++++++++++++++--- requirements.txt | 4 +- 2 files changed, 108 insertions(+), 12 deletions(-) diff --git a/.github/workflows/ci-on_pr_main_bash.yml b/.github/workflows/ci-on_pr_main_bash.yml index c3fe5c0..6e4d9c7 100644 --- a/.github/workflows/ci-on_pr_main_bash.yml +++ b/.github/workflows/ci-on_pr_main_bash.yml @@ -4,10 +4,6 @@ name: CI Script - Python Lint and tests on: - push: - branches: - - 'develop' - - 'master' pull_request: branches: - 'develop' @@ -26,8 +22,26 @@ env: TBTT_NOTION_API_KEY: e jobs: - lint-and-test: + lint: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: "3.11" + cache: "pip" + + - name: Install dependencies + run: pip install -r requirements.txt + + - name: Run lint script + run: | + ./scripts/lint.sh + shell: bash + + tests: runs-on: ubuntu-latest strategy: fail-fast: false @@ -43,10 +57,90 @@ jobs: - name: Install dependencies run: | python -m pip install --upgrade pip - if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - python -m pip install flake8 pytest - - name: Run ci-on_pr_main script + pip install -r requirements.txt + + - name: Run pytest run: | - chmod +x ./scripts/ci-on_pr_main.sh - ./scripts/ci-on_pr_main.sh - shell: bash \ No newline at end of file + pytest -m "not staging_env" --cov=. --cov-report=xml + shell: bash + + - name: Upload coverage report + uses: actions/upload-artifact@v3 + with: + name: coverage-report-${{ github.run_id }} + path: coverage.xml + retention-days: 1 + + coverage: + runs-on: ubuntu-latest + needs: tests + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 # Fetch all history for all branches + + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: "3.11" + cache: "pip" + + - name: Download coverage report + uses: actions/download-artifact@v3 + with: + name: coverage-report-${{ github.run_id }} + + - name: Install dependencies + run: pip install git+https://github.com/MathieuLamiot/diff_cover + + - name: Generate diff-coverage report + if: github.event_name == 'pull_request' + run: | + diff-cover coverage.xml --compare-branch=origin/${{ github.base_ref }} --markdown-report diff-cover-report.md --exclude test*.py --fail-under=50 --expand_coverage_report + echo "DIFF_COVER_EXIT_STATUS=$?" >> $GITHUB_ENV + shell: bash + + - name: Delete previous diff-cover reports + uses: actions/github-script@v6 + with: + script: | + const { data: comments } = await github.rest.issues.listComments({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: context.issue.number + }); + + for (const comment of comments) { + if (comment.user.login === 'github-actions[bot]' && comment.body.includes('# Diff Coverage')) { + console.log(`Deleting comment with ID: ${comment.id}`); + await github.rest.issues.deleteComment({ + owner: context.repo.owner, + repo: context.repo.repo, + comment_id: comment.id + }); + } + } + env: + GITHUB_TOKEN: ${{ secrets.DIFF_COVER_COMMENT }} + + - name: Post diff-cover report to PR + if: github.event_name == 'pull_request' + uses: actions/github-script@v6 + with: + script: | + const fs = require('fs'); + const comment = fs.readFileSync('diff-cover-report.md', 'utf8'); + await github.rest.issues.createComment({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + body: comment, + }); + - name: Fail job if coverage is below threshold + if: github.event_name == 'pull_request' + run: | + if [[ "${{ env.DIFF_COVER_EXIT_STATUS }}" -ne 0 ]]; then + echo "Coverage below threshold; failing the job." + exit 1 + fi + shell: bash \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 297bcbe..f2fa7aa 100644 --- a/requirements.txt +++ b/requirements.txt @@ -50,4 +50,6 @@ Werkzeug==2.3.6 wrapt==1.15.0 yarl==1.9.2 flask-slacksigauth==1.0.9 -freezegun==1.2.2 \ No newline at end of file +freezegun==1.2.2 +pytest-cov==5.0.0 +diff-cover==9.1.1 \ No newline at end of file