-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add useGeminiReview hook with comment cleanup #189
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
0c90065
c445743
68019b0
229923b
90727a7
5b1cd59
9afa559
5a0ad38
a2cf162
f7b4063
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 |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import { useMutation, useQueryClient } from "@tanstack/react-query"; | ||
| import { useApiClient } from "./useApiClient"; | ||
|
|
||
| const GEMINI_USER_LOGIN = "gemini-code-assist[bot]"; | ||
| const GEMINI_REVIEW_COMMAND = "/gemini review"; | ||
|
|
||
| type MutationProps = { | ||
| owner: string; | ||
| repo: string; | ||
| pull_number: number; | ||
| }; | ||
|
|
||
| export function useGeminiReview() { | ||
| const qc = useQueryClient(); | ||
| const { octokit } = useApiClient(); | ||
|
|
||
| return useMutation({ | ||
| mutationFn: async ({ owner, repo, pull_number }: MutationProps) => { | ||
| const comments = await octokit.paginate(octokit.issues.listComments, { | ||
| owner, | ||
| repo, | ||
| issue_number: pull_number, | ||
| per_page: 100, | ||
| }); | ||
|
|
||
| const geminiComments = comments.filter((c) => c.user?.login === GEMINI_USER_LOGIN); | ||
|
|
||
| const reviewComments = await octokit.paginate(octokit.pulls.listReviewComments, { | ||
| owner, | ||
| repo, | ||
| pull_number, | ||
| per_page: 100, | ||
| }); | ||
|
|
||
| const geminiReviewComments = reviewComments.filter((c) => c.user?.login === GEMINI_USER_LOGIN); | ||
|
|
||
| for (const comment of geminiComments) { | ||
| try { | ||
| await octokit.issues.deleteComment({ owner, repo, comment_id: comment.id }); | ||
| } catch (e) { | ||
| console.error(`Failed to delete issue comment ${comment.id}`, e); | ||
| } | ||
| } | ||
|
|
||
| for (const comment of geminiReviewComments) { | ||
| try { | ||
| await octokit.pulls.deleteReviewComment({ owner, repo, comment_id: comment.id }); | ||
| } catch (e) { | ||
| console.error(`Failed to delete review comment ${comment.id}`, e); | ||
| } | ||
| } | ||
|
Comment on lines
+37
to
+51
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The loops for deleting comments run sequentially, which can be slow if there are many comments to delete. You can significantly improve performance by running these deletion requests in parallel using const deletionPromises = [
...geminiComments.map((comment) =>
octokit.issues.deleteComment({ owner, repo, comment_id: comment.id }).catch((e) => {
console.error(`Failed to delete issue comment ${comment.id}`, e);
}),
),
...geminiReviewComments.map((comment) =>
octokit.pulls.deleteReviewComment({ owner, repo, comment_id: comment.id }).catch((e) => {
console.error(`Failed to delete review comment ${comment.id}`, e);
}),
),
];
await Promise.all(deletionPromises); |
||
|
|
||
| return octokit.issues.createComment({ | ||
| owner, | ||
| repo, | ||
| issue_number: pull_number, | ||
| body: GEMINI_REVIEW_COMMAND, | ||
| }); | ||
| }, | ||
| onSuccess: () => qc.invalidateQueries({ queryKey: ["open-prs"] }), | ||
| }); | ||
| } | ||
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 two
paginatecalls to fetch issue comments and review comments are executed sequentially. Since they are independent, you can run them in parallel usingPromise.allto improve performance by reducing the total wait time for data fetching.