diff --git a/src/bot_tools.py b/src/bot_tools.py index ac9316d..b970507 100644 --- a/src/bot_tools.py +++ b/src/bot_tools.py @@ -2,10 +2,13 @@ Tools for the agents to use. """ +from branch_handler import get_issue_related_branches, delete_branch import os +from github.Issue import Issue import sys src_dir = os.path.dirname(os.path.abspath(__file__)) + base_dir = os.path.dirname(src_dir) token_threshold = 100_000 @@ -286,7 +289,7 @@ def readfile( warning = (f"File exceeds token threshold of {token_threshold}. " f"Showing {len(included_lines)} of {len(data)} lines " f"({current_tokens}/{total_tokens} tokens). " - f"Use readlines({filepath}, start_line, end_line) " + f"Use readlines({file_path}, start_line, end_line) " f"to read specific ranges.") data = "".join(included_lines) @@ -457,6 +460,31 @@ def run_python_script( return out +def delete_issue_branch_and_pr(issue: Issue, repo_path: str) -> None: + """ + Delete branches and pull requests related to an issue + + Args: + issue: The GitHub issue + repo_path: Path to the local repository + """ + branches = get_issue_related_branches(repo_path, issue.number) + for branch, _ in branches: + delete_branch(repo_path, branch, force=True) + + +def remove_under_development_tag(issue: Issue) -> None: + """ + Remove the 'under_development' tag from an issue + + Args: + issue: The GitHub issue + """ + for label in issue.labels: + if label.name == "under_development": + issue.remove_from_labels(label) + + def run_bash_script( script_path: str, ) -> str: diff --git a/src/branch_handler.py b/src/branch_handler.py index 6802bd4..5666bc3 100644 --- a/src/branch_handler.py +++ b/src/branch_handler.py @@ -112,6 +112,17 @@ def delete_branch(repo_path: str, branch_name: str, force: bool = False) -> None repo = git.Repo(repo_path) if branch_name in repo.heads: repo.delete_head(branch_name, force=force) + """ + Delete a git branch + + Args: + repo_path: Path to local git repository + branch_name: Name of branch to delete + force: If True, force delete even if not merged + """ + repo = git.Repo(repo_path) + if branch_name in repo.heads: + repo.delete_head(branch_name, force=force) def back_to_master_branch(repo_path: str) -> None: diff --git a/src/git_utils.py b/src/git_utils.py index 707d8c8..19aaefb 100644 --- a/src/git_utils.py +++ b/src/git_utils.py @@ -72,6 +72,19 @@ def search_issues(repo: Repository, query: str) -> List[Issue]: return list(repo.get_issues(state='all', labels=[query])) +def remove_label_from_issue(issue: Issue, label_name: str) -> None: + """ + Remove a label from an issue + + Args: + issue: The GitHub issue + label_name: The name of the label to remove + """ + for label in issue.labels: + if label.name == label_name: + issue.remove_from_labels(label) + + def write_issue_response(issue: Issue, response_text: str) -> IssueComment: """ Write a response to an issue with the blech_bot signature diff --git a/src/response_agent.py b/src/response_agent.py index 06ff6a6..dcae69e 100644 --- a/src/response_agent.py +++ b/src/response_agent.py @@ -267,7 +267,8 @@ def check_triggers(issue: Issue) -> str: return "generate_edit_command" elif triggers.has_user_feedback(issue): return "feedback" - else: + elif triggers.has_reset_development_trigger(issue): + return "reset_development" return "new_response" @@ -307,7 +308,14 @@ def process_issue( if not ignore_checks: has_bot_mention = triggers.has_blech_bot_tag( issue) or "[ blech_bot ]" in issue.title.lower() - if not has_bot_mention: + if triggers.has_reset_development_trigger(issue): + repo_path = bot_tools.get_local_repo_path(repo_name) + bot_tools.delete_issue_branch_and_pr(issue, repo_path) + bot_tools.remove_under_development_tag(issue) + write_issue_response(issue, "Development reset successfully.") + return True, None + + elif not has_bot_mention: return False, "Issue does not have blech_bot tag or mention in title" if triggers.has_bot_response(issue) and not triggers.has_user_feedback(issue): return False, "Issue already has a bot response without feedback from user" diff --git a/src/triggers.py b/src/triggers.py index 2fb782c..77d86d2 100644 --- a/src/triggers.py +++ b/src/triggers.py @@ -84,6 +84,22 @@ def has_develop_issue_trigger(issue: Issue) -> bool: return "[ develop_issue ]" in comments[-1].body +def has_reset_development_trigger(issue: Issue) -> bool: + """ + Check if the latest comment contains the reset_development trigger + + Args: + issue: The GitHub issue to check + + Returns: + True if the latest comment contains "[ reset_development ]" + """ + comments = get_issue_comments(issue) + if not comments: + return False + return "[ reset_development ]" in comments[-1].body + + def has_pull_request_trigger(issue: Issue) -> bool: """ Check if the latest comment contains the pull_request trigger