|
| 1 | +name: 📊 Coverage |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + inputs: |
| 6 | + monorepo: |
| 7 | + description: 'Whether this is a monorepo (true) or single repo (false)' |
| 8 | + required: false |
| 9 | + type: boolean |
| 10 | + default: false |
| 11 | + event-type: |
| 12 | + description: 'Type of event: pr or push' |
| 13 | + required: true |
| 14 | + type: string |
| 15 | + |
| 16 | +permissions: |
| 17 | + contents: read |
| 18 | + actions: read |
| 19 | + pull-requests: write |
| 20 | + |
| 21 | +jobs: |
| 22 | + coverage: |
| 23 | + runs-on: ubuntu-24.04 |
| 24 | + permissions: |
| 25 | + actions: read # download base coverage artifact via workflow run APIs |
| 26 | + contents: read # checkout |
| 27 | + pull-requests: write # comment coverage diff on PRs |
| 28 | + steps: |
| 29 | + - name: 📥 Checkout Repository |
| 30 | + uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 |
| 31 | + |
| 32 | + - name: 💻 Node setup |
| 33 | + uses: ./.github/actions/node-setup |
| 34 | + |
| 35 | + - name: 🧪 Test |
| 36 | + run: pnpm test:coverage |
| 37 | + shell: bash |
| 38 | + |
| 39 | + - name: 📊 Aggregate coverage results |
| 40 | + env: |
| 41 | + MONOREPO: ${{ inputs.monorepo }} |
| 42 | + run: node scripts/aggregate-coverage-results.js |
| 43 | + shell: bash |
| 44 | + |
| 45 | + - name: 🔍 Locate base coverage workflow run |
| 46 | + id: locate-base-coverage |
| 47 | + if: inputs.event-type == 'pr' |
| 48 | + uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7 |
| 49 | + env: |
| 50 | + BASE_WORKFLOW: base-coverage.yml |
| 51 | + with: |
| 52 | + script: | |
| 53 | + const workflowId = process.env.BASE_WORKFLOW; |
| 54 | + const baseSha = context.payload.pull_request.base.sha; |
| 55 | + const { data } = await github.rest.actions.listWorkflowRuns({ |
| 56 | + owner: context.repo.owner, |
| 57 | + repo: context.repo.repo, |
| 58 | + workflow_id: workflowId, |
| 59 | + head_sha: baseSha, |
| 60 | + per_page: 20, |
| 61 | + }); |
| 62 | + const run = data.workflow_runs.find((run) => run.conclusion === 'success'); |
| 63 | + if (run) { |
| 64 | + core.info(`Found base coverage run ${run.id}`); |
| 65 | + core.setOutput('run-id', String(run.id)); |
| 66 | + } else { |
| 67 | + core.warning(`No successful base coverage run found for ${baseSha}`); |
| 68 | + core.setOutput('run-id', ''); |
| 69 | + } |
| 70 | +
|
| 71 | + - name: 📥 Download base branch coverage artifact |
| 72 | + if: inputs.event-type == 'pr' && steps.locate-base-coverage.outputs.run-id != '' |
| 73 | + continue-on-error: true |
| 74 | + uses: actions/download-artifact@634f93cb2916e3fdff6788551b99b062d0335ce0 # v5 |
| 75 | + with: |
| 76 | + name: base-coverage |
| 77 | + path: base-coverage |
| 78 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 79 | + run-id: ${{ steps.locate-base-coverage.outputs.run-id }} |
| 80 | + |
| 81 | + - name: 💬 Comment coverage diff |
| 82 | + if: inputs.event-type == 'pr' |
| 83 | + env: |
| 84 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 85 | + PR_NUMBER: ${{ github.event.pull_request.number }} |
| 86 | + BASE_COVERAGE_ROOT: base-coverage |
| 87 | + MONOREPO: ${{ inputs.monorepo }} |
| 88 | + run: node scripts/comment-coverage-diff.js |
| 89 | + shell: bash |
| 90 | + |
| 91 | + - name: ♻️ Collect coverage summaries |
| 92 | + if: inputs.event-type == 'push' |
| 93 | + env: |
| 94 | + MONOREPO: ${{ inputs.monorepo }} |
| 95 | + run: | |
| 96 | + rm -rf base-coverage |
| 97 | + if [ "$MONOREPO" = "true" ]; then |
| 98 | + rsync -a \ |
| 99 | + --include '*/' \ |
| 100 | + --include 'coverage-summary.json' \ |
| 101 | + --exclude '*' \ |
| 102 | + coverage/ base-coverage/ |
| 103 | + else |
| 104 | + mkdir -p base-coverage |
| 105 | + cp coverage/coverage-summary.json base-coverage/coverage-summary.json |
| 106 | + fi |
| 107 | + shell: bash |
| 108 | + |
| 109 | + - name: 📤 Upload coverage artifact |
| 110 | + if: inputs.event-type == 'push' |
| 111 | + uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 |
| 112 | + with: |
| 113 | + name: base-coverage |
| 114 | + path: base-coverage |
| 115 | + retention-days: 14 |
0 commit comments