From 26882c03cd3c8d7c383e149c4a020cb0e956929d Mon Sep 17 00:00:00 2001 From: uy/sun Date: Wed, 27 Nov 2024 19:52:17 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E4=B8=AD=E7=9A=84=E6=8F=90=E7=A4=BA=20(#294)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit close #235 --- CHANGELOG.md | 1 + src/plugins/github/models/issue.py | 3 + .../github/plugins/publish/__init__.py | 4 + .../github/plugins/publish/constants.py | 3 +- src/plugins/github/plugins/publish/utils.py | 34 +++++--- .../publish/process/test_publish_check.py | 80 +++++++++++++++++-- .../test_ensure_issue_plugin_test_button.py | 69 ++++++++++++++-- 7 files changed, 171 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c811f07..edbcedd6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/lang/zh-CN/ - 从插件测试中提取环境信息 - 支持修改插件配置并重新测试 +- 添加测试中的提示 ### Fixed diff --git a/src/plugins/github/models/issue.py b/src/plugins/github/models/issue.py index 2094e3d8..fa6e933c 100644 --- a/src/plugins/github/models/issue.py +++ b/src/plugins/github/models/issue.py @@ -51,6 +51,9 @@ async def update_issue_content(self, body: str, issue_number: int | None = None) if issue_number is None: issue_number = self.issue_number + if self.issue_number == issue_number and self.issue.body == body: + return + await super().update_issue_content(body, issue_number) # 更新缓存属性,避免重复或错误操作 diff --git a/src/plugins/github/plugins/publish/__init__.py b/src/plugins/github/plugins/publish/__init__.py index 2c93820d..9f38896f 100644 --- a/src/plugins/github/plugins/publish/__init__.py +++ b/src/plugins/github/plugins/publish/__init__.py @@ -42,6 +42,7 @@ from .utils import ( ensure_issue_content, ensure_issue_plugin_test_button, + ensure_issue_plugin_test_button_in_progress, process_pull_request, resolve_conflict_pull_requests, trigger_registry_update, @@ -109,6 +110,9 @@ async def handle_publish_plugin_check( logger.info("议题未开启,已跳过") await publish_check_matcher.finish() + # 提示插件正在测试中 + await ensure_issue_plugin_test_button_in_progress(handler) + # 是否需要跳过插件测试 skip_test = await handler.should_skip_test() # 如果需要跳过插件测试,则修改议题内容,确保其包含插件所需信息 diff --git a/src/plugins/github/plugins/publish/constants.py b/src/plugins/github/plugins/publish/constants.py index 13b43bf5..553b639e 100644 --- a/src/plugins/github/plugins/publish/constants.py +++ b/src/plugins/github/plugins/publish/constants.py @@ -26,9 +26,10 @@ PLUGIN_TYPE_PATTERN = re.compile(ISSUE_PATTERN.format(PLUGIN_TYPE_STRING)) PLUGIN_CONFIG_PATTERN = re.compile(r"### 插件配置项\s+```(?:\w+)?\s?([\s\S]*?)```") PLUGIN_TEST_STRING = "插件测试" +PLUGIN_TEST_PATTERN = re.compile(ISSUE_PATTERN.format(PLUGIN_TEST_STRING)) PLUGIN_TEST_BUTTON_TIPS = "如需重新运行插件测试,请勾选左侧勾选框" PLUGIN_TEST_BUTTON_STRING = f"- [ ] {PLUGIN_TEST_BUTTON_TIPS}" -PLUGIN_TEST_BUTTON_PATTERN = re.compile(rf"- \[([ |x])\] {PLUGIN_TEST_BUTTON_TIPS}") +PLUGIN_TEST_BUTTON_IN_PROGRESS_STRING = "- [x] 🔥插件测试中,请稍后" PLUGIN_SUPPORTED_ADAPTERS_STRING = "插件支持的适配器" PLUGIN_SUPPORTED_ADAPTERS_PATTERN = re.compile( ISSUE_PATTERN.format(PLUGIN_SUPPORTED_ADAPTERS_STRING) diff --git a/src/plugins/github/plugins/publish/utils.py b/src/plugins/github/plugins/publish/utils.py index f840c4c7..96d20a9e 100644 --- a/src/plugins/github/plugins/publish/utils.py +++ b/src/plugins/github/plugins/publish/utils.py @@ -18,8 +18,9 @@ BRANCH_NAME_PREFIX, COMMIT_MESSAGE_PREFIX, PLUGIN_STRING_LIST, - PLUGIN_TEST_BUTTON_PATTERN, + PLUGIN_TEST_BUTTON_IN_PROGRESS_STRING, PLUGIN_TEST_BUTTON_STRING, + PLUGIN_TEST_PATTERN, PLUGIN_TEST_STRING, ) from .validation import ( @@ -167,21 +168,32 @@ async def ensure_issue_content(handler: IssueHandler): async def ensure_issue_plugin_test_button(handler: IssueHandler): - """确保议题内容中包含插件重测按钮""" + """确保议题内容中包含插件测试按钮""" issue_body = handler.issue.body or "" - search_result = PLUGIN_TEST_BUTTON_PATTERN.search(issue_body) - if not search_result: + match = PLUGIN_TEST_PATTERN.search(issue_body) + if not match: new_content = f"{ISSUE_FIELD_TEMPLATE.format(PLUGIN_TEST_STRING)}\n\n{PLUGIN_TEST_BUTTON_STRING}" + new_content = f"{issue_body}\n\n{new_content}" + logger.info("为议题添加插件重测按钮") + else: + new_content = f"{ISSUE_FIELD_TEMPLATE.format(PLUGIN_TEST_STRING)}\n\n{PLUGIN_TEST_BUTTON_STRING}" + new_content = PLUGIN_TEST_PATTERN.sub(new_content, issue_body) + logger.info("重置插件重测按钮文本") + + await handler.update_issue_content(new_content) + + +async def ensure_issue_plugin_test_button_in_progress(handler: IssueHandler): + """确保议题内容中包含插件测试进行中的提示""" + issue_body = handler.issue.body or "" + + match = PLUGIN_TEST_PATTERN.search(issue_body) + if not match: + new_content = f"{ISSUE_FIELD_TEMPLATE.format(PLUGIN_TEST_STRING)}\n\n{PLUGIN_TEST_BUTTON_IN_PROGRESS_STRING}" await handler.update_issue_content(f"{issue_body}\n\n{new_content}") - logger.info("为议题添加插件重测按钮") - elif search_result.group(1) == "x": - new_content = issue_body.replace( - search_result.group(0), PLUGIN_TEST_BUTTON_STRING - ) - await handler.update_issue_content(f"{new_content}") - logger.info("取消勾选议题的插件测试按钮") + logger.info("为议题添加插件测试进行中的提示") async def process_pull_request( diff --git a/tests/github/publish/process/test_publish_check.py b/tests/github/publish/process/test_publish_check.py index 2b2723ff..8a55da2a 100644 --- a/tests/github/publish/process/test_publish_check.py +++ b/tests/github/publish/process/test_publish_check.py @@ -1056,6 +1056,40 @@ async def test_skip_plugin_check( {"owner": "he0119", "repo": "action-test", "issue_number": 70}, mock_issues_resp, ) + ctx.should_call_api( + "rest.issues.async_update", + snapshot( + { + "owner": "he0119", + "repo": "action-test", + "issue_number": 70, + "body": """\ +### PyPI 项目名 + +project_link + +### 插件 import 包名 + +module_name + +### 标签 + +[{"label": "test", "color": "#ffffff"}] + +### 插件配置项 + +```dotenv +log_level=DEBUG +``` + +### 插件测试 + +- [x] 🔥插件测试中,请稍后\ +""", + } + ), + True, + ) ctx.should_call_api( "rest.issues.async_list_comments", {"owner": "he0119", "repo": "action-test", "issue_number": 70}, @@ -1063,12 +1097,46 @@ async def test_skip_plugin_check( ) ctx.should_call_api( "rest.issues.async_update", - { - "owner": "he0119", - "repo": "action-test", - "issue_number": 70, - "body": '### 插件名称\n\n### 插件描述\n\n### 插件项目仓库/主页链接\n\n### 插件类型\n\n### 插件支持的适配器\n\n### PyPI 项目名\n\nproject_link\n\n### 插件 import 包名\n\nmodule_name\n\n### 标签\n\n[{"label": "test", "color": "#ffffff"}]\n\n### 插件配置项\n\n```dotenv\nlog_level=DEBUG\n```', - }, + snapshot( + { + "owner": "he0119", + "repo": "action-test", + "issue_number": 70, + "body": """\ +### 插件名称 + +### 插件描述 + +### 插件项目仓库/主页链接 + +### 插件类型 + +### 插件支持的适配器 + +### PyPI 项目名 + +project_link + +### 插件 import 包名 + +module_name + +### 标签 + +[{"label": "test", "color": "#ffffff"}] + +### 插件配置项 + +```dotenv +log_level=DEBUG +``` + +### 插件测试 + +- [x] 🔥插件测试中,请稍后\ +""", + } + ), True, ) diff --git a/tests/github/publish/utils/test_ensure_issue_plugin_test_button.py b/tests/github/publish/utils/test_ensure_issue_plugin_test_button.py index 681c65e8..11368d5a 100644 --- a/tests/github/publish/utils/test_ensure_issue_plugin_test_button.py +++ b/tests/github/publish/utils/test_ensure_issue_plugin_test_button.py @@ -64,10 +64,6 @@ async def test_ensure_issue_plugin_test_button(app: App, mocker: MockerFixture): await ensure_issue_plugin_test_button(handler) -from nonebug import App -from pytest_mock import MockerFixture - - async def test_ensure_issue_plugin_test_button_checked(app: App, mocker: MockerFixture): """如果测试按钮勾选,则自动取消勾选""" from src.plugins.github.models import IssueHandler, RepoInfo @@ -132,7 +128,9 @@ async def test_ensure_issue_plugin_test_button_unchecked( ): """如果测试按钮未勾选,则不进行操作""" from src.plugins.github.models import IssueHandler, RepoInfo - from src.plugins.github.plugins.publish.utils import ensure_issue_plugin_test_button + from src.plugins.github.plugins.publish.utils import ( + ensure_issue_plugin_test_button, + ) mock_issue = MockIssue( body=MockBody(type="plugin", test_button=False).generate(), @@ -149,3 +147,64 @@ async def test_ensure_issue_plugin_test_button_unchecked( ) await ensure_issue_plugin_test_button(handler) + + +async def test_ensure_issue_plugin_test_button_in_progress( + app: App, mocker: MockerFixture +): + """确保添加插件测试按钮""" + from src.plugins.github.models import IssueHandler, RepoInfo + from src.plugins.github.plugins.publish.utils import ( + ensure_issue_plugin_test_button_in_progress, + ) + + mock_issue = MockIssue( + body=MockBody(type="plugin").generate(), + number=1, + ).as_mock(mocker) + + async with app.test_api() as ctx: + _, bot = get_github_bot(ctx) + + ctx.should_call_api( + "rest.issues.async_update", + snapshot( + { + "owner": "owner", + "repo": "repo", + "issue_number": 1, + "body": """\ +### PyPI 项目名 + +project_link + +### 插件 import 包名 + +module_name + +### 标签 + +[{"label": "test", "color": "#ffffff"}] + +### 插件配置项 + +```dotenv +log_level=DEBUG +``` + +### 插件测试 + +- [x] 🔥插件测试中,请稍后\ +""", + } + ), + True, + ) + + handler = IssueHandler( + bot=bot, + repo_info=RepoInfo(owner="owner", repo="repo"), + issue=mock_issue, + ) + + await ensure_issue_plugin_test_button_in_progress(handler)