Compare commits

...

3 Commits

Author SHA1 Message Date
Ivan Shymko
de1af81601
Revert "Remove test file to produce coverage delta"
This reverts commit f775336a4346dc4e3a91fa7773d112e519c99d95.
2025-12-15 16:21:15 +00:00
Ivan Shymko
f775336a43
Remove test file to produce coverage delta 2025-12-15 16:11:07 +00:00
Ivan Shymko
e7c83e940b
ci: compare coverage 2025-12-15 16:04:58 +00:00
2 changed files with 114 additions and 1 deletions

View File

@ -0,0 +1,113 @@
name: Coverage Delta
on:
pull_request:
branches: [main]
jobs:
compare-coverage:
runs-on: ubuntu-latest
steps:
- uses: actions/setup-node@v3
with:
node-version: 24
- name: Checkout Main
uses: actions/checkout@v4
with:
ref: main
- name: Install & Test (Main)
run: |
npm ci
npm run coverage
mv ./coverage/coverage-summary.json ./coverage-main-summary.json
- name: Checkout PR Branch
uses: actions/checkout@v4
with:
clean: true
- name: Install & Test (PR)
run: |
npm ci
npm run coverage
- name: Report Coverage Delta
uses: actions/github-script@v6
with:
script: |
const fs = require('fs');
const loadSummary = (path) => {
try {
return JSON.parse(fs.readFileSync(path, 'utf8'));
} catch (error) {
console.log(`Could not load ${path}: ${error.message}`);
return null;
}
};
const mainSummary = loadSummary('./coverage-main-summary.json');
const prSummary = loadSummary('./coverage/coverage-summary.json');
if (!mainSummary || !prSummary) {
core.setFailed('Could not load coverage summaries for comparison.');
return;
}
const metrics = ['lines', 'statements', 'functions', 'branches'];
const getPct = (summary, metric) => summary.total[metric].pct;
let markdown = `### 🧪 Code Coverage Delta\n\n`;
markdown += `| Metric | Main | PR | Delta |\n`;
markdown += `| :--- | :---: | :---: | :---: |\n`;
metrics.forEach(metric => {
const oldPct = getPct(mainSummary, metric);
const newPct = getPct(prSummary, metric);
const diff = (newPct - oldPct).toFixed(2);
let icon = '';
if (diff > 0) icon = '🟢';
else if (diff < 0) icon = '🔴';
else icon = '⚪️';
// Format with + sign for positive numbers
const diffStr = diff > 0 ? `+${diff}%` : `${diff}%`;
markdown += `| **${metric}** | ${oldPct}% | ${newPct}% | ${icon} ${diffStr} |\n`;
});
markdown += `\n_Generated by c8 and GitHub Actions_`;
const { owner, repo } = context.repo;
const issue_number = context.issue.number;
const comments = await github.rest.issues.listComments({
owner,
repo,
issue_number,
});
const botComment = comments.data.find(c =>
c.body.includes('### 🧪 Code Coverage Delta') &&
c.user.type === 'Bot'
);
if (botComment) {
await github.rest.issues.updateComment({
owner,
repo,
comment_id: botComment.id,
body: markdown
});
} else {
await github.rest.issues.createComment({
owner,
repo,
issue_number,
body: markdown
});
}

View File

@ -7,7 +7,7 @@ export default defineConfig({
include: ['test/**/*.spec.ts'],
coverage: {
provider: 'v8',
reporter: ['text', 'json', 'html'],
reporter: ['text', 'json', 'html', 'json-summary'],
},
},
});