-
Notifications
You must be signed in to change notification settings - Fork 604
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
🔨 提取GitHub相关操作 #1609
🔨 提取GitHub相关操作 #1609
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,10 +10,10 @@ | |
from zhenxun.services.log import logger | ||
from zhenxun.utils.http_utils import AsyncHttpx | ||
from zhenxun.utils.platform import PlatformUtils | ||
from zhenxun.utils.github_utils.models import RepoInfo | ||
from zhenxun.utils.github_utils import parse_github_url | ||
|
||
from .config import ( | ||
DEV_URL, | ||
MAIN_URL, | ||
TMP_PATH, | ||
BASE_PATH, | ||
BACKUP_PATH, | ||
|
@@ -25,6 +25,7 @@ | |
BASE_PATH_STRING, | ||
DOWNLOAD_GZ_FILE, | ||
DOWNLOAD_ZIP_FILE, | ||
DEFAULT_GITHUB_URL, | ||
PYPROJECT_LOCK_FILE, | ||
REQ_TXT_FILE_STRING, | ||
PYPROJECT_FILE_STRING, | ||
|
@@ -169,23 +170,19 @@ async def update(cls, bot: Bot, user_id: str, version_type: str) -> str | None: | |
cur_version = cls.__get_version() | ||
url = None | ||
new_version = None | ||
if version_type == "dev": | ||
url = DEV_URL | ||
new_version = await cls.__get_version_from_branch("dev") | ||
if new_version: | ||
new_version = new_version.split(":")[-1].strip() | ||
elif version_type == "main": | ||
url = MAIN_URL | ||
new_version = await cls.__get_version_from_branch("main") | ||
repo_info = parse_github_url(DEFAULT_GITHUB_URL) | ||
if version_type in {"dev", "main"}: | ||
repo_info.branch = version_type | ||
new_version = await cls.__get_version_from_repo(repo_info) | ||
if new_version: | ||
new_version = new_version.split(":")[-1].strip() | ||
url = await repo_info.get_archive_download_url() | ||
elif version_type == "release": | ||
data = await cls.__get_latest_data() | ||
if not data: | ||
return "获取更新版本失败..." | ||
url = data.get("tarball_url") | ||
new_version = data.get("name") | ||
url = (await AsyncHttpx.get(url)).headers.get("Location") # type: ignore | ||
new_version = data.get("name", "") | ||
url = await repo_info.get_release_source_download_url_tgz(new_version) | ||
if not url: | ||
return "获取版本下载链接失败..." | ||
if TMP_PATH.exists(): | ||
|
@@ -247,7 +244,7 @@ async def __get_latest_data(cls) -> dict: | |
return {} | ||
|
||
@classmethod | ||
async def __get_version_from_branch(cls, branch: str) -> str: | ||
async def __get_version_from_repo(cls, repo_info: RepoInfo) -> str: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 建议: 改进__get_version_from_repo方法中的错误处理 考虑重试请求或以不同方式处理特定类型的异常。这可以使更新过程更加稳健。例如,您可能希望在网络错误时重试,但在身份验证错误时不重试。
Original comment in Englishsuggestion: Improve error handling in __get_version_from_repo method Consider retrying the request or handling specific types of exceptions differently. This could make the update process more robust. For example, you might want to retry on network errors but not on authentication errors.
|
||
"""从指定分支获取版本号 | ||
|
||
参数: | ||
|
@@ -256,11 +253,11 @@ async def __get_version_from_branch(cls, branch: str) -> str: | |
返回: | ||
str: 版本号 | ||
""" | ||
version_url = f"https://raw.githubusercontent.com/HibiKier/zhenxun_bot/{branch}/__version__" | ||
version_url = await repo_info.get_raw_download_url(path="__version__") | ||
try: | ||
res = await AsyncHttpx.get(version_url) | ||
if res.status_code == 200: | ||
return res.text.strip() | ||
except Exception as e: | ||
logger.error(f"获取 {branch} 分支版本失败", e=e) | ||
logger.error(f"获取 {repo_info.branch} 分支版本失败", e=e) | ||
return "未知版本" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
建议(测试): 为与GitHub相关的URL添加新的模拟响应
这些新的模拟响应针对raw.githubusercontent.com、github.com和codeload.github.com,改进了与GitHub相关操作的测试覆盖率。考虑添加断言以验证在测试期间这些端点是否按预期被调用。
Original comment in English
suggestion (testing): New mock responses added for GitHub-related URLs
These new mock responses for raw.githubusercontent.com, github.com, and codeload.github.com improve the test coverage for GitHub-related operations. Consider adding assertions to verify that these endpoints are called as expected during the tests.