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)