1- name : 📊 Coverage
1+ name : 📊 Tests & Coverage
2+
3+ # Configuration
4+ # Set to 'true' for monorepo, 'false' for single repository
5+ env :
6+ MONOREPO : ' false'
7+ BASE_BRANCH : ' main'
28
39on :
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
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
1519
1620permissions :
17- contents : read
18- actions : read
19- pull-requests : write
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
2024
2125jobs :
2226 coverage :
2529 actions : read # download base coverage artifact via workflow run APIs
2630 contents : read # checkout
2731 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
2835 steps :
2936 - name : 📥 Checkout Repository
3037 uses : actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5
@@ -35,41 +42,47 @@ jobs:
3542 - name : 🧪 Test
3643 run : pnpm test:coverage
3744 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.
3847
39- - name : 📊 Aggregate coverage results
40- env :
41- MONOREPO : ${{ inputs.monorepo }}
48+ - name : 📊 Aggregate coverage results and create summary
49+ if : env.EVENT_TYPE == 'pr'
50+ # MONOREPO is inherited from workflow-level env
4251 run : node scripts/aggregate-coverage-results.js
4352 shell : bash
4453
4554 - name : 🔍 Locate base coverage workflow run
4655 id : locate-base-coverage
47- if : inputs.event-type == 'pr'
56+ if : env.EVENT_TYPE == 'pr'
4857 uses : actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7
4958 env :
50- BASE_WORKFLOW : base-coverage.yml
59+ BASE_WORKFLOW : coverage.yml
60+ # BASE_BRANCH is inherited from workflow-level env
5161 with :
5262 script : |
5363 const workflowId = process.env.BASE_WORKFLOW;
54- const baseSha = context.payload.pull_request.base.sha;
64+ const baseBranch = process.env.BASE_BRANCH || 'main';
65+
66+ // Get the latest successful workflow run from the base branch
5567 const { data } = await github.rest.actions.listWorkflowRuns({
5668 owner: context.repo.owner,
5769 repo: context.repo.repo,
5870 workflow_id: workflowId,
59- head_sha: baseSha ,
71+ branch: baseBranch ,
6072 per_page: 20,
6173 });
74+
6275 const run = data.workflow_runs.find((run) => run.conclusion === 'success');
6376 if (run) {
64- core.info(`Found base coverage run ${run.id}`);
77+ core.info(`Found base coverage run ${run.id} from ${baseBranch} branch `);
6578 core.setOutput('run-id', String(run.id));
6679 } else {
67- core.warning(`No successful base coverage run found for ${baseSha} `);
80+ core.warning(`No successful base coverage run found for ${baseBranch} branch `);
6881 core.setOutput('run-id', '');
6982 }
7083
7184 - name : 📥 Download base branch coverage artifact
72- if : inputs.event-type == 'pr' && steps.locate-base-coverage.outputs.run-id != ''
85+ if : env.EVENT_TYPE == 'pr' && steps.locate-base-coverage.outputs.run-id != ''
7386 continue-on-error : true
7487 uses : actions/download-artifact@634f93cb2916e3fdff6788551b99b062d0335ce0 # v5
7588 with :
@@ -79,19 +92,18 @@ jobs:
7992 run-id : ${{ steps.locate-base-coverage.outputs.run-id }}
8093
8194 - name : 💬 Comment coverage diff
82- if : inputs.event-type == 'pr'
95+ if : env.EVENT_TYPE == 'pr'
8396 env :
8497 GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
8598 PR_NUMBER : ${{ github.event.pull_request.number }}
8699 BASE_COVERAGE_ROOT : base-coverage
87- MONOREPO : ${{ inputs.monorepo }}
100+ # MONOREPO and BASE_BRANCH are inherited from workflow-level env
88101 run : node scripts/comment-coverage-diff.js
89102 shell : bash
90103
91104 - name : ♻️ Collect coverage summaries
92- if : inputs.event-type == 'push'
93- env :
94- MONOREPO : ${{ inputs.monorepo }}
105+ if : env.EVENT_TYPE == 'push' && github.ref_name == 'main'
106+ # MONOREPO is inherited from workflow-level env
95107 run : |
96108 rm -rf base-coverage
97109 if [ "$MONOREPO" = "true" ]; then
@@ -107,9 +119,9 @@ jobs:
107119 shell : bash
108120
109121 - name : 📤 Upload coverage artifact
110- if : inputs.event-type == 'push'
122+ if : env.EVENT_TYPE == 'push' && github.ref_name == 'main '
111123 uses : actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02
112124 with :
113125 name : base-coverage
114126 path : base-coverage
115- retention-days : 14
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