Skip to content

Commit

Permalink
feat: add skip key words for the pull request handler and issue handl…
Browse files Browse the repository at this point in the history
…er (#601)

* feat: add skip key words for the issue handler

* feat: add skip key words for the pull request handler
  • Loading branch information
xingwanying authored Dec 20, 2024
1 parent f5443ba commit 6b31767
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 8 deletions.
11 changes: 7 additions & 4 deletions server/agent/prompts/pull_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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).
Expand Down Expand Up @@ -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)
8 changes: 7 additions & 1 deletion server/event_handler/discussion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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])
Expand Down
7 changes: 6 additions & 1 deletion server/event_handler/issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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,
Expand Down
9 changes: 7 additions & 2 deletions server/event_handler/pull_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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)

Expand Down Expand Up @@ -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"]
Expand All @@ -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)

Expand Down Expand Up @@ -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}")
Expand Down
15 changes: 15 additions & 0 deletions server/utils/fuzzy_match.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 6b31767

Please sign in to comment.