add polling

This commit is contained in:
salberts
2023-07-08 08:52:11 +03:00
parent e63a4f47ce
commit 75167c2700
7 changed files with 85 additions and 13 deletions

View File

@ -6,12 +6,12 @@ from pr_agent.tools.pr_reviewer import PRReviewer
class PRAgent: class PRAgent:
def __init__(self, installation_id: Optional[int] = None): def __init__(self):
self.installation_id = installation_id pass
async def handle_request(self, pr_url, request): async def handle_request(self, pr_url, request):
if 'please review' in request.lower() or 'review' == request.lower().strip() or len(request) == 0: if 'please review' in request.lower() or 'review' == request.lower().strip() or len(request) == 0:
reviewer = PRReviewer(pr_url, self.installation_id) reviewer = PRReviewer(pr_url)
await reviewer.review() await reviewer.review()
else: else:
@ -21,5 +21,5 @@ class PRAgent:
question = re.split(r'(?i)answer', request)[1].strip() question = re.split(r'(?i)answer', request)[1].strip()
else: else:
question = request question = request
answerer = PRQuestions(pr_url, question, self.installation_id) answerer = PRQuestions(pr_url, question)
await answerer.answer() await answerer.answer()

View File

@ -5,6 +5,7 @@ from dynaconf import Dynaconf
current_dir = dirname(abspath(__file__)) current_dir = dirname(abspath(__file__))
settings = Dynaconf( settings = Dynaconf(
envvar_prefix=False, envvar_prefix=False,
merge_enabled=True,
settings_files=[join(current_dir, f) for f in [ settings_files=[join(current_dir, f) for f in [
"settings/.secrets.toml", "settings/.secrets.toml",
"settings/configuration.toml", "settings/configuration.toml",

View File

@ -0,0 +1,66 @@
import asyncio
import time
from urllib.parse import urlparse
import gitlab
from pr_agent.agent.pr_agent import PRAgent
from pr_agent.config_loader import settings
gl = gitlab.Gitlab(
settings.get("GITLAB.URL"),
private_token=settings.get("GITLAB.PERSONAL_ACCESS_TOKEN")
)
# Set the list of projects to monitor
projects_to_monitor = settings.get("GITLAB.PROJECTS_TO_MONITOR")
magic_word = settings.get("GITLAB.MAGIC_WORD")
print(projects_to_monitor, magic_word, settings.get("GITLAB.PERSONAL_ACCESS_TOKEN"))
# Hold the previous seen comments
previous_comments = set()
def check_comments():
print('Polling')
new_comments = {}
for project in projects_to_monitor:
project = gl.projects.get(project)
merge_requests = project.mergerequests.list(state='opened')
for mr in merge_requests:
notes = mr.notes.list(iterator=True)
for note in notes:
if note.id not in previous_comments and note.body.startswith(magic_word):
new_comments[note.id] = dict(
body=note.body[len(magic_word):],
project=project.name,
mr=mr
)
previous_comments.add(note.id)
print(f"New comment in project {project.name}, merge request {mr.title}: {note.body}")
return new_comments
def handle_new_comments(new_comments):
print('Handling new comments')
agent = PRAgent()
for _, comment in new_comments.items():
print(f"Handling comment: {comment['body']}")
asyncio.run(agent.handle_request(comment['mr'].web_url, comment['body']))
def run():
# Initial run to populate previous_comments
check_comments()
# Run the check every minute
while True:
# time.sleep(60)
new_comments = check_comments()
if new_comments:
handle_new_comments(new_comments)
if __name__ == '__main__':
run()

View File

@ -27,8 +27,4 @@ app_id = 123456 # The GitHub App ID, replace with your own.
webhook_secret = "<WEBHOOK SECRET>" # Optional, may be commented out. webhook_secret = "<WEBHOOK SECRET>" # Optional, may be commented out.
[gitlab] [gitlab]
# The type of deployment to create. Valid values are 'app' or 'user'. personal_access_token = ""
personal_access_token = ""
# URL to the gitlab service
gitlab_url = "https://gitlab.com"

View File

@ -12,4 +12,14 @@ extended_code_suggestions=false
num_code_suggestions=4 num_code_suggestions=4
[pr_questions] [pr_questions]
[gitlab]
# URL to the gitlab service
gitlab_url = "https://gitlab.com"
# Polling
projects_to_monitor = [47494341]
# Polling trigger
magic_word = "MagicRound"

View File

@ -13,12 +13,11 @@ from pr_agent.git_providers.git_provider import get_main_pr_language
class PRQuestions: class PRQuestions:
def __init__(self, pr_url: str, question_str: str, installation_id: Optional[int] = None): def __init__(self, pr_url: str, question_str: str):
self.git_provider = get_git_provider()(pr_url) self.git_provider = get_git_provider()(pr_url)
self.main_pr_language = get_main_pr_language( self.main_pr_language = get_main_pr_language(
self.git_provider.get_languages(), self.git_provider.get_files() self.git_provider.get_languages(), self.git_provider.get_files()
) )
self.installation_id = installation_id
self.ai_handler = AiHandler() self.ai_handler = AiHandler()
self.question_str = question_str self.question_str = question_str
self.vars = { self.vars = {

View File

@ -15,7 +15,7 @@ from pr_agent.git_providers.git_provider import get_main_pr_language
class PRReviewer: class PRReviewer:
def __init__(self, pr_url: str, installation_id: Optional[int] = None, cli_mode=False): def __init__(self, pr_url: str, cli_mode=False):
self.git_provider = get_git_provider()(pr_url) self.git_provider = get_git_provider()(pr_url)
self.main_language = get_main_pr_language( self.main_language = get_main_pr_language(