From 99073d9fc8f78bc2817a0b9806a88f05abcb6040 Mon Sep 17 00:00:00 2001 From: AkashiCoin Date: Sat, 15 Feb 2025 16:08:42 +0800 Subject: [PATCH] =?UTF-8?q?:white=5Fcheck=5Fmark:=20=20perf(github-utils):?= =?UTF-8?q?=20=E4=BD=BF=E7=94=A8sourcery=E5=BB=BA=E8=AE=AE=EF=BC=8C?= =?UTF-8?q?=E5=B0=81=E8=A3=85=E6=9B=B4=E6=96=B0commit=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plugin_store/data_source.py | 26 ++++++------------- zhenxun/utils/github_utils/models.py | 11 ++++++++ 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/zhenxun/builtin_plugins/plugin_store/data_source.py b/zhenxun/builtin_plugins/plugin_store/data_source.py index 135aa1f1..6e662a81 100644 --- a/zhenxun/builtin_plugins/plugin_store/data_source.py +++ b/zhenxun/builtin_plugins/plugin_store/data_source.py @@ -83,15 +83,10 @@ class ShopManage: default_github_repo = GithubUtils.parse_github_url(DEFAULT_GITHUB_URL) extra_github_repo = GithubUtils.parse_github_url(EXTRA_GITHUB_URL) for repo_info in [default_github_repo, extra_github_repo]: - try: - newest_commit = await repo_info.get_newest_commit( - repo_info.owner, repo_info.repo, repo_info.branch - ) - if newest_commit: - repo_info.branch = newest_commit - logger.info(f"获取最新提交: {newest_commit}", "插件管理") - except Exception as e: - logger.warning(f"获取最新提交失败: {e}", "插件管理") + if await repo_info.update_repo_commit(): + logger.info(f"获取最新提交: {repo_info.branch}", "插件管理") + else: + logger.warning(f"获取最新提交失败: {repo_info}", "插件管理") default_github_url = await default_github_repo.get_raw_download_urls( "plugins.json" ) @@ -228,15 +223,10 @@ class ShopManage: files: list[str] repo_api: RepoAPI repo_info = GithubUtils.parse_github_url(github_url) - try: - newest_commit = await repo_info.get_newest_commit( - repo_info.owner, repo_info.repo, repo_info.branch - ) - if newest_commit: - repo_info.branch = newest_commit - logger.info(f"获取最新提交: {newest_commit}", "插件管理") - except Exception as e: - logger.warning(f"获取最新提交失败: {e}", "插件管理") + if await repo_info.update_repo_commit(): + logger.info(f"获取最新提交: {repo_info.branch}", "插件管理") + else: + logger.warning(f"获取最新提交失败: {repo_info}", "插件管理") logger.debug(f"成功获取仓库信息: {repo_info}", "插件管理") for repo_api in GithubUtils.iter_api_strategies(): try: diff --git a/zhenxun/utils/github_utils/models.py b/zhenxun/utils/github_utils/models.py index 64e2dda5..e3e5dfe3 100644 --- a/zhenxun/utils/github_utils/models.py +++ b/zhenxun/utils/github_utils/models.py @@ -1,3 +1,4 @@ +import contextlib from typing import Protocol from aiocache import cached @@ -64,6 +65,16 @@ class RepoInfo(BaseModel): for url_format in url_formats ] + async def update_repo_commit(self): + with contextlib.suppress(Exception): + newest_commit = await self.get_newest_commit( + self.owner, self.repo, self.branch + ) + if newest_commit: + self.branch = newest_commit + return True + return False + def to_dict(self, **kwargs): return model_dump(self, **kwargs)