Skip to content

Commit

Permalink
fix: 修复发布后 registry_update 中版本号为默认值的问题 (#289)
Browse files Browse the repository at this point in the history
插件发布流程没有填写 version 字段,现在直接删掉了默认值
  • Loading branch information
he0119 authored Nov 23, 2024
1 parent 5e10f5f commit 41f5ed5
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 51 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/lang/zh-CN/

## [Unreleased]

### Fixed

- 修复发布后 registry_update 中版本号为默认值的问题

## [4.0.10] - 2024-11-21

### Fixed
Expand Down
5 changes: 4 additions & 1 deletion src/plugins/github/plugins/publish/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from src.plugins.github.models.issue import IssueHandler
from src.plugins.github.utils import extract_issue_info_from_issue
from src.providers.docker_test import DockerPluginTest, Metadata
from src.providers.utils import load_json_from_file
from src.providers.utils import get_latest_version, load_json_from_file
from src.providers.validation import PublishType, ValidationDict, validate_info

from .constants import (
Expand Down Expand Up @@ -100,6 +100,8 @@ async def validate_plugin_info_from_issue(
)
raw_data.update(metadata)

# 跳过测试的时候只能从 pypi 获取版本号
raw_data["version"] = get_latest_version(project_link)
raw_data["load"] = False
raw_data["test_output"] = "插件未进行测试"
raw_data["metadata"] = bool(metadata)
Expand All @@ -116,6 +118,7 @@ async def validate_plugin_info_from_issue(
# 从插件测试结果中获得元数据
raw_data.update(metadata)

raw_data["version"] = test_result.version
raw_data["load"] = test_result.load
raw_data["test_output"] = test_output
raw_data["metadata"] = bool(metadata)
Expand Down
5 changes: 1 addition & 4 deletions src/providers/store_test/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
StorePlugin,
StoreTestResult,
)
from src.providers.utils import dump_json, load_json_from_web
from src.providers.utils import dump_json, get_latest_version, load_json_from_web
from src.providers.validation.utils import get_author_name

from .constants import (
Expand All @@ -37,11 +37,8 @@
PLUGINS_PATH,
RESULTS_PATH,
)
from .utils import get_latest_version
from .validation import validate_plugin

print = click.echo


class StoreTest:
"""商店测试"""
Expand Down
35 changes: 1 addition & 34 deletions src/providers/store_test/utils.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,4 @@
from functools import cache
from typing import Any

import httpx

from src.providers.utils import load_json, load_json_from_web


@cache
def get_pypi_data(project_link: str) -> dict[str, Any]:
"""获取 PyPI 数据"""
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36"
}
url = f"https://pypi.org/pypi/{project_link}/json"
try:
r = httpx.get(url, headers=headers)
except Exception as e:
raise ValueError(f"获取 PyPI 数据失败:{e}")
if r.status_code != 200:
raise ValueError(f"获取 PyPI 数据失败:{r.text}")
return load_json(r.text)


def get_latest_version(project_link: str) -> str:
"""获取插件的最新版本号"""
data = get_pypi_data(project_link)
return data["info"]["version"]


def get_upload_time(project_link: str) -> str:
"""获取插件的上传时间"""
data = get_pypi_data(project_link)
return data["urls"][0]["upload_time_iso_8601"]
from src.providers.utils import load_json_from_web


def get_user_id(name: str) -> int:
Expand Down
4 changes: 1 addition & 3 deletions src/providers/store_test/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
ValidationDict,
validate_info,
)
from src.providers.validation.utils import get_author_name

from .utils import get_upload_time
from src.providers.validation.utils import get_author_name, get_upload_time


async def validate_plugin(
Expand Down
29 changes: 29 additions & 0 deletions src/providers/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
from functools import cache
from pathlib import Path
from typing import Any

Expand Down Expand Up @@ -62,3 +63,31 @@ def dump_json5(path: Path, data: Any) -> None:

with open(path, "w", encoding="utf-8") as f:
f.write(content)


@cache
def get_pypi_data(project_link: str) -> dict[str, Any]:
"""获取 PyPI 数据"""
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36"
}
url = f"https://pypi.org/pypi/{project_link}/json"
try:
r = httpx.get(url, headers=headers)
except Exception as e:
raise ValueError(f"获取 PyPI 数据失败:{e}")
if r.status_code != 200:
raise ValueError(f"获取 PyPI 数据失败:{r.text}")
return load_json(r.text)


def get_latest_version(project_link: str) -> str:
"""获取插件的最新版本号"""
data = get_pypi_data(project_link)
return data["info"]["version"]


def get_upload_time(project_link: str) -> str:
"""获取插件的上传时间"""
data = get_pypi_data(project_link)
return data["urls"][0]["upload_time_iso_8601"]
2 changes: 1 addition & 1 deletion src/providers/validation/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ class PluginPublishInfo(PublishInfo, PyPIMixin):
"""插件测试元数据"""
skip_test: bool
"""是否跳过插件测试"""
version: str = "0.0.1"
version: str
"""插件版本号
从 PyPI 获取或者测试中获取
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ async def app(app: App, tmp_path: Path, mocker: MockerFixture):

yield app

from src.providers.store_test.utils import get_pypi_data
from src.providers.utils import get_pypi_data

get_pypi_data.cache_clear()

Expand Down
5 changes: 3 additions & 2 deletions tests/github/publish/process/test_publish_pull_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ async def test_process_pull_request(
supported_adapters=["~onebot.v11"],
)
mock_test_result.load = True
mock_test_result.version = "1.0.0"
mock_docker = mocker.patch("src.providers.docker_test.DockerPluginTest.run")
mock_docker.return_value = mock_test_result

Expand Down Expand Up @@ -100,13 +101,13 @@ async def test_process_pull_request(
"supported_adapters": ["nonebot.adapters.onebot.v11"],
"valid": True,
"time": "2023-09-01T00:00:00+00:00Z",
"version": "0.0.1",
"version": "1.0.0",
"skip_test": False,
},
"result": {
"time": "2023-09-01T00:00:00+00:00Z",
"config": "log_level=DEBUG",
"version": "0.0.1",
"version": "1.0.0",
"test_env": {"python==3.12": True},
"results": {
"validation": True,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ async def test_resolve_conflict_pull_requests_plugin(
type="application",
supported_adapters=["~onebot.v11"],
)
mock_test_result.version = "1.0.0"
mock_docker = mocker.patch("src.providers.docker_test.DockerPluginTest.run")
mock_docker.return_value = mock_test_result

Expand Down
15 changes: 11 additions & 4 deletions tests/github/publish/utils/test_trigger_registry_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ async def test_trigger_registry_update(
supported_adapters=["~onebot.v11"],
)
mock_test_result.load = True
mock_test_result.version = "1.0.0"
mock_docker = mocker.patch("src.providers.docker_test.DockerPluginTest.run")
mock_docker.return_value = mock_test_result

Expand Down Expand Up @@ -77,13 +78,13 @@ async def test_trigger_registry_update(
"supported_adapters": ["nonebot.adapters.onebot.v11"],
"valid": True,
"time": "2023-09-01T00:00:00+00:00Z",
"version": "0.0.1",
"version": "1.0.0",
"skip_test": False,
},
"result": {
"time": "2023-09-01T00:00:00+00:00Z",
"config": "log_level=DEBUG",
"version": "0.0.1",
"version": "1.0.0",
"test_env": {"python==3.12": True},
"results": {
"validation": True,
Expand Down Expand Up @@ -216,7 +217,9 @@ async def test_trigger_registry_update_skip_test(
await trigger_registry_update(handler, PublishType.PLUGIN)


async def test_trigger_registry_update_bot(app: App, mocker: MockerFixture):
async def test_trigger_registry_update_bot(
app: App, mocker: MockerFixture, mocked_api: MockRouter
):
"""机器人发布的情况
已经有相同机器人的时候,registry_update 不会影响到机器人的测试
Expand Down Expand Up @@ -265,9 +268,11 @@ async def test_trigger_registry_update_bot(app: App, mocker: MockerFixture):

await trigger_registry_update(handler, PublishType.BOT)

assert mocked_api["homepage_v2"].called


async def test_trigger_registry_update_plugins_issue_body_info_missing(
app: App, mocker: MockerFixture
app: App, mocker: MockerFixture, mocked_api: MockRouter
):
"""如果议题信息不全,应该不会触发更新"""
from githubkit.rest import Issue
Expand Down Expand Up @@ -322,6 +327,8 @@ async def test_trigger_registry_update_plugins_issue_body_info_missing(

await trigger_registry_update(handler, PublishType.PLUGIN)

assert mocked_api["homepage"].called


async def test_trigger_registry_update_validation_failed(
app: App, mocker: MockerFixture, mocked_api: MockRouter
Expand Down
2 changes: 1 addition & 1 deletion tests/utils/store_test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ async def test_load_json_failed(mocked_api: MockRouter):

async def test_get_pypi_data_failed(mocked_api: MockRouter):
"""获取 PyPI 数据失败"""
from src.providers.store_test.utils import get_pypi_data
from src.providers.utils import get_pypi_data

with pytest.raises(ValueError, match="获取 PyPI 数据失败:"):
get_pypi_data("project_link_failed")

0 comments on commit 41f5ed5

Please sign in to comment.