Skip to content

Commit

Permalink
fix: 修复了跳过测试的插件名字无法被正确识别的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
BigOrangeQWQ committed Oct 13, 2024
1 parent ac1550a commit 15e6e53
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 45 deletions.
26 changes: 13 additions & 13 deletions src/plugins/github/plugins/publish/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ async def validate_plugin_info_from_issue(issue: Issue) -> ValidationDict:
},
body,
)
raw_data.update(extract_author_info(issue)) # 更新作者信息
# 更新作者信息
raw_data.update(extract_author_info(issue))

test_config: str = raw_data["test_config"]
module_name: str = raw_data["module_name"]
Expand All @@ -71,13 +72,10 @@ async def validate_plugin_info_from_issue(issue: Issue) -> ValidationDict:
with plugin_config.input_config.plugin_path.open("r", encoding="utf-8") as f:
previous_data: list[dict[str, str]] = json.load(f)

raw_data["name"] = project_link # 若插件没有元数据,则 name 默认为 project_link
raw_data["metadata"] = None

# 如果插件被跳过,则从议题获取插件信息
plugin_test_output: str = "插件未进行测试"
if plugin_config.skip_plugin_test:
plugin_info = extract_publish_info_from_issue(
metadata = extract_publish_info_from_issue(
{
"name": PLUGIN_NAME_PATTERN,
"desc": PLUGIN_DESC_PATTERN,
Expand All @@ -87,24 +85,26 @@ async def validate_plugin_info_from_issue(issue: Issue) -> ValidationDict:
},
body,
)
raw_data.update(plugin_info)
logger.info(f"插件已跳过测试,从议题中获取的插件元信息:{plugin_info}")

raw_data.update(metadata)
raw_data["metadata"] = metadata
logger.info(f"插件已跳过测试,从议题中获取的插件元信息:{metadata}")
else:
# 插件不跳过则运行插件测试
plugin_test_result: DockerTestResult = await DockerPluginTest(

Check warning on line 94 in src/plugins/github/plugins/publish/validation.py

View check run for this annotation

Codecov / codecov/patch

src/plugins/github/plugins/publish/validation.py#L94

Added line #L94 was not covered by tests
DOCKER_IMAGES, project_link, module_name, test_config
).run("3.10")
plugin_metadata: Metadata | None = plugin_test_result.metadata

Check warning on line 97 in src/plugins/github/plugins/publish/validation.py

View check run for this annotation

Codecov / codecov/patch

src/plugins/github/plugins/publish/validation.py#L97

Added line #L97 was not covered by tests
# 去除颜色字符
plugin_test_output = strip_ansi("\n".join(plugin_test_result.outputs))

Check warning on line 99 in src/plugins/github/plugins/publish/validation.py

View check run for this annotation

Codecov / codecov/patch

src/plugins/github/plugins/publish/validation.py#L99

Added line #L99 was not covered by tests

# 更新 load 和 metadata 字段
raw_data["load"] = plugin_test_result.load
raw_data["metadata"] = plugin_metadata

Check warning on line 103 in src/plugins/github/plugins/publish/validation.py

View check run for this annotation

Codecov / codecov/patch

src/plugins/github/plugins/publish/validation.py#L102-L103

Added lines #L102 - L103 were not covered by tests

logger.info(f"插件测试结果: {plugin_test_result}")
logger.info(f"插件元数据: {plugin_metadata}")

Check warning on line 106 in src/plugins/github/plugins/publish/validation.py

View check run for this annotation

Codecov / codecov/patch

src/plugins/github/plugins/publish/validation.py#L105-L106

Added lines #L105 - L106 were not covered by tests
raw_data.update(
{
"load": plugin_test_result.load,
"metadata": plugin_metadata,
}
)

if plugin_metadata:

Check warning on line 108 in src/plugins/github/plugins/publish/validation.py

View check run for this annotation

Codecov / codecov/patch

src/plugins/github/plugins/publish/validation.py#L108

Added line #L108 was not covered by tests
# 从插件测试结果中获得元数据
raw_data.update(plugin_metadata.model_dump())

Check warning on line 110 in src/plugins/github/plugins/publish/validation.py

View check run for this annotation

Codecov / codecov/patch

src/plugins/github/plugins/publish/validation.py#L110

Added line #L110 was not covered by tests
Expand Down
13 changes: 13 additions & 0 deletions src/providers/store_test/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from datetime import datetime
import json
from typing import Any, Literal
from zoneinfo import ZoneInfo

Expand All @@ -9,6 +10,7 @@
field_validator,
model_validator,
)
from pydantic_core import PydanticCustomError
from pydantic_extra_types.color import Color


