Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 55 additions & 1 deletion .github/workflows/ci-cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down Expand Up @@ -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 +166 to +183
Copy link

Copilot AI Dec 15, 2025

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.

Suggested change
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}`);
// Paginate through all comments to find and delete old pre-commit failure comments
let allComments = [];
let page = 1;
let fetched;
do {
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
per_page: 100,
page: page,
});
allComments = allComments.concat(comments);
fetched = comments.length;
page += 1;
} while (fetched === 100);
const existingComments = allComments.filter(comment =>
comment.body && comment.body.includes(commentMarker)
);
for (const comment of existingComments) {
await github.rest.issues.deleteComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: comment.id,
});
core.info(`Deleted old pre-commit failure comment #${comment.id}`);

Copilot uses AI. Check for mistakes.
Comment on lines +178 to +183
Copy link

Copilot AI Dec 15, 2025

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.

Suggested change
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}`);
}

Copilot uses AI. Check for mistakes.
}
Comment on lines +177 to +184
Copy link

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


try {
await github.rest.issues.createComment({
owner: context.repo.owner,
Expand All @@ -174,6 +197,37 @@ jobs:
throw error;
}
}
- name: Remove pre-commit failure comment if pre-commit passes
if: github.event_name == 'pull_request_target' && steps.pre-commit.outputs.exit_code == '0'
Copy link

Copilot AI Dec 15, 2025

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.

Suggested change
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 uses AI. Check for mistakes.
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const commentMarker = '<!-- pre-commit-check -->';
const owner = context.repo.owner;
const repo = context.repo.repo;
const pull_number = context.issue.number;

// Delete old pre-commit failure comment when pre-commit passes
const { data: comments } = await github.rest.issues.listComments({
owner,
repo,
issue_number: pull_number,
per_page: 100,
});
Comment on lines +212 to +217
Copy link

Copilot AI Dec 15, 2025

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.

Suggested change
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 uses AI. Check for mistakes.

const existingComment = comments.find(comment =>
comment.body && comment.body.includes(commentMarker)
);

if (existingComment) {
await github.rest.issues.deleteComment({
owner,
repo,
comment_id: existingComment.id,
});
core.info(`Deleted pre-commit failure comment #${existingComment.id} since checks now pass`);
Comment on lines +224 to +229
Copy link

Copilot AI Dec 15, 2025

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.

Suggested change
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}`);
}

Copilot uses AI. Check for mistakes.
}
- name: Add pre-commit status label
if: github.event_name == 'pull_request_target'
uses: actions/github-script@v7
Expand Down
Loading