Update sonar-check.yml to manage coverage comments - #13878
Conversation
Refactor coverage comment handling to update existing comments if present.
There was a problem hiding this comment.
Pull request overview
This PR updates the Sonar/coverage GitHub Actions workflow so the “coverage grade” bot comment on pull requests is updated in-place when a prior comment exists, instead of always posting a new comment.
Changes:
- Adds a hidden marker (
<!-- coverage-grade-bot -->) to reliably identify the workflow’s coverage-grade comment. - Switches from always creating a PR comment to listing comments and updating an existing matching comment when present.
- Falls back to creating a new comment when no existing marked comment is found.
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| const { data: comments } = await github.rest.issues.listComments({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| issue_number: context.issue.number, | ||
| body: body, | ||
| }); | ||
| console.log('Posted coverage grade comment'); | ||
| const existing = comments.find(c => c.user.type === 'Bot' && c.body.includes(marker)); |
Codecov Report✅ All modified and coverable lines are covered by tests.
Additional details and impacted files@@ Coverage Diff @@
## main #13878 +/- ##
=============================================
- Coverage 19.64% 3.41% -16.24%
=============================================
Files 6368 487 -5881
Lines 574889 41867 -533022
Branches 70353 7912 -62441
=============================================
- Hits 112935 1429 -111506
+ Misses 449684 40238 -409446
+ Partials 12270 200 -12070
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.
Suppressed comments (2)
.github/workflows/sonar-check.yml:115
issues.listCommentsreturns only the first page by default (30 comments). On PRs with many comments, the bot’s previous coverage comment may not be in the first page, causing this job to post a duplicate instead of updating. Paginate (or at least increaseper_page) and make the marker check resilient to a missing body.
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
});
.github/workflows/sonar-check.yml:114
- This step calls
issues.listComments,issues.updateComment, andissues.createComment, which requireissuespermission onGITHUB_TOKEN. The workflow currently only grantscontents: readandpull-requests: write, so these API calls can fail with 403 depending on repository default token permissions. Consider explicitly addingissues: writeunder the workflow/jobpermissions:block.
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
Refactor coverage comment handling to update existing comments if present.
Description
This PR...
Types of changes
Feature/Enhancement Scale or Bug Severity
Feature/Enhancement Scale
Bug Severity
Screenshots (if appropriate):
How Has This Been Tested?
How did you try to break this feature and the system with this change?