2023-07-06 11:37:44 +03:00
|
|
|
import argparse
|
|
|
|
import asyncio
|
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
|
2023-07-15 09:30:50 +03:00
|
|
|
from pr_agent.tools.pr_code_suggestions import PRCodeSuggestions
|
2023-07-13 17:24:56 +03:00
|
|
|
from pr_agent.tools.pr_description import PRDescription
|
2023-07-16 19:36:20 +03:00
|
|
|
from pr_agent.tools.pr_information_from_user import PRInformationFromUser
|
2023-07-06 11:37:44 +03:00
|
|
|
from pr_agent.tools.pr_questions import PRQuestions
|
|
|
|
from pr_agent.tools.pr_reviewer import PRReviewer
|
2023-07-26 09:21:31 +03:00
|
|
|
from pr_agent.tools.pr_update_changelog import PRUpdateChangelog
|
2023-07-06 11:37:44 +03:00
|
|
|
|
|
|
|
|
2023-07-18 16:22:41 +03:00
|
|
|
def run(args=None):
|
2023-07-21 22:12:51 +03:00
|
|
|
parser = argparse.ArgumentParser(description='AI based pull request analyzer', usage=
|
|
|
|
"""\
|
2023-07-16 13:18:29 +03:00
|
|
|
Usage: cli.py --pr-url <URL on supported git hosting service> <command> [<args>].
|
2023-07-16 14:59:40 +03:00
|
|
|
For example:
|
2023-07-17 08:18:42 +03:00
|
|
|
- cli.py --pr-url=... review
|
|
|
|
- cli.py --pr-url=... describe
|
|
|
|
- cli.py --pr-url=... improve
|
|
|
|
- cli.py --pr-url=... ask "write me a poem about this PR"
|
2023-07-18 08:22:25 +03:00
|
|
|
- cli.py --pr-url=... reflect
|
2023-07-16 13:18:29 +03:00
|
|
|
|
|
|
|
Supported commands:
|
|
|
|
review / review_pr - Add a review that includes a summary of the PR and specific suggestions for improvement.
|
|
|
|
ask / ask_question [question] - Ask a question about the PR.
|
|
|
|
describe / describe_pr - Modify the PR title and description based on the PR's contents.
|
|
|
|
improve / improve_code - Suggest improvements to the code in the PR as pull request comments ready to commit.
|
2023-07-18 08:22:25 +03:00
|
|
|
reflect - Ask the PR author questions about the PR.
|
2023-07-26 09:21:31 +03:00
|
|
|
update_changelog - Update the changelog based on the PR's contents.
|
2023-07-16 13:18:29 +03:00
|
|
|
""")
|
2023-07-06 11:37:44 +03:00
|
|
|
parser.add_argument('--pr_url', type=str, help='The URL of the PR to review', required=True)
|
2023-07-16 13:18:29 +03:00
|
|
|
parser.add_argument('command', type=str, help='The', choices=['review', 'review_pr',
|
|
|
|
'ask', 'ask_question',
|
|
|
|
'describe', 'describe_pr',
|
2023-07-16 19:36:20 +03:00
|
|
|
'improve', 'improve_code',
|
2023-07-26 09:21:31 +03:00
|
|
|
'reflect', 'review_after_reflect',
|
|
|
|
'update_changelog'],
|
2023-07-21 22:12:51 +03:00
|
|
|
default='review')
|
2023-07-16 13:18:29 +03:00
|
|
|
parser.add_argument('rest', nargs=argparse.REMAINDER, default=[])
|
2023-07-18 16:22:41 +03:00
|
|
|
args = parser.parse_args(args)
|
2023-07-06 11:37:44 +03:00
|
|
|
logging.basicConfig(level=os.environ.get("LOGLEVEL", "INFO"))
|
2023-07-16 13:18:29 +03:00
|
|
|
command = args.command.lower()
|
2023-07-21 22:12:51 +03:00
|
|
|
commands = {
|
|
|
|
'ask': _handle_ask_command,
|
|
|
|
'ask_question': _handle_ask_command,
|
|
|
|
'describe': _handle_describe_command,
|
|
|
|
'describe_pr': _handle_describe_command,
|
|
|
|
'improve': _handle_improve_command,
|
|
|
|
'improve_code': _handle_improve_command,
|
|
|
|
'review': _handle_review_command,
|
|
|
|
'review_pr': _handle_review_command,
|
|
|
|
'reflect': _handle_reflect_command,
|
2023-07-26 09:21:31 +03:00
|
|
|
'review_after_reflect': _handle_review_after_reflect_command,
|
|
|
|
'update_changelog': _handle_update_changelog,
|
2023-07-21 22:12:51 +03:00
|
|
|
}
|
|
|
|
if command in commands:
|
|
|
|
commands[command](args.pr_url, args.rest)
|
2023-07-16 13:18:29 +03:00
|
|
|
else:
|
|
|
|
print(f"Unknown command: {command}")
|
|
|
|
parser.print_help()
|
2023-07-06 11:37:44 +03:00
|
|
|
|
|
|
|
|
2023-07-21 22:12:51 +03:00
|
|
|
def _handle_ask_command(pr_url: str, rest: list):
|
|
|
|
if len(rest) == 0:
|
|
|
|
print("Please specify a question")
|
|
|
|
return
|
|
|
|
print(f"Question: {' '.join(rest)} about PR {pr_url}")
|
|
|
|
reviewer = PRQuestions(pr_url, rest)
|
|
|
|
asyncio.run(reviewer.answer())
|
|
|
|
|
|
|
|
|
|
|
|
def _handle_describe_command(pr_url: str, rest: list):
|
|
|
|
print(f"PR description: {pr_url}")
|
|
|
|
reviewer = PRDescription(pr_url)
|
|
|
|
asyncio.run(reviewer.describe())
|
|
|
|
|
|
|
|
|
|
|
|
def _handle_improve_command(pr_url: str, rest: list):
|
|
|
|
print(f"PR code suggestions: {pr_url}")
|
|
|
|
reviewer = PRCodeSuggestions(pr_url)
|
|
|
|
asyncio.run(reviewer.suggest())
|
|
|
|
|
|
|
|
|
|
|
|
def _handle_review_command(pr_url: str, rest: list):
|
|
|
|
print(f"Reviewing PR: {pr_url}")
|
|
|
|
reviewer = PRReviewer(pr_url, cli_mode=True, args=rest)
|
|
|
|
asyncio.run(reviewer.review())
|
|
|
|
|
|
|
|
|
|
|
|
def _handle_reflect_command(pr_url: str, rest: list):
|
|
|
|
print(f"Asking the PR author questions: {pr_url}")
|
|
|
|
reviewer = PRInformationFromUser(pr_url)
|
|
|
|
asyncio.run(reviewer.generate_questions())
|
|
|
|
|
|
|
|
|
|
|
|
def _handle_review_after_reflect_command(pr_url: str, rest: list):
|
|
|
|
print(f"Processing author's answers and sending review: {pr_url}")
|
|
|
|
reviewer = PRReviewer(pr_url, cli_mode=True, is_answer=True)
|
|
|
|
asyncio.run(reviewer.review())
|
|
|
|
|
2023-07-26 09:21:31 +03:00
|
|
|
def _handle_update_changelog(pr_url: str, rest: list):
|
|
|
|
print(f"Updating changlog for: {pr_url}")
|
2023-07-27 08:47:26 +03:00
|
|
|
reviewer = PRUpdateChangelog(pr_url, cli_mode=True, args=rest)
|
2023-07-26 09:21:31 +03:00
|
|
|
asyncio.run(reviewer.update_changelog())
|
2023-07-21 22:12:51 +03:00
|
|
|
|
2023-07-06 11:37:44 +03:00
|
|
|
if __name__ == '__main__':
|
|
|
|
run()
|