From 6648c04799a79c2d18df894cdf08c5606b6e8721 Mon Sep 17 00:00:00 2001 From: Ori Kotek Date: Thu, 6 Jul 2023 17:46:43 +0300 Subject: [PATCH 1/3] Protect from notifications that may be handled twice by keeping a set of handled notification IDs --- .dockerignore | 3 ++- pr_agent/config_loader.py | 3 ++- pr_agent/servers/github_polling.py | 4 ++++ 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/.dockerignore b/.dockerignore index eba74f4c..9c8ebe57 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1 +1,2 @@ -venv/ \ No newline at end of file +venv/ +pr_agent/settings/.secrets.toml \ No newline at end of file diff --git a/pr_agent/config_loader.py b/pr_agent/config_loader.py index 394c877f..24dfdea7 100644 --- a/pr_agent/config_loader.py +++ b/pr_agent/config_loader.py @@ -9,6 +9,7 @@ settings = Dynaconf( "settings/.secrets.toml", "settings/configuration.toml", "settings/pr_reviewer_prompts.toml", - "settings/pr_questions_prompts.toml" + "settings/pr_questions_prompts.toml", + "settings_prod/.secrets.toml" ]] ) diff --git a/pr_agent/servers/github_polling.py b/pr_agent/servers/github_polling.py index e1e6fa84..46adb4f0 100644 --- a/pr_agent/servers/github_polling.py +++ b/pr_agent/servers/github_polling.py @@ -20,6 +20,7 @@ def now() -> str: async def polling_loop(): + handled_ids = set() since = [now()] last_modified = [None] git_provider = get_git_provider()() @@ -54,6 +55,9 @@ async def polling_loop(): since[0] = None notifications = await response.json() for notification in notifications: + if 'id' in notification and notification['id'] in handled_ids: + continue + handled_ids.add(notification['id']) if 'reason' in notification and notification['reason'] == 'mention': if 'subject' in notification and notification['subject']['type'] == 'PullRequest': pr_url = notification['subject']['url'] From d04c0f490c8577b2860dedee3abdec3168306dbb Mon Sep 17 00:00:00 2001 From: Ori Kotek Date: Thu, 6 Jul 2023 17:52:12 +0300 Subject: [PATCH 2/3] Don't add "How to use" when running from the command line --- pr_agent/cli.py | 4 ++-- pr_agent/tools/pr_reviewer.py | 7 +++++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/pr_agent/cli.py b/pr_agent/cli.py index 44d631a1..0d32e921 100644 --- a/pr_agent/cli.py +++ b/pr_agent/cli.py @@ -15,11 +15,11 @@ def run(): logging.basicConfig(level=os.environ.get("LOGLEVEL", "INFO")) if args.question: print(f"Question: {args.question} about PR {args.pr_url}") - reviewer = PRQuestions(args.pr_url, args.question, None) + reviewer = PRQuestions(args.pr_url, args.question, installation_id=None) asyncio.run(reviewer.answer()) else: print(f"Reviewing PR: {args.pr_url}") - reviewer = PRReviewer(args.pr_url, None) + reviewer = PRReviewer(args.pr_url, installation_id=None, cli_mode=True) asyncio.run(reviewer.review()) diff --git a/pr_agent/tools/pr_reviewer.py b/pr_agent/tools/pr_reviewer.py index a7ebc2ef..33dd0162 100644 --- a/pr_agent/tools/pr_reviewer.py +++ b/pr_agent/tools/pr_reviewer.py @@ -14,7 +14,7 @@ from pr_agent.git_providers import get_git_provider class PRReviewer: - def __init__(self, pr_url: str, installation_id: Optional[int] = None): + def __init__(self, pr_url: str, installation_id: Optional[int] = None, cli_mode=False): self.git_provider = get_git_provider()(pr_url, installation_id) self.main_language = self.git_provider.get_main_pr_language() @@ -22,6 +22,7 @@ class PRReviewer: self.ai_handler = AiHandler() self.patches_diff = None self.prediction = None + self.cli_mode = cli_mode self.vars = { "title": self.git_provider.pr.title, "branch": self.git_provider.get_pr_branch(), @@ -92,7 +93,9 @@ class PRReviewer: markdown_text = convert_to_markdown(data) user = self.git_provider.get_user_id() markdown_text += "\n### How to use\n" - if user and '[bot]' not in user: + if self.cli_mode: + pass + elif user and '[bot]' not in user: markdown_text += f"> Tag me in a comment '@{user}' to ask for a new review after you update the PR.\n" markdown_text += "> You can also tag me and ask any question, " \ f"for example '@{user} is the PR ready for merge?'" From 4331610e0135ebeef1471181cbf44ac6e7fc9fc5 Mon Sep 17 00:00:00 2001 From: Ori Kotek Date: Thu, 6 Jul 2023 17:53:52 +0300 Subject: [PATCH 3/3] Don't add "How to use" when running from the command line - a small correction --- pr_agent/tools/pr_reviewer.py | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/pr_agent/tools/pr_reviewer.py b/pr_agent/tools/pr_reviewer.py index 33dd0162..e88af62a 100644 --- a/pr_agent/tools/pr_reviewer.py +++ b/pr_agent/tools/pr_reviewer.py @@ -92,18 +92,19 @@ class PRReviewer: markdown_text = convert_to_markdown(data) user = self.git_provider.get_user_id() - markdown_text += "\n### How to use\n" - if self.cli_mode: - pass - elif user and '[bot]' not in user: - markdown_text += f"> Tag me in a comment '@{user}' to ask for a new review after you update the PR.\n" - markdown_text += "> You can also tag me and ask any question, " \ - f"for example '@{user} is the PR ready for merge?'" - else: - markdown_text += "> Add a comment that says 'review' to ask for a new review " \ - "after you update the PR.\n" - markdown_text += "> You can also add a comment that says 'answer QUESTION', " \ - "for example 'answer is the PR ready for merge?'" + + if not self.cli_mode: + markdown_text += "\n### How to use\n" + if user and '[bot]' not in user: + markdown_text += "\n### How to use\n" + markdown_text += f"> Tag me in a comment '@{user}' to ask for a new review after you update the PR.\n" + markdown_text += "> You can also tag me and ask any question, " \ + f"for example '@{user} is the PR ready for merge?'" + else: + markdown_text += "> Add a comment that says 'review' to ask for a new review " \ + "after you update the PR.\n" + markdown_text += "> You can also add a comment that says 'answer QUESTION', " \ + "for example 'answer is the PR ready for merge?'" if settings.config.verbosity_level >= 2: logging.info(f"Markdown response:\n{markdown_text}")