Expand Down Expand Up @@ -70,6 +72,17 @@ def model_validator(cls, data: dict[str, Any]):
data["desc"] = data.get("description")
return data

@field_validator("supported_adapters", mode="before")
@classmethod
def supported_adapters_validator(cls, v: list[str] | str | None):
if isinstance(v, str):
try:
v = json.loads(v)
except json.JSONDecodeError:
raise PydanticCustomError("json_type", "JSON 格式不合法")

Check warning on line 82 in src/providers/store_test/models.py

View check run for this annotation

Codecov / codecov/patch

src/providers/store_test/models.py#L81-L82

Added lines #L81 - L82 were not covered by tests

return v


class Plugin(TagModel):
"""NoneBot 商店插件数据"""
Expand Down
5 changes: 4 additions & 1 deletion src/providers/validation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ def validate_info(
data=data,
errors=errors, # 方便插件使用的数据
type=publish_type,
name=data.get("name") or raw_data.get("name") or "",
name=data.get("name")
or raw_data.get("name")
or raw_data.get("project_link")
or "",
author=data.get("author", ""),
author_id=data.get("author_id", 0),
)
3 changes: 2 additions & 1 deletion src/providers/validation/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ def plugin_test_metadata_validator(
context = info.context
if context is None:
raise PydanticCustomError("validation_context", "未获取到验证上下文")

Check warning on line 260 in src/providers/validation/models.py

View check run for this annotation

Codecov / codecov/patch

src/providers/validation/models.py#L260

Added line #L260 was not covered by tests

if v is None:
# 如果没有传入插件元数据,尝试从上下文中获取
try:
Expand All @@ -267,7 +268,7 @@ def plugin_test_metadata_validator(
"plugin.metadata",
"插件无法获取到元数据",
{
"load": context.get("load", True),
"load": context.get("load", False),
},
)
return v
Expand Down
18 changes: 2 additions & 16 deletions tests/github/publish/process/test_publish_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -860,9 +860,6 @@ async def test_skip_plugin_check(
mock_user.login = "test"
mock_user.id = 1
mock_issue.user = mock_user
# mock_issue = MockIssue(
# title="Plugin: project_link", number=70, body=generate_issue_body_plugin()
# ).as_mock(mocker)

mock_event = mocker.MagicMock()
mock_event.issue = mock_issue
Expand Down Expand Up @@ -961,17 +958,6 @@ async def test_skip_plugin_check(
},
True,
)
# 修改标题
ctx.should_call_api(
"rest.issues.async_update",
{
"owner": "he0119",
"repo": "action-test",
"issue_number": 70,
"title": "Plugin: ",
},
True,
)
ctx.should_call_api(
"rest.issues.async_list_comments",
{"owner": "he0119", "repo": "action-test", "issue_number": 70},
Expand All @@ -987,11 +973,11 @@ async def test_skip_plugin_check(
"""\
# 📃 商店发布检查结果
> Plugin:
> Plugin: project_link
**⚠️ 在发布检查过程中,我们发现以下问题:**
<pre><code><li>⚠️ 名称: 无法匹配到数据或值并不合法。<dt>请确保填写该数据项。</dt></li><li>⚠️ 描述: 无法匹配到数据或值并不合法。<dt>请确保填写该数据项。</dt></li><li>⚠️ 项目仓库/主页链接: 无法匹配到数据或值并不合法。<dt>请确保填写该数据项。</dt></li><li>⚠️ 插件类型 None 不符合规范。<dt>请确保插件类型正确,当前仅支持 application 与 library。</dt></li><li>⚠️ 无法获取到插件元数据。<dt>请填写插件元数据。</dt></li></code></pre>
<pre><code><li>⚠️ 名称: 无法匹配到数据或值并不合法。<dt>请确保填写该数据项。</dt></li><li>⚠️ 描述: 无法匹配到数据或值并不合法。<dt>请确保填写该数据项。</dt></li><li>⚠️ 项目仓库/主页链接: 无法匹配到数据或值并不合法。<dt>请确保填写该数据项。</dt></li><li>⚠️ 插件类型 None 不符合规范。<dt>请确保插件类型正确,当前仅支持 application 与 library。</dt></li><li>⚠️ 插件测试元数据 &gt; 名称: 无法匹配到数据或值并不合法。<dt>请确保填写该数据项。</dt></li><li>⚠️ 插件测试元数据 &gt; 描述: 无法匹配到数据或值并不合法。<dt>请确保填写该数据项。</dt></li><li>⚠️ 插件测试元数据 &gt; 项目仓库/主页链接: 无法匹配到数据或值并不合法。<dt>请确保填写该数据项。</dt></li></code></pre>
<details>
<summary>详情</summary>
Expand Down
2 changes: 1 addition & 1 deletion tests/github/publish/process/test_publish_pull_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ async def test_process_pull_request_skip_plugin_test(
"type": "Plugin",
"key": "project_link:module_name",
"config": "log_level=DEBUG\n",
"data": '{"module_name": "module_name", "project_link": "project_link", "name": "name", "desc": "desc", "author": "user", "author_id": 1, "homepage": "https://nonebot.dev", "tags": [{"label": "test", "color": "#ffffff"}], "is_official": false, "type": "application", "supported_adapters": ["nonebot.adapters.onebot.v11"], "load": false, "metadata": {"name": "name", "desc": "desc", "homepage": "https://nonebot.dev", "type": "application", "supported_adapters": ["nonebot.adapters.onebot.v11"]}}',
"data": '{"module_name": "module_name", "project_link": "project_link", "name": "name", "desc": "desc", "author": "user", "author_id": 1, "homepage": "https://nonebot.dev", "tags": [{"label": "test", "color": "#ffffff"}], "is_official": false, "type": "application", "supported_adapters": ["nonebot.adapters.onebot.v11"], "load": false, "metadata": {"name": "name", "desc": "desc", "homepage": "https://nonebot.dev", "type": "application", "supported_adapters": ["~onebot.v11"]}}',
}
),
},
Expand Down
26 changes: 13 additions & 13 deletions tests/github/publish/utils/test_trigger_registry_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,19 +100,19 @@ async def test_trigger_registry_update_skip_test(
)
ctx.should_call_api(
"rest.repos.async_create_dispatch_event",
{
"repo": "registry",
"owner": "owner",
"event_type": "registry_update",
"client_payload": {
"type": "Plugin",
"key": "project_link:module_name",
"config": "log_level=DEBUG\n",
"data": snapshot(
'{"module_name": "module_name", "project_link": "project_link", "name": "name", "desc": "desc", "author": "user", "author_id": 1, "homepage": "https://nonebot.dev", "tags": [{"label": "test", "color": "#ffffff"}], "is_official": false, "type": "application", "supported_adapters": ["nonebot.adapters.onebot.v11"], "load": false, "metadata": {"name": "name", "desc": "desc", "homepage": "https://nonebot.dev", "type": "application", "supported_adapters": ["nonebot.adapters.onebot.v11"]}}'
),
},
},
snapshot(
{
"repo": "registry",
"owner": "owner",
"event_type": "registry_update",
"client_payload": {
"type": "Plugin",
"key": "project_link:module_name",
"config": "log_level=DEBUG\n",
"data": '{"module_name": "module_name", "project_link": "project_link", "name": "name", "desc": "desc", "author": "user", "author_id": 1, "homepage": "https://nonebot.dev", "tags": [{"label": "test", "color": "#ffffff"}], "is_official": false, "type": "application", "supported_adapters": ["nonebot.adapters.onebot.v11"], "load": false, "metadata": {"name": "name", "desc": "desc", "homepage": "https://nonebot.dev", "type": "application", "supported_adapters": ["~onebot.v11"]}}',
},
}
),
True,
)

Expand Down

0 comments on commit 15e6e53

Please sign in to comment.