Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 重新加回作业摘要 #274

Merged
merged 3 commits into from
Nov 19, 2024
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
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ GITHUB_REPOSITORY
GITHUB_RUN_ID
GITHUB_EVENT_NAME
GITHUB_EVENT_PATH
GITHUB_STEP_SUMMARY

# 配置
GITHUB_APPS
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/lang/zh-CN/

## [Unreleased]

### Added

- 重新加回作业摘要

### Changed

- 简化插件测试输出
Expand Down
1 change: 1 addition & 0 deletions src/plugins/github/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ class Config(BaseModel, extra="ignore"):
input_config: PublishConfig
github_repository: str
github_run_id: str
github_step_summary: Path
17 changes: 14 additions & 3 deletions src/plugins/github/plugins/publish/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
5 changes: 4 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}


Expand Down Expand Up @@ -77,7 +78,6 @@ async def app(app: App, tmp_path: Path, mocker: MockerFixture):
}
],
)

plugin_path = tmp_path / "plugins.json5"
dump_json5(
plugin_path,
Expand All @@ -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

Expand Down
67 changes: 67 additions & 0 deletions tests/github/publish/utils/test_validate_info_from_issue.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from githubkit.rest import Issue
from inline_snapshot import snapshot
from nonebug import App
from pytest_mock import MockerFixture
from respx import MockRouter

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,
)
Expand Down Expand Up @@ -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 (
Expand Down
Loading