Skip to content

Commit

Permalink
feat: 添加 remote_branch_exists 方法
Browse files Browse the repository at this point in the history
  • Loading branch information
he0119 committed Dec 31, 2024
1 parent 39b0d04 commit 2a4eafb
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
12 changes: 11 additions & 1 deletion src/plugins/github/handlers/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/github/plugins/resolve/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("对应分支不存在或已删除")
Expand Down
2 changes: 1 addition & 1 deletion tests/plugins/github/handlers/test_git_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
[
Expand Down

0 comments on commit 2a4eafb

Please sign in to comment.