From 5ac040c6a6c96b0fcdf6263ff200475f1301b0fc Mon Sep 17 00:00:00 2001 From: uy/sun Date: Tue, 19 Nov 2024 22:46:03 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E9=87=8D=E6=96=B0=E5=8A=A0=E5=9B=9E?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A=E6=91=98=E8=A6=81=20(#274)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: 重新加回作业摘要 * refactor: 简化写法 * test: 添加测试 --- .env | 1 + CHANGELOG.md | 4 ++ src/plugins/github/config.py | 1 + .../github/plugins/publish/validation.py | 17 ++++- tests/conftest.py | 5 +- .../utils/test_validate_info_from_issue.py | 67 +++++++++++++++++++ 6 files changed, 91 insertions(+), 4 deletions(-) diff --git a/.env b/.env index 714aa124..485c2bbb 100644 --- a/.env +++ b/.env @@ -3,6 +3,7 @@ GITHUB_REPOSITORY GITHUB_RUN_ID GITHUB_EVENT_NAME GITHUB_EVENT_PATH +GITHUB_STEP_SUMMARY # 配置 GITHUB_APPS diff --git a/CHANGELOG.md b/CHANGELOG.md index 55d38ce1..7a474de2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/lang/zh-CN/ ## [Unreleased] +### Added + +- 重新加回作业摘要 + ### Changed - 简化插件测试输出 diff --git a/src/plugins/github/config.py b/src/plugins/github/config.py index 8eabc1c6..39493cc0 100644 --- a/src/plugins/github/config.py +++ b/src/plugins/github/config.py @@ -32,3 +32,4 @@ class Config(BaseModel, extra="ignore"): input_config: PublishConfig github_repository: str github_run_id: str + github_step_summary: Path diff --git a/src/plugins/github/plugins/publish/validation.py b/src/plugins/github/plugins/publish/validation.py index 7b0f5d6f..191f9721 100644 --- a/src/plugins/github/plugins/publish/validation.py +++ b/src/plugins/github/plugins/publish/validation.py @@ -40,6 +40,12 @@ def strip_ansi(text: str | None) -> str: return ansi_escape.sub("", text) +def add_step_summary(summary: str): + """添加作业摘要""" + with plugin_config.github_step_summary.open("a") as f: + f.write(summary + "\n") + + async def validate_plugin_info_from_issue( handler: IssueHandler, skip_test: bool | None = None ) -> ValidationDict: @@ -108,9 +114,14 @@ async def validate_plugin_info_from_issue( raw_data["metadata"] = bool(metadata) # 输出插件测试相关信息 - logger.info( - f"插件 {project_link}({test_result.version}) 加载{'成功' if test_result.load else '失败'},运行{'开始' if test_result.run else '失败'}" - ) + test_status = f"插件 {project_link}({test_result.version}) 加载{'成功' if test_result.load else '失败'},运行{'开始' if test_result.run else '失败'}" + + add_step_summary(test_status) + add_step_summary(f"插件元数据:{metadata}") + add_step_summary("插件测试输出:") + add_step_summary(test_output) + + logger.info(test_status) logger.info(f"插件元数据:{metadata}") logger.info("插件测试输出:") for output in test_result.outputs: diff --git a/tests/conftest.py b/tests/conftest.py index 40f0f498..12f676a5 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -31,6 +31,7 @@ def pytest_configure(config: pytest.Config) -> None: "github_run_id": "123456", "github_event_path": "event_path", "github_apps": [], + "github_step_summary": "step_summary", } @@ -77,7 +78,6 @@ async def app(app: App, tmp_path: Path, mocker: MockerFixture): } ], ) - plugin_path = tmp_path / "plugins.json5" dump_json5( plugin_path, @@ -95,6 +95,9 @@ async def app(app: App, tmp_path: Path, mocker: MockerFixture): mocker.patch.object(plugin_config.input_config, "adapter_path", adapter_path) mocker.patch.object(plugin_config.input_config, "bot_path", bot_path) mocker.patch.object(plugin_config.input_config, "plugin_path", plugin_path) + mocker.patch.object( + plugin_config, "github_step_summary", tmp_path / "step_summary.txt" + ) yield app diff --git a/tests/github/publish/utils/test_validate_info_from_issue.py b/tests/github/publish/utils/test_validate_info_from_issue.py index 015a8ec1..6864c7a7 100644 --- a/tests/github/publish/utils/test_validate_info_from_issue.py +++ b/tests/github/publish/utils/test_validate_info_from_issue.py @@ -1,4 +1,5 @@ from githubkit.rest import Issue +from inline_snapshot import snapshot from nonebug import App from pytest_mock import MockerFixture from respx import MockRouter @@ -6,6 +7,7 @@ from tests.github.utils import ( generate_issue_body_adapter, generate_issue_body_bot, + generate_issue_body_plugin, generate_issue_body_plugin_skip_test, get_github_bot, ) @@ -48,6 +50,71 @@ async def test_validate_info_from_issue_bot( async def test_validate_info_from_issue_plugin( app: App, mocker: MockerFixture, mocked_api: MockRouter ): + from src.plugins.github import plugin_config + from src.plugins.github.models import RepoInfo + from src.plugins.github.models.issue import IssueHandler + from src.plugins.github.plugins.publish.validation import ( + validate_plugin_info_from_issue, + ) + from src.providers.docker_test import Metadata + + mock_user = mocker.MagicMock() + mock_user.login = "test" + mock_user.id = 1 + + mock_issue = mocker.MagicMock(spec=Issue) + mock_issue.body = generate_issue_body_plugin() + mock_issue.number = 1 + mock_issue.user = mock_user + + mock_list_comments_resp = mocker.MagicMock() + mock_list_comments_resp.parsed_data = [] + + mock_test_result = mocker.MagicMock() + mock_test_result.metadata = Metadata( + name="name", + desc="desc", + homepage="https://nonebot.dev", + type="application", + supported_adapters=["~onebot.v11"], + ) + mock_test_result.version = "1.0.0" + mock_test_result.load = True + mock_test_result.outputs = ['require("nonebot_plugin_alconna")', "test"] + mock_docker = mocker.patch("src.providers.docker_test.DockerPluginTest.run") + mock_docker.return_value = mock_test_result + + async with app.test_api() as ctx: + _, bot = get_github_bot(ctx) + handler = IssueHandler( + bot=bot, repo_info=RepoInfo(owner="owner", repo="repo"), issue=mock_issue + ) + + ctx.should_call_api( + "rest.issues.async_list_comments", + {"owner": "owner", "repo": "repo", "issue_number": 1}, + mock_list_comments_resp, + ) + + result = await validate_plugin_info_from_issue(handler) + + assert result.valid + assert mocked_api["homepage"].called + assert plugin_config.github_step_summary.read_text() == snapshot( + """\ +插件 project_link(1.0.0) 加载成功,运行开始 +插件元数据:name='name' desc='desc' homepage='https://nonebot.dev' type='application' supported_adapters=['~onebot.v11'] +插件测试输出: +require("nonebot_plugin_alconna") +test +""" + ) + + +async def test_validate_info_from_issue_plugin_skip_test( + app: App, mocker: MockerFixture, mocked_api: MockRouter +): + """跳过插件测试的情况""" from src.plugins.github.models import RepoInfo from src.plugins.github.models.issue import IssueHandler from src.plugins.github.plugins.publish.validation import (