Skip to content

Commit

Permalink
fix: 统一数据显示顺序并且时间添加时区 (#330)
Browse files Browse the repository at this point in the history
* fix: 统一数据显示顺序并且时间添加时区

* refactor: 使用统一的 TIME_ZONE 常量

* chore: 删除多余注释

* refactor: 不再需要 copy 数据
  • Loading branch information
he0119 authored Dec 20, 2024
1 parent 98d5fe9 commit 2f2f595
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 34 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

- 统一数据显示顺序并且时间添加时区

## [4.2.0] - 2024-12-18

### Added
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ select = [
"PT", # flake8-pytest-style
"Q", # flake8-quotes
"TC", # flake8-type-checking
"DTZ", # flake8-datetimez
"RUF", # Ruff-specific rules
"I", # isort
]
Expand Down
15 changes: 7 additions & 8 deletions src/plugins/github/plugins/publish/render.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from datetime import datetime
from pathlib import Path
from typing import Any

import jinja2

from src.plugins.github import plugin_config
from src.providers.constants import TIME_ZONE
from src.providers.docker_test import DockerTestResult
from src.providers.utils import dumps_json
from src.providers.validation import ValidationDict
Expand Down Expand Up @@ -45,7 +45,8 @@ def key_to_name(key: str) -> str:
def format_time(time: str) -> str:
"""格式化时间"""
dt = datetime.fromisoformat(time)
return dt.strftime("%Y-%m-%d %H:%M:%S")
dt = dt.astimezone(tz=TIME_ZONE)
return dt.strftime("%Y-%m-%d %H:%M:%S %Z")


env = jinja2.Environment(
Expand All @@ -68,9 +69,6 @@ async def render_comment(result: ValidationDict, reuse: bool = False) -> str:
"""将验证结果转换为评论内容"""
title = f"{result.type}: {result.name}"

# 将 data 字段拷贝一份,避免修改原数据
data: dict[str, Any] = result.valid_data.copy()

# 仅显示必要字段
display_keys = [
"homepage",
Expand All @@ -82,9 +80,10 @@ async def render_comment(result: ValidationDict, reuse: bool = False) -> str:
"version",
]

for key in data.copy():
if key not in display_keys:
data.pop(key)
# 按照 display_keys 顺序展示数据
data = {
key: result.valid_data[key] for key in display_keys if key in result.valid_data
}

if not data.get("tags"):
data.pop("tags", None)
Expand Down
4 changes: 4 additions & 0 deletions src/providers/constants.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import os
from zoneinfo import ZoneInfo

TIME_ZONE = ZoneInfo("Asia/Shanghai")
""" NoneFlow 统一的时区 """

BOT_KEY_TEMPLATE = "{name}:{homepage}"
""" 机器人键名模板 """
Expand Down
7 changes: 2 additions & 5 deletions src/providers/models.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
# ruff: noqa: UP040
from datetime import datetime
from typing import Any, Literal, Self, TypeAlias
from zoneinfo import ZoneInfo

from pydantic import BaseModel, Field, field_serializer, field_validator
from pydantic_extra_types.color import Color

from src.providers.constants import BOT_KEY_TEMPLATE, PYPI_KEY_TEMPLATE
from src.providers.constants import BOT_KEY_TEMPLATE, PYPI_KEY_TEMPLATE, TIME_ZONE
from src.providers.docker_test import Metadata
from src.providers.utils import get_author_name, get_pypi_upload_time, get_pypi_version
from src.providers.validation import validate_info
Expand Down Expand Up @@ -412,9 +411,7 @@ def update(self, store: StorePlugin) -> "RegistryPlugin":


class StoreTestResult(BaseModel):
time: str = Field(
default_factory=lambda: datetime.now(ZoneInfo("Asia/Shanghai")).isoformat()
)
time: str = Field(default_factory=lambda: datetime.now(TIME_ZONE).isoformat())
"""测试时间"""
config: str = ""
"""测试插件的配置"""
Expand Down
3 changes: 2 additions & 1 deletion src/providers/store_test/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
STORE_BOTS_URL,
STORE_DRIVERS_URL,
STORE_PLUGINS_URL,
TIME_ZONE,
)
from src.providers.logger import logger
from src.providers.models import (
Expand Down Expand Up @@ -379,7 +380,7 @@ def generate_github_summary(self, results: dict[str, StoreTestResult]):
]
summary = f"""# 📃 商店测试结果
> 📅 {datetime.now().strftime("%Y-%m-%d %H:%M:%S")}
> 📅 {datetime.now(TIME_ZONE).strftime("%Y-%m-%d %H:%M:%S %Z")}
> ♻️ 共测试 {len(results)} 个插件
> ✅ 更新成功:{len(valid_plugins)}
> ❌ 更新失败:{len(invalid_plugins)}
Expand Down
6 changes: 3 additions & 3 deletions tests/plugins/github/config/process/test_config_check.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import os
from datetime import datetime
from pathlib import Path
from zoneinfo import ZoneInfo

from inline_snapshot import snapshot
from nonebot.adapters.github import IssuesOpened
Expand Down Expand Up @@ -32,14 +31,15 @@ async def test_process_config_check(
mock_results: dict[str, Path],
) -> None:
"""测试发布检查不通过"""
from src.providers.constants import TIME_ZONE
from src.providers.docker_test import Metadata

# 更改当前工作目录为临时目录
os.chdir(tmp_path)

mock_datetime = mocker.patch("src.providers.models.datetime")
mock_datetime.now.return_value = datetime(
2023, 8, 23, 9, 22, 14, 836035, tzinfo=ZoneInfo("Asia/Shanghai")
2023, 8, 23, 9, 22, 14, 836035, tzinfo=TIME_ZONE
)

mock_subprocess_run = mocker.patch(
Expand Down Expand Up @@ -148,7 +148,7 @@ async def test_process_config_check(
<details>
<summary>详情</summary>
<pre><code><li>✅ 项目 <a href="https://pypi.org/project/nonebot-plugin-treehelp/">nonebot-plugin-treehelp</a> 已发布至 PyPI。</li><li>✅ 插件发布时间:2024-07-13 04:41:40。</li><li>✅ 插件版本号: 1.0.0。</li><li>✅ 项目 <a href="https://nonebot.dev">主页</a> 返回状态码 200。</li><li>✅ 标签: test-#ffffff。</li><li>✅ 插件类型: application。</li><li>✅ 插件支持的适配器: nonebot.adapters.onebot.v11。</li><li>✅ 插件 <a href="https://github.com/owner/repo/actions/runs/123456">加载测试</a> 通过。</li></code></pre>
<pre><code><li>✅ 项目 <a href="https://nonebot.dev">主页</a> 返回状态码 200。</li><li>✅ 标签: test-#ffffff。</li><li>✅ 项目 <a href="https://pypi.org/project/nonebot-plugin-treehelp/">nonebot-plugin-treehelp</a> 已发布至 PyPI。</li><li>✅ 插件类型: application。</li><li>✅ 插件支持的适配器: nonebot.adapters.onebot.v11。</li><li>✅ 插件发布时间:2024-07-13 12:41:40 CST。</li><li>✅ 插件版本号: 1.0.0。</li><li>✅ 插件 <a href="https://github.com/owner/repo/actions/runs/123456">加载测试</a> 通过。</li></code></pre>
</details>
---
Expand Down
4 changes: 2 additions & 2 deletions tests/plugins/github/config/utils/test_config_update_file.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import os
from datetime import datetime
from pathlib import Path
from zoneinfo import ZoneInfo

from inline_snapshot import snapshot
from nonebug import App
Expand All @@ -17,6 +16,7 @@ async def test_update_file(
mock_results: dict[str, Path],
) -> None:
from src.plugins.github.plugins.config.utils import update_file
from src.providers.constants import TIME_ZONE
from src.providers.validation.models import (
PluginPublishInfo,
PublishType,
Expand All @@ -28,7 +28,7 @@ async def test_update_file(

mock_datetime = mocker.patch("src.providers.models.datetime")
mock_datetime.now.return_value = datetime(
2023, 8, 23, 9, 22, 14, 836035, tzinfo=ZoneInfo("Asia/Shanghai")
2023, 8, 23, 9, 22, 14, 836035, tzinfo=TIME_ZONE
)

raw_data = {
Expand Down
8 changes: 4 additions & 4 deletions tests/plugins/github/publish/process/test_publish_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ async def test_adapter_process_publish_check(
<details>
<summary>详情</summary>
<pre><code><li>✅ 项目 <a href="https://pypi.org/project/project_link/">project_link</a> 已发布至 PyPI。</li><li>✅ 插件发布时间:2023-09-01 00:00:00。</li><li>✅ 插件版本号: 0.0.1。</li><li>✅ 项目 <a href="https://nonebot.dev">主页</a> 返回状态码 200。</li><li>✅ 标签: test-#ffffff。</li></code></pre>
<pre><code><li>✅ 项目 <a href="https://nonebot.dev">主页</a> 返回状态码 200。</li><li>✅ 标签: test-#ffffff。</li><li>✅ 项目 <a href="https://pypi.org/project/project_link/">project_link</a> 已发布至 PyPI。</li><li>✅ 插件发布时间:2023-09-01 08:00:00 CST。</li><li>✅ 插件版本号: 0.0.1。</li></code></pre>
</details>
---
Expand Down Expand Up @@ -568,7 +568,7 @@ async def test_plugin_process_publish_check(
<details>
<summary>详情</summary>
<pre><code><li>✅ 项目 <a href="https://pypi.org/project/project_link/">project_link</a> 已发布至 PyPI。</li><li>✅ 插件发布时间:2023-09-01 00:00:00。</li><li>✅ 插件版本号: 1.0.0。</li><li>✅ 项目 <a href="https://nonebot.dev">主页</a> 返回状态码 200。</li><li>✅ 标签: test-#ffffff。</li><li>✅ 插件类型: application。</li><li>✅ 插件支持的适配器: 所有。</li><li>✅ 插件 <a href="https://github.com/owner/repo/actions/runs/123456">加载测试</a> 通过。</li></code></pre>
<pre><code><li>✅ 项目 <a href="https://nonebot.dev">主页</a> 返回状态码 200。</li><li>✅ 标签: test-#ffffff。</li><li>✅ 项目 <a href="https://pypi.org/project/project_link/">project_link</a> 已发布至 PyPI。</li><li>✅ 插件类型: application。</li><li>✅ 插件支持的适配器: 所有。</li><li>✅ 插件发布时间:2023-09-01 08:00:00 CST。</li><li>✅ 插件版本号: 1.0.0。</li><li>✅ 插件 <a href="https://github.com/owner/repo/actions/runs/123456">加载测试</a> 通过。</li></code></pre>
</details>
---
Expand Down Expand Up @@ -857,7 +857,7 @@ async def test_plugin_process_publish_check_re_run(
<details>
<summary>详情</summary>
<pre><code><li>✅ 项目 <a href="https://pypi.org/project/project_link/">project_link</a> 已发布至 PyPI。</li><li>✅ 插件发布时间:2023-09-01 00:00:00。</li><li>✅ 插件版本号: 1.0.0。</li><li>✅ 项目 <a href="https://nonebot.dev">主页</a> 返回状态码 200。</li><li>✅ 标签: test-#ffffff。</li><li>✅ 插件类型: application。</li><li>✅ 插件支持的适配器: 所有。</li><li>✅ 插件 <a href="https://github.com/owner/repo/actions/runs/123456">加载测试</a> 通过。</li></code></pre>
<pre><code><li>✅ 项目 <a href="https://nonebot.dev">主页</a> 返回状态码 200。</li><li>✅ 标签: test-#ffffff。</li><li>✅ 项目 <a href="https://pypi.org/project/project_link/">project_link</a> 已发布至 PyPI。</li><li>✅ 插件类型: application。</li><li>✅ 插件支持的适配器: 所有。</li><li>✅ 插件发布时间:2023-09-01 08:00:00 CST。</li><li>✅ 插件版本号: 1.0.0。</li><li>✅ 插件 <a href="https://github.com/owner/repo/actions/runs/123456">加载测试</a> 通过。</li></code></pre>
</details>
---
Expand Down Expand Up @@ -1754,7 +1754,7 @@ async def test_skip_plugin_check(
<details>
<summary>详情</summary>
<pre><code><li>✅ 项目 <a href="https://pypi.org/project/project_link/">project_link</a> 已发布至 PyPI。</li><li>✅ 插件发布时间:2023-09-01 00:00:00。</li><li>✅ 插件版本号: 0.0.1。</li><li>✅ 标签: test-#ffffff。</li><li>✅ 插件 <a href="https://github.com/owner/repo/actions/runs/123456">加载测试</a> 已跳过。</li></code></pre>
<pre><code><li>✅ 标签: test-#ffffff。</li><li>✅ 项目 <a href="https://pypi.org/project/project_link/">project_link</a> 已发布至 PyPI。</li><li>✅ 插件发布时间:2023-09-01 08:00:00 CST。</li><li>✅ 插件版本号: 0.0.1。</li><li>✅ 插件 <a href="https://github.com/owner/repo/actions/runs/123456">加载测试</a> 已跳过。</li></code></pre>
</details>
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ async def test_render_data_adapter(app: App):
<details>
<summary>详情</summary>
<pre><code><li>✅ 项目 <a href="https://pypi.org/project/nonebot-adapter-villa/">nonebot-adapter-villa</a> 已发布至 PyPI。</li><li>✅ 项目 <a href="https://github.com/CMHopeSunshine/nonebot-adapter-villa">主页</a> 返回状态码 200。</li><li>✅ 标签: 米哈游-#e10909。</li></code></pre>
<pre><code><li>✅ 项目 <a href="https://github.com/CMHopeSunshine/nonebot-adapter-villa">主页</a> 返回状态码 200。</li><li>✅ 标签: 米哈游-#e10909。</li><li>✅ 项目 <a href="https://pypi.org/project/nonebot-adapter-villa/">nonebot-adapter-villa</a> 已发布至 PyPI。</li></code></pre>
</details>
---
Expand Down Expand Up @@ -168,6 +168,8 @@ async def test_render_data_plugin(app: App, mocker: MockFixture):
"supported_adapters": None,
"load": True,
"skip_test": False,
"time": "2024-07-13T04:41:40.905441Z",
"version": "0.5.0",
}
result = ValidationDict(
type=PublishType.PLUGIN,
Expand All @@ -189,7 +191,7 @@ async def test_render_data_plugin(app: App, mocker: MockFixture):
<details>
<summary>详情</summary>
<pre><code><li>✅ 项目 <a href="https://pypi.org/project/nonebot-plugin-treehelp/">nonebot-plugin-treehelp</a> 已发布至 PyPI。</li><li>✅ 项目 <a href="https://github.com/he0119/nonebot-plugin-treehelp">主页</a> 返回状态码 200。</li><li>✅ 插件类型: application。</li><li>✅ 插件支持的适配器: 所有。</li><li>✅ 插件 <a href="https://github.com/owner/repo/actions/runs/123456">加载测试</a> 通过。</li></code></pre>
<pre><code><li>✅ 项目 <a href="https://github.com/he0119/nonebot-plugin-treehelp">主页</a> 返回状态码 200。</li><li>✅ 项目 <a href="https://pypi.org/project/nonebot-plugin-treehelp/">nonebot-plugin-treehelp</a> 已发布至 PyPI。</li><li>✅ 插件类型: application。</li><li>✅ 插件支持的适配器: 所有。</li><li>✅ 插件发布时间:2024-07-13 12:41:40 CST。</li><li>✅ 插件版本号: 0.5.0。</li><li>✅ 插件 <a href="https://github.com/owner/repo/actions/runs/123456">加载测试</a> 通过。</li></code></pre>
</details>
---
Expand Down
6 changes: 3 additions & 3 deletions tests/providers/store_test/test_step_summary.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from datetime import datetime
from pathlib import Path
from zoneinfo import ZoneInfo

from inline_snapshot import snapshot
from pytest_mock import MockerFixture
Expand All @@ -11,12 +10,13 @@ async def test_step_summary(
mocked_store_data: dict[str, Path], mocked_api: MockRouter, mocker: MockerFixture
) -> None:
"""验证插件信息"""
from src.providers.constants import TIME_ZONE
from src.providers.models import StoreTestResult
from src.providers.store_test.store import StoreTest

mock_datetime = mocker.patch("src.providers.store_test.store.datetime")
mock_datetime.now.return_value = datetime(
2023, 8, 23, 9, 22, 14, 836035, tzinfo=ZoneInfo("Asia/Shanghai")
2023, 8, 23, 9, 22, 14, 836035, tzinfo=TIME_ZONE
)

store_test = {
Expand Down Expand Up @@ -66,7 +66,7 @@ async def test_step_summary(
"""\
# 📃 商店测试结果
> 📅 2023-08-23 09:22:14
> 📅 2023-08-23 09:22:14 CST
> ♻️ 共测试 2 个插件
> ✅ 更新成功:1 个
> ❌ 更新失败:1 个
Expand Down
16 changes: 10 additions & 6 deletions tests/providers/store_test/test_validate_plugin.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import json
from datetime import datetime
from pathlib import Path
from zoneinfo import ZoneInfo

from inline_snapshot import snapshot
from pytest_mock import MockerFixture
Expand All @@ -26,12 +25,13 @@ def mock_docker_result(path: Path, mocker: MockerFixture):

async def test_validate_plugin(mocked_api: MockRouter, mocker: MockerFixture) -> None:
"""验证插件信息"""
from src.providers.constants import TIME_ZONE
from src.providers.models import RegistryPlugin, StorePlugin, StoreTestResult
from src.providers.store_test.validation import validate_plugin

mock_datetime = mocker.patch("src.providers.models.datetime")
mock_datetime.now.return_value = datetime(
2023, 8, 23, 9, 22, 14, 836035, tzinfo=ZoneInfo("Asia/Shanghai")
2023, 8, 23, 9, 22, 14, 836035, tzinfo=TIME_ZONE
)

output_path = Path(__file__).parent / "output.json"
Expand Down Expand Up @@ -103,12 +103,13 @@ async def test_validate_plugin_with_previous(
需要能够正常更新 author_id, tags 和 is_official 等信息
"""
from src.providers.constants import TIME_ZONE
from src.providers.models import Color, RegistryPlugin, StoreTestResult, Tag
from src.providers.store_test.validation import StorePlugin, validate_plugin

mock_datetime = mocker.patch("src.providers.models.datetime")
mock_datetime.now.return_value = datetime(
2023, 8, 23, 9, 22, 14, 836035, tzinfo=ZoneInfo("Asia/Shanghai")
2023, 8, 23, 9, 22, 14, 836035, tzinfo=TIME_ZONE
)

output_path = Path(__file__).parent / "output.json"
Expand Down Expand Up @@ -200,12 +201,13 @@ async def test_validate_plugin_skip_test(
如果插件之前是跳过测试的,如果插件测试成功,应将 skip_test 设置为 False。
"""
from src.providers.constants import TIME_ZONE
from src.providers.models import RegistryPlugin, StoreTestResult
from src.providers.store_test.validation import StorePlugin, validate_plugin

mock_datetime = mocker.patch("src.providers.models.datetime")
mock_datetime.now.return_value = datetime(
2023, 8, 23, 9, 22, 14, 836035, tzinfo=ZoneInfo("Asia/Shanghai")
2023, 8, 23, 9, 22, 14, 836035, tzinfo=TIME_ZONE
)

output_path = Path(__file__).parent / "output.json"
Expand Down Expand Up @@ -277,12 +279,13 @@ async def test_validate_plugin_skip_test_plugin_test_failed(
如果插件之前是跳过测试的,如果插件测试失败,应不改变 skip_test 的值。
"""
from src.providers.constants import TIME_ZONE
from src.providers.models import RegistryPlugin, StoreTestResult
from src.providers.store_test.validation import StorePlugin, validate_plugin

mock_datetime = mocker.patch("src.providers.models.datetime")
mock_datetime.now.return_value = datetime(
2023, 8, 23, 9, 22, 14, 836035, tzinfo=ZoneInfo("Asia/Shanghai")
2023, 8, 23, 9, 22, 14, 836035, tzinfo=TIME_ZONE
)

output_path = Path(__file__).parent / "output_failed.json"
Expand Down Expand Up @@ -362,12 +365,13 @@ async def test_validate_plugin_failed_with_previous(
需要能够正常更新 author_id, tags 和 is_official 等信息
"""
from src.providers.constants import TIME_ZONE
from src.providers.models import RegistryPlugin, StoreTestResult
from src.providers.store_test.validation import StorePlugin, validate_plugin

mock_datetime = mocker.patch("src.providers.models.datetime")
mock_datetime.now.return_value = datetime(
2023, 8, 23, 9, 22, 14, 836035, tzinfo=ZoneInfo("Asia/Shanghai")
2023, 8, 23, 9, 22, 14, 836035, tzinfo=TIME_ZONE
)

output_path = Path(__file__).parent / "output_failed.json"
Expand Down

0 comments on commit 2f2f595

Please sign in to comment.