From 5e665c62debb3e3865023409d8eab23e713ba072 Mon Sep 17 00:00:00 2001 From: BigOrangeQWQ <2284086963@qq.com> Date: Sat, 10 May 2025 15:27:29 +0800 Subject: [PATCH 1/3] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=90=88=E5=B9=B6?= =?UTF-8?q?=E6=8B=89=E5=8F=96=E8=AF=B7=E6=B1=82=E6=97=B6=EF=BC=8C=E8=B4=9F?= =?UTF-8?q?=E8=B4=A3=E8=A7=A3=E5=86=B3=E6=8B=89=E5=8F=96=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E5=86=B2=E7=AA=81=E7=9A=84=E5=8A=9F=E8=83=BD=E6=9C=AA=E8=83=BD?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E6=9C=80=E6=96=B0=E7=9A=84=E8=BF=9C=E7=A8=8B?= =?UTF-8?q?=E5=88=86=E6=94=AF=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/plugins/github/handlers/git.py | 5 +++++ src/plugins/github/plugins/publish/utils.py | 1 + tests/plugins/github/resolve/test_resolve_pull_request.py | 1 + 3 files changed, 7 insertions(+) diff --git a/src/plugins/github/handlers/git.py b/src/plugins/github/handlers/git.py index d586cc3d..6e184d05 100644 --- a/src/plugins/github/handlers/git.py +++ b/src/plugins/github/handlers/git.py @@ -12,6 +12,11 @@ def checkout_branch(self, branch_name: str): run_shell_command(["git", "checkout", branch_name]) + def update_branch(self, branch_name: str): + """更新本地分支""" + + run_shell_command(["git", "pull", "origin", branch_name]) + def checkout_remote_branch(self, branch_name: str): """检出远程分支""" diff --git a/src/plugins/github/plugins/publish/utils.py b/src/plugins/github/plugins/publish/utils.py index c646c822..4faaf8bf 100644 --- a/src/plugins/github/plugins/publish/utils.py +++ b/src/plugins/github/plugins/publish/utils.py @@ -117,6 +117,7 @@ async def resolve_conflict_pull_requests( logger.error(f"验证结果: {result}") continue + handler.update_branch(plugin_config.input_config.base) # 每次切换前都要确保回到主分支 handler.checkout_branch(plugin_config.input_config.base) # 切换到对应分支 diff --git a/tests/plugins/github/resolve/test_resolve_pull_request.py b/tests/plugins/github/resolve/test_resolve_pull_request.py index 9280245c..b765e951 100644 --- a/tests/plugins/github/resolve/test_resolve_pull_request.py +++ b/tests/plugins/github/resolve/test_resolve_pull_request.py @@ -125,6 +125,7 @@ async def test_resolve_pull_request( ["git", "config", "--global", "safe.directory", "*"], ["git", "push", "origin", "--delete", "publish/issue76"], # 处理发布 + ["git", "pull", "origin", "master"], ["git", "checkout", "master"], ["git", "switch", "-C", "publish/issue100"], ["git", "config", "--global", "user.name", "test"], From daa5b83fb53a48c1e481d3a80842fb9b131ccfb1 Mon Sep 17 00:00:00 2001 From: uy/sun Date: Sat, 10 May 2025 18:26:51 +0800 Subject: [PATCH 2/3] =?UTF-8?q?feat:=20=E5=9C=A8=E6=A3=80=E5=87=BA?= =?UTF-8?q?=E5=88=86=E6=94=AF=E6=96=B9=E6=B3=95=E4=B8=AD=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E6=8B=89=E5=8F=96=E6=9C=80=E6=96=B0=E6=9B=B4?= =?UTF-8?q?=E6=94=B9=E7=9A=84=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/plugins/github/handlers/git.py | 9 +- src/plugins/github/plugins/publish/utils.py | 3 +- src/plugins/github/plugins/remove/utils.py | 2 +- .../github/handlers/test_git_handler.py | 14 ++ ..._publish_resolve_conflict_pull_requests.py | 182 ++++++------------ .../test_remove_resolve_pull_requests.py | 126 ++++-------- .../resolve/test_resolve_pull_request.py | 3 +- 7 files changed, 119 insertions(+), 220 deletions(-) diff --git a/src/plugins/github/handlers/git.py b/src/plugins/github/handlers/git.py index 6e184d05..a4516f01 100644 --- a/src/plugins/github/handlers/git.py +++ b/src/plugins/github/handlers/git.py @@ -7,15 +7,12 @@ class GitHandler(BaseModel): """Git 操作""" - def checkout_branch(self, branch_name: str): + def checkout_branch(self, branch_name: str, update: bool = False): """检出分支""" run_shell_command(["git", "checkout", branch_name]) - - def update_branch(self, branch_name: str): - """更新本地分支""" - - run_shell_command(["git", "pull", "origin", branch_name]) + if update: + run_shell_command(["git", "pull"]) def checkout_remote_branch(self, branch_name: str): """检出远程分支""" diff --git a/src/plugins/github/plugins/publish/utils.py b/src/plugins/github/plugins/publish/utils.py index 4faaf8bf..9166fdce 100644 --- a/src/plugins/github/plugins/publish/utils.py +++ b/src/plugins/github/plugins/publish/utils.py @@ -117,9 +117,8 @@ async def resolve_conflict_pull_requests( logger.error(f"验证结果: {result}") continue - handler.update_branch(plugin_config.input_config.base) # 每次切换前都要确保回到主分支 - handler.checkout_branch(plugin_config.input_config.base) + handler.checkout_branch(plugin_config.input_config.base, update=True) # 切换到对应分支 handler.switch_branch(pull.head.ref) # 更新文件 diff --git a/src/plugins/github/plugins/remove/utils.py b/src/plugins/github/plugins/remove/utils.py index 90a1c0d4..d81e9d5d 100644 --- a/src/plugins/github/plugins/remove/utils.py +++ b/src/plugins/github/plugins/remove/utils.py @@ -107,7 +107,7 @@ async def resolve_conflict_pull_requests( continue # 每次切换前都要确保回到主分支 - handler.checkout_branch(plugin_config.input_config.base) + handler.checkout_branch(plugin_config.input_config.base, update=True) # 切换到对应分支 handler.switch_branch(pull.head.ref) # 更新文件 diff --git a/tests/plugins/github/handlers/test_git_handler.py b/tests/plugins/github/handlers/test_git_handler.py index c483d637..8558c7f5 100644 --- a/tests/plugins/github/handlers/test_git_handler.py +++ b/tests/plugins/github/handlers/test_git_handler.py @@ -22,6 +22,20 @@ async def test_checkout_branch(mock_run_shell_command): ) +async def test_checkout_branch_with_update(mock_run_shell_command): + from src.plugins.github.handlers.git import GitHandler + + git_handler = GitHandler() + git_handler.checkout_branch("main", update=True) + + mock_run_shell_command.assert_has_calls( + [ + call(["git", "checkout", "main"]), + call(["git", "pull"]), + ] + ) + + async def test_checkout_remote_branch(mock_run_shell_command): from src.plugins.github.handlers.git import GitHandler diff --git a/tests/plugins/github/publish/utils/test_publish_resolve_conflict_pull_requests.py b/tests/plugins/github/publish/utils/test_publish_resolve_conflict_pull_requests.py index 7d26aa16..58461623 100644 --- a/tests/plugins/github/publish/utils/test_publish_resolve_conflict_pull_requests.py +++ b/tests/plugins/github/publish/utils/test_publish_resolve_conflict_pull_requests.py @@ -10,6 +10,7 @@ MockBody, MockIssue, MockUser, + assert_subprocess_run_calls, check_json_data, get_github_bot, ) @@ -78,49 +79,28 @@ async def test_resolve_conflict_pull_requests_adapter( ) await resolve_conflict_pull_requests(handler, [mock_pull]) + # 测试 git 命令 - mock_subprocess_run.assert_has_calls( + assert_subprocess_run_calls( + mock_subprocess_run, [ - mocker.call(["git", "checkout", "master"], check=True, capture_output=True), - mocker.call( - ["git", "switch", "-C", "publish/issue1"], - check=True, - capture_output=True, - ), - mocker.call( - ["git", "config", "--global", "user.name", "he0119"], - check=True, - capture_output=True, - ), - mocker.call( - [ - "git", - "config", - "--global", - "user.email", - "he0119@users.noreply.github.com", - ], - check=True, - capture_output=True, - ), - mocker.call(["git", "add", "-A"], check=True, capture_output=True), - mocker.call( - ["git", "commit", "-m", ":beers: publish adapter name (#1)"], - check=True, - capture_output=True, - ), - mocker.call(["git", "fetch", "origin"], check=True, capture_output=True), - mocker.call( - ["git", "diff", "origin/publish/issue1", "publish/issue1"], - check=True, - capture_output=True, - ), - mocker.call( - ["git", "push", "origin", "publish/issue1", "-f"], - check=True, - capture_output=True, - ), - ] # type: ignore + ["git", "checkout", "master"], + ["git", "pull"], + ["git", "switch", "-C", "publish/issue1"], + ["git", "config", "--global", "user.name", "he0119"], + [ + "git", + "config", + "--global", + "user.email", + "he0119@users.noreply.github.com", + ], + ["git", "add", "-A"], + ["git", "commit", "-m", ":beers: publish adapter name (#1)"], + ["git", "fetch", "origin"], + ["git", "diff", "origin/publish/issue1", "publish/issue1"], + ["git", "push", "origin", "publish/issue1", "-f"], + ], ) # 检查文件是否正确 @@ -209,48 +189,26 @@ async def test_resolve_conflict_pull_requests_bot( await resolve_conflict_pull_requests(handler, [mock_pull]) # 测试 git 命令 - mock_subprocess_run.assert_has_calls( + assert_subprocess_run_calls( + mock_subprocess_run, [ - mocker.call(["git", "checkout", "master"], check=True, capture_output=True), - mocker.call( - ["git", "switch", "-C", "publish/issue1"], - check=True, - capture_output=True, - ), - mocker.call( - ["git", "config", "--global", "user.name", "he0119"], - check=True, - capture_output=True, - ), - mocker.call( - [ - "git", - "config", - "--global", - "user.email", - "he0119@users.noreply.github.com", - ], - check=True, - capture_output=True, - ), - mocker.call(["git", "add", "-A"], check=True, capture_output=True), - mocker.call( - ["git", "commit", "-m", ":beers: publish bot name (#1)"], - check=True, - capture_output=True, - ), - mocker.call(["git", "fetch", "origin"], check=True, capture_output=True), - mocker.call( - ["git", "diff", "origin/publish/issue1", "publish/issue1"], - check=True, - capture_output=True, - ), - mocker.call( - ["git", "push", "origin", "publish/issue1", "-f"], - check=True, - capture_output=True, - ), - ] # type: ignore + ["git", "checkout", "master"], + ["git", "pull"], + ["git", "switch", "-C", "publish/issue1"], + ["git", "config", "--global", "user.name", "he0119"], + [ + "git", + "config", + "--global", + "user.email", + "he0119@users.noreply.github.com", + ], + ["git", "add", "-A"], + ["git", "commit", "-m", ":beers: publish bot name (#1)"], + ["git", "fetch", "origin"], + ["git", "diff", "origin/publish/issue1", "publish/issue1"], + ["git", "push", "origin", "publish/issue1", "-f"], + ], ) # 检查文件是否正确 @@ -359,48 +317,26 @@ async def test_resolve_conflict_pull_requests_plugin( await resolve_conflict_pull_requests(handler, [mock_pull]) # 测试 git 命令 - mock_subprocess_run.assert_has_calls( + assert_subprocess_run_calls( + mock_subprocess_run, [ - mocker.call(["git", "checkout", "master"], check=True, capture_output=True), - mocker.call( - ["git", "switch", "-C", "publish/issue1"], - check=True, - capture_output=True, - ), - mocker.call( - ["git", "config", "--global", "user.name", "he0119"], - check=True, - capture_output=True, - ), - mocker.call( - [ - "git", - "config", - "--global", - "user.email", - "he0119@users.noreply.github.com", - ], - check=True, - capture_output=True, - ), - mocker.call(["git", "add", "-A"], check=True, capture_output=True), - mocker.call( - ["git", "commit", "-m", ":beers: publish plugin name (#1)"], - check=True, - capture_output=True, - ), - mocker.call(["git", "fetch", "origin"], check=True, capture_output=True), - mocker.call( - ["git", "diff", "origin/publish/issue1", "publish/issue1"], - check=True, - capture_output=True, - ), - mocker.call( - ["git", "push", "origin", "publish/issue1", "-f"], - check=True, - capture_output=True, - ), - ] # type: ignore + ["git", "checkout", "master"], + ["git", "pull"], + ["git", "switch", "-C", "publish/issue1"], + ["git", "config", "--global", "user.name", "he0119"], + [ + "git", + "config", + "--global", + "user.email", + "he0119@users.noreply.github.com", + ], + ["git", "add", "-A"], + ["git", "commit", "-m", ":beers: publish plugin name (#1)"], + ["git", "fetch", "origin"], + ["git", "diff", "origin/publish/issue1", "publish/issue1"], + ["git", "push", "origin", "publish/issue1", "-f"], + ], ) # 检查文件是否正确 diff --git a/tests/plugins/github/remove/utils/test_remove_resolve_pull_requests.py b/tests/plugins/github/remove/utils/test_remove_resolve_pull_requests.py index f07f4a00..74833d0f 100644 --- a/tests/plugins/github/remove/utils/test_remove_resolve_pull_requests.py +++ b/tests/plugins/github/remove/utils/test_remove_resolve_pull_requests.py @@ -9,6 +9,7 @@ from tests.plugins.github.utils import ( MockIssue, MockUser, + assert_subprocess_run_calls, check_json_data, generate_issue_body_remove, get_github_bot, @@ -86,48 +87,26 @@ async def test_resolve_conflict_pull_requests_bot( await resolve_conflict_pull_requests(handler, [mock_pull]) # 测试 git 命令 - mock_subprocess_run.assert_has_calls( + assert_subprocess_run_calls( + mock_subprocess_run, [ - mocker.call(["git", "checkout", "master"], check=True, capture_output=True), - mocker.call( - ["git", "switch", "-C", "remove/issue1"], - check=True, - capture_output=True, - ), - mocker.call( - ["git", "config", "--global", "user.name", "he0119"], - check=True, - capture_output=True, - ), - mocker.call( - [ - "git", - "config", - "--global", - "user.email", - "he0119@users.noreply.github.com", - ], - check=True, - capture_output=True, - ), - mocker.call(["git", "add", "-A"], check=True, capture_output=True), - mocker.call( - ["git", "commit", "-m", ":pencil2: remove CoolQBot (#1)"], - check=True, - capture_output=True, - ), - mocker.call(["git", "fetch", "origin"], check=True, capture_output=True), - mocker.call( - ["git", "diff", "origin/remove/issue1", "remove/issue1"], - check=True, - capture_output=True, - ), - mocker.call( - ["git", "push", "origin", "remove/issue1", "-f"], - check=True, - capture_output=True, - ), - ] # type: ignore + ["git", "checkout", "master"], + ["git", "pull"], + ["git", "switch", "-C", "remove/issue1"], + ["git", "config", "--global", "user.name", "he0119"], + [ + "git", + "config", + "--global", + "user.email", + "he0119@users.noreply.github.com", + ], + ["git", "add", "-A"], + ["git", "commit", "-m", ":pencil2: remove CoolQBot (#1)"], + ["git", "fetch", "origin"], + ["git", "diff", "origin/remove/issue1", "remove/issue1"], + ["git", "push", "origin", "remove/issue1", "-f"], + ], ) # 检查文件是否正确 @@ -195,53 +174,26 @@ async def test_resolve_conflict_pull_requests_plugin( await resolve_conflict_pull_requests(handler, [mock_pull]) # 测试 git 命令 - mock_subprocess_run.assert_has_calls( + assert_subprocess_run_calls( + mock_subprocess_run, [ - mocker.call(["git", "checkout", "master"], check=True, capture_output=True), - mocker.call( - ["git", "switch", "-C", "remove/issue1"], - check=True, - capture_output=True, - ), - mocker.call( - ["git", "config", "--global", "user.name", "he0119"], - check=True, - capture_output=True, - ), - mocker.call( - [ - "git", - "config", - "--global", - "user.email", - "he0119@users.noreply.github.com", - ], - check=True, - capture_output=True, - ), - mocker.call(["git", "add", "-A"], check=True, capture_output=True), - mocker.call( - [ - "git", - "commit", - "-m", - ":pencil2: remove nonebot_plugin_treehelp (#1)", - ], - check=True, - capture_output=True, - ), - mocker.call(["git", "fetch", "origin"], check=True, capture_output=True), - mocker.call( - ["git", "diff", "origin/remove/issue1", "remove/issue1"], - check=True, - capture_output=True, - ), - mocker.call( - ["git", "push", "origin", "remove/issue1", "-f"], - check=True, - capture_output=True, - ), - ] # type: ignore + ["git", "checkout", "master"], + ["git", "pull"], + ["git", "switch", "-C", "remove/issue1"], + ["git", "config", "--global", "user.name", "he0119"], + [ + "git", + "config", + "--global", + "user.email", + "he0119@users.noreply.github.com", + ], + ["git", "add", "-A"], + ["git", "commit", "-m", ":pencil2: remove nonebot_plugin_treehelp (#1)"], + ["git", "fetch", "origin"], + ["git", "diff", "origin/remove/issue1", "remove/issue1"], + ["git", "push", "origin", "remove/issue1", "-f"], + ], ) check_json_data( diff --git a/tests/plugins/github/resolve/test_resolve_pull_request.py b/tests/plugins/github/resolve/test_resolve_pull_request.py index b765e951..5683d699 100644 --- a/tests/plugins/github/resolve/test_resolve_pull_request.py +++ b/tests/plugins/github/resolve/test_resolve_pull_request.py @@ -125,8 +125,8 @@ async def test_resolve_pull_request( ["git", "config", "--global", "safe.directory", "*"], ["git", "push", "origin", "--delete", "publish/issue76"], # 处理发布 - ["git", "pull", "origin", "master"], ["git", "checkout", "master"], + ["git", "pull"], ["git", "switch", "-C", "publish/issue100"], ["git", "config", "--global", "user.name", "test"], [ @@ -143,6 +143,7 @@ async def test_resolve_pull_request( ["git", "push", "origin", "publish/issue100", "-f"], # 处理移除 ["git", "checkout", "master"], + ["git", "pull"], ["git", "switch", "-C", "remove/issue101"], ["git", "config", "--global", "user.name", "test"], [ From cc9c5945072f591f65323d60f891b906a9abc235 Mon Sep 17 00:00:00 2001 From: uy/sun Date: Sat, 10 May 2025 18:30:20 +0800 Subject: [PATCH 3/3] =?UTF-8?q?docs:=20=E6=9B=B4=E6=96=B0=E6=97=A5?= =?UTF-8?q?=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9cc3c314..ff03fff3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/lang/zh-CN/ ### Fixed - 修复插件元数据适配器中存在非字符串报错的问题 +- 修复解决拉取请求冲突时未更新主分支的问题 ## [4.3.0] - 2025-04-18