diff --git a/.github/actions_scripts/file.py b/.github/actions_scripts/file.py index d414a30..d310292 100644 --- a/.github/actions_scripts/file.py +++ b/.github/actions_scripts/file.py @@ -18,7 +18,6 @@ homepage = os.environ["HOMEPAGE"] tags = os.environ["TAGS"].replace("[", "").replace("]", "").replace("'", "").replace('"', "").split(",") - def get_json() -> dict[str, Any]: """获取对应json文件并解析.""" with Path(type_info + "s.json").open(encoding="utf-8") as f: diff --git a/.github/actions_scripts/parse.py b/.github/actions_scripts/parse.py index 7bbc5cb..5722640 100644 --- a/.github/actions_scripts/parse.py +++ b/.github/actions_scripts/parse.py @@ -33,6 +33,21 @@ def check_pypi(name: str) -> bool: return False +def check_module(module_name: str) -> bool: + """Check module name.""" + import importlib + if module_name == "null": + return False + if "-" in module_name: + return False + try: + importlib.invalidate_caches() + module = importlib.import_module(module_name) + importlib.reload(module) + except Exception: # noqa: BLE001 + return False + else: + return True def parse_title(title: str) -> dict[str, Any]: """Prase Title.""" @@ -48,19 +63,23 @@ def main() -> None: """信息解析:1. 标题的type解析,如果不符合就报错 2. 提取name、module_name、pypi_name,如果不符合就报错 3. pypi_name在pip网站中检查,不存在则报错.""" title = os.environ["TITLE"] pypi_name = os.environ["PYPI_NAME"] + module_name = os.environ["MODULE_NAME"] try: - parsed = parse_title(title) + if check_module(module_name) is False: + set_action_outputs({"result": "error", "output": "输入的module_name存在问题"}) + return if check_pypi(pypi_name) is False: set_action_outputs({"result": "error", "output": "输入的pypi_name存在问题"}) - else: - set_action_outputs( - { - "result": "success", - "output": "", - "type": parsed.get("type", ""), - "name": parsed.get("name", ""), - } - ) + return + parsed = parse_title(title) + set_action_outputs( + { + "result": "success", + "output": "", + "type": parsed.get("type", ""), + "name": parsed.get("name", ""), + } + ) except ValueError as e: set_action_outputs({"result": "error", "output": str(e)}) diff --git a/.github/actions_scripts/plugin_test.py b/.github/actions_scripts/plugin_test.py new file mode 100644 index 0000000..bb282ec --- /dev/null +++ b/.github/actions_scripts/plugin_test.py @@ -0,0 +1,22 @@ +""".""" +import sys + +from alicebot.bot import Bot + +PLUGIN_MODULE_NAME = sys.argv[1] + +if PLUGIN_MODULE_NAME == "null": + sys.exit(1) + +bot = Bot(config_file=None) +bot.load_plugins(PLUGIN_MODULE_NAME) + +@bot.bot_run_hook +async def bot_run_hook(bot: Bot) -> None: + """在 Bot 启动后直接退出。.""" + bot.should_exit.set() + + +if __name__ == "__main__": + bot.run() + diff --git a/.github/actions_scripts/test.py b/.github/actions_scripts/test.py index 765a014..a4384dc 100644 --- a/.github/actions_scripts/test.py +++ b/.github/actions_scripts/test.py @@ -2,10 +2,33 @@ from __future__ import annotations import os +import subprocess from utils import set_action_outputs pypi_name = os.environ["PYPI_NAME"] +module_name = os.environ["MODULE_NAME"] + + + +def alicebot_test() -> None: + """验证插件是否能在 alicebot 中正常运行.""" + try: + + # 要执行的 Python 脚本路径 + python_script_path = ".github/actions_scripts/plugin_test.py" + # 整个命令 + command = f"pdm run {python_script_path} {module_name}" + result = subprocess.run(command, timeout=10, capture_output=True, text=True) # noqa: S603 + if result.returncode != 0: + msg = f"脚本执行失败: {result.stdout}" + raise ValueError(msg) from None + except subprocess.TimeoutExpired: + print("Script execution timed out!")# noqa: T201 + raise + except subprocess.CalledProcessError as e: + msg = f"Script execution failed with error code {e.returncode}" + raise ValueError(msg) from e def get_meta_info() -> None: @@ -54,4 +77,9 @@ def get_meta_info() -> None: if __name__ == "__main__": - get_meta_info() + try: + alicebot_test() + except Exception as e: # noqa: BLE001 + set_action_outputs({"result": "error", "output": str(e)}) + else: + get_meta_info() diff --git a/.github/workflows/validate.yaml b/.github/workflows/validate.yaml index c005208..d2b5372 100644 --- a/.github/workflows/validate.yaml +++ b/.github/workflows/validate.yaml @@ -125,6 +125,7 @@ jobs: env: TITLE: ${{ env.ISSUE_TITLE }} PYPI_NAME: ${{ steps.set-output.outputs.pypi_name }} + MODULE_NAME: ${{ steps.set-output.outputs.module_name }} run: python .github/actions_scripts/parse.py parse-failed: diff --git a/pyproject.toml b/pyproject.toml index 6308cc1..b0b3ba1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ version = "0.1.0" requires-python = ">=3.8" dependencies = [ "importlib>=1.0.4", - "alicebot[all]>=0.7.1", + "alicebot>=0.7.1", "requests>=2.31.0", ] diff --git a/requirements.txt b/requirements.txt index b50c0a9..9e6f793 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,3 @@ requests importlib -alicebot[all] \ No newline at end of file +alicebot \ No newline at end of file