From 6b3176777023e6f761a2bbb4684976413e19becc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BC=A8=E7=BC=A8?= Date: Fri, 20 Dec 2024 17:38:35 +0800 Subject: [PATCH] feat: add skip key words for the pull request handler and issue handler (#601) * feat: add skip key words for the issue handler * feat: add skip key words for the pull request handler --- server/agent/prompts/pull_request.py | 11 +++++++---- server/event_handler/discussion.py | 8 +++++++- server/event_handler/issue.py | 7 ++++++- server/event_handler/pull_request.py | 9 +++++++-- server/utils/fuzzy_match.py | 15 +++++++++++++++ 5 files changed, 42 insertions(+), 8 deletions(-) create mode 100644 server/utils/fuzzy_match.py diff --git a/server/agent/prompts/pull_request.py b/server/agent/prompts/pull_request.py index 2b6666c4..8d54e90f 100644 --- a/server/agent/prompts/pull_request.py +++ b/server/agent/prompts/pull_request.py @@ -15,7 +15,7 @@ - create_review_comment: Used to leave a review comment on specific files. # Task -You have two Pull Requst review task with basic infomation: +You have two Pull Request review task with basic information: ``` repo_name: {repo_name} pull_number: {pull_number} @@ -55,6 +55,10 @@ - The + sign means that code has been added. - The - sign means that code has been removed. +# Skip Task Whitelist +**SKIP_KEYWORDS**: A list of keywords. If any of these keywords are present in the PR title or description, the corresponding task will be skipped. +- Examples: "skip", "ignore", "wip", "merge", "[skip ci]" + # Constraints - Strictly avoid commenting on minor style inconsistencies, formatting issues, or changes that do not impact functionality. - Do not review files outside of the modified changeset (i.e., if a file has no diffs, it should not be reviewed). @@ -123,7 +127,6 @@ def get_role_prompt(repo_name: str, pull_number: int, title: str, description: s description=description, ) + def generate_pr_review_comment_prompt(pr_number: str, pr_content: str): - return PR_REVIEW_COMMENT_PROMPT.format( - pr_number=pr_number, pr_content=pr_content - ) + return PR_REVIEW_COMMENT_PROMPT.format(pr_number=pr_number, pr_content=pr_content) diff --git a/server/event_handler/discussion.py b/server/event_handler/discussion.py index 037c9e9c..caa65fa8 100644 --- a/server/event_handler/discussion.py +++ b/server/event_handler/discussion.py @@ -12,8 +12,10 @@ from agent.qa_chat import agent_chat +from utils.fuzzy_match import contains_keyword_fuzzy BOT_NAME = "petercat-assistant" +SKIP_KEYWORDS = ["RFC", "skip"] class DiscussionEventHandler: @@ -91,7 +93,11 @@ async def handle_discussion_event(self, action: str): owner = self.event["organization"]["login"] repo_name = self.event["repository"]["full_name"] discussion = self.event["discussion"] - discussion_content = f"{discussion['title']}: {discussion['body']}" + title = discussion["title"] + is_skip = contains_keyword_fuzzy(title, SKIP_KEYWORDS) + if is_skip: + return {"success": True} + discussion_content = f"{title}: {discussion['body']}" text_block = TextContentBlock(type="text", text=discussion_content) discussion_number = discussion["number"] message = Message(role="user", content=[text_block]) diff --git a/server/event_handler/issue.py b/server/event_handler/issue.py index be1734f1..e03f3627 100644 --- a/server/event_handler/issue.py +++ b/server/event_handler/issue.py @@ -8,11 +8,13 @@ generate_issue_comment_prompt, generate_issue_prompt, ) - from core.dao.repositoryConfigDAO import RepositoryConfigDAO from petercat_utils.data_class import ChatData, Message, TextContentBlock from agent.qa_chat import agent_chat +from utils.fuzzy_match import contains_keyword_fuzzy + +SKIP_KEYWORDS = ["RFC", "skip"] class IssueEventHandler: @@ -40,6 +42,9 @@ async def execute(self): return {"success": True} if action in ["opened", "reopened"]: issue, repo = self.get_issue() + is_skip = contains_keyword_fuzzy(issue.title, SKIP_KEYWORDS) + if is_skip: + return {"success": True} prompt = generate_issue_prompt( repo_name=repo.full_name, diff --git a/server/event_handler/pull_request.py b/server/event_handler/pull_request.py index 4eb06366..f42eed4b 100644 --- a/server/event_handler/pull_request.py +++ b/server/event_handler/pull_request.py @@ -12,6 +12,7 @@ from agent.bot.get_bot import get_bot_by_id from core.models.bot import BotModel +from utils.fuzzy_match import contains_keyword_fuzzy from utils.path_to_hunk import convert_patch_to_hunk from utils.random_str import random_str from agent.prompts.pull_request import ( @@ -22,6 +23,7 @@ from core.dao.repositoryConfigDAO import RepositoryConfigDAO from petercat_utils.data_class import ChatData, Message, TextContentBlock + def file_match(filename: str, patterns: List[str]): return any(fnmatch.fnmatch(filename, pattern) for pattern in patterns) @@ -132,6 +134,7 @@ async def execute(self): print(f"处理 GitHub 请求时出错:{e}") return {"success": False, "error": str(e)} + class PullRequestReviewCommentEventHandler(PullRequestEventHandler): def not_mentioned_me(self): return "@petercat-assistant" not in self.event["comment"]["body"] @@ -145,7 +148,7 @@ async def execute(self): if self.not_mentioned_me(): return {"success": True} - comment_id = self.event["comment"]['id'] + comment_id = self.event["comment"]["id"] pr, diff, repo = self.get_pull_request() file_diff = self.get_file_diff(diff) @@ -188,7 +191,9 @@ async def execute(self): bot, ) - pr.create_review_comment_reply(comment_id, analysis_result["output"]) + pr.create_review_comment_reply( + comment_id, analysis_result["output"] + ) except GithubException as e: print(f"处理 GitHub 请求时出错:{e}") diff --git a/server/utils/fuzzy_match.py b/server/utils/fuzzy_match.py new file mode 100644 index 00000000..ab3ea285 --- /dev/null +++ b/server/utils/fuzzy_match.py @@ -0,0 +1,15 @@ +import difflib + + +def contains_keyword_fuzzy(text, keywords, cutoff=0.8): + text_lower = text.lower() + for keyword in keywords: + keyword_lower = keyword.lower() + len_keyword = len(keyword_lower) + + for i in range(len(text_lower) - len_keyword + 1): + substring = text_lower[i : i + len_keyword] + matcher = difflib.SequenceMatcher(None, keyword_lower, substring) + if matcher.ratio() >= cutoff: + return True + return False