From c5688f81539ff5d4b86bae5702d1a4a274523808 Mon Sep 17 00:00:00 2001 From: uy_sun Date: Mon, 26 Jun 2023 20:46:29 +0800 Subject: [PATCH 1/3] =?UTF-8?q?feat:=20=E5=B0=86=E6=8F=92=E4=BB=B6?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E9=A1=B9=E4=BC=A0=E9=80=92=E7=BB=99=20regist?= =?UTF-8?q?ry?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 4 ++++ README.md | 3 +-- src/plugins/publish/__init__.py | 9 ++++--- src/plugins/publish/constants.py | 1 + src/plugins/publish/utils.py | 8 ++++++- src/utils/store_test/__main__.py | 5 ++-- src/utils/store_test/constants.py | 2 -- src/utils/store_test/models.py | 1 + src/utils/store_test/store.py | 14 +++++++++-- src/utils/store_test/validation.py | 10 +++----- tests/publish/process/test_pull_request.py | 28 ++++++++++++---------- 11 files changed, 53 insertions(+), 32 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b46ab2ef..d0f0ee5e 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 + +- 将插件配置项传递给 registry + ## [2.6.0] - 2023-06-26 ### Added diff --git a/README.md b/README.md index f58c2573..09a32b16 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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: diff --git a/src/plugins/publish/__init__.py b/src/plugins/publish/__init__.py index a1ac27ff..8bd1f971 100644 --- a/src/plugins/publish/__init__.py +++ b/src/plugins/publish/__init__.py @@ -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 @@ -96,6 +93,12 @@ 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( [ diff --git a/src/plugins/publish/constants.py b/src/plugins/publish/constants.py index 7f41e9f6..3cfc29fd 100644 --- a/src/plugins/publish/constants.py +++ b/src/plugins/publish/constants.py @@ -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) diff --git a/src/plugins/publish/utils.py b/src/plugins/publish/utils.py index 152ce17b..7a15a4a2 100644 --- a/src/plugins/publish/utils.py +++ b/src/plugins/publish/utils.py @@ -17,6 +17,7 @@ ISSUE_FIELD_PATTERN, ISSUE_FIELD_TEMPLATE, NONEFLOW_MARKER, + PLUGIN_CONFIG_PATTERN, PLUGIN_STRING_LIST, POWERED_BY_NONEFLOW_MESSAGE, REUSE_MESSAGE, @@ -326,7 +327,10 @@ 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 触发商店测试更新""" if not pull.merged: @@ -342,10 +346,12 @@ 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} diff --git a/src/utils/store_test/__main__.py b/src/utils/store_test/__main__.py index b7581ccf..7dcb497e 100644 --- a/src/utils/store_test/__main__.py +++ b/src/utils/store_test/__main__.py @@ -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__": diff --git a/src/utils/store_test/constants.py b/src/utils/store_test/constants.py index 12b135e4..a1ee773d 100644 --- a/src/utils/store_test/constants.py +++ b/src/utils/store_test/constants.py @@ -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" diff --git a/src/utils/store_test/models.py b/src/utils/store_test/models.py index 9efea4f6..653e0c73 100644 --- a/src/utils/store_test/models.py +++ b/src/utils/store_test/models.py @@ -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] diff --git a/src/utils/store_test/store.py b/src/utils/store_test/store.py index e8455d28..4330c41f 100644 --- a/src/utils/store_test/store.py +++ b/src/utils/store_test/store.py @@ -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] = {} @@ -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 diff --git a/src/utils/store_test/validation.py b/src/utils/store_test/validation.py index c9085804..1eddf04a 100644 --- a/src/utils/store_test/validation.py +++ b/src/utils/store_test/validation.py @@ -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: @@ -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() @@ -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, diff --git a/tests/publish/process/test_pull_request.py b/tests/publish/process/test_pull_request.py index 01840d99..e9f66935 100644 --- a/tests/publish/process/test_pull_request.py +++ b/tests/publish/process/test_pull_request.py @@ -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 @@ -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}, @@ -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"}, From f0b20ecfdd0b9cb068106f2c1493edcf736abde0 Mon Sep 17 00:00:00 2001 From: uy_sun Date: Mon, 26 Jun 2023 20:53:52 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E8=B0=83=E6=95=B4=E6=B3=A8=E9=87=8A?= =?UTF-8?q?=E5=B9=B6=E6=B7=BB=E5=8A=A0=E6=97=A5=E5=BF=97=E8=BE=93=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/plugins/publish/__init__.py | 1 - src/plugins/publish/utils.py | 6 ++++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/plugins/publish/__init__.py b/src/plugins/publish/__init__.py index 8bd1f971..a2c31213 100644 --- a/src/plugins/publish/__init__.py +++ b/src/plugins/publish/__init__.py @@ -94,7 +94,6 @@ async def handle_pr_close( logger.info(f"议题 #{related_issue_number} 已关闭") # 如果商店更新则触发 registry 更新 - # 因为需要读取最新的插件数据,所以需要在删除分支之前运行 await trigger_registry_update( bot, publish_type, event.payload.pull_request, issue ) diff --git a/src/plugins/publish/utils.py b/src/plugins/publish/utils.py index 7a15a4a2..a71df347 100644 --- a/src/plugins/publish/utils.py +++ b/src/plugins/publish/utils.py @@ -332,8 +332,9 @@ async def trigger_registry_update( pull: "PullRequestClosedPropPullRequest", issue: "Issue", ): - """通过 repository_dispatch 触发商店测试更新""" + """通过 repository_dispatch 触发商店列表更新""" if not pull.merged: + logger.info("拉取请求未合并,跳过触发商店列表更新") return if publish_type == PublishType.PLUGIN: @@ -357,10 +358,11 @@ async def trigger_registry_update( 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("已触发商店列表更新") From 4908c33bb99afbcda58d727f7054f1d0c1853109 Mon Sep 17 00:00:00 2001 From: uy_sun Date: Mon, 26 Jun 2023 21:00:23 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=BC=95=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 09a32b16..aab731dd 100644 --- a/README.md +++ b/README.md @@ -201,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 }} -c ${{ github.event.client_payload.config }} -f + 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: