Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 修复在评论议题后立即关闭拉取请求,拉取请求会被再次创建的问题 #346

Merged
merged 4 commits into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/lang/zh-CN/

## [Unreleased]

### Fixed

- 修复在评论议题后立即关闭拉取请求,拉取请求会被再次创建的问题

## [4.2.3] - 2024-12-30

### Added
Expand Down
9 changes: 8 additions & 1 deletion src/plugins/github/handlers/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,14 @@ 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:
"""检查远程分支是否存在"""
result = run_shell_command(
["git", "ls-remote", "--heads", "origin", branch_name]
)
return bool(result.stdout.decode().strip())

def delete_remote_branch(self, branch_name: str):
"""删除远程分支"""

run_shell_command(["git", "push", "origin", "--delete", branch_name])
Expand Down
3 changes: 3 additions & 0 deletions src/plugins/github/handlers/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ async def merge_pull_request(
async def update_pull_request_status(self, title: str, branch_name: str):
"""拉取请求若为草稿状态则标记为可评审,若标题不符则修改标题"""
pull = await self.get_pull_request_by_branch(branch_name)
# 若拉取请求已关闭,则不进行任何操作
if pull.state == "closed":
return
if pull.title != title:
await self.update_pull_request_title(title, pull.number)
if pull.draft:
Expand Down
35 changes: 22 additions & 13 deletions src/plugins/github/plugins/publish/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,16 +208,24 @@ async def ensure_issue_plugin_test_button_in_progress(handler: IssueHandler):
async def process_pull_request(
handler: IssueHandler, result: ValidationDict, branch_name: str, title: str
):
"""
根据发布信息合法性创建拉取请求或将请求改为草稿
"""
if result.valid:
commit_message = f"{COMMIT_MESSAGE_PREFIX} {result.type.value.lower()} {result.name} (#{handler.issue_number})"
"""根据发布信息合法性创建拉取请求或将请求改为草稿"""
if not result.valid:
# 如果之前已经创建了拉取请求,则将其转换为草稿
await handler.draft_pull_request(branch_name)
return

# 更新文件
handler.switch_branch(branch_name)
update_file(result)

handler.switch_branch(branch_name)
# 更新文件
update_file(result)
handler.commit_and_push(commit_message, branch_name, handler.author)
# 只有当远程分支不存在时才创建拉取请求
# 需要在 commit_and_push 前判断,否则远程一定存在
remote_branch_exists = handler.remote_branch_exists(branch_name)

commit_message = f"{COMMIT_MESSAGE_PREFIX} {result.type.value.lower()} {result.name} (#{handler.issue_number})"
handler.commit_and_push(commit_message, branch_name, handler.author)

if not remote_branch_exists:
# 创建拉取请求
try:
pull_number = await handler.create_pull_request(
Expand All @@ -226,13 +234,14 @@ async def process_pull_request(
branch_name,
)
await handler.add_labels(pull_number, [PUBLISH_LABEL, result.type.value])
return
except RequestFailed:
# 如果之前已经创建了拉取请求,则将其转换为草稿
logger.info("该分支的拉取请求已创建,请前往查看")
await handler.update_pull_request_status(title, branch_name)
else:
# 如果之前已经创建了拉取请求,则将其转换为草稿
await handler.draft_pull_request(branch_name)
logger.info("远程分支已存在,跳过创建拉取请求")

# 如果之前已经创建了拉取请求,则将其转换为可评审
await handler.update_pull_request_status(title, branch_name)


async def trigger_registry_update(handler: IssueHandler, publish_type: PublishType):
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
Loading
Loading