Skip to content

Commit

Permalink
feat: 添加测试中的提示 (#294)
Browse files Browse the repository at this point in the history
close #235
  • Loading branch information
he0119 authored Nov 27, 2024
1 parent e1a7756 commit 26882c0
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 23 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/lang/zh-CN/

- 从插件测试中提取环境信息
- 支持修改插件配置并重新测试
- 添加测试中的提示

### Fixed

Expand Down
3 changes: 3 additions & 0 deletions src/plugins/github/models/issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

# 更新缓存属性,避免重复或错误操作
Expand Down
4 changes: 4 additions & 0 deletions src/plugins/github/plugins/publish/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()
# 如果需要跳过插件测试,则修改议题内容,确保其包含插件所需信息
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/github/plugins/publish/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
34 changes: 23 additions & 11 deletions src/plugins/github/plugins/publish/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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(
Expand Down
80 changes: 74 additions & 6 deletions tests/github/publish/process/test_publish_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -1056,19 +1056,87 @@ 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},
mock_list_comments_resp,
)
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,
)

Expand Down
69 changes: 64 additions & 5 deletions tests/github/publish/utils/test_ensure_issue_plugin_test_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(),
Expand All @@ -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)

0 comments on commit 26882c0

Please sign in to comment.