From 15e6e53f4a94dc46c79cdf2add448073d3651dad Mon Sep 17 00:00:00 2001 From: BigOrangeQWQ <2284086963@qq.com> Date: Sun, 13 Oct 2024 15:19:54 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E4=BA=86=E8=B7=B3?= =?UTF-8?q?=E8=BF=87=E6=B5=8B=E8=AF=95=E7=9A=84=E6=8F=92=E4=BB=B6=E5=90=8D?= =?UTF-8?q?=E5=AD=97=E6=97=A0=E6=B3=95=E8=A2=AB=E6=AD=A3=E7=A1=AE=E8=AF=86?= =?UTF-8?q?=E5=88=AB=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../github/plugins/publish/validation.py | 26 +++++++++---------- src/providers/store_test/models.py | 13 ++++++++++ src/providers/validation/__init__.py | 5 +++- src/providers/validation/models.py | 3 ++- .../publish/process/test_publish_check.py | 18 ++----------- .../process/test_publish_pull_request.py | 2 +- .../utils/test_trigger_registry_update.py | 26 +++++++++---------- 7 files changed, 48 insertions(+), 45 deletions(-) diff --git a/src/plugins/github/plugins/publish/validation.py b/src/plugins/github/plugins/publish/validation.py index 7c852d02..7f725d2f 100644 --- a/src/plugins/github/plugins/publish/validation.py +++ b/src/plugins/github/plugins/publish/validation.py @@ -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"] @@ -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, @@ -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( DOCKER_IMAGES, project_link, module_name, test_config ).run("3.10") plugin_metadata: Metadata | None = plugin_test_result.metadata + # 去除颜色字符 plugin_test_output = strip_ansi("\n".join(plugin_test_result.outputs)) + # 更新 load 和 metadata 字段 + raw_data["load"] = plugin_test_result.load + raw_data["metadata"] = plugin_metadata + logger.info(f"插件测试结果: {plugin_test_result}") logger.info(f"插件元数据: {plugin_metadata}") - raw_data.update( - { - "load": plugin_test_result.load, - "metadata": plugin_metadata, - } - ) + if plugin_metadata: # 从插件测试结果中获得元数据 raw_data.update(plugin_metadata.model_dump()) diff --git a/src/providers/store_test/models.py b/src/providers/store_test/models.py index 50857a4e..1a39c38a 100644 --- a/src/providers/store_test/models.py +++ b/src/providers/store_test/models.py @@ -1,4 +1,5 @@ from datetime import datetime +import json from typing import Any, Literal from zoneinfo import ZoneInfo @@ -9,6 +10,7 @@ field_validator, model_validator, ) +from pydantic_core import PydanticCustomError from pydantic_extra_types.color import Color @@ -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 格式不合法") + + return v + class Plugin(TagModel): """NoneBot 商店插件数据""" diff --git a/src/providers/validation/__init__.py b/src/providers/validation/__init__.py index c4be4756..769127e4 100644 --- a/src/providers/validation/__init__.py +++ b/src/providers/validation/__init__.py @@ -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), ) diff --git a/src/providers/validation/models.py b/src/providers/validation/models.py index 168b77bd..0bfe1c0e 100644 --- a/src/providers/validation/models.py +++ b/src/providers/validation/models.py @@ -258,6 +258,7 @@ def plugin_test_metadata_validator( context = info.context if context is None: raise PydanticCustomError("validation_context", "未获取到验证上下文") + if v is None: # 如果没有传入插件元数据,尝试从上下文中获取 try: @@ -267,7 +268,7 @@ def plugin_test_metadata_validator( "plugin.metadata", "插件无法获取到元数据", { - "load": context.get("load", True), + "load": context.get("load", False), }, ) return v diff --git a/tests/github/publish/process/test_publish_check.py b/tests/github/publish/process/test_publish_check.py index 7edc58a2..33fe63e7 100644 --- a/tests/github/publish/process/test_publish_check.py +++ b/tests/github/publish/process/test_publish_check.py @@ -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 @@ -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}, @@ -987,11 +973,11 @@ async def test_skip_plugin_check( """\ # 📃 商店发布检查结果 -> Plugin: +> Plugin: project_link **⚠️ 在发布检查过程中,我们发现以下问题:** -
  • ⚠️ 名称: 无法匹配到数据或值并不合法。
    请确保填写该数据项。
  • ⚠️ 描述: 无法匹配到数据或值并不合法。
    请确保填写该数据项。
  • ⚠️ 项目仓库/主页链接: 无法匹配到数据或值并不合法。
    请确保填写该数据项。
  • ⚠️ 插件类型 None 不符合规范。
    请确保插件类型正确,当前仅支持 application 与 library。
  • ⚠️ 无法获取到插件元数据。
    请填写插件元数据。
  • +
  • ⚠️ 名称: 无法匹配到数据或值并不合法。
    请确保填写该数据项。
  • ⚠️ 描述: 无法匹配到数据或值并不合法。
    请确保填写该数据项。
  • ⚠️ 项目仓库/主页链接: 无法匹配到数据或值并不合法。
    请确保填写该数据项。
  • ⚠️ 插件类型 None 不符合规范。
    请确保插件类型正确,当前仅支持 application 与 library。
  • ⚠️ 插件测试元数据 > 名称: 无法匹配到数据或值并不合法。
    请确保填写该数据项。
  • ⚠️ 插件测试元数据 > 描述: 无法匹配到数据或值并不合法。
    请确保填写该数据项。
  • ⚠️ 插件测试元数据 > 项目仓库/主页链接: 无法匹配到数据或值并不合法。
    请确保填写该数据项。
  • 详情 diff --git a/tests/github/publish/process/test_publish_pull_request.py b/tests/github/publish/process/test_publish_pull_request.py index cfb7f2dc..54783b41 100644 --- a/tests/github/publish/process/test_publish_pull_request.py +++ b/tests/github/publish/process/test_publish_pull_request.py @@ -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"]}}', } ), }, diff --git a/tests/github/publish/utils/test_trigger_registry_update.py b/tests/github/publish/utils/test_trigger_registry_update.py index 267fd85e..f1f18b2e 100644 --- a/tests/github/publish/utils/test_trigger_registry_update.py +++ b/tests/github/publish/utils/test_trigger_registry_update.py @@ -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, )