chore: updates coverage GH action to compare coverage against base branch #126
Workflow file for this run
This file contains hidden or 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: Coverage Check | |
| on: | |
| pull_request: | |
| concurrency: | |
| group: coverage-${{ github.event.pull_request.number }} | |
| cancel-in-progress: true | |
| env: | |
| COVERAGE_SENSITIVITY_PERCENT: 1 | |
| MAINNET_RPC: ${{ secrets.MAINNET_RPC }} | |
| SEPOLIA_RPC: ${{ secrets.SEPOLIA_RPC }} | |
| jobs: | |
| upload-coverage: | |
| name: Upload Coverage | |
| runs-on: ubuntu-latest | |
| permissions: | |
| actions: read | |
| contents: read | |
| pull-requests: write | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Install Foundry | |
| uses: foundry-rs/foundry-toolchain@v1 | |
| with: | |
| version: stable | |
| - name: Use Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: 20.x | |
| cache: "yarn" | |
| - name: Install dependencies | |
| run: yarn --frozen-lockfile --network-concurrency 1 | |
| - name: Run coverage | |
| shell: bash | |
| run: yarn coverage | |
| - name: Setup LCOV | |
| uses: hrishikesh-kadam/setup-lcov@v1 | |
| - name: Get previous successful run id (PR branch) | |
| id: prev-run | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| const {owner, repo} = context.repo; | |
| // Derive workflow file name from GITHUB_WORKFLOW_REF ("<owner>/<repo>/.github/workflows/coverage.yml@<ref>") | |
| const wfRef = process.env.GITHUB_WORKFLOW_REF || ''; | |
| const wfPath = wfRef.split('@')[0].split('/').slice(2).join('/'); | |
| const workflow = wfPath.split('/').pop(); // e.g., coverage.yml | |
| const headBranch = context.payload.pull_request.head.ref; | |
| const runs = await github.rest.actions.listWorkflowRuns({ | |
| owner, | |
| repo, | |
| workflow_id: workflow, | |
| event: 'pull_request', | |
| branch: headBranch, | |
| status: 'success', | |
| per_page: 10 | |
| }); | |
| let selectedRunId = ''; | |
| for (const r of runs.data.workflow_runs) { | |
| if (r.id >= context.runId) continue; | |
| const artifacts = await github.rest.actions.listWorkflowRunArtifacts({ owner, repo, run_id: r.id }); | |
| if (artifacts.data.artifacts.some(a => a.name === 'coverage.info' && !a.expired)) { | |
| selectedRunId = String(r.id); | |
| break; | |
| } | |
| } | |
| core.setOutput('run-id', selectedRunId); | |
| - name: Capture coverage output | |
| id: new-coverage | |
| uses: zgosalvez/github-actions-report-lcov@v4 | |
| with: | |
| coverage-files: lcov.info | |
| github-token: ${{ github.token }} | |
| update-comment: true | |
| - name: Retrieve previous coverage | |
| uses: actions/download-artifact@v4 | |
| if: steps.prev-run.outputs.run-id != '' | |
| with: | |
| name: coverage.info | |
| run-id: ${{ steps.prev-run.outputs.run-id }} | |
| repository: ${{ github.repository }} | |
| continue-on-error: true | |
| - name: Check if a previous coverage exists | |
| run: | | |
| if [ ! -f coverage.info ]; then | |
| echo "Previous coverage artifact not found; baseline will be 0%" | |
| fi | |
| # Normalize artifact download path (v4 downloads into a folder named after the artifact) | |
| if [ -d coverage.info ] && [ -f coverage.info/coverage.info ]; then | |
| mv -f coverage.info/coverage.info coverage.info | |
| fi | |
| - name: Compare previous coverage | |
| run: | | |
| if [ -f coverage.info ]; then | |
| old=$(lcov --summary coverage.info 2>/dev/null | awk '/lines\.+:/ {print $2}' | tr -d '%') | |
| else | |
| old=0 | |
| fi | |
| new_total=${{ steps.new-coverage.outputs.total-coverage }} | |
| new=$(awk -v a="$new_total" -v b="${{ env.COVERAGE_SENSITIVITY_PERCENT }}" 'BEGIN{printf "%.2f", a+b}') | |
| if awk "BEGIN {exit !($new < $old)}"; then | |
| echo "Coverage decreased from $old to $new" | |
| exit 1 | |
| fi | |
| cp lcov.info coverage.info | |
| - name: Upload the new coverage | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: coverage.info | |
| path: ./coverage.info |