Merge pull request #59 from Codium-ai/enhancement/github_action_apply

Update Github polling
This commit is contained in:
Ori Kotek
2023-07-16 15:03:44 +03:00
committed by GitHub
3 changed files with 42 additions and 13 deletions

View File

@ -1,5 +1,6 @@
import asyncio import asyncio
import logging import logging
import re
import sys import sys
from datetime import datetime, timezone from datetime import datetime, timezone
@ -8,6 +9,11 @@ import aiohttp
from pr_agent.agent.pr_agent import PRAgent from pr_agent.agent.pr_agent import PRAgent
from pr_agent.config_loader import settings from pr_agent.config_loader import settings
from pr_agent.git_providers import get_git_provider from pr_agent.git_providers import get_git_provider
from pr_agent.servers.help import bot_help_text
from pr_agent.tools.pr_code_suggestions import PRCodeSuggestions
from pr_agent.tools.pr_description import PRDescription
from pr_agent.tools.pr_questions import PRQuestions
from pr_agent.tools.pr_reviewer import PRReviewer
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG) logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
NOTIFICATION_URL = "https://api.github.com/notifications" NOTIFICATION_URL = "https://api.github.com/notifications"
@ -83,8 +89,24 @@ async def polling_loop():
if user_tag not in comment_body: if user_tag not in comment_body:
continue continue
rest_of_comment = comment_body.split(user_tag)[1].strip() rest_of_comment = comment_body.split(user_tag)[1].strip()
agent = PRAgent()
await agent.handle_request(pr_url, rest_of_comment) if any(cmd in rest_of_comment for cmd in ["/review", "/review_pr"]):
await PRReviewer(pr_url).review()
elif any(cmd in rest_of_comment for cmd in ["/describe", "/describe_pr"]):
await PRDescription(pr_url).describe()
elif any(cmd in rest_of_comment for cmd in ["/improve", "/improve_code"]):
await PRCodeSuggestions(pr_url).suggest()
elif any(cmd in rest_of_comment for cmd in ["/ask", "/ask_question"]):
pattern = r'(/ask|/ask_question)\s*(.*)'
matches = re.findall(pattern, rest_of_comment, re.IGNORECASE)
if matches:
question = matches[0][1]
await PRQuestions(pr_url, question).answer()
else:
git_provider.set_pr(pr_url)
git_provider.publish_comment("### How to user PR-Agent\n" +
bot_help_text(user_id))
elif response.status != 304: elif response.status != 304:
print(f"Failed to fetch notifications. Status code: {response.status}") print(f"Failed to fetch notifications. Status code: {response.status}")

14
pr_agent/servers/help.py Normal file
View File

@ -0,0 +1,14 @@
commands_text = "> /review - Ask for a new review after your update the PR\n" \
"> /describe - Modify the PR title and description based " \
"on the PR's contents.\n" \
"> /improve - Suggest improvements to the code in the PR as pull " \
"request comments ready to commit.\n" \
"> /ask <QUESTION> - Ask a question about the PR.\n"
def bot_help_text(user: str):
return f"> Tag me in a comment '@{user}' and add one of the following commands:\n" + commands_text
actions_help_text = "> Add a comment to to invoke PR-Agent, use one of the following commands:\n" + \
commands_text

View File

@ -11,6 +11,7 @@ from pr_agent.algo.utils import convert_to_markdown, try_fix_json
from pr_agent.config_loader import settings from pr_agent.config_loader import settings
from pr_agent.git_providers import get_git_provider from pr_agent.git_providers import get_git_provider
from pr_agent.git_providers.git_provider import get_main_pr_language from pr_agent.git_providers.git_provider import get_main_pr_language
from pr_agent.servers.help import bot_help_text, actions_help_text
class PRReviewer: class PRReviewer:
@ -43,7 +44,7 @@ class PRReviewer:
async def review(self): async def review(self):
logging.info('Reviewing PR...') logging.info('Reviewing PR...')
if settings.config.publish_review: if settings.config.publish_review:
self.git_provider.publish_comment("Preparing review...", is_temporary=True) self.git_provider.publish_comment("Preparing review...", is_temporary=True)
logging.info('Getting PR diff...') logging.info('Getting PR diff...')
self.patches_diff = get_pr_diff(self.git_provider, self.token_handler) self.patches_diff = get_pr_diff(self.git_provider, self.token_handler)
logging.info('Getting AI prediction...') logging.info('Getting AI prediction...')
@ -96,18 +97,10 @@ class PRReviewer:
if not self.cli_mode: if not self.cli_mode:
markdown_text += "\n### How to use\n" markdown_text += "\n### How to use\n"
commands_text = "> /review - Ask for a new review after your update the PR\n" \
"> /describe - Modify the PR title and description based " \
"on the PR's contents.\n" \
"> /improve - Suggest improvements to the code in the PR as pull " \
"request comments ready to commit.\n" \
"> /ask <QUESTION> - Ask a question about the PR.\n"
if user and '[bot]' not in user: if user and '[bot]' not in user:
markdown_text += f"> Tag me in a comment '@{user}' and add one of the following commands:\n" + \ markdown_text += bot_help_text(user)
commands_text
else: else:
markdown_text += "> Add a comment to to invoke PR-Agent, use one of the following commands:\n" + \ markdown_text += actions_help_text
commands_text
if settings.config.verbosity_level >= 2: if settings.config.verbosity_level >= 2:
logging.info(f"Markdown response:\n{markdown_text}") logging.info(f"Markdown response:\n{markdown_text}")