diff --git a/src/agents.py b/src/agents.py index 396cdf9..b131444 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,36 @@ 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 "Everything up-to-date" not in result.stdout + 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..f1fe700 100644 --- a/src/response_agent.py +++ b/src/response_agent.py @@ -601,7 +601,12 @@ 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..3b61aea 100644 --- a/src/triggers.py +++ b/src/triggers.py @@ -18,18 +18,41 @@ 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: