Skip to content
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
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

- 将插件配置项传递给 registry

## [2.6.0] - 2023-06-26

### Added
Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,6 @@ jobs:
poetry install
mkdir -p plugin_test/store
curl -sSL https://raw.githubusercontent.com/nonebot/registry/results/results.json -o plugin_test/store/results.json
curl -sSL https://raw.githubusercontent.com/nonebot/registry/main/inputs/configs.json -o plugin_test/store/configs.json
curl -sSL https://raw.githubusercontent.com/nonebot/nonebot2/master/website/static/plugins.json -o plugin_test/store/plugins.json
curl -sSL https://raw.githubusercontent.com/nonebot/nonebot2/master/website/static/bots.json -o plugin_test/bots.json
curl -sSL https://raw.githubusercontent.com/nonebot/nonebot2/master/website/static/adapters.json -o plugin_test/adapters.json
Expand All @@ -202,7 +201,7 @@ jobs:
poetry run python -m src.utils.store_test --offset ${{ github.event.inputs.offset || 0 }} --limit ${{ github.event.inputs.limit || 50 }} ${{ github.event.inputs.args }}
- name: Update registry(Plugin)
if: github.event.client_payload.type == 'Plugin'
run: poetry run python -m src.utils.store_test -k ${{ github.event.client_payload.key }}
run: poetry run python -m src.utils.store_test -k "${{ github.event.client_payload.key }}" -c "${{ github.event.client_payload.config }}" -f
- name: Upload results
uses: actions/upload-artifact@v3
with:
Expand Down
8 changes: 5 additions & 3 deletions src/plugins/publish/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,6 @@ async def handle_pr_close(
related_issue_number: int = Depends(get_related_issue_number),
) -> None:
async with bot.as_installation(installation_id):
# 如果商店更新则触发 registry 更新
await trigger_registry_update(bot, event.payload.pull_request, publish_type)

issue = (
await bot.rest.issues.async_get(
**repo_info.dict(), issue_number=related_issue_number
Expand All @@ -96,6 +93,11 @@ async def handle_pr_close(
)
logger.info(f"议题 #{related_issue_number} 已关闭")

# 如果商店更新则触发 registry 更新
await trigger_registry_update(
bot, publish_type, event.payload.pull_request, issue
)

try:
run_shell_command(
[
Expand Down
1 change: 1 addition & 0 deletions src/plugins/publish/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
PLUGIN_HOMEPAGE_PATTERN = re.compile(ISSUE_PATTERN.format(PLUGIN_HOMEPAGE_STRING))
PLUGIN_TYPE_STRING = "插件类型"
PLUGIN_TYPE_PATTERN = re.compile(ISSUE_PATTERN.format(PLUGIN_TYPE_STRING))
PLUGIN_CONFIG_PATTERN = re.compile(r"### 插件配置项\s+```(?:\w+)?\s?([\s\S]*?)```")
PLUGIN_SUPPORTED_ADAPTERS_STRING = "插件支持的适配器"
PLUGIN_SUPPORTED_ADAPTERS_PATTERN = re.compile(
ISSUE_PATTERN.format(PLUGIN_SUPPORTED_ADAPTERS_STRING)
Expand Down
14 changes: 11 additions & 3 deletions src/plugins/publish/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
ISSUE_FIELD_PATTERN,
ISSUE_FIELD_TEMPLATE,
NONEFLOW_MARKER,
PLUGIN_CONFIG_PATTERN,
PLUGIN_STRING_LIST,
POWERED_BY_NONEFLOW_MESSAGE,
REUSE_MESSAGE,
Expand Down Expand Up @@ -326,10 +327,14 @@ async def ensure_issue_content(


async def trigger_registry_update(
bot: GitHubBot, pull: "PullRequestClosedPropPullRequest", publish_type: PublishType
bot: GitHubBot,
publish_type: PublishType,
pull: "PullRequestClosedPropPullRequest",
issue: "Issue",
):
"""通过 repository_dispatch 触发商店测试更新"""
"""通过 repository_dispatch 触发商店列表更新"""
if not pull.merged:
logger.info("拉取请求未合并,跳过触发商店列表更新")
return

if publish_type == PublishType.PLUGIN:
Expand All @@ -342,19 +347,22 @@ async def trigger_registry_update(
plugin = plugins[-1]
project_link = plugin["project_link"]
module_name = plugin["module_name"]
config = PLUGIN_CONFIG_PATTERN.search(issue.body) if issue.body else ""

client_payload = {
"type": publish_type.value,
"key": f"{project_link}:{module_name}",
"config": config.group(1) if config else "",
}
else:
client_payload = {"type": publish_type.value}

owner, repo = plugin_config.input_config.registry_repository.split("/")
# 触发插件测试
# 触发商店列表更新
await bot.rest.repos.async_create_dispatch_event(
repo=repo,
owner=owner,
event_type="registry_update",
client_payload=client_payload, # type: ignore
)
logger.info("已触发商店列表更新")
5 changes: 3 additions & 2 deletions src/utils/store_test/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@
@click.option("-o", "--offset", default=0, show_default=True, help="测试插件偏移量")
@click.option("-f", "--force", is_flag=True, help="强制重新测试")
@click.option("-k", "--key", default=None, show_default=True, help="测试插件标识符")
def main(limit: int, offset: int, force: bool, key: str | None):
@click.option("-c", "--config", default=None, show_default=True, help="测试插件配置")
def main(limit: int, offset: int, force: bool, key: str | None, config: str | None):
from .store import StoreTest

test = StoreTest(offset, limit, force)
run(test.run(key))
run(test.run(key, config))


if __name__ == "__main__":
Expand Down
2 changes: 0 additions & 2 deletions src/utils/store_test/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
""" 机器人列表文件路径 """
STORE_ADAPTERS_PATH = STORE_DIR / "adapters.json"
""" 适配器列表文件路径 """
STORE_CONFIGS_PATH = STORE_DIR / "configs.json"
""" 配置文件路径 """
PREVIOUS_RESULTS_PATH = STORE_DIR / "results.json"
""" 上次测试结果文件路径 """
MOCK_PLUGINS_PATH = STORE_DIR / "mock_plugins.json"
Expand Down
1 change: 1 addition & 0 deletions src/utils/store_test/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,5 @@ class TestResult(TypedDict):
version: str | None
results: dict[Literal["validation", "load", "metadata"], bool]
outputs: dict[Literal["validation", "load", "metadata"], Any]
inputs: dict[Literal["config"], str]
plugin: dict[Literal["old", "new"], PluginData | None]
14 changes: 12 additions & 2 deletions src/utils/store_test/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,19 @@ def generate_plugin_list(self, results: Iterable[TestResult]):
with open(PLUGINS_PATH, "w", encoding="utf8") as f:
json.dump(plugins, f, indent=2, ensure_ascii=False)

async def run(self, key: str | None = None):
async def run(self, key: str | None = None, config: str | None = None):
"""测试商店内插件情况"""
if key:
test_plugins = [(key, self._plugin_list[key])]
plugin_configs = {key: config or ""}
else:
test_plugins = list(self._plugin_list.items())[self._offset :]
plugin_configs = {
key: self._previous_results.get(key, {})
.get("inputs", {})
.get("config", "")
for key, _ in test_plugins
}

new_results: dict[str, TestResult] = {}

Expand All @@ -92,7 +99,10 @@ async def run(self, key: str | None = None):
continue

print(f"{i}/{self._limit} 正在测试插件 {key} ...")
new_results[key] = await validate_plugin(key, plugin)

new_results[key] = await validate_plugin(
plugin, plugin_configs.get(key, "")
)

i += 1

Expand Down
10 changes: 3 additions & 7 deletions src/utils/store_test/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@
from src.plugins.publish.validation import PluginPublishInfo
from src.utils.plugin_test import PluginTest, strip_ansi

from .constants import STORE_CONFIGS_PATH
from .models import Metadata, PluginData, PluginValidation, TestResult
from .utils import load_json

_CONFIGS = load_json(STORE_CONFIGS_PATH)


def extract_metadata(path: Path) -> Metadata | None:
Expand Down Expand Up @@ -89,13 +85,12 @@ async def validate_metadata(
}


async def validate_plugin(key: str, plugin: PluginData) -> TestResult:
async def validate_plugin(plugin: PluginData, config: str) -> TestResult:
"""验证插件"""
project_link = plugin["project_link"]
module_name = plugin["module_name"]

plugin_config = "\n".join(_CONFIGS.get(key, []))
test = PluginTest(project_link, module_name, plugin_config)
test = PluginTest(project_link, module_name, config)

# 将 GitHub Action 的输出文件重定向到测试文件夹内
test.github_output_file = (test.path / "output.txt").resolve()
Expand Down Expand Up @@ -132,6 +127,7 @@ async def validate_plugin(key: str, plugin: PluginData) -> TestResult:
"load": load_output,
"metadata": metadata,
},
"inputs": {"config": config},
"plugin": {
"old": plugin,
"new": new_plugin,
Expand Down
28 changes: 15 additions & 13 deletions tests/publish/process/test_pull_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ async def test_process_pull_request(app: App, mocker: MockerFixture) -> None:

mock_issue = mocker.MagicMock()
mock_issue.state = "open"
mock_issue.body = "### 插件配置项\n\n```dotenv\nlog_level=DEBUG\n```"

mock_issues_resp = mocker.MagicMock()
mock_issues_resp.parsed_data = mock_issue
Expand Down Expand Up @@ -47,19 +48,6 @@ async def test_process_pull_request(app: App, mocker: MockerFixture) -> None:
{"owner": "he0119", "repo": "action-test"},
mock_installation_resp,
)
ctx.should_call_api(
"rest.repos.async_create_dispatch_event",
{
"repo": "registry",
"owner": "owner",
"event_type": "registry_update",
"client_payload": {
"type": "Plugin",
"key": "project_link1:module_name1",
},
},
True,
)
ctx.should_call_api(
"rest.issues.async_get",
{"owner": "he0119", "repo": "action-test", "issue_number": 76},
Expand All @@ -76,6 +64,20 @@ async def test_process_pull_request(app: App, mocker: MockerFixture) -> None:
},
True,
)
ctx.should_call_api(
"rest.repos.async_create_dispatch_event",
{
"repo": "registry",
"owner": "owner",
"event_type": "registry_update",
"client_payload": {
"type": "Plugin",
"key": "project_link1:module_name1",
"config": "log_level=DEBUG\n",
},
},
True,
)
ctx.should_call_api(
"rest.pulls.async_list",
{"owner": "he0119", "repo": "action-test", "state": "open"},
Expand Down