Skip to content

Commit ef47fa4

Browse files
committed
Fix coverage extraction and update comment behavior
Changes: 1. Extract coverage percentage from clover XML file instead of re-running tests - More reliable as it uses the actual coverage data - Parses statements/coveredstatements from coverage.xml 2. Update existing coverage comments instead of creating new ones - Finds existing bot comments with 'Code Coverage Report' - Updates the comment if found, creates new one if not - Reduces PR comment spam Fixes NaN% issue and addresses comment duplication.
1 parent 90c6d8d commit ef47fa4

File tree

1 file changed

+38
-4
lines changed

1 file changed

+38
-4
lines changed

.github/workflows/code-coverage.yml

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,19 @@ jobs:
8080
- name: Generate coverage report summary
8181
id: coverage
8282
run: |
83-
# Extract coverage percentage from the saved output
84-
COVERAGE=$(grep "Lines:" coverage-output.txt | awk '{print $2}' | sed 's/%//')
83+
# Extract coverage percentage from clover XML
84+
if [ -f coverage.xml ]; then
85+
# Extract lines covered and total from XML
86+
LINES=$(grep -o 'statements="[0-9]*"' coverage.xml | head -1 | grep -o '[0-9]*')
87+
COVERED=$(grep -o 'coveredstatements="[0-9]*"' coverage.xml | head -1 | grep -o '[0-9]*')
88+
if [ -n "$LINES" ] && [ -n "$COVERED" ] && [ "$LINES" -gt 0 ]; then
89+
COVERAGE=$(awk "BEGIN {printf \"%.2f\", ($COVERED/$LINES)*100}")
90+
else
91+
COVERAGE="0"
92+
fi
93+
else
94+
COVERAGE="0"
95+
fi
8596
echo "current_coverage=$COVERAGE" >> $GITHUB_OUTPUT
8697
echo "Current code coverage: $COVERAGE%"
8798
@@ -158,13 +169,36 @@ jobs:
158169
${diff >= 0 ? '🎉 Great job maintaining/improving code coverage!' : ''}
159170
`;
160171
161-
github.rest.issues.createComment({
172+
// Find existing coverage report comment
173+
const {data: comments} = await github.rest.issues.listComments({
162174
issue_number: context.issue.number,
163175
owner: context.repo.owner,
164176
repo: context.repo.repo,
165-
body: comment
166177
});
167178
179+
const botComment = comments.find(comment =>
180+
comment.user.type === 'Bot' &&
181+
comment.body.includes('Code Coverage Report')
182+
);
183+
184+
if (botComment) {
185+
// Update existing comment
186+
await github.rest.issues.updateComment({
187+
comment_id: botComment.id,
188+
owner: context.repo.owner,
189+
repo: context.repo.repo,
190+
body: comment
191+
});
192+
} else {
193+
// Create new comment
194+
await github.rest.issues.createComment({
195+
issue_number: context.issue.number,
196+
owner: context.repo.owner,
197+
repo: context.repo.repo,
198+
body: comment
199+
});
200+
}
201+
168202
- name: Generate HTML coverage report
169203
if: always()
170204
run: |

0 commit comments

Comments
 (0)