Skip to content

Commit

Permalink
test: 更新了 publish 相关的测试
Browse files Browse the repository at this point in the history
  • Loading branch information
BigOrangeQWQ committed Sep 26, 2024
1 parent 47a18cb commit 2417e87
Show file tree
Hide file tree
Showing 15 changed files with 207 additions and 155 deletions.
4 changes: 4 additions & 0 deletions src/plugins/github/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from pydantic import (
BaseModel,
ConfigDict,
Field,
model_validator,
)
from nonebot.adapters.github import Bot
Expand Down Expand Up @@ -240,6 +241,9 @@ def issuehandler_validator(cls, data):
if data.get("author") is None and data.get("issue"):
issue = data["issue"]
data["author"] = issue.user.login if issue.user else ""
if data.get("issue_number") is None and data.get("issue"):
issue = data["issue"]
data["issue_number"] = issue.number
return data

async def update_issue_title(
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/github/plugins/publish/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ async def handle_publish_plugin_check(

# 验证之后创建拉取请求和修改议题的标题
await process_pull_request(handler, result, branch_name, title)

# 确保插件重测按钮存在
await ensure_issue_test_button(handler)

comment = await render_comment(result, True)
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/github/plugins/publish/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
from src.providers.validation import (
PublishType,
ValidationDict,
extract_publish_info_from_issue,
validate_info,
)
from src.plugins.github.utils import extract_publish_info_from_issue
from src.providers.constants import DOCKER_IMAGES
from src.providers.docker_test import DockerPluginTest
from src.providers.store_test.models import DockerTestResult, Metadata
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/github/plugins/remove/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@


async def pr_close_rule(
is_remove: bool = check_labels("remove"),
is_remove: bool = check_labels(REMOVE_LABEL),
related_issue_number: int | None = Depends(get_related_issue_number),
) -> bool:
if not is_remove:
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/github/plugins/remove/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from githubkit.rest import Issue
from pydantic_core import PydanticCustomError

from src.providers.validation import extract_publish_info_from_issue
from src.plugins.github.utils import extract_publish_info_from_issue
from src.providers.validation.models import ValidationDict

from .constants import REMOVE_HOMEPAGE_PATTERN, PUBLISH_PATH
Expand Down
15 changes: 15 additions & 0 deletions src/plugins/github/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
from pathlib import Path
from re import Pattern
import subprocess
from typing import Any
from nonebot import logger
Expand Down Expand Up @@ -40,3 +41,17 @@ def dump_json(path: Path, data: Any, indent: int = 4):
# 结尾加上换行符,不然会被 pre-commit fix
json.dump(to_jsonable_python(data), f, ensure_ascii=False, indent=indent)
f.write("\n")


def extract_publish_info_from_issue(
patterns: dict[str, Pattern[str]], body: str
) -> dict[str, str | None]:
"""
根据提供的正则表达式和议题内容来提取所需的信息
"""
matchers = {key: pattern.search(body) for key, pattern in patterns.items()}
data = {
key: match.group(1).strip() if match else None
for key, match in matchers.items()
}
return data
15 changes: 0 additions & 15 deletions src/providers/validation/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""验证数据是否符合规范"""

from re import Pattern
from typing import Any
from pydantic import ValidationError
from pydantic_core import ErrorDetails
Expand All @@ -22,20 +21,6 @@
}


def extract_publish_info_from_issue(
patterns: dict[str, Pattern[str]], body: str
) -> dict[str, str | None]:
"""
根据提供的正则表达式和议题内容来提取所需的信息
"""
matchers = {key: pattern.search(body) for key, pattern in patterns.items()}
data = {
key: match.group(1).strip() if match else None
for key, match in matchers.items()
}
return data


def validate_info(
publish_type: PublishType,
raw_data: dict[str, Any],
Expand Down
10 changes: 9 additions & 1 deletion tests/publish/process/test_auto_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ async def test_auto_merge_need_rebase(app: App, mocker: MockerFixture) -> None:
需要 rebase 的情况
"""
from src.plugins.github.plugins.publish import auto_merge_matcher
from src.plugins.github.models import GithubHandler, RepoInfo
from nonebot.adapters.github import Bot

mock_subprocess_run = mocker.patch("subprocess.run")
mock_resolve_conflict_pull_requests = mocker.patch(
Expand Down Expand Up @@ -162,7 +164,13 @@ async def test_auto_merge_need_rebase(app: App, mocker: MockerFixture) -> None:
],
any_order=True,
)
mock_resolve_conflict_pull_requests.assert_called_once_with([mock_pull])
mock_resolve_conflict_pull_requests.assert_called_once_with(
GithubHandler(
bot=bot,
repo_info=RepoInfo(owner="he0119", repo="action-test"),
),
[mock_pull],
)


async def test_auto_merge_not_publish(app: App, mocker: MockerFixture) -> None:
Expand Down
Loading

0 comments on commit 2417e87

Please sign in to comment.