Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

fix: 使用 PyPI 提供的标准项目名 #245

Merged
merged 2 commits into from
Oct 31, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/lang/zh-CN/
### Fixed

- 修复商店数据不能及时同步的问题
- 使用 PyPI 提供的标准项目名

## [3.3.3] - 2024-07-18

Expand Down
4 changes: 4 additions & 0 deletions src/providers/validation/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
check_pypi,
check_url,
get_adapters,
get_pypi_name,
get_upload_time,
resolve_adapter_name,
)
Expand Down Expand Up @@ -119,6 +120,9 @@ def project_link_validator(cls, v: str) -> str:

if v and not check_pypi(v):
raise PydanticCustomError("project_link.not_found", "PyPI 项目名不存在")

# 使用 PyPI 提供的标准项目名
v = get_pypi_name(v)
return v

@model_validator(mode="before")
Expand Down
9 changes: 9 additions & 0 deletions src/providers/validation/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ def get_url(url: str) -> httpx.Response:
return httpx.get(url, follow_redirects=True)


def get_pypi_name(project_link: str) -> str:
"""获取 PyPI 项目名"""
url = f"https://pypi.org/pypi/{project_link}/json"
r = get_url(url)
r.raise_for_status()
data = r.json()
return data["info"]["name"]


def get_upload_time(project_link: str) -> str | None:
"""获取插件的上传时间"""
url = f"https://pypi.org/pypi/{project_link}/json"
Expand Down
32 changes: 26 additions & 6 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,15 @@ def mocked_api(respx_mock: MockRouter):
"https://pypi.org/pypi/project_link/json", name="project_link"
).respond(
json={
"info": {"version": "0.0.1"},
"info": {"name": "project_link", "version": "0.0.1"},
"urls": [{"upload_time_iso_8601": "2023-09-01T00:00:00+00:00Z"}],
}
)
respx_mock.get(
"https://pypi.org/pypi/project_link//json", name="project_link/"
).respond(
json={
"info": {"version": "0.0.1"},
"info": {"name": "project_link/", "version": "0.0.1"},
"urls": [{"upload_time_iso_8601": "2023-10-01T00:00:00+00:00Z"}],
}
)
Expand All @@ -136,24 +136,44 @@ def mocked_api(respx_mock: MockRouter):
name="project_link_treehelp",
).respond(
json={
"info": {"version": "0.3.1"},
"info": {"name": "nonebot-plugin-treehelp", "version": "0.3.1"},
"urls": [{"upload_time_iso_8601": "2021-08-01T00:00:00+00:00"}],
}
)
respx_mock.get(
"https://pypi.org/pypi/nonebot-plugin-datastore/json",
name="project_link_datastore",
).respond(json={"info": {"version": "1.0.0"}})
).respond(
json={
"info": {"name": "nonebot-plugin-datastore", "version": "1.0.0"},
}
)
respx_mock.get(
"https://pypi.org/pypi/nonebot-plugin-wordcloud/json",
name="project_link_wordcloud",
).respond(json={"info": {"version": "0.5.0"}})
).respond(
json={"info": {"name": "nonebot-plugin-wordcloud", "version": "0.5.0"}},
)
respx_mock.get(
"https://pypi.org/pypi/project_link1/json", name="project_link1"
).respond(json={"urls": [{"upload_time_iso_8601": "2023-10-01T00:00:00+00:00Z"}]})
).respond(
json={
"info": {"name": "project_link1", "version": "0.5.0"},
"urls": [{"upload_time_iso_8601": "2023-10-01T00:00:00+00:00Z"}],
}
)
respx_mock.get(
"https://pypi.org/pypi/project_link_failed/json", name="project_link_failed"
).respond(404)
respx_mock.get(
"https://pypi.org/pypi/project_link_normalization/json",
name="project_link_normalization",
).respond(
json={
"info": {"name": "project-link-normalization", "version": "0.0.1"},
"urls": [{"upload_time_iso_8601": "2023-10-01T00:00:00+00:00Z"}],
}
)
respx_mock.get("https://www.baidu.com", name="homepage_failed").respond(404)
respx_mock.get("https://nonebot.dev/", name="homepage").respond()
respx_mock.get(STORE_ADAPTERS_URL, name="store_adapters").respond(
Expand Down
30 changes: 30 additions & 0 deletions tests/utils/validation/fields/test_pypi.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,33 @@ async def test_name_duplication_previos_data_missing(mocked_api: MockRouter) ->

assert not mocked_api["project_link1"].called
assert not mocked_api["homepage"].called


async def test_project_link_normalization(mocked_api: MockRouter) -> None:
"""测试 PyPI 项目名的规范化"""
from src.providers.validation import PublishType, validate_info

data = generate_adapter_data(project_link="project_link_normalization")

result = validate_info(PublishType.ADAPTER, data, [])

assert result.valid
assert result.type == PublishType.ADAPTER
assert result.valid_data == snapshot(
{
"module_name": "module_name",
"project_link": "project-link-normalization",
"time": "2023-10-01T00:00:00+00:00Z",
"name": "name",
"desc": "desc",
"author": "author",
"author_id": 1,
"homepage": "https://nonebot.dev",
"tags": [{"label": "test", "color": "#ffffff"}],
}
)
assert result.info
assert result.errors == []

assert mocked_api["homepage"].called
assert mocked_api["project_link_normalization"].called
Loading