Skip to content

Commit

Permalink
feat: alice plugin test
Browse files Browse the repository at this point in the history
  • Loading branch information
MarleneJiang committed Aug 22, 2023
1 parent e06e19d commit 449aacc
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 14 deletions.
1 change: 0 additions & 1 deletion .github/actions_scripts/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
39 changes: 29 additions & 10 deletions .github/actions_scripts/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand All @@ -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)})

Expand Down
22 changes: 22 additions & 0 deletions .github/actions_scripts/plugin_test.py
Original file line number Diff line number Diff line change
@@ -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()

30 changes: 29 additions & 1 deletion .github/actions_scripts/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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()
1 change: 1 addition & 0 deletions .github/workflows/validate.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]

Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
requests
importlib
alicebot[all]
alicebot

0 comments on commit 449aacc

Please sign in to comment.