Skip to content

Commit

Permalink
fix: 修复在评论议题后立即关闭拉取请求,拉取请求会被再次创建的问题
Browse files Browse the repository at this point in the history
分支和拉取请求绑定起来,分支不存在才能创建拉取请求。
  • Loading branch information
he0119 committed Dec 31, 2024
1 parent 2a4eafb commit 15e855f
Show file tree
Hide file tree
Showing 6 changed files with 330 additions and 313 deletions.
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
11 changes: 4 additions & 7 deletions src/plugins/github/handlers/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,10 @@ def commit_and_push(self, message: str, branch_name: str, author: 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
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):
"""删除远程分支"""
Expand Down
30 changes: 20 additions & 10 deletions src/plugins/github/plugins/publish/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,13 +211,23 @@ async def process_pull_request(
"""
根据发布信息合法性创建拉取请求或将请求改为草稿
"""
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 +236,13 @@ 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
Loading

0 comments on commit 15e855f

Please sign in to comment.