-
-
Notifications
You must be signed in to change notification settings - Fork 313
Remove duplicate pre-commit failure comments in CI/CD workflow #5300
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -126,7 +126,9 @@ jobs: | |||||||||||||||||||||||||||||||||||||||||||||
| const last100Lines = lines.slice(-100).join('\n'); | ||||||||||||||||||||||||||||||||||||||||||||||
| const truncated = lines.length > 100; | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| const message = `## ❌ Pre-commit checks failed | ||||||||||||||||||||||||||||||||||||||||||||||
| const commentMarker = '<!-- pre-commit-check -->'; | ||||||||||||||||||||||||||||||||||||||||||||||
| const message = commentMarker + '\n' + | ||||||||||||||||||||||||||||||||||||||||||||||
| `## ❌ Pre-commit checks failed | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| The pre-commit hooks found issues that need to be fixed. Please run the following commands locally to fix them: | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -160,6 +162,27 @@ jobs: | |||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| For more information, see the [pre-commit documentation](https://pre-commit.com/).`; | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| // Delete old pre-commit failure comments before posting new one | ||||||||||||||||||||||||||||||||||||||||||||||
| const { data: comments } = await github.rest.issues.listComments({ | ||||||||||||||||||||||||||||||||||||||||||||||
| owner: context.repo.owner, | ||||||||||||||||||||||||||||||||||||||||||||||
| repo: context.repo.repo, | ||||||||||||||||||||||||||||||||||||||||||||||
| issue_number: context.issue.number, | ||||||||||||||||||||||||||||||||||||||||||||||
| per_page: 100, | ||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| const existingComment = comments.find(comment => | ||||||||||||||||||||||||||||||||||||||||||||||
| comment.body && comment.body.includes(commentMarker) | ||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| if (existingComment) { | ||||||||||||||||||||||||||||||||||||||||||||||
| await github.rest.issues.deleteComment({ | ||||||||||||||||||||||||||||||||||||||||||||||
| owner: context.repo.owner, | ||||||||||||||||||||||||||||||||||||||||||||||
| repo: context.repo.repo, | ||||||||||||||||||||||||||||||||||||||||||||||
| comment_id: existingComment.id, | ||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||
| core.info(`Deleted old pre-commit failure comment #${existingComment.id}`); | ||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+178
to
+183
|
||||||||||||||||||||||||||||||||||||||||||||||
| await github.rest.issues.deleteComment({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| comment_id: existingComment.id, | |
| }); | |
| core.info(`Deleted old pre-commit failure comment #${existingComment.id}`); | |
| try { | |
| await github.rest.issues.deleteComment({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| comment_id: existingComment.id, | |
| }); | |
| core.info(`Deleted old pre-commit failure comment #${existingComment.id}`); | |
| } catch (error) { | |
| core.warning(`Failed to delete old pre-commit failure comment #${existingComment.id}: ${error.message}`); | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: The deleteComment call for pre-commit failures lacks error handling. This will crash the workflow if the comment has already been deleted.
Severity: MEDIUM | Confidence: High
🔍 Detailed Analysis
The github.rest.issues.deleteComment() call, used to remove previous pre-commit failure comments, is not wrapped in a try-catch block. If the comment is deleted by another process or manually before this step runs, the GitHub API will return a 404 error. The octokit client will treat this as an unhandled exception, causing the workflow step to fail. This is inconsistent with other parts of the same workflow, such as the test failure comment deletion logic, which correctly handle this potential error by catching it and logging a warning.
💡 Suggested Fix
Wrap the github.rest.issues.deleteComment() call within a try-catch block. In the catch block, log a warning message, such as core.warning('Could not delete previous comment: ${error.message}'), to prevent the workflow from failing if the comment is already gone. This will make its behavior consistent with the existing test failure comment handling logic.
🤖 Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.
Location: .github/workflows/ci-cd.yml#L177-L184
Potential issue: The `github.rest.issues.deleteComment()` call, used to remove previous
pre-commit failure comments, is not wrapped in a `try-catch` block. If the comment is
deleted by another process or manually before this step runs, the GitHub API will return
a 404 error. The `octokit` client will treat this as an unhandled exception, causing the
workflow step to fail. This is inconsistent with other parts of the same workflow, such
as the test failure comment deletion logic, which correctly handle this potential error
by catching it and logging a warning.
Did we get this right? 👍 / 👎 to inform future reviews.
Reference ID: 7515487
Copilot
AI
Dec 15, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The condition should check if the pre-commit step was executed and did not fail, not if it has a specific exit code. With the current condition, this step only runs when the pre-commit step outputs exit_code of '0', but if the pre-commit step is skipped (e.g., on non-PR events), the step.pre-commit.outputs.exit_code will be empty, not '0'. Consider using steps.pre-commit.outcome == 'success' instead to handle all success cases correctly.
| if: github.event_name == 'pull_request_target' && steps.pre-commit.outputs.exit_code == '0' | |
| if: github.event_name == 'pull_request_target' && steps.pre-commit.outcome == 'success' |
Copilot
AI
Dec 15, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The per_page parameter is set to 100, which means if there are more than 100 comments on the PR, older pre-commit failure comments beyond the first 100 won't be found and deleted. This could lead to multiple stale comments accumulating on PRs with many comments. Consider either increasing per_page to the API maximum or implementing pagination to fetch all comments.
| const { data: comments } = await github.rest.issues.listComments({ | |
| owner, | |
| repo, | |
| issue_number: pull_number, | |
| per_page: 100, | |
| }); | |
| // Paginate through all comments to ensure we find all pre-commit failure comments | |
| let comments = []; | |
| let page = 1; | |
| let fetched; | |
| do { | |
| const { data } = await github.rest.issues.listComments({ | |
| owner, | |
| repo, | |
| issue_number: pull_number, | |
| per_page: 100, | |
| page, | |
| }); | |
| fetched = data.length; | |
| comments = comments.concat(data); | |
| page += 1; | |
| } while (fetched === 100); |
Copilot
AI
Dec 15, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing error handling for the deleteComment API call. If the comment deletion fails (e.g., comment was already deleted, permission issues), the workflow should handle it gracefully instead of failing. Consider wrapping the deletion in a try-catch block, similar to the pattern used in the test failure comment cleanup at line 524 or the console.log workflow at line 261.
| await github.rest.issues.deleteComment({ | |
| owner, | |
| repo, | |
| comment_id: existingComment.id, | |
| }); | |
| core.info(`Deleted pre-commit failure comment #${existingComment.id} since checks now pass`); | |
| try { | |
| await github.rest.issues.deleteComment({ | |
| owner, | |
| repo, | |
| comment_id: existingComment.id, | |
| }); | |
| core.info(`Deleted pre-commit failure comment #${existingComment.id} since checks now pass`); | |
| } catch (error) { | |
| core.warning(`Failed to delete pre-commit failure comment #${existingComment.id}: ${error.message}`); | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The per_page parameter is set to 100, which means if there are more than 100 comments on the PR, older pre-commit failure comments beyond the first 100 won't be found and deleted. This could lead to multiple stale comments accumulating on PRs with many comments. Consider either increasing per_page to the API maximum or implementing pagination to fetch all comments. The test failure comment deletion code at line 425 has the same limitation.