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
46 changes: 19 additions & 27 deletions tests/plugins/github/config/process/test_config_auto_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
assert_subprocess_run_calls,
get_github_bot,
mock_subprocess_run_with_side_effect,
should_call_apis,
)


Expand Down Expand Up @@ -48,32 +47,25 @@ async def test_config_auto_merge(
event = get_mock_event(PullRequestReviewSubmitted)
event.payload.pull_request.labels = get_issue_labels(["Config", "Plugin"])

should_call_apis(
ctx,
[
{
"api": "rest.apps.async_get_repo_installation",
"result": mock_installation,
},
{
"api": "rest.apps.async_create_installation_access_token",
"result": mock_installation_token,
},
{
"api": "rest.pulls.async_merge",
"result": True,
},
],
[
{"owner": "he0119", "repo": "action-test"},
{"installation_id": mock_installation.parsed_data.id},
{
"owner": "he0119",
"repo": "action-test",
"pull_number": 100,
"merge_method": "rebase",
},
],
ctx.should_call_api(
"rest.apps.async_get_repo_installation",
{"owner": "he0119", "repo": "action-test"},
mock_installation,
)
ctx.should_call_api(
"rest.apps.async_create_installation_access_token",
{"installation_id": mock_installation.parsed_data.id},
mock_installation_token,
)
ctx.should_call_api(
"rest.pulls.async_merge",
{
"owner": "he0119",
"repo": "action-test",
"pull_number": 100,
"merge_method": "rebase",
},
True,
)

ctx.receive_event(bot, event)
Expand Down
92 changes: 40 additions & 52 deletions tests/plugins/github/config/process/test_config_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@
from tests.plugins.github.config.utils import generate_issue_body
from tests.plugins.github.event import get_mock_event
from tests.plugins.github.utils import (
GitHubApi,
MockIssue,
assert_subprocess_run_calls,
check_json_data,
get_github_bot,
get_issue_labels,
mock_subprocess_run_with_side_effect,
should_call_apis,
)


Expand Down Expand Up @@ -113,55 +111,23 @@ async def test_process_config_check(
event = get_mock_event(IssuesOpened)
event.payload.issue.labels = get_config_labels()

# 定义要调用的 API 列表
apis: list[GitHubApi] = [
{
"api": "rest.apps.async_get_repo_installation",
"result": mock_installation,
},
{
"api": "rest.apps.async_create_installation_access_token",
"result": mock_installation_token,
},
{
"api": "rest.issues.async_get",
"result": mock_issues_resp,
},
{
"api": "rest.issues.async_update",
"result": None,
},
{
"api": "rest.issues.async_list_comments",
"result": mock_list_comments_resp,
},
{
"api": "rest.issues.async_update_comment",
"result": True,
},
{
"api": "rest.issues.async_update",
"result": None,
},
{
"api": "rest.pulls.async_create",
"result": mock_pull_resp,
},
{
"api": "rest.issues.async_add_labels",
"result": None,
},
{
"api": "rest.issues.async_update",
"result": None,
},
]

# 对应的 API 数据
api_data = [
ctx.should_call_api(
"rest.apps.async_get_repo_installation",
{"owner": "he0119", "repo": "action-test"},
mock_installation,
)
ctx.should_call_api(
"rest.apps.async_create_installation_access_token",
{"installation_id": mock_installation.parsed_data.id},
mock_installation_token,
)
ctx.should_call_api(
"rest.issues.async_get",
{"owner": "he0119", "repo": "action-test", "issue_number": 80},
mock_issues_resp,
)
ctx.should_call_api(
"rest.issues.async_update",
snapshot(
{
"owner": "he0119",
Expand All @@ -188,7 +154,15 @@ async def test_process_config_check(
""",
}
),
None,
)
ctx.should_call_api(
"rest.issues.async_list_comments",
{"owner": "he0119", "repo": "action-test", "issue_number": 80},
mock_list_comments_resp,
)
ctx.should_call_api(
"rest.issues.async_update_comment",
snapshot(
{
"owner": "he0119",
Expand Down Expand Up @@ -225,6 +199,10 @@ async def test_process_config_check(
""",
}
),
True,
)
ctx.should_call_api(
"rest.issues.async_update",
snapshot(
{
"owner": "he0119",
Expand All @@ -233,6 +211,10 @@ async def test_process_config_check(
"title": "Plugin: name",
}
),
None,
)
ctx.should_call_api(
"rest.pulls.async_create",
snapshot(
{
"owner": "he0119",
Expand All @@ -243,6 +225,10 @@ async def test_process_config_check(
"head": "config/issue80",
}
),
mock_pull_resp,
)
ctx.should_call_api(
"rest.issues.async_add_labels",
snapshot(
{
"owner": "he0119",
Expand All @@ -251,6 +237,10 @@ async def test_process_config_check(
"labels": ["Plugin", "Config"],
}
),
None,
)
ctx.should_call_api(
"rest.issues.async_update",
snapshot(
{
"owner": "he0119",
Expand All @@ -277,10 +267,8 @@ async def test_process_config_check(
""",
}
),
]

# 使用辅助函数调用 API
should_call_apis(ctx, apis, api_data)
None,
)

ctx.receive_event(bot, event) # 测试 git 命令

Expand Down
125 changes: 55 additions & 70 deletions tests/plugins/github/config/process/test_config_pull_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
assert_subprocess_run_calls,
get_github_bot,
mock_subprocess_run_with_side_effect,
should_call_apis,
)


Expand Down Expand Up @@ -45,43 +44,36 @@ async def test_config_process_pull_request(
event.payload.pull_request.labels = get_pr_labels(["Config", "Plugin"])
event.payload.pull_request.merged = True

should_call_apis(
ctx,
[
{
"api": "rest.apps.async_get_repo_installation",
"result": mock_installation,
},
{
"api": "rest.apps.async_create_installation_access_token",
"result": mock_installation_token,
},
{
"api": "rest.issues.async_get",
"result": mock_issues_resp,
},
{
"api": "rest.issues.async_update",
"result": True,
},
{
"api": "rest.pulls.async_list",
"result": mock_pulls_resp,
},
],
[
{"owner": "he0119", "repo": "action-test"},
{"installation_id": mock_installation.parsed_data.id},
{"owner": "he0119", "repo": "action-test", "issue_number": 76},
{
"owner": "he0119",
"repo": "action-test",
"issue_number": 80,
"state": "closed",
"state_reason": "completed",
},
{"owner": "he0119", "repo": "action-test", "state": "open"},
],
ctx.should_call_api(
"rest.apps.async_get_repo_installation",
{"owner": "he0119", "repo": "action-test"},
mock_installation,
)
ctx.should_call_api(
"rest.apps.async_create_installation_access_token",
{"installation_id": mock_installation.parsed_data.id},
mock_installation_token,
)
ctx.should_call_api(
"rest.issues.async_get",
{"owner": "he0119", "repo": "action-test", "issue_number": 76},
mock_issues_resp,
)
ctx.should_call_api(
"rest.issues.async_update",
{
"owner": "he0119",
"repo": "action-test",
"issue_number": 80,
"state": "closed",
"state_reason": "completed",
},
True,
)
ctx.should_call_api(
"rest.pulls.async_list",
{"owner": "he0119", "repo": "action-test", "state": "open"},
mock_pulls_resp,
)
ctx.receive_event(bot, event)

Expand Down Expand Up @@ -133,38 +125,31 @@ async def test_process_config_pull_request_not_merged(
event = get_mock_event(PullRequestClosed)
event.payload.pull_request.labels = get_pr_labels(["Config", "Plugin"])

should_call_apis(
ctx,
[
{
"api": "rest.apps.async_get_repo_installation",
"result": mock_installation,
},
{
"api": "rest.apps.async_create_installation_access_token",
"result": mock_installation_token,
},
{
"api": "rest.issues.async_get",
"result": mock_issues_resp,
},
{
"api": "rest.issues.async_update",
"result": True,
},
],
[
{"owner": "he0119", "repo": "action-test"},
{"installation_id": mock_installation.parsed_data.id},
{"owner": "he0119", "repo": "action-test", "issue_number": 76},
{
"owner": "he0119",
"repo": "action-test",
"issue_number": 80,
"state": "closed",
"state_reason": "not_planned",
},
],
ctx.should_call_api(
"rest.apps.async_get_repo_installation",
{"owner": "he0119", "repo": "action-test"},
mock_installation,
)
ctx.should_call_api(
"rest.apps.async_create_installation_access_token",
{"installation_id": mock_installation.parsed_data.id},
mock_installation_token,
)
ctx.should_call_api(
"rest.issues.async_get",
{"owner": "he0119", "repo": "action-test", "issue_number": 76},
mock_issues_resp,
)
ctx.should_call_api(
"rest.issues.async_update",
{
"owner": "he0119",
"repo": "action-test",
"issue_number": 80,
"state": "closed",
"state_reason": "not_planned",
},
True,
)

ctx.receive_event(bot, event)
Expand Down
Loading
Loading