From f7769fe9021f07d91df188d73c8ffcb64f9f1025 Mon Sep 17 00:00:00 2001 From: "Abuzar Mahmood (aider)" Date: Wed, 2 Apr 2025 15:06:52 -0400 Subject: [PATCH 1/3] feat: Add branch pushability check before generating edit command --- src/agents.py | 30 ++++++++++++++++++++++++++++++ src/response_agent.py | 6 +++++- src/triggers.py | 30 ++++++++++++++++++++++++++---- 3 files changed, 61 insertions(+), 5 deletions(-) diff --git a/src/agents.py b/src/agents.py index 396cdf9..b329104 100644 --- a/src/agents.py +++ b/src/agents.py @@ -11,6 +11,7 @@ get_repository, get_issue_comments, ) +from branch_handler import get_current_branch from github.Issue import Issue import random import string @@ -150,6 +151,35 @@ def create_agent(agent_name: str, llm_config: dict) -> AssistantAgent: ############################################################ +def can_push_to_branch(repo_path: str, branch_name: str = None) -> bool: + """ + Check if the branch can be pushed to the remote repository. + + Args: + repo_path: Path to the local repository. + branch_name: Name of the branch to check. If None, uses current branch. + + Returns: + True if the branch can be pushed, False otherwise. + """ + try: + if branch_name is None: + branch_name = get_current_branch(repo_path) + + # Use git push --dry-run to check pushability + result = subprocess.run( + ['git', 'push', '--dry-run', 'origin', branch_name], + cwd=repo_path, + check=True, + capture_output=True, + text=True + ) + # If "Everything up-to-date" is in the output, there's nothing to push + # If there's no error and something to push, we can push + return True + except subprocess.CalledProcessError: + return False + def parse_comments(repo_name: str, repo_path: str, details: dict, issue: Issue) -> str: """Parse comments for the issue or pull request""" from git_utils import has_linked_pr, get_linked_pr diff --git a/src/response_agent.py b/src/response_agent.py index 0c68f88..726b6dd 100644 --- a/src/response_agent.py +++ b/src/response_agent.py @@ -601,7 +601,11 @@ def check_triggers(issue: Issue) -> str: Returns: The trigger phrase found in the issue """ - if triggers.has_generate_edit_command_trigger(issue): + has_edit_trigger, error_msg = triggers.has_generate_edit_command_trigger(issue) + if has_edit_trigger: + if error_msg: + tab_print(f'Triggered by generate_edit_command but: {error_msg}') + return None tab_print('Triggered by generate_edit_command') return "generate_edit_command" elif triggers.has_user_feedback(issue): diff --git a/src/triggers.py b/src/triggers.py index d5f1da4..e4c4fc5 100644 --- a/src/triggers.py +++ b/src/triggers.py @@ -18,18 +18,40 @@ def has_blech_bot_tag(issue: Issue) -> bool: return any(label.name == "blech_bot" for label in issue.labels) -def has_generate_edit_command_trigger(issue: Issue) -> bool: +def has_generate_edit_command_trigger(issue: Issue) -> tuple[bool, str]: """ Check if the issue comments contain the trigger for generate_edit_command + and if the current branch can be pushed Args: issue: The GitHub issue to check Returns: - True if the trigger phrase is found in any comment - """ + Tuple of (trigger_found, error_message) + - trigger_found: True if the trigger phrase is found in any comment + - error_message: Error message if branch can't be pushed, empty string otherwise + """ + from agents import can_push_to_branch + from git_utils import get_development_branch + import os + comments = get_issue_comments(issue) - return any("[ generate_edit_command ]" in comment.body for comment in comments) + trigger_found = any("[ generate_edit_command ]" in comment.body for comment in comments) + + if trigger_found: + # Get the repository path from the issue + from bot_tools import get_local_repo_path + + # Extract repo name from issue URL + repo_name = issue.repository.full_name + repo_path = get_local_repo_path(repo_name) + + # Check if the branch can be pushed + branch_name = get_development_branch(issue, repo_path, create=False) + if branch_name and not can_push_to_branch(repo_path, branch_name): + return True, f"Cannot push to branch {branch_name}" + + return trigger_found, "" def has_bot_response(issue: Issue) -> bool: From 18aa92f50cde3574e26716caf7af93b83c7a05ba Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 2 Apr 2025 19:07:00 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/agents.py | 3 ++- src/response_agent.py | 3 ++- src/triggers.py | 13 +++++++------ 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/agents.py b/src/agents.py index b329104..5d189c9 100644 --- a/src/agents.py +++ b/src/agents.py @@ -165,7 +165,7 @@ def can_push_to_branch(repo_path: str, branch_name: str = None) -> bool: try: if branch_name is None: branch_name = get_current_branch(repo_path) - + # Use git push --dry-run to check pushability result = subprocess.run( ['git', 'push', '--dry-run', 'origin', branch_name], @@ -180,6 +180,7 @@ def can_push_to_branch(repo_path: str, branch_name: str = None) -> bool: except subprocess.CalledProcessError: return False + def parse_comments(repo_name: str, repo_path: str, details: dict, issue: Issue) -> str: """Parse comments for the issue or pull request""" from git_utils import has_linked_pr, get_linked_pr diff --git a/src/response_agent.py b/src/response_agent.py index 726b6dd..f1fe700 100644 --- a/src/response_agent.py +++ b/src/response_agent.py @@ -601,7 +601,8 @@ def check_triggers(issue: Issue) -> str: Returns: The trigger phrase found in the issue """ - has_edit_trigger, error_msg = triggers.has_generate_edit_command_trigger(issue) + has_edit_trigger, error_msg = triggers.has_generate_edit_command_trigger( + issue) if has_edit_trigger: if error_msg: tab_print(f'Triggered by generate_edit_command but: {error_msg}') diff --git a/src/triggers.py b/src/triggers.py index e4c4fc5..3b61aea 100644 --- a/src/triggers.py +++ b/src/triggers.py @@ -34,23 +34,24 @@ def has_generate_edit_command_trigger(issue: Issue) -> tuple[bool, str]: from agents import can_push_to_branch from git_utils import get_development_branch import os - + comments = get_issue_comments(issue) - trigger_found = any("[ generate_edit_command ]" in comment.body for comment in comments) - + trigger_found = any( + "[ generate_edit_command ]" in comment.body for comment in comments) + if trigger_found: # Get the repository path from the issue from bot_tools import get_local_repo_path - + # Extract repo name from issue URL repo_name = issue.repository.full_name repo_path = get_local_repo_path(repo_name) - + # Check if the branch can be pushed branch_name = get_development_branch(issue, repo_path, create=False) if branch_name and not can_push_to_branch(repo_path, branch_name): return True, f"Cannot push to branch {branch_name}" - + return trigger_found, "" From 6261c1a88c3966ff349b1af3a3d4c1657503c2f7 Mon Sep 17 00:00:00 2001 From: "Abuzar Mahmood (aider)" Date: Wed, 2 Apr 2025 15:11:57 -0400 Subject: [PATCH 3/3] feat: Improve branch pushability check to accurately detect pushable changes --- src/agents.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/agents.py b/src/agents.py index 5d189c9..b131444 100644 --- a/src/agents.py +++ b/src/agents.py @@ -176,7 +176,7 @@ def can_push_to_branch(repo_path: str, branch_name: str = None) -> bool: ) # If "Everything up-to-date" is in the output, there's nothing to push # If there's no error and something to push, we can push - return True + return "Everything up-to-date" not in result.stdout except subprocess.CalledProcessError: return False