perf(github-utils): 使用sourcery建议,封装更新commit方法

This commit is contained in:
AkashiCoin 2025-02-15 16:08:42 +08:00
parent a7d208736e
commit 99073d9fc8
2 changed files with 19 additions and 18 deletions

View File

@ -83,15 +83,10 @@ class ShopManage:
default_github_repo = GithubUtils.parse_github_url(DEFAULT_GITHUB_URL) default_github_repo = GithubUtils.parse_github_url(DEFAULT_GITHUB_URL)
extra_github_repo = GithubUtils.parse_github_url(EXTRA_GITHUB_URL) extra_github_repo = GithubUtils.parse_github_url(EXTRA_GITHUB_URL)
for repo_info in [default_github_repo, extra_github_repo]: for repo_info in [default_github_repo, extra_github_repo]:
try: if await repo_info.update_repo_commit():
newest_commit = await repo_info.get_newest_commit( logger.info(f"获取最新提交: {repo_info.branch}", "插件管理")
repo_info.owner, repo_info.repo, repo_info.branch else:
) logger.warning(f"获取最新提交失败: {repo_info}", "插件管理")
if newest_commit:
repo_info.branch = newest_commit
logger.info(f"获取最新提交: {newest_commit}", "插件管理")
except Exception as e:
logger.warning(f"获取最新提交失败: {e}", "插件管理")
default_github_url = await default_github_repo.get_raw_download_urls( default_github_url = await default_github_repo.get_raw_download_urls(
"plugins.json" "plugins.json"
) )
@ -228,15 +223,10 @@ class ShopManage:
files: list[str] files: list[str]
repo_api: RepoAPI repo_api: RepoAPI
repo_info = GithubUtils.parse_github_url(github_url) repo_info = GithubUtils.parse_github_url(github_url)
try: if await repo_info.update_repo_commit():
newest_commit = await repo_info.get_newest_commit( logger.info(f"获取最新提交: {repo_info.branch}", "插件管理")
repo_info.owner, repo_info.repo, repo_info.branch else:
) logger.warning(f"获取最新提交失败: {repo_info}", "插件管理")
if newest_commit:
repo_info.branch = newest_commit
logger.info(f"获取最新提交: {newest_commit}", "插件管理")
except Exception as e:
logger.warning(f"获取最新提交失败: {e}", "插件管理")
logger.debug(f"成功获取仓库信息: {repo_info}", "插件管理") logger.debug(f"成功获取仓库信息: {repo_info}", "插件管理")
for repo_api in GithubUtils.iter_api_strategies(): for repo_api in GithubUtils.iter_api_strategies():
try: try:

View File

@ -1,3 +1,4 @@
import contextlib
from typing import Protocol from typing import Protocol
from aiocache import cached from aiocache import cached
@ -64,6 +65,16 @@ class RepoInfo(BaseModel):
for url_format in url_formats 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): def to_dict(self, **kwargs):
return model_dump(self, **kwargs) return model_dump(self, **kwargs)