Skip to content

Update existing review comment instead of posting a new one#6

Merged
tarmojussila merged 1 commit intomainfrom
bugfix/prevent-duplicate-review-comments
Feb 22, 2026
Merged

Update existing review comment instead of posting a new one#6
tarmojussila merged 1 commit intomainfrom
bugfix/prevent-duplicate-review-comments

Conversation

@tarmojussila
Copy link
Owner

Uses a hidden HTML marker to find and update the previous review comment on each PR push, preventing comment spam.

Uses a hidden HTML marker to find and update the previous review
comment on each PR push, preventing comment spam.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@github-actions
Copy link

Z.ai Code Review

This is a valuable improvement that prevents the bot from spamming the pull request with multiple comments. However, there are two potential bugs regarding API pagination and permission errors.

Here are the specific issues and recommendations:

1. Handle API Pagination

Issue: octokit.rest.issues.listComments returns a paginated response. By default, GitHub only returns the first 30 comments. If the PR has a long history and the bot's comment is pushed to the second page, comments.find(...) will fail to locate it. Consequently, the bot will post a duplicate comment on every run.

Recommendation: Use octokit.paginate to iterate through all pages of comments to reliably find the existing marker.

// Use paginate to ensure you check all comments
const comments = await octokit.paginate(octokit.rest.issues.listComments, {
  owner,
  repo,
  issue_number: pullNumber,
});
const existing = comments.find(c => c.body.includes(COMMENT_MARKER));

2. Verify Comment Author/Ownership

Issue: The code currently updates any comment containing the COMMENT_MARKER. If a user copies and pastes the marker into their own comment, the bot will attempt to edit that user's comment. Since the bot's token likely does not have permission to modify other users' comments, this will throw a 403 Forbidden error, causing the workflow to fail.

Recommendation: Ensure the comment belongs to the bot (or the user associated with the GITHUB_TOKEN) before attempting to update it.

const existing = comments.find(c => 
  c.body.includes(COMMENT_MARKER) && 
  c.user.type === 'Bot' // Or check specifically for your bot's name
);

3. Robustness of the Marker

Issue: If the AI generated by callZaiApi happens to output the string <!-- zai-code-review --> inside the review content, the logic might behave unexpectedly (e.g., truncating the body or confusing future updates).

Recommendation: While unlikely, consider using a more unique marker or HTML encoding the review content before appending the marker. Alternatively, placing the marker at the start of the body makes it easier to find and avoids parsing issues if the AI generates HTML-like content.

@tarmojussila tarmojussila merged commit 5692cad into main Feb 22, 2026
1 check passed
@tarmojussila tarmojussila deleted the bugfix/prevent-duplicate-review-comments branch February 22, 2026 16:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant