Skip to content

Commit 8e13752

Browse files
committed
Improvs final
1 parent 93ce775 commit 8e13752

File tree

5 files changed

+47
-126
lines changed

5 files changed

+47
-126
lines changed

.github/actions/test-coverage/action.yml

Lines changed: 0 additions & 64 deletions
This file was deleted.

.github/workflows/base-coverage.yml

Lines changed: 0 additions & 18 deletions
This file was deleted.

.github/workflows/ci.yml

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,3 @@ jobs:
4949
- name: 🏗️ Build packages
5050
run: pnpm build
5151
shell: bash
52-
53-
coverage:
54-
permissions:
55-
actions: read # download base coverage artifact via workflow run APIs
56-
contents: read # checkout
57-
pull-requests: write # comment coverage diff on PRs
58-
uses: ./.github/workflows/coverage.yml
59-
with:
60-
monorepo: true
61-
event-type: pr

.github/workflows/coverage.yml

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
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

39
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
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

1620
permissions:
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

2125
jobs:
2226
coverage:
@@ -25,6 +29,9 @@ jobs:
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.

scripts/comment-coverage-diff.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const MARKER = '<!-- coverage-diff -->';
1818
const SINGLE_REPO_PACKAGE_NAME = 'root';
1919
const PERCENTAGE_DECIMAL_PLACES = 2;
2020
const IS_MONOREPO = process.env.MONOREPO === 'true';
21+
const BASE_BRANCH_NAME = process.env.BASE_BRANCH || 'main';
2122

2223
// ============================================================================
2324
// Configuration
@@ -136,7 +137,7 @@ function capitalize(str) {
136137
* @returns {string} Markdown table
137138
*/
138139
function buildDiffTable(headTotals, baseTotals) {
139-
let md = '| Metric | PR | main | Δ |\n';
140+
let md = `| Metric | PR | ${BASE_BRANCH_NAME} | Δ |\n`;
140141
md += '| ------ | ---: | ---: | ---: |\n';
141142

142143
COVERAGE_METRICS.forEach((metric) => {
@@ -175,7 +176,7 @@ function buildPerPackageTable(prPackages, basePackages) {
175176
return null;
176177
}
177178

178-
let md = '| Package | Metric | PR | main | Δ |\n';
179+
let md = `| Package | Metric | PR | ${BASE_BRANCH_NAME} | Δ |\n`;
179180
md += '| ------- | ------ | ---: | ---: | ---: |\n';
180181

181182
sortedNames.forEach((packageName) => {

0 commit comments

Comments
 (0)