Skip to content

Commit c369660

Browse files
committed
separate the coverage and the comment posting for security access
1 parent e6120ba commit c369660

File tree

3 files changed

+116
-7
lines changed

3 files changed

+116
-7
lines changed

.github/scripts/coverage-comment.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,5 +138,34 @@ async function postCoverageComment(github, context) {
138138
console.log('Coverage comment posted successfully!');
139139
}
140140

141-
module.exports = { postCoverageComment };
141+
/**
142+
* Generate coverage report and save to file (for workflow artifacts)
143+
*/
144+
function generateCoverageFile() {
145+
const file = 'lcov.info';
146+
147+
if (!fs.existsSync(file)) {
148+
console.log('Coverage file not found.');
149+
return;
150+
}
151+
152+
const content = fs.readFileSync(file, 'utf8');
153+
const metrics = parseLcovContent(content);
154+
155+
console.log('Coverage Metrics:');
156+
console.log('- Lines:', metrics.coveredLines, '/', metrics.totalLines);
157+
console.log('- Functions:', metrics.coveredFunctions, '/', metrics.totalFunctions);
158+
console.log('- Branches:', metrics.coveredBranches, '/', metrics.totalBranches);
159+
160+
const body = generateCoverageReport(metrics);
161+
fs.writeFileSync('coverage-report.md', body);
162+
console.log('Coverage report saved to coverage-report.md');
163+
}
164+
165+
// If run directly (not as module), generate the file
166+
if (require.main === module) {
167+
generateCoverageFile();
168+
}
169+
170+
module.exports = { postCoverageComment, generateCoverageFile };
142171

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
name: Post Coverage Comment
2+
3+
on:
4+
workflow_run:
5+
workflows: ["CI"]
6+
types:
7+
- completed
8+
9+
permissions:
10+
pull-requests: write
11+
issues: write
12+
13+
jobs:
14+
comment:
15+
name: Post Coverage Comment
16+
runs-on: ubuntu-latest
17+
if: >
18+
github.event.workflow_run.event == 'pull_request' &&
19+
github.event.workflow_run.conclusion == 'success'
20+
steps:
21+
- name: Download coverage data
22+
uses: actions/download-artifact@v4
23+
with:
24+
name: coverage-data
25+
github-token: ${{ secrets.GITHUB_TOKEN }}
26+
run-id: ${{ github.event.workflow_run.id }}
27+
28+
- name: Extract PR number
29+
id: pr
30+
run: |
31+
PR_NUMBER=$(cat coverage-data.txt | grep PR_NUMBER | cut -d'=' -f2)
32+
echo "number=$PR_NUMBER" >> $GITHUB_OUTPUT
33+
34+
- name: Post coverage comment
35+
uses: actions/github-script@v7
36+
with:
37+
github-token: ${{ secrets.GITHUB_TOKEN }}
38+
script: |
39+
const fs = require('fs');
40+
const prNumber = ${{ steps.pr.outputs.number }};
41+
const body = fs.readFileSync('coverage-report.md', 'utf8');
42+
43+
// Check if a coverage comment already exists
44+
const comments = await github.rest.issues.listComments({
45+
owner: context.repo.owner,
46+
repo: context.repo.repo,
47+
issue_number: prNumber
48+
});
49+
50+
const botComment = comments.data.find(comment =>
51+
comment.user.type === 'Bot' &&
52+
comment.body.includes('## Coverage Report')
53+
);
54+
55+
if (botComment) {
56+
// Update existing comment
57+
await github.rest.issues.updateComment({
58+
owner: context.repo.owner,
59+
repo: context.repo.repo,
60+
comment_id: botComment.id,
61+
body: body
62+
});
63+
console.log('Coverage comment updated successfully!');
64+
} else {
65+
// Create new comment
66+
await github.rest.issues.createComment({
67+
owner: context.repo.owner,
68+
repo: context.repo.repo,
69+
issue_number: prNumber,
70+
body: body
71+
});
72+
console.log('Coverage comment posted successfully!');
73+
}

.github/workflows/coverage.yml

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,18 @@ jobs:
2828
forge coverage --report summary --report lcov
2929
ls -la lcov.info || echo "lcov.info not found"
3030
31-
- name: Comment on PR
31+
- name: Generate coverage report
3232
if: github.event_name == 'pull_request'
33-
uses: actions/github-script@v7
33+
run: |
34+
node .github/scripts/coverage-comment.js
35+
echo "PR_NUMBER=${{ github.event.pull_request.number }}" >> coverage-data.txt
36+
37+
- name: Upload coverage data
38+
if: github.event_name == 'pull_request'
39+
uses: actions/upload-artifact@v4
3440
with:
35-
github-token: ${{ secrets.GITHUB_TOKEN }}
36-
script: |
37-
const { postCoverageComment } = require('./.github/scripts/coverage-comment.js');
38-
await postCoverageComment(github, context);
41+
name: coverage-data
42+
path: |
43+
coverage-report.md
44+
coverage-data.txt
45+
retention-days: 1

0 commit comments

Comments
 (0)