Skip to content

Commit bfa4619

Browse files
committed
Add PR comments for code coverage reporting
Use github-script action to post coverage percentage and threshold status as PR comments. Updates existing comment on subsequent pushes to avoid duplicate comments. Added: PR comments showing code coverage percentage and threshold status
1 parent 764d6ba commit bfa4619

1 file changed

Lines changed: 56 additions & 0 deletions

File tree

.github/workflows/test.yml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,62 @@ jobs:
6464
name: coverage-report
6565
path: coverage/
6666
retention-days: 30
67+
- name: Comment Coverage on PR
68+
if: github.event_name == 'pull_request' && always()
69+
uses: actions/github-script@v7
70+
with:
71+
script: |
72+
const fs = require('fs');
73+
74+
if (!fs.existsSync('coverage/.last_run.json')) {
75+
console.log('Coverage report not found');
76+
return;
77+
}
78+
79+
const coverage = JSON.parse(fs.readFileSync('coverage/.last_run.json', 'utf8'));
80+
const coveragePercent = coverage.result.line;
81+
const threshold = 50;
82+
const meetsThreshold = coveragePercent >= threshold;
83+
const emoji = meetsThreshold ? '✅' : '⚠️';
84+
85+
const comment = `## ${emoji} Code Coverage Report
86+
87+
**Coverage:** ${coveragePercent}%
88+
**Threshold:** ${threshold}%
89+
90+
${emoji} Coverage ${meetsThreshold ? 'meets' : 'is below'} minimum threshold
91+
92+
<sub>📊 [View detailed coverage report in artifacts](${context.payload.repository.html_url}/actions/runs/${context.runId})</sub>`;
93+
94+
// Find existing coverage comment
95+
const { data: comments } = await github.rest.issues.listComments({
96+
owner: context.repo.owner,
97+
repo: context.repo.repo,
98+
issue_number: context.issue.number,
99+
});
100+
101+
const botComment = comments.find(comment =>
102+
comment.user.type === 'Bot' &&
103+
comment.body.includes('Code Coverage Report')
104+
);
105+
106+
if (botComment) {
107+
// Update existing comment
108+
await github.rest.issues.updateComment({
109+
owner: context.repo.owner,
110+
repo: context.repo.repo,
111+
comment_id: botComment.id,
112+
body: comment
113+
});
114+
} else {
115+
// Create new comment
116+
await github.rest.issues.createComment({
117+
owner: context.repo.owner,
118+
repo: context.repo.repo,
119+
issue_number: context.issue.number,
120+
body: comment
121+
});
122+
}
67123
68124
linter:
69125
name: Linter

0 commit comments

Comments
 (0)