Skip to content

Commit b608cb8

Browse files
authored
refactor: 整理常量同时直接获取所需数据 (#202)
* refactor: 整理常量 * fix: 修复 event_id 类型错误 * improve: 直接获取所需数据 * test: 完善测试
1 parent 8fad612 commit b608cb8

21 files changed

+109
-95
lines changed

bot.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ async def handle_github_action_event():
2828
try:
2929
config = driver.config
3030
# 从环境变量中获取事件信息
31-
event_id = config.github_run_id
31+
# 读取到的 gitub_run_id 会因为 nonebot 配置加载机制转成 int,需要转回 str
32+
event_id = str(config.github_run_id)
3233
event_name = config.github_event_name
3334
github_event_path = Path(config.github_event_path)
3435
# 生成事件

examples/store-test.yml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,6 @@ jobs:
4646
run: |
4747
git checkout `git describe --abbrev=0 --tags`
4848
poetry install
49-
mkdir -p plugin_test/store
50-
curl -sSL https://raw.githubusercontent.com/nonebot/registry/results/results.json -o plugin_test/store/previous_results.json
51-
curl -sSL https://raw.githubusercontent.com/nonebot/registry/results/plugins.json -o plugin_test/store/previous_plugins.json
52-
curl -sSL https://raw.githubusercontent.com/nonebot/nonebot2/master/assets/adapters.json -o plugin_test/store/adapters.json
53-
curl -sSL https://raw.githubusercontent.com/nonebot/nonebot2/master/assets/bots.json -o plugin_test/store/bots.json
54-
curl -sSL https://raw.githubusercontent.com/nonebot/nonebot2/master/assets/drivers.json -o plugin_test/store/drivers.json
55-
curl -sSL https://raw.githubusercontent.com/nonebot/nonebot2/master/assets/plugins.json -o plugin_test/store/plugins.json
5649
5750
- name: Test plugin
5851
if: ${{ !contains(fromJSON('["Bot", "Adapter", "Plugin"]'), github.event.client_payload.type) }}

src/plugins/publish/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from src.utils.validation.models import PublishType
1414

1515
from .config import plugin_config
16-
from .constants import BOT_MARKER, BRANCH_NAME_PREFIX, MAX_NAME_LENGTH
16+
from .constants import BOT_MARKER, BRANCH_NAME_PREFIX, TITLE_MAX_LENGTH
1717
from .depends import (
1818
get_installation_id,
1919
get_issue_number,
@@ -190,7 +190,7 @@ async def handle_publish_check(
190190

191191
# 设置拉取请求与议题的标题
192192
# 限制标题长度,过长的标题不好看
193-
title = f"{publish_type.value}: {result['name'][:MAX_NAME_LENGTH]}"
193+
title = f"{publish_type.value}: {result['name'][:TITLE_MAX_LENGTH]}"
194194

195195
# 分支命名示例 publish/issue123
196196
branch_name = f"{BRANCH_NAME_PREFIX}{issue_number}"

src/plugins/publish/constants.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
BRANCH_NAME_PREFIX = "publish/issue"
1313

14-
MAX_NAME_LENGTH = 50
15-
"""名称最大长度"""
14+
TITLE_MAX_LENGTH = 50
15+
"""标题最大长度"""
1616

1717
# 匹配信息的正则表达式
1818
# 格式:### {标题}\n\n{内容}

src/utils/constants.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# NoneBot 插件商店测试结果
2+
# https://github.com/nonebot/registry/tree/results
3+
REGISTRY_BASE_URL = "https://raw.githubusercontent.com/nonebot/registry/results"
4+
REGISTRY_RESULTS_URL = f"{REGISTRY_BASE_URL}/results.json"
5+
REGISTRY_PLUGINS_URL = f"{REGISTRY_BASE_URL}/plugins.json"
6+
7+
# NoneBot 插件商店
8+
# https://github.com/nonebot/nonebot2/tree/master/assets
9+
STORE_BASE_URL = "https://raw.githubusercontent.com/nonebot/nonebot2/master/assets"
10+
STORE_ADAPTERS_URL = f"{STORE_BASE_URL}/adapters.json"
11+
STORE_BOTS_URL = f"{STORE_BASE_URL}/bots.json"
12+
STORE_DRIVERS_URL = f"{STORE_BASE_URL}/drivers.json"
13+
STORE_PLUGINS_URL = f"{STORE_BASE_URL}/plugins.json"
14+
"""plugin_test.py 中也有一个常量,需要同时修改"""

src/utils/store_test/__main__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import asyncio
12
import os
2-
from asyncio import run
33

44
import click
55

@@ -18,7 +18,7 @@ def main(limit: int, offset: int, force: bool, key: str | None):
1818
config = os.environ.get("PLUGIN_CONFIG")
1919
data = os.environ.get("PLUGIN_DATA")
2020

21-
run(test.run(key, config, data))
21+
asyncio.run(test.run(key, config, data))
2222

2323

2424
if __name__ == "__main__":

src/utils/store_test/constants.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,3 @@
1717
""" 生成的驱动器列表保存路径 """
1818
PLUGINS_PATH = TEST_DIR / "plugins.json"
1919
""" 生成的插件列表保存路径 """
20-
21-
STORE_DIR = Path("plugin_test") / "store"
22-
""" 商店信息文件夹 """
23-
STORE_ADAPTERS_PATH = STORE_DIR / "adapters.json"
24-
""" 适配器列表文件路径 """
25-
STORE_BOTS_PATH = STORE_DIR / "bots.json"
26-
""" 机器人列表文件路径 """
27-
STORE_DRIVERS_PATH = STORE_DIR / "drivers.json"
28-
""" 驱动器列表文件路径 """
29-
STORE_PLUGINS_PATH = STORE_DIR / "plugins.json"
30-
""" 插件列表文件路径 """
31-
PREVIOUS_RESULTS_PATH = STORE_DIR / "previous_results.json"
32-
""" 上次测试生成的结果文件路径 """
33-
PREVIOUS_PLUGINS_PATH = STORE_DIR / "previous_plugins.json"
34-
""" 上次测试生成的插件列表文件路径 """

src/utils/store_test/store.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
import click
22

3+
from src.utils.constants import (
4+
REGISTRY_PLUGINS_URL,
5+
REGISTRY_RESULTS_URL,
6+
STORE_ADAPTERS_URL,
7+
STORE_BOTS_URL,
8+
STORE_DRIVERS_URL,
9+
STORE_PLUGINS_URL,
10+
)
11+
312
from .constants import (
413
ADAPTERS_PATH,
514
BOTS_PATH,
615
DRIVERS_PATH,
716
PLUGIN_KEY_TEMPLATE,
817
PLUGINS_PATH,
9-
PREVIOUS_PLUGINS_PATH,
10-
PREVIOUS_RESULTS_PATH,
1118
RESULTS_PATH,
12-
STORE_ADAPTERS_PATH,
13-
STORE_BOTS_PATH,
14-
STORE_DRIVERS_PATH,
15-
STORE_PLUGINS_PATH,
1619
)
1720
from .models import Plugin, StorePlugin, TestResult
1821
from .utils import dump_json, get_latest_version, load_json
@@ -33,24 +36,24 @@ def __init__(
3336
self._force = force
3437

3538
# NoneBot 仓库中的数据
36-
self._store_adapters = load_json(STORE_ADAPTERS_PATH)
37-
self._store_bots = load_json(STORE_BOTS_PATH)
38-
self._store_drivers = load_json(STORE_DRIVERS_PATH)
39+
self._store_adapters = load_json(STORE_ADAPTERS_URL)
40+
self._store_bots = load_json(STORE_BOTS_URL)
41+
self._store_drivers = load_json(STORE_DRIVERS_URL)
3942
self._store_plugins: dict[str, StorePlugin] = {
4043
PLUGIN_KEY_TEMPLATE.format(
4144
project_link=plugin["project_link"],
4245
module_name=plugin["module_name"],
4346
): plugin
44-
for plugin in load_json(STORE_PLUGINS_PATH)
47+
for plugin in load_json(STORE_PLUGINS_URL)
4548
}
4649
# 上次测试的结果
47-
self._previous_results: dict[str, TestResult] = load_json(PREVIOUS_RESULTS_PATH)
50+
self._previous_results: dict[str, TestResult] = load_json(REGISTRY_RESULTS_URL)
4851
self._previous_plugins: dict[str, Plugin] = {
4952
PLUGIN_KEY_TEMPLATE.format(
5053
project_link=plugin["project_link"],
5154
module_name=plugin["module_name"],
5255
): plugin
53-
for plugin in load_json(PREVIOUS_PLUGINS_PATH)
56+
for plugin in load_json(REGISTRY_PLUGINS_URL)
5457
}
5558

5659
def should_skip(self, key: str) -> bool:
@@ -161,7 +164,6 @@ async def run(
161164
self, key: str | None = None, config: str | None = None, data: str | None = None
162165
):
163166
"""测试商店内插件情况"""
164-
165167
results, plugins = await self.test_plugins(key, config, data)
166168

167169
# 保存测试结果与生成的列表

src/utils/store_test/utils.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@
66
import httpx
77

88

9-
def load_json(path: Path) -> dict:
10-
"""加载 JSON 文件"""
11-
if not path.exists():
12-
raise Exception(f"文件 {path} 不存在")
13-
14-
with open(path, encoding="utf8") as f:
15-
return json.load(f)
9+
def load_json(url: str):
10+
"""从网络加载 JSON 文件"""
11+
r = httpx.get(url)
12+
if r.status_code != 200:
13+
raise ValueError(f"下载文件失败:{r.text}")
14+
return r.json()
1615

1716

1817
def dump_json(path: Path, data: dict | list):
@@ -32,9 +31,9 @@ def get_pypi_data(project_link: str) -> dict[str, Any]:
3231
}
3332
url = f"https://pypi.org/pypi/{project_link}/json"
3433
r = httpx.get(url, headers=headers)
35-
if r.status_code == 200:
36-
return r.json()
37-
raise ValueError(f"获取 PyPI 数据失败:{r.text}")
34+
if r.status_code != 200:
35+
raise ValueError(f"获取 PyPI 数据失败:{r.text}")
36+
return r.json()
3837

3938

4039
def get_latest_version(project_link: str) -> str:

src/utils/validation/constants.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,12 @@
1111
re.IGNORECASE,
1212
)
1313

14-
MAX_NAME_LENGTH = 50
14+
NAME_MAX_LENGTH = 50
1515
"""名称最大长度"""
1616

1717
PLUGIN_VALID_TYPE = ["application", "library"]
1818
"""插件类型当前只支持 application 和 library"""
1919

20-
# NoneBot Store
21-
STORE_ADAPTERS_URL = (
22-
"https://raw.githubusercontent.com/nonebot/nonebot2/master/assets/adapters.json"
23-
)
24-
2520
# Pydantic 错误信息翻译
2621
MESSAGE_TRANSLATIONS = {
2722
"model_type": "值不是合法的字典",

0 commit comments

Comments
 (0)