Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

🎈 perf: 使用commit号下载插件 #1641

Merged
merged 2 commits into from
Sep 21, 2024
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
2 changes: 1 addition & 1 deletion __version__
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__: v0.2.2-e6f17c4
__version__: v0.2.2-f9c7360
64 changes: 63 additions & 1 deletion tests/builtin_plugins/plugin_store/test_add_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,68 @@ async def test_add_plugin_basic(
assert (mock_base_path / "plugins" / "search_image" / "__init__.py").is_file()


@pytest.mark.parametrize("package_api", ["jsd", "gh"])
async def test_add_plugin_basic_commit_version(
package_api: str,
app: App,
mocker: MockerFixture,
mocked_api: MockRouter,
create_bot: Callable,
tmp_path: Path,
) -> None:
"""
测试添加基础插件
"""
from zhenxun.builtin_plugins.plugin_store import _matcher

init_mocked_api(mocked_api=mocked_api)
mock_base_path = mocker.patch(
"zhenxun.builtin_plugins.plugin_store.data_source.BASE_PATH",
new=tmp_path / "zhenxun",
)

if package_api != "jsd":
mocked_api["zhenxun_bot_plugins_metadata_commit"].respond(404)
if package_api != "gh":
mocked_api["zhenxun_bot_plugins_tree_commit"].respond(404)

plugin_id = 3

async with app.test_matcher(_matcher) as ctx:
bot = create_bot(ctx)
bot: Bot = cast(Bot, bot)
raw_message = f"添加插件 {plugin_id}"
event: GroupMessageEvent = _v11_group_message_event(
message=raw_message,
self_id=BotId.QQ_BOT,
user_id=UserId.SUPERUSER,
group_id=GroupId.GROUP_ID_LEVEL_5,
message_id=MessageId.MESSAGE_ID,
to_me=True,
)
ctx.receive_event(bot=bot, event=event)
ctx.should_call_send(
event=event,
message=Message(message=f"正在添加插件 Id: {plugin_id}"),
result=None,
bot=bot,
)
ctx.should_call_send(
event=event,
message=Message(message="插件 B站订阅 安装成功! 重启后生效"),
result=None,
bot=bot,
)
assert mocked_api["basic_plugins"].called
assert mocked_api["extra_plugins"].called
if package_api == "jsd":
assert mocked_api["zhenxun_bot_plugins_metadata_commit"].called
if package_api == "gh":
assert mocked_api["zhenxun_bot_plugins_tree_commit"].called
assert mocked_api["bilibili_sub_plugin_file_init"].called
assert (mock_base_path / "plugins" / "bilibili_sub" / "__init__.py").is_file()


@pytest.mark.parametrize("package_api", ["jsd", "gh"])
async def test_add_plugin_basic_is_not_dir(
package_api: str,
Expand Down Expand Up @@ -156,7 +218,7 @@ async def test_add_plugin_extra(
if package_api != "gh":
mocked_api["zhenxun_github_sub_tree"].respond(404)

plugin_id = 3
plugin_id = 4

async with app.test_matcher(_matcher) as ctx:
bot = create_bot(ctx)
Expand Down
11 changes: 10 additions & 1 deletion tests/builtin_plugins/plugin_store/test_plugin_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ async def test_plugin_store(
[
"",
3,
"B站订阅",
"非常便利的B站订阅通知",
"HibiKier",
"0.3-b101fbc",
"普通插件",
],
[
"",
4,
"github订阅",
"订阅github用户或仓库",
"xuanerwa",
Expand All @@ -72,7 +81,7 @@ async def test_plugin_store(
],
[
"",
4,
5,
"Minecraft查服",
"Minecraft服务器状态查询,支持IPv6",
"molanp",
Expand Down
4 changes: 2 additions & 2 deletions tests/builtin_plugins/plugin_store/test_search_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ async def test_search_plugin_name(
[
[
"",
3,
4,
"github订阅",
"订阅github用户或仓库",
"xuanerwa",
Expand Down Expand Up @@ -129,7 +129,7 @@ async def test_search_plugin_author(
[
[
"",
3,
4,
"github订阅",
"订阅github用户或仓库",
"xuanerwa",
Expand Down
14 changes: 14 additions & 0 deletions tests/builtin_plugins/plugin_store/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def get_content_bytes(file: str) -> bytes:


def init_mocked_api(mocked_api: MockRouter) -> None:
# metadata
mocked_api.get(
"https://data.jsdelivr.com/v1/packages/gh/zhenxun-org/zhenxun_bot_plugins@main",
name="zhenxun_bot_plugins_metadata",
Expand All @@ -25,7 +26,12 @@ def init_mocked_api(mocked_api: MockRouter) -> None:
"https://data.jsdelivr.com/v1/packages/gh/xuanerwa/zhenxun_github_sub@main",
name="zhenxun_github_sub_metadata",
).respond(json=get_response_json("zhenxun_github_sub_metadata.json"))
mocked_api.get(
"https://data.jsdelivr.com/v1/packages/gh/zhenxun-org/zhenxun_bot_plugins@b101fbc",
name="zhenxun_bot_plugins_metadata_commit",
).respond(json=get_response_json("zhenxun_bot_plugins_metadata.json"))

# tree
mocked_api.get(
"https://api.github.com/repos/zhenxun-org/zhenxun_bot_plugins/git/trees/main?recursive=1",
name="zhenxun_bot_plugins_tree",
Expand All @@ -34,6 +40,10 @@ def init_mocked_api(mocked_api: MockRouter) -> None:
"https://api.github.com/repos/xuanerwa/zhenxun_github_sub/git/trees/main?recursive=1",
name="zhenxun_github_sub_tree",
).respond(json=get_response_json("zhenxun_github_sub_tree.json"))
mocked_api.get(
"https://api.github.com/repos/zhenxun-org/zhenxun_bot_plugins/git/trees/b101fbc?recursive=1",
name="zhenxun_bot_plugins_tree_commit",
).respond(json=get_response_json("zhenxun_bot_plugins_tree.json"))

mocked_api.head(
"https://raw.githubusercontent.com/",
Expand Down Expand Up @@ -70,3 +80,7 @@ def init_mocked_api(mocked_api: MockRouter) -> None:
"https://raw.githubusercontent.com/xuanerwa/zhenxun_github_sub/main/github_sub/__init__.py",
name="github_sub_plugin_file_init",
).respond(content=get_content_bytes("github_sub.py"))
mocked_api.get(
"https://raw.githubusercontent.com/zhenxun-org/zhenxun_bot_plugins/b101fbc/plugins/bilibili_sub/__init__.py",
name="bilibili_sub_plugin_file_init",
).respond(content=get_content_bytes("bilibili_sub.py"))
37 changes: 37 additions & 0 deletions tests/content/plugin_store/bilibili_sub.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from nonebot.plugin import PluginMetadata

from zhenxun.configs.utils import PluginExtraData

__plugin_meta__ = PluginMetadata(
name="B站订阅",
description="非常便利的B站订阅通知",
usage="""
usage:
B站直播,番剧,UP动态开播等提醒
主播订阅相当于 直播间订阅 + UP订阅
指令:
添加订阅 ['主播'/'UP'/'番剧'] [id/链接/番名]
删除订阅 ['主播'/'UP'/'id'] [id]
查看订阅
示例:
添加订阅主播 2345344 <-(直播房间id)
添加订阅UP 2355543 <-(个人主页id)
添加订阅番剧 史莱姆 <-(支持模糊搜索)
添加订阅番剧 125344 <-(番剧id)
删除订阅id 2324344 <-(任意id,通过查看订阅获取)
""".strip(),
extra=PluginExtraData(
author="HibiKier",
version="0.3-b101fbc",
superuser_help="""
登录b站获取cookie防止风控:
bil_check/检测b站
bil_login/登录b站
bil_logout/退出b站 uid
示例:
登录b站
检测b站
bil_logout 12345<-(退出登录的b站uid,通过检测b站获取)
""",
).dict(),
)
10 changes: 10 additions & 0 deletions tests/response/plugin_store/basic_plugins.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,15 @@
"version": "0.1",
"plugin_type": "NORMAL",
"is_dir": false
},
"B站订阅": {
"module": "bilibili_sub",
"module_path": "plugins.bilibili_sub",
"description": "非常便利的B站订阅通知",
"usage": "B站直播,番剧,UP动态开播等提醒",
"author": "HibiKier",
"version": "0.3-b101fbc",
"plugin_type": "NORMAL",
"is_dir": true
}
}
12 changes: 12 additions & 0 deletions tests/response/plugin_store/zhenxun_bot_plugins_metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,18 @@
"size": 1530
}
]
},
{
"type": "directory",
"name": "bilibili_sub",
"files": [
{
"type": "file",
"name": "__init__.py",
"hash": "407DCgNFcZnuEK+d716j8EWrFQc4Nlxa35V3yemy3WQ=",
"size": 14293
}
]
}
]
}
Expand Down
48 changes: 0 additions & 48 deletions tests/response/plugin_store/zhenxun_bot_plugins_tree.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,22 +134,6 @@
"sha": "236b6a8ce846884fb7dbb10aab58f95782844c27",
"url": "https://api.github.com/repos/zhenxun-org/zhenxun_bot_plugins/git/trees/236b6a8ce846884fb7dbb10aab58f95782844c27"
},
{
"path": "plugins/bilibili_sub/README.md",
"mode": "100644",
"type": "blob",
"sha": "b05b281b72dc1276e176411f7167f5c7c52d90bb",
"size": 853,
"url": "https://api.github.com/repos/zhenxun-org/zhenxun_bot_plugins/git/blobs/b05b281b72dc1276e176411f7167f5c7c52d90bb"
},
{
"path": "plugins/bilibili_sub/Wbi.py",
"mode": "100644",
"type": "blob",
"sha": "7ee68e789125b110a5796eab05f1a3cf0b32e481",
"size": 2503,
"url": "https://api.github.com/repos/zhenxun-org/zhenxun_bot_plugins/git/blobs/7ee68e789125b110a5796eab05f1a3cf0b32e481"
},
{
"path": "plugins/bilibili_sub/__init__.py",
"mode": "100644",
Expand All @@ -158,38 +142,6 @@
"size": 14302,
"url": "https://api.github.com/repos/zhenxun-org/zhenxun_bot_plugins/git/blobs/2eec6bd0c8208e4026b0fe1400838c161ac826c4"
},
{
"path": "plugins/bilibili_sub/auth.py",
"mode": "100644",
"type": "blob",
"sha": "0bf48390942a71070d8a176414e8131820ad60b4",
"size": 2490,
"url": "https://api.github.com/repos/zhenxun-org/zhenxun_bot_plugins/git/blobs/0bf48390942a71070d8a176414e8131820ad60b4"
},
{
"path": "plugins/bilibili_sub/data_source.py",
"mode": "100644",
"type": "blob",
"sha": "2140347d5338f945829a3c7ecf2931d059a2cf33",
"size": 15101,
"url": "https://api.github.com/repos/zhenxun-org/zhenxun_bot_plugins/git/blobs/2140347d5338f945829a3c7ecf2931d059a2cf33"
},
{
"path": "plugins/bilibili_sub/model.py",
"mode": "100644",
"type": "blob",
"sha": "3df5e9a4b180fa014daf5338a5de05f1f42fb514",
"size": 6855,
"url": "https://api.github.com/repos/zhenxun-org/zhenxun_bot_plugins/git/blobs/3df5e9a4b180fa014daf5338a5de05f1f42fb514"
},
{
"path": "plugins/bilibili_sub/utils.py",
"mode": "100644",
"type": "blob",
"sha": "6852ac0b05eb9e716e2eef3d0dcc35dd30992d6f",
"size": 7695,
"url": "https://api.github.com/repos/zhenxun-org/zhenxun_bot_plugins/git/blobs/6852ac0b05eb9e716e2eef3d0dcc35dd30992d6f"
},
{
"path": "plugins/black_word",
"mode": "040000",
Expand Down
4 changes: 4 additions & 0 deletions zhenxun/builtin_plugins/plugin_store/data_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ async def add_plugin(cls, plugin_id: int) -> str:
if plugin_info.github_url is None:
plugin_info.github_url = DEFAULT_GITHUB_URL
is_external = False
version_split = plugin_info.version.split("-")
if len(version_split) > 1:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

建议: 为URL修改过程添加错误处理

考虑添加错误处理,以优雅地管理GitHub URL不符合预期格式或URL修改过程失败的情况。这可能涉及使用try-except块并记录遇到的任何错误。

Suggested change
if len(version_split) > 1:
try:
if len(version_split) > 1:
github_url_split = plugin_info.github_url.split("/tree/")
if len(github_url_split) == 2:
plugin_info.github_url = f"{github_url_split[0]}/tree/{version_split[1]}"
else:
raise ValueError("Unexpected GitHub URL format")
except Exception as e:
logger.error(f"Error modifying GitHub URL: {e}")
Original comment in English

suggestion: Add error handling for URL modification process

Consider adding error handling to gracefully manage cases where the GitHub URL doesn't match the expected format or when the URL modification process fails. This could involve using a try-except block and logging any errors encountered.

Suggested change
if len(version_split) > 1:
try:
if len(version_split) > 1:
github_url_split = plugin_info.github_url.split("/tree/")
if len(github_url_split) == 2:
plugin_info.github_url = f"{github_url_split[0]}/tree/{version_split[1]}"
else:
raise ValueError("Unexpected GitHub URL format")
except Exception as e:
logger.error(f"Error modifying GitHub URL: {e}")

github_url_split = plugin_info.github_url.split("/tree/")
plugin_info.github_url = f"{github_url_split[0]}/tree/{version_split[1]}"
logger.info(f"正在安装插件 {plugin_key}...")
await cls.install_plugin_with_repo(
plugin_info.github_url,
Expand Down