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)
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:

View File

@ -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)