|
| 1 | +name: 📊 Tests & Coverage |
| 2 | + |
| 3 | +# Configuration |
| 4 | +# Set to 'true' for monorepo, 'false' for single repository |
| 5 | +env: |
| 6 | + MONOREPO: 'true' |
| 7 | + BASE_BRANCH: 'main' |
| 8 | + |
| 9 | +on: |
| 10 | + push: |
| 11 | + branches: |
| 12 | + - main |
| 13 | + pull_request: |
| 14 | + types: [opened, synchronize, reopened] |
| 15 | + |
| 16 | +concurrency: |
| 17 | + group: coverage-${{ github.ref }} |
| 18 | + cancel-in-progress: true |
| 19 | + |
| 20 | +permissions: |
| 21 | + contents: read # required so checkout/actions using GITHUB_TOKEN can read repo |
| 22 | + actions: read # required to download base coverage artifact via workflow run APIs |
| 23 | + pull-requests: write # required to post/update coverage diff comments on PRs |
| 24 | + |
| 25 | +jobs: |
| 26 | + coverage: |
| 27 | + runs-on: ubuntu-24.04 |
| 28 | + permissions: |
| 29 | + actions: read # download base coverage artifact via workflow run APIs |
| 30 | + contents: read # checkout |
| 31 | + pull-requests: write # comment coverage diff on PRs |
| 32 | + env: |
| 33 | + EVENT_TYPE: ${{ github.event_name == 'push' && 'push' || 'pr' }} |
| 34 | + # Workflow-level env variables (MONOREPO, BASE_BRANCH) are automatically inherited by jobs |
| 35 | + steps: |
| 36 | + - name: 📥 Checkout Repository |
| 37 | + uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 |
| 38 | + |
| 39 | + - name: 💻 Node setup |
| 40 | + uses: ./.github/actions/node-setup |
| 41 | + |
| 42 | + - name: 🧪 Test |
| 43 | + run: pnpm test:coverage |
| 44 | + shell: bash |
| 45 | + # Note: If tests fail, the workflow will fail and coverage steps won't run. |
| 46 | + # This ensures we only report coverage for passing tests. |
| 47 | + |
| 48 | + - name: 📊 Aggregate coverage results and create summary |
| 49 | + if: env.EVENT_TYPE == 'pr' |
| 50 | + # MONOREPO is inherited from workflow-level env |
| 51 | + run: node scripts/aggregate-coverage-results.js |
| 52 | + shell: bash |
| 53 | + |
| 54 | + - name: 🔍 Locate base coverage workflow run |
| 55 | + id: locate-base-coverage |
| 56 | + if: env.EVENT_TYPE == 'pr' |
| 57 | + uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7 |
| 58 | + env: |
| 59 | + BASE_WORKFLOW: coverage.yml |
| 60 | + # BASE_BRANCH is inherited from workflow-level env |
| 61 | + with: |
| 62 | + script: | |
| 63 | + const workflowId = process.env.BASE_WORKFLOW; |
| 64 | + const baseBranch = process.env.BASE_BRANCH || 'main'; |
| 65 | +
|
| 66 | + // Get the latest successful workflow run from the base branch |
| 67 | + const { data } = await github.rest.actions.listWorkflowRuns({ |
| 68 | + owner: context.repo.owner, |
| 69 | + repo: context.repo.repo, |
| 70 | + workflow_id: workflowId, |
| 71 | + branch: baseBranch, |
| 72 | + per_page: 20, |
| 73 | + }); |
| 74 | +
|
| 75 | + const run = data.workflow_runs.find((run) => run.conclusion === 'success'); |
| 76 | + if (run) { |
| 77 | + core.info(`Found base coverage run ${run.id} from ${baseBranch} branch`); |
| 78 | + core.setOutput('run-id', String(run.id)); |
| 79 | + } else { |
| 80 | + core.warning(`No successful base coverage run found for ${baseBranch} branch`); |
| 81 | + core.setOutput('run-id', ''); |
| 82 | + } |
| 83 | +
|
| 84 | + - name: 📥 Download base branch coverage artifact |
| 85 | + if: env.EVENT_TYPE == 'pr' && steps.locate-base-coverage.outputs.run-id != '' |
| 86 | + continue-on-error: true |
| 87 | + uses: actions/download-artifact@634f93cb2916e3fdff6788551b99b062d0335ce0 # v5 |
| 88 | + with: |
| 89 | + name: base-coverage |
| 90 | + path: base-coverage |
| 91 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 92 | + run-id: ${{ steps.locate-base-coverage.outputs.run-id }} |
| 93 | + |
| 94 | + - name: 💬 Comment coverage diff |
| 95 | + if: env.EVENT_TYPE == 'pr' |
| 96 | + env: |
| 97 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 98 | + PR_NUMBER: ${{ github.event.pull_request.number }} |
| 99 | + BASE_COVERAGE_ROOT: base-coverage |
| 100 | + # MONOREPO and BASE_BRANCH are inherited from workflow-level env |
| 101 | + run: node scripts/comment-coverage-diff.js |
| 102 | + shell: bash |
| 103 | + |
| 104 | + - name: ♻️ Collect coverage summaries |
| 105 | + if: env.EVENT_TYPE == 'push' && github.ref_name == 'main' |
| 106 | + # MONOREPO is inherited from workflow-level env |
| 107 | + run: | |
| 108 | + rm -rf base-coverage |
| 109 | + if [ "$MONOREPO" = "true" ]; then |
| 110 | + rsync -a \ |
| 111 | + --include '*/' \ |
| 112 | + --include 'coverage-summary.json' \ |
| 113 | + --exclude '*' \ |
| 114 | + coverage/ base-coverage/ |
| 115 | + else |
| 116 | + mkdir -p base-coverage |
| 117 | + cp coverage/coverage-summary.json base-coverage/coverage-summary.json |
| 118 | + fi |
| 119 | + shell: bash |
| 120 | + |
| 121 | + - name: 📤 Upload coverage artifact |
| 122 | + if: env.EVENT_TYPE == 'push' && github.ref_name == 'main' |
| 123 | + uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 |
| 124 | + with: |
| 125 | + name: base-coverage |
| 126 | + path: base-coverage |
| 127 | + retention-days: 14 # Artifacts expire after 14 days. Adjust if needed, but note that PRs opened after expiration won't have base coverage to compare against. |
0 commit comments