From f75ffc0736942c8ffba2e05aea5fb2192fde1ad8 Mon Sep 17 00:00:00 2001 From: Hiroshi Nishio Date: Sun, 27 Oct 2024 19:59:42 -0700 Subject: [PATCH] Refactor real-time comment updates --- services/check_run_handler.py | 38 ++++++++++++----------------- services/gitauto_handler.py | 40 ++++++++++++++----------------- services/github/github_manager.py | 5 +++- 3 files changed, 37 insertions(+), 46 deletions(-) diff --git a/services/check_run_handler.py b/services/check_run_handler.py index f4f2475d..77cc5c21 100644 --- a/services/check_run_handler.py +++ b/services/check_run_handler.py @@ -131,17 +131,15 @@ def handle_check_run(payload: CheckRunCompletedPayload) -> None: base_args["comment_url"] = comment_url # Get title, body, and code changes in the PR - msg = "Checking out the pull request title, body, and code changes..." - comment_body = create_progress_bar(p=5, msg=msg) - update_comment(body=comment_body, base_args=base_args) + comment_body = "Checking out the pull request title, body, and code changes..." + update_comment(body=comment_body, base_args=base_args, p=5) pull_title, pull_body = get_pull_request(url=pull_url, token=token) pull_file_url = f"{pull_url}/files" pull_changes = get_pull_request_files(url=pull_file_url, token=token) # Get the GitHub workflow file content - msg = "Checking out the GitHub Action workflow file..." - comment_body = create_progress_bar(p=10, msg=msg) - update_comment(body=comment_body, base_args=base_args) + comment_body = "Checking out the GitHub Action workflow file..." + update_comment(body=comment_body, base_args=base_args, p=10) workflow_path = get_workflow_run_path( owner=owner_name, repo=repo_name, run_id=workflow_run_id, token=token ) @@ -150,15 +148,13 @@ def handle_check_run(payload: CheckRunCompletedPayload) -> None: ) # Get the file tree in the root of the repo - msg = "Checking out the file tree in the root of the repo..." - comment_body = create_progress_bar(p=15, msg=msg) - update_comment(body=comment_body, base_args=base_args) + comment_body = "Checking out the file tree in the root of the repo..." + update_comment(body=comment_body, base_args=base_args, p=15) file_tree: str = get_remote_file_tree(base_args=base_args) # Get the error log from the workflow run - msg = "Checking out the error log from the workflow run..." - comment_body = create_progress_bar(p=20, msg=msg) - update_comment(body=comment_body, base_args=base_args) + comment_body = "Checking out the error log from the workflow run..." + update_comment(body=comment_body, base_args=base_args, p=20) error_log: str | None = get_workflow_run_logs( owner=owner_name, repo=repo_name, run_id=workflow_run_id, token=token ) @@ -166,9 +162,8 @@ def handle_check_run(payload: CheckRunCompletedPayload) -> None: return # Plan how to fix the error - msg = "Planning how to fix the error..." - comment_body = create_progress_bar(p=25, msg=msg) - update_comment(body=comment_body, base_args=base_args) + comment_body = "Planning how to fix the error..." + update_comment(body=comment_body, base_args=base_args, p=25) input_message: dict[str, str] = { "pull_request_title": pull_title, "pull_request_body": pull_body, @@ -183,9 +178,8 @@ def handle_check_run(payload: CheckRunCompletedPayload) -> None: print(how_to_fix) # Update the comment if any obstacles are found - msg = "Checking if I can solve it or if I should just hit you up..." - comment_body = create_progress_bar(p=30, msg=msg) - update_comment(body=comment_body, base_args=base_args) + comment_body = "Checking if I can solve it or if I should just hit you up..." + update_comment(body=comment_body, base_args=base_args, p=30) messages = [{"role": "user", "content": how_to_fix}] ( _messages, @@ -228,9 +222,8 @@ def handle_check_run(payload: CheckRunCompletedPayload) -> None: mode="get", # explore can not be used here because "search_remote_file_contents" can search files only in the default branch NOT in the branch that is merged into the default branch previous_calls=previous_calls, ) - msg = f"Calling `{tool_name}()` with `{tool_args}`..." - comment_body = create_progress_bar(p=p, msg=msg) - update_comment(body=comment_body, base_args=base_args) + comment_body = f"Calling `{tool_name}()` with `{tool_args}`..." + update_comment(body=comment_body, base_args=base_args, p=p) p = min(p + 5, 95) # Commit changes based on the exploration information @@ -249,8 +242,7 @@ def handle_check_run(payload: CheckRunCompletedPayload) -> None: previous_calls=previous_calls, ) msg = f"Calling `{tool_name}()` with `{tool_args}`..." - comment_body = create_progress_bar(p=p, msg=msg) - update_comment(body=comment_body, base_args=base_args) + update_comment(body=comment_body, base_args=base_args, p=p) p = min(p + 5, 95) # If no new file is found and no changes are made, it means that the agent has completed the ticket or got stuck for some reason diff --git a/services/gitauto_handler.py b/services/gitauto_handler.py index 4613d98a..da1c64f4 100644 --- a/services/gitauto_handler.py +++ b/services/gitauto_handler.py @@ -146,25 +146,25 @@ async def handle_gitauto(payload: GitHubLabeledPayload, trigger_type: str) -> No ) # Check out the issue comments, and root files/directories list - msg = "Checking out the issue title, body, comments, and root files/directories list..." - comment_body = create_progress_bar(p=10, msg=msg) - update_comment(body=comment_body, base_args=base_args) + comment_body = ( + "Checking out the issue title, body, comments, and root files list..." + ) + update_comment(body=comment_body, base_args=base_args, p=10) root_files_and_dirs: list[str] = get_remote_file_tree(base_args=base_args) issue_comments = get_issue_comments(issue_number=issue_number, base_args=base_args) # Check out the URLs in the issue body reference_contents: list[str] = [] for url in github_urls: - msg = "Also checking out the URLs in the issue body..." - comment_body = create_progress_bar(p=15, msg=msg) - update_comment(body=comment_body, base_args=base_args) + comment_body = "Also checking out the URLs in the issue body..." + update_comment(body=comment_body, base_args=base_args, p=15) content = get_remote_file_content_by_url(url=url, token=token) print(f"```{url}\n{content}```\n") reference_contents.append(content) # Write a pull request body - comment_body = create_progress_bar(p=20, msg="Writing up the pull request body...") - update_comment(body=comment_body, base_args=base_args) + comment_body = "Writing up the pull request body..." + update_comment(body=comment_body, base_args=base_args, p=20) pr_body: str = chat_with_ai( system_input=WRITE_PR_BODY, user_input=json.dumps( @@ -180,9 +180,8 @@ async def handle_gitauto(payload: GitHubLabeledPayload, trigger_type: str) -> No base_args["pr_body"] = pr_body # Update the comment if any obstacles are found - msg = "Checking if I can solve it or if I should just hit you up..." - comment_body = create_progress_bar(p=25, msg=msg) - update_comment(body=comment_body, base_args=base_args) + comment_body = "Checking if I can solve it or if I should just hit you up..." + update_comment(body=comment_body, base_args=base_args, p=25) messages = [{"role": "user", "content": pr_body}] ( _messages, @@ -197,9 +196,8 @@ async def handle_gitauto(payload: GitHubLabeledPayload, trigger_type: str) -> No return # Create a remote branch - msg = "Looks like it's doable. Creating the remote branch..." - comment_body = create_progress_bar(p=30, msg=msg) - update_comment(body=comment_body, base_args=base_args) + comment_body = "Looks like it's doable. Creating the remote branch..." + update_comment(body=comment_body, base_args=base_args, p=30) latest_commit_sha: str = get_latest_remote_commit_sha( unique_issue_id=unique_issue_id, clone_url=repo["clone_url"], @@ -227,9 +225,8 @@ async def handle_gitauto(payload: GitHubLabeledPayload, trigger_type: str) -> No mode="explore", previous_calls=previous_calls, ) - msg = f"Calling `{tool_name}()` with `{tool_args}`..." - comment_body = create_progress_bar(p=p, msg=msg) - update_comment(body=comment_body, base_args=base_args) + comment_body = f"Calling `{tool_name}()` with `{tool_args}`..." + update_comment(body=comment_body, base_args=base_args, p=p) p = min(p + 5, 85) # Commit changes based on the exploration information @@ -247,9 +244,8 @@ async def handle_gitauto(payload: GitHubLabeledPayload, trigger_type: str) -> No mode="commit", previous_calls=previous_calls, ) - msg = f"Calling `{tool_name}()` with `{tool_args}`..." - comment_body = create_progress_bar(p=p, msg=msg) - update_comment(body=comment_body, base_args=base_args) + comment_body = f"Calling `{tool_name}()` with `{tool_args}`..." + update_comment(body=comment_body, base_args=base_args, p=p) p = min(p + 5, 85) # If no new file is found and no changes are made, it means that the agent has completed the ticket or got stuck for some reason @@ -272,8 +268,8 @@ async def handle_gitauto(payload: GitHubLabeledPayload, trigger_type: str) -> No retry_count = 0 # Create a pull request to the base branch - comment_body = create_progress_bar(p=90, msg="Creating a pull request...") - update_comment(body=comment_body, base_args=base_args) + comment_body = "Creating a pull request..." + update_comment(body=comment_body, base_args=base_args, p=90) title = f"{PRODUCT_NAME}: {issue_title}" issue_link: str = f"{PR_BODY_STARTS_WITH}{issue_number}\n\n" pr_body = issue_link + pr_body + git_command(new_branch_name=new_branch_name) diff --git a/services/github/github_manager.py b/services/github/github_manager.py index 956940c8..600a72bc 100644 --- a/services/github/github_manager.py +++ b/services/github/github_manager.py @@ -51,6 +51,7 @@ from utils.file_manager import apply_patch, get_file_content, run_command from utils.handle_exceptions import handle_exceptions from utils.parse_urls import parse_github_url +from utils.progress_bar import create_progress_bar from utils.text_copy import request_issue_comment, request_limit_reached @@ -764,9 +765,11 @@ async def verify_webhook_signature(request: Request, secret: str) -> None: @handle_exceptions(default_return_value=None, raise_on_error=False) -def update_comment(body: str, base_args: BaseArgs) -> dict[str, Any]: +def update_comment(body: str, base_args: BaseArgs, p: int | None = None) -> dict[str, Any]: """https://docs.github.com/en/rest/issues/comments#update-an-issue-comment""" comment_url, token = base_args["comment_url"], base_args["token"] + if p is not None: + body = create_progress_bar(p=p, msg=body) print(body + "\n") response: requests.Response = requests.patch( url=comment_url,