Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/lang/zh-CN/
### Fixed

- 修复插件元数据适配器中存在非字符串报错的问题
- 修复解决拉取请求冲突时未更新主分支的问题

## [4.3.0] - 2025-04-18

Expand Down
4 changes: 3 additions & 1 deletion src/plugins/github/handlers/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +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])
if update:
run_shell_command(["git", "pull"])

def checkout_remote_branch(self, branch_name: str):
"""检出远程分支"""
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/github/plugins/publish/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,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)
# 更新文件
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/github/plugins/remove/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
# 更新文件
Expand Down
14 changes: 14 additions & 0 deletions tests/plugins/github/handlers/test_git_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
MockBody,
MockIssue,
MockUser,
assert_subprocess_run_calls,
check_json_data,
get_github_bot,
)
Expand Down Expand Up @@ -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"],
],
)

# 检查文件是否正确
Expand Down Expand Up @@ -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"],
],
)

# 检查文件是否正确
Expand Down Expand Up @@ -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"],
],
)

# 检查文件是否正确
Expand Down
Loading
Loading