Skip to content

Commit

Permalink
Refactor CLI argument handling and request processing
Browse files Browse the repository at this point in the history
  • Loading branch information
okotek committed Nov 12, 2023
1 parent 5e43c20 commit 9e5e9af
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 15 deletions.
11 changes: 7 additions & 4 deletions pr_agent/agent/pr_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,13 @@ async def handle_request(self, pr_url, request, notify=None) -> bool:
apply_repo_settings(pr_url)

# Then, apply user specific settings if exists
request = request.replace("'", "\\'")
lexer = shlex.shlex(request, posix=True)
lexer.whitespace_split = True
action, *args = list(lexer)
if isinstance(request, str):
request = request.replace("'", "\\'")
lexer = shlex.shlex(request, posix=True)
lexer.whitespace_split = True
action, *args = list(lexer)
else:
action, *args = request
args = update_settings_from_args(args)

action = action.lstrip("/").lower()
Expand Down
13 changes: 2 additions & 11 deletions pr_agent/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,6 @@
setup_logger()


def handle_args_with_quotes(args):
if args.rest:
for i, r in enumerate(args.rest):
if r.startswith("--") and r.count("=") == 1 and " " in r:
r_split = r.split("=")
args.rest[i] = r_split[0] + "=" + '"' + r_split[1] + '"'
return args


def run(inargs=None):
parser = argparse.ArgumentParser(description='AI based pull request analyzer', usage=
Expand Down Expand Up @@ -54,17 +46,16 @@ def run(inargs=None):
parser.add_argument('command', type=str, help='The', choices=commands, default='review')
parser.add_argument('rest', nargs=argparse.REMAINDER, default=[])
args = parser.parse_args(inargs)
args = handle_args_with_quotes(args)
if not args.pr_url and not args.issue_url:
parser.print_help()
return

command = args.command.lower()
get_settings().set("CONFIG.CLI_MODE", True)
if args.issue_url:
result = asyncio.run(PRAgent().handle_request(args.issue_url, command + " " + " ".join(args.rest)))
result = asyncio.run(PRAgent().handle_request(args.issue_url, [command] + args.rest))
else:
result = asyncio.run(PRAgent().handle_request(args.pr_url, command + " " + " ".join(args.rest)))
result = asyncio.run(PRAgent().handle_request(args.pr_url, [command] + args.rest))
if not result:
parser.print_help()

Expand Down

0 comments on commit 9e5e9af

Please sign in to comment.