From 70f964070d94fbe899e3c1219761d959118f6bc3 Mon Sep 17 00:00:00 2001 From: wdawdwa <2864460459@qq.com> Date: Sat, 22 Aug 2026 17:51:45 +0800 Subject: [PATCH] feat: isolate comment command handlers and skip bot authors (4.2, 8.1) - /recheck, /conflict, and @bot now run independently via allSettled - bot-authored comments are skipped before dispatch to prevent loops --- src/actions/runReview.ts | 86 +++++++++++++++++++++++---------- test/chat-mention-rules.test.ts | 21 ++++++++ 2 files changed, 82 insertions(+), 25 deletions(-) create mode 100644 test/chat-mention-rules.test.ts diff --git a/src/actions/runReview.ts b/src/actions/runReview.ts index 3756de2..5e12aef 100644 --- a/src/actions/runReview.ts +++ b/src/actions/runReview.ts @@ -50,6 +50,7 @@ type IssueCommentPayload = { body: string; user: { login: string; + type?: string; }; }; repository: GitHubRepository; @@ -239,31 +240,66 @@ async function main(): Promise { return; } - await processRecheckComment(octokit, { - owner: commentPayload.repository.owner.login, - repo: commentPayload.repository.name, - pullNumber: commentPayload.issue.number, - commentId: commentPayload.comment.id, - commenterLogin: commentPayload.comment.user.login, - commentBody: commentPayload.comment.body, - gitToken: github.token - }); - await processConflictComment(octokit, { - owner: commentPayload.repository.owner.login, - repo: commentPayload.repository.name, - pullNumber: commentPayload.issue.number, - commentId: commentPayload.comment.id, - commenterLogin: commentPayload.comment.user.login, - commentBody: commentPayload.comment.body, - gitToken: github.token - }); - await processPullRequestChat(octokit, { - owner: commentPayload.repository.owner.login, - repo: commentPayload.repository.name, - pullNumber: commentPayload.issue.number, - commentId: commentPayload.comment.id, - commenterLogin: commentPayload.comment.user.login, - commentBody: commentPayload.comment.body + if (commentPayload.comment.user?.type === "Bot") { + logger.info( + { issueNumber: commentPayload.issue.number, login: commentPayload.comment.user.login }, + "Skipping bot-authored comment to prevent automation loops." + ); + return; + } + + // The three comment commands are independent features; one failing or + // not matching must not prevent the others from running. + const commentTasks: Array<[string, () => Promise]> = [ + [ + "recheck", + () => + processRecheckComment(octokit, { + owner: commentPayload.repository.owner.login, + repo: commentPayload.repository.name, + pullNumber: commentPayload.issue.number, + commentId: commentPayload.comment.id, + commenterLogin: commentPayload.comment.user.login, + commentBody: commentPayload.comment.body, + gitToken: github.token + }) + ], + [ + "conflict", + () => + processConflictComment(octokit, { + owner: commentPayload.repository.owner.login, + repo: commentPayload.repository.name, + pullNumber: commentPayload.issue.number, + commentId: commentPayload.comment.id, + commenterLogin: commentPayload.comment.user.login, + commentBody: commentPayload.comment.body, + gitToken: github.token + }) + ], + [ + "chat", + () => + processPullRequestChat(octokit, { + owner: commentPayload.repository.owner.login, + repo: commentPayload.repository.name, + pullNumber: commentPayload.issue.number, + commentId: commentPayload.comment.id, + commenterLogin: commentPayload.comment.user.login, + commentBody: commentPayload.comment.body + }) + ] + ]; + + const results = await Promise.allSettled(commentTasks.map(([, run]) => run())); + results.forEach((result, index) => { + if (result.status === "rejected") { + const [label] = commentTasks[index]!; + logger.error( + { error: result.reason, task: label, issueNumber: commentPayload.issue.number }, + "Comment command handler failed; other handlers still ran." + ); + } }); return; } diff --git a/test/chat-mention-rules.test.ts b/test/chat-mention-rules.test.ts new file mode 100644 index 0000000..3dfeddb --- /dev/null +++ b/test/chat-mention-rules.test.ts @@ -0,0 +1,21 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { containsBotMention } from "../src/chat/processor.js"; + +test("bot mention detection accepts configured aliases", () => { + assert.equal(containsBotMention("@bot help me", "ghbot"), true); + assert.equal(containsBotMention("@ghbot please review", "ghbot"), true); + assert.equal(containsBotMention("@ghbot[bot] ping", "ghbot[bot]"), true); +}); + +test("partial usernames and ordinary text never trigger the bot", () => { + assert.equal(containsBotMention("email me at user@botmail.com", "ghbot"), false); + assert.equal(containsBotMention("the @botanist knows", "ghbot"), false); + assert.equal(containsBotMention("no mention here", "ghbot"), false); + assert.equal(containsBotMention("@anotherbot hi", "ghbot"), false); +}); + +test("mention detection is case-insensitive", () => { + assert.equal(containsBotMention("@BOT hello", "ghbot"), true); + assert.equal(containsBotMention("@GHBOT[BOT] hello", "ghbot[bot]"), true); +});