Skip to content

Commit

Permalink
Refactor real-time comment updates
Browse files Browse the repository at this point in the history
  • Loading branch information
hiroshinishio committed Oct 28, 2024
1 parent 520e0a6 commit f75ffc0
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 46 deletions.
38 changes: 15 additions & 23 deletions services/check_run_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand All @@ -150,25 +148,22 @@ 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
)
if error_log is 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,
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
40 changes: 18 additions & 22 deletions services/gitauto_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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,
Expand All @@ -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"],
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion services/github/github_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit f75ffc0

Please sign in to comment.