Files
claude-code-action/src/github/operations/comments/update-claude-comment.ts
Ashwin Bhat 1d4d6c4b93 feat: add unified update_claude_comment tool (#98)
* feat: add unified update_claude_comment tool

- Add new update_claude_comment tool that automatically handles both issue and PR comments
- Remove individual update_issue_comment and update_pull_request_comment tools
- Pass CLAUDE_COMMENT_ID, GITHUB_EVENT_NAME, and IS_PR to MCP server environment
- Simplify Claude's comment update workflow by removing need for owner/repo/commentId params
- Update prompts and tests to use the new unified tool

* feat: add unified update_claude_comment tool

- Add new update_claude_comment tool that automatically handles both issue and PR comments
- Remove individual update_issue_comment and update_pull_request_comment tools
- Pass CLAUDE_COMMENT_ID, GITHUB_EVENT_NAME, and IS_PR to MCP server environment
- Use Octokit instead of raw fetch for better type safety and error handling
- Simplify Claude's comment update workflow by removing need for owner/repo/commentId params
- Update prompts and tests to use the new unified tool

* refactor: extract update_claude_comment logic to standalone testable function

- Create new updateClaudeComment function in operations/comments
- Add comprehensive unit tests following image-downloader pattern
- Update MCP server to use extracted function
- Refactor update-comment-link.ts and update-with-branch.ts to eliminate duplication
- All tests passing (10 new tests for update-claude-comment)

Co-authored-by: ashwin-ant <ashwin-ant@users.noreply.github.com>

* prettier

* tsc

* clean up comments

---------

Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
Co-authored-by: ashwin-ant <ashwin-ant@users.noreply.github.com>
2025-06-02 12:15:25 -07:00

71 lines
1.7 KiB
TypeScript

import { Octokit } from "@octokit/rest";
export type UpdateClaudeCommentParams = {
owner: string;
repo: string;
commentId: number;
body: string;
isPullRequestReviewComment: boolean;
};
export type UpdateClaudeCommentResult = {
id: number;
html_url: string;
updated_at: string;
};
/**
* Updates a Claude comment on GitHub (either an issue/PR comment or a PR review comment)
*
* @param octokit - Authenticated Octokit instance
* @param params - Parameters for updating the comment
* @returns The updated comment details
* @throws Error if the update fails
*/
export async function updateClaudeComment(
octokit: Octokit,
params: UpdateClaudeCommentParams,
): Promise<UpdateClaudeCommentResult> {
const { owner, repo, commentId, body, isPullRequestReviewComment } = params;
let response;
try {
if (isPullRequestReviewComment) {
// Try PR review comment API first
response = await octokit.rest.pulls.updateReviewComment({
owner,
repo,
comment_id: commentId,
body,
});
} else {
// Use issue comment API (works for both issues and PR general comments)
response = await octokit.rest.issues.updateComment({
owner,
repo,
comment_id: commentId,
body,
});
}
} catch (error: any) {
// If PR review comment update fails with 404, fall back to issue comment API
if (isPullRequestReviewComment && error.status === 404) {
response = await octokit.rest.issues.updateComment({
owner,
repo,
comment_id: commentId,
body,
});
} else {
throw error;
}
}
return {
id: response.data.id,
html_url: response.data.html_url,
updated_at: response.data.updated_at,
};
}