From 06c6bf19f83a71480d1023b9a194784c9361ce44 Mon Sep 17 00:00:00 2001 From: Natalie Johnson <98086483+Copaan@users.noreply.github.com> Date: Fri, 14 Feb 2025 15:02:13 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=8F=92=E4=BB=B6=E5=95=86?= =?UTF-8?q?=E5=BA=97=E4=B8=8B=E8=BD=BD=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plugin_store/data_source.py | 21 ++++++++++++------- zhenxun/utils/github_utils/models.py | 19 +++++++++++++++++ 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/zhenxun/builtin_plugins/plugin_store/data_source.py b/zhenxun/builtin_plugins/plugin_store/data_source.py index 39c7d263..3920e5ff 100644 --- a/zhenxun/builtin_plugins/plugin_store/data_source.py +++ b/zhenxun/builtin_plugins/plugin_store/data_source.py @@ -1,9 +1,9 @@ -from pathlib import Path import shutil import subprocess +from pathlib import Path -from aiocache import cached import ujson as json +from aiocache import cached from zhenxun.builtin_plugins.auto_update.config import REQ_TXT_FILE_STRING from zhenxun.builtin_plugins.plugin_store.models import StorePluginInfo @@ -198,10 +198,10 @@ class ShopManage: if plugin_info.github_url is None: plugin_info.github_url = DEFAULT_GITHUB_URL is_external = False - version_split = plugin_info.version.split("-") - if len(version_split) > 1: - github_url_split = plugin_info.github_url.split("/tree/") - plugin_info.github_url = f"{github_url_split[0]}/tree/{version_split[1]}" + # version_split = plugin_info.version.split("-") + # if len(version_split) > 1: + # github_url_split = plugin_info.github_url.split("/tree/") + # plugin_info.github_url = f"{github_url_split[0]}/tree/{version_split[1]}" logger.info(f"正在安装插件 {plugin_key}...") await cls.install_plugin_with_repo( plugin_info.github_url, @@ -218,7 +218,14 @@ class ShopManage: files: list[str] repo_api: RepoAPI repo_info = GithubUtils.parse_github_url(github_url) - logger.debug(f"成功获取仓库信息: {repo_info}", "插件管理") + try: + # 获取最新commit并更新分支 + if not repo_info.is_commit_hash(): + latest_commit = await repo_info.get_latest_commit() + repo_info.branch = latest_commit + except Exception as e: + logger.error(f"获取最新commit失败: {e}", "插件管理") + raise for repo_api in GithubUtils.iter_api_strategies(): try: await repo_api.parse_repo_info(repo_info) diff --git a/zhenxun/utils/github_utils/models.py b/zhenxun/utils/github_utils/models.py index d21c2387..4cab6255 100644 --- a/zhenxun/utils/github_utils/models.py +++ b/zhenxun/utils/github_utils/models.py @@ -1,3 +1,4 @@ +import string from typing import Protocol from aiocache import cached @@ -61,6 +62,24 @@ class RepoInfo(BaseModel): def to_dict(self, **kwargs): return model_dump(self, **kwargs) + async def get_latest_commit(self) -> str: + """获取分支最新commit""" + if self.is_commit_hash(): + return self.branch + url = f"https://api.github.com/repos/{self.owner}/{self.repo}/branches/{self.branch}" + headers = {"Accept": "application/vnd.github.v3+json"} + response = await AsyncHttpx.get(url, headers=headers) + if response.status_code == 200: + data = response.json() + return data["commit"]["sha"] + raise ValueError(f"获取最新commit失败: {response.text}") + + def is_commit_hash(self) -> bool: + """判断branch是否为commit hash""" + return len(self.branch) in (7, 40) and all( + c in string.hexdigits.lower() for c in self.branch + ) + class APIStrategy(Protocol): """API策略"""