From c495c5d9b569e7fd9c99e25280215a57f37b601b Mon Sep 17 00:00:00 2001 From: webjoin111 <455457521@qq.com> Date: Sun, 7 Sep 2025 23:58:08 +0800 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20refactor(repo):=20?= =?UTF-8?q?=E7=BB=9F=E4=B8=80=E4=BB=93=E5=BA=93=E6=9C=80=E6=96=B0=E6=8F=90?= =?UTF-8?q?=E4=BA=A4=E8=8E=B7=E5=8F=96=E6=8E=A5=E5=8F=A3=E5=B9=B6=E8=BF=81?= =?UTF-8?q?=E7=A7=BB=E6=96=87=E4=BB=B6=E6=8F=90=E4=BA=A4=E6=97=A5=E6=9C=9F?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../auto_update/_data_source.py | 22 +++---------- zhenxun/utils/github_utils/const.py | 3 ++ zhenxun/utils/repo_utils/aliyun_manager.py | 5 +++ zhenxun/utils/repo_utils/base_manager.py | 14 +++++++++ zhenxun/utils/repo_utils/file_manager.py | 31 +++++++++++++++++++ zhenxun/utils/repo_utils/github_manager.py | 6 ++++ 6 files changed, 63 insertions(+), 18 deletions(-) diff --git a/zhenxun/builtin_plugins/auto_update/_data_source.py b/zhenxun/builtin_plugins/auto_update/_data_source.py index 18f80f38..7a35e090 100644 --- a/zhenxun/builtin_plugins/auto_update/_data_source.py +++ b/zhenxun/builtin_plugins/auto_update/_data_source.py @@ -6,7 +6,6 @@ from packaging.specifiers import SpecifierSet from packaging.version import InvalidVersion, Version from zhenxun.services.log import logger -from zhenxun.utils.http_utils import AsyncHttpx from zhenxun.utils.manager.virtual_env_package_manager import VirtualEnvPackageManager from zhenxun.utils.manager.zhenxun_repo_manager import ( ZhenxunRepoConfig, @@ -19,19 +18,6 @@ LOG_COMMAND = "AutoUpdate" class UpdateManager: - @staticmethod - async def _get_latest_commit_date(owner: str, repo: str, path: str) -> str: - """获取文件最新 commit 日期""" - api_url = f"https://api.github.com/repos/{owner}/{repo}/commits" - params = {"path": path, "page": 1, "per_page": 1} - try: - data = await AsyncHttpx.get_json(api_url, params=params) - if data and isinstance(data, list) and data[0]: - date_str = data[0]["commit"]["committer"]["date"] - return date_str.split("T")[0] - except Exception as e: - logger.warning(f"获取 {owner}/{repo}/{path} 的 commit 日期失败", e=e) - return "获取失败" @classmethod async def check_version(cls) -> str: @@ -42,11 +28,11 @@ class UpdateManager: dev_version_task = RepoFileManager.get_file_content( ZhenxunRepoConfig.ZHENXUN_BOT_GITHUB_URL, "__version__" ) - bot_commit_date_task = cls._get_latest_commit_date( - "HibiKier", "zhenxun_bot", "__version__" + bot_commit_date_task = RepoFileManager.get_file_last_commit_date( + ZhenxunRepoConfig.ZHENXUN_BOT_GITHUB_URL, "__version__" ) - res_commit_date_task = cls._get_latest_commit_date( - "zhenxun-org", "zhenxun-bot-resources", "__version__" + res_commit_date_task = RepoFileManager.get_file_last_commit_date( + ZhenxunRepoConfig.RESOURCE_GITHUB_URL, "__version__" ) ( diff --git a/zhenxun/utils/github_utils/const.py b/zhenxun/utils/github_utils/const.py index 102e6f19..e83f351d 100644 --- a/zhenxun/utils/github_utils/const.py +++ b/zhenxun/utils/github_utils/const.py @@ -40,6 +40,9 @@ RELEASE_SOURCE_FORMAT = ( GIT_API_COMMIT_FORMAT = "https://api.github.com/repos/{owner}/{repo}/commits/{branch}" """git api commit地址格式""" +GIT_API_COMMIT_LIST_FORMAT = "https://api.github.com/repos/{owner}/{repo}/commits" +"""git api 列出commits的地址格式""" + GIT_API_PROXY_COMMIT_FORMAT = ( "https://git-api.zhenxun.org/repos/{owner}/{repo}/commits/{branch}" ) diff --git a/zhenxun/utils/repo_utils/aliyun_manager.py b/zhenxun/utils/repo_utils/aliyun_manager.py index 302a24de..2f76003a 100644 --- a/zhenxun/utils/repo_utils/aliyun_manager.py +++ b/zhenxun/utils/repo_utils/aliyun_manager.py @@ -348,6 +348,11 @@ class AliyunCodeupManager(BaseRepoManager): if not self.config.aliyun_codeup.organization_id: raise AuthenticationError("阿里云CodeUp") + async def get_latest_commit(self, repo_url: str, branch: str = "main") -> str: + """获取阿里云CodeUp仓库指定分支的最新提交哈希值。""" + repo_name = repo_url.split("/tree/")[0].split("/")[-1].replace(".git", "") + return await self._get_newest_commit(repo_name, branch) + async def _get_newest_commit(self, repo_name: str, branch: str) -> str: """ 获取仓库最新提交ID diff --git a/zhenxun/utils/repo_utils/base_manager.py b/zhenxun/utils/repo_utils/base_manager.py index efe306b6..dd7d248e 100644 --- a/zhenxun/utils/repo_utils/base_manager.py +++ b/zhenxun/utils/repo_utils/base_manager.py @@ -117,6 +117,20 @@ class BaseRepoManager(ABC): """ pass + @abstractmethod + async def get_latest_commit(self, repo_url: str, branch: str = "main") -> str: + """ + 获取仓库指定分支的最新提交哈希值。 + + 参数: + repo_url: 仓库URL或名称。 + branch: 分支名称。 + + 返回: + str: 最新的提交哈希值。 + """ + pass + async def save_file_content(self, content: bytes, local_path: Path) -> int: """ 保存文件内容 diff --git a/zhenxun/utils/repo_utils/file_manager.py b/zhenxun/utils/repo_utils/file_manager.py index 94d50db3..eafe9ae1 100644 --- a/zhenxun/utils/repo_utils/file_manager.py +++ b/zhenxun/utils/repo_utils/file_manager.py @@ -11,6 +11,7 @@ from httpx import Response from zhenxun.services.log import logger from zhenxun.utils.github_utils import GithubUtils +from zhenxun.utils.github_utils.const import GIT_API_COMMIT_LIST_FORMAT from zhenxun.utils.github_utils.models import AliyunTreeType, GitHubStrategy, TreeType from zhenxun.utils.http_utils import AsyncHttpx from zhenxun.utils.utils import is_binary_file @@ -633,3 +634,33 @@ class RepoFileManager: result.success = False result.error_message = str(e) return result + async def get_file_last_commit_date( + self, repo_url: str, file_path: str + ) -> str | None: + """ + 获取 GitHub 仓库中指定文件的最新提交日期。 + + 参数: + repo_url: 仓库的URL。 + file_path: 文件在仓库中的路径。 + + 返回: + str | None: "YYYY-MM-DD" 格式的日期字符串,如果失败则返回 None。 + """ + try: + repo_info = GithubUtils.parse_github_url(repo_url) + api_url = GIT_API_COMMIT_LIST_FORMAT.format( + owner=repo_info.owner, repo=repo_info.repo + ) + params = {"sha": repo_info.branch, "path": file_path, + "page": 1, "per_page": 1} + + data = await AsyncHttpx.get_json(api_url, params=params) + if data and isinstance(data, list) and data[0]: + date_str = data[0]["commit"]["committer"]["date"] + return date_str.split("T")[0] + except Exception as e: + logger.warning( + f"获取 {repo_url} 中 {file_path} 的 commit 日期失败", LOG_COMMAND, e=e + ) + return None diff --git a/zhenxun/utils/repo_utils/github_manager.py b/zhenxun/utils/repo_utils/github_manager.py index 462c2723..8acaf82d 100644 --- a/zhenxun/utils/repo_utils/github_manager.py +++ b/zhenxun/utils/repo_utils/github_manager.py @@ -320,6 +320,12 @@ class GithubManager(BaseRepoManager): logger.error("获取提交信息失败", LOG_COMMAND, e=e) return None + async def get_latest_commit(self, repo_url: str, branch: str = "main") -> str: + """获取GitHub仓库指定分支的最新提交哈希值。""" + repo_info = GithubUtils.parse_github_url(repo_url) + repo_name = repo_info.repo.replace(".git", "") + return await self._get_newest_commit(repo_info.owner, repo_name, branch) + async def _get_newest_commit(self, owner: str, repo: str, branch: str) -> str: """ 获取仓库最新提交ID