Skip to content

Commit 31f3f0c

Browse files
authored
fix: 修复在评论议题后立即关闭拉取请求,拉取请求会被再次创建的问题 (#346)
* feat: 添加 remote_branch_exists 方法 * fix: 修复在评论议题后立即关闭拉取请求,拉取请求会被再次创建的问题 分支和拉取请求绑定起来,分支不存在才能创建拉取请求。 * chore: 移除不需要的代码 * fix: 如果拉取请求关闭则不进行任何操作
1 parent 39b0d04 commit 31f3f0c

File tree

9 files changed

+335
-314
lines changed

9 files changed

+335
-314
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/lang/zh-CN/
77

88
## [Unreleased]
99

10+
### Fixed
11+
12+
- 修复在评论议题后立即关闭拉取请求,拉取请求会被再次创建的问题
13+
1014
## [4.2.3] - 2024-12-30
1115

1216
### Added

src/plugins/github/handlers/git.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,14 @@ def commit_and_push(self, message: str, branch_name: str, author: str):
3737
logger.info("检测到本地分支与远程分支不一致,尝试强制推送")
3838
run_shell_command(["git", "push", "origin", branch_name, "-f"])
3939

40-
def delete_origin_branch(self, branch_name: str):
40+
def remote_branch_exists(self, branch_name: str) -> bool:
41+
"""检查远程分支是否存在"""
42+
result = run_shell_command(
43+
["git", "ls-remote", "--heads", "origin", branch_name]
44+
)
45+
return bool(result.stdout.decode().strip())
46+
47+
def delete_remote_branch(self, branch_name: str):
4148
"""删除远程分支"""
4249

4350
run_shell_command(["git", "push", "origin", "--delete", branch_name])

src/plugins/github/handlers/github.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ async def merge_pull_request(
162162
async def update_pull_request_status(self, title: str, branch_name: str):
163163
"""拉取请求若为草稿状态则标记为可评审,若标题不符则修改标题"""
164164
pull = await self.get_pull_request_by_branch(branch_name)
165+
# 若拉取请求已关闭,则不进行任何操作
166+
if pull.state == "closed":
167+
return
165168
if pull.title != title:
166169
await self.update_pull_request_title(title, pull.number)
167170
if pull.draft:

src/plugins/github/plugins/publish/utils.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -208,16 +208,24 @@ async def ensure_issue_plugin_test_button_in_progress(handler: IssueHandler):
208208
async def process_pull_request(
209209
handler: IssueHandler, result: ValidationDict, branch_name: str, title: str
210210
):
211-
"""
212-
根据发布信息合法性创建拉取请求或将请求改为草稿
213-
"""
214-
if result.valid:
215-
commit_message = f"{COMMIT_MESSAGE_PREFIX} {result.type.value.lower()} {result.name} (#{handler.issue_number})"
211+
"""根据发布信息合法性创建拉取请求或将请求改为草稿"""
212+
if not result.valid:
213+
# 如果之前已经创建了拉取请求,则将其转换为草稿
214+
await handler.draft_pull_request(branch_name)
215+
return
216+
217+
# 更新文件
218+
handler.switch_branch(branch_name)
219+
update_file(result)
216220

217-
handler.switch_branch(branch_name)
218-
# 更新文件
219-
update_file(result)
220-
handler.commit_and_push(commit_message, branch_name, handler.author)
221+
# 只有当远程分支不存在时才创建拉取请求
222+
# 需要在 commit_and_push 前判断,否则远程一定存在
223+
remote_branch_exists = handler.remote_branch_exists(branch_name)
224+
225+
commit_message = f"{COMMIT_MESSAGE_PREFIX} {result.type.value.lower()} {result.name} (#{handler.issue_number})"
226+
handler.commit_and_push(commit_message, branch_name, handler.author)
227+
228+
if not remote_branch_exists:
221229
# 创建拉取请求
222230
try:
223231
pull_number = await handler.create_pull_request(
@@ -226,13 +234,14 @@ async def process_pull_request(
226234
branch_name,
227235
)
228236
await handler.add_labels(pull_number, [PUBLISH_LABEL, result.type.value])
237+
return
229238
except RequestFailed:
230-
# 如果之前已经创建了拉取请求,则将其转换为草稿
231239
logger.info("该分支的拉取请求已创建,请前往查看")
232-
await handler.update_pull_request_status(title, branch_name)
233240
else:
234-
# 如果之前已经创建了拉取请求,则将其转换为草稿
235-
await handler.draft_pull_request(branch_name)
241+
logger.info("远程分支已存在,跳过创建拉取请求")
242+
243+
# 如果之前已经创建了拉取请求,则将其转换为可评审
244+
await handler.update_pull_request_status(title, branch_name)
236245

237246

238247
async def trigger_registry_update(handler: IssueHandler, publish_type: PublishType):

src/plugins/github/plugins/resolve/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ async def handle_pr_close(
7272
logger.info(f"议题 #{handler.issue.number} 已关闭")
7373

7474
try:
75-
handler.delete_origin_branch(event.payload.pull_request.head.ref)
75+
handler.delete_remote_branch(event.payload.pull_request.head.ref)
7676
logger.info("已删除对应分支")
7777
except Exception:
7878
logger.info("对应分支不存在或已删除")

tests/plugins/github/handlers/test_git_handler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ async def test_delete_origin_branch(mock_run_shell_command):
9898
from src.plugins.github.handlers.git import GitHandler
9999

100100
git_handler = GitHandler()
101-
git_handler.delete_origin_branch("main")
101+
git_handler.delete_remote_branch("main")
102102

103103
mock_run_shell_command.assert_has_calls(
104104
[

0 commit comments

Comments
 (0)