diff --git a/src/plugins/github/handlers/git.py b/src/plugins/github/handlers/git.py index f74d732f..dd086488 100644 --- a/src/plugins/github/handlers/git.py +++ b/src/plugins/github/handlers/git.py @@ -37,7 +37,17 @@ def commit_and_push(self, message: str, branch_name: str, author: str): logger.info("检测到本地分支与远程分支不一致,尝试强制推送") run_shell_command(["git", "push", "origin", branch_name, "-f"]) - def delete_origin_branch(self, branch_name: str): + def remote_branch_exists(self, branch_name: str) -> bool: + """检查远程分支是否存在""" + try: + result = run_shell_command( + ["git", "ls-remote", "--heads", "origin", branch_name] + ) + return bool(result.stdout.strip()) + except Exception: + return False + + def delete_remote_branch(self, branch_name: str): """删除远程分支""" run_shell_command(["git", "push", "origin", "--delete", branch_name]) diff --git a/src/plugins/github/plugins/resolve/__init__.py b/src/plugins/github/plugins/resolve/__init__.py index a409ddc4..183f2dc1 100644 --- a/src/plugins/github/plugins/resolve/__init__.py +++ b/src/plugins/github/plugins/resolve/__init__.py @@ -72,7 +72,7 @@ async def handle_pr_close( logger.info(f"议题 #{handler.issue.number} 已关闭") try: - handler.delete_origin_branch(event.payload.pull_request.head.ref) + handler.delete_remote_branch(event.payload.pull_request.head.ref) logger.info("已删除对应分支") except Exception: logger.info("对应分支不存在或已删除") diff --git a/tests/plugins/github/handlers/test_git_handler.py b/tests/plugins/github/handlers/test_git_handler.py index f58c1e59..c483d637 100644 --- a/tests/plugins/github/handlers/test_git_handler.py +++ b/tests/plugins/github/handlers/test_git_handler.py @@ -98,7 +98,7 @@ async def test_delete_origin_branch(mock_run_shell_command): from src.plugins.github.handlers.git import GitHandler git_handler = GitHandler() - git_handler.delete_origin_branch("main") + git_handler.delete_remote_branch("main") mock_run_shell_command.assert_has_calls( [