🐛 fix(plugin-store): 优先获取commit号在进行插件下载 (#1853)

* 🐛  fix(plugin-store): 优先获取commit号在进行插件下载

* ✅  perf(github-utils): 使用sourcery建议,封装更新commit方法
This commit is contained in:
AkashiCoin
2025-02-18 15:32:26 +08:00
committed by GitHub
parent aef2597b46
commit 0e5a79ce2c
11 changed files with 485 additions and 25 deletions
@@ -80,12 +80,17 @@ class ShopManage:
返回:
dict: 插件信息数据
"""
default_github_url = await GithubUtils.parse_github_url(
DEFAULT_GITHUB_URL
).get_raw_download_urls("plugins.json")
extra_github_url = await GithubUtils.parse_github_url(
EXTRA_GITHUB_URL
).get_raw_download_urls("plugins.json")
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]:
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"
)
extra_github_url = await extra_github_repo.get_raw_download_urls("plugins.json")
res = await AsyncHttpx.get(default_github_url)
res2 = await AsyncHttpx.get(extra_github_url)
@@ -218,6 +223,10 @@ class ShopManage:
files: list[str]
repo_api: RepoAPI
repo_info = GithubUtils.parse_github_url(github_url)
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:
+8
View File
@@ -33,3 +33,11 @@ RELEASE_SOURCE_FORMAT = (
"https://codeload.github.com/{owner}/{repo}/legacy.{compress}/refs/tags/{version}"
)
"""release 源码格式"""
GIT_API_COMMIT_FORMAT = "https://api.github.com/repos/{owner}/{repo}/commits/{branch}"
"""git api commit地址格式"""
GIT_API_PROXY_COMMIT_FORMAT = (
"https://git-api.zhenxun.org/repos/{owner}/{repo}/commits/{branch}"
)
"""git api commit地址格式 (代理)"""
+28 -1
View File
@@ -1,3 +1,4 @@
import contextlib
from typing import Protocol
from aiocache import cached
@@ -7,7 +8,13 @@ from strenum import StrEnum
from zhenxun.utils.http_utils import AsyncHttpx
from .const import CACHED_API_TTL, GIT_API_TREES_FORMAT, JSD_PACKAGE_API_FORMAT
from .const import (
CACHED_API_TTL,
GIT_API_COMMIT_FORMAT,
GIT_API_PROXY_COMMIT_FORMAT,
GIT_API_TREES_FORMAT,
JSD_PACKAGE_API_FORMAT,
)
from .func import (
get_fastest_archive_formats,
get_fastest_raw_formats,
@@ -58,9 +65,29 @@ 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)
@classmethod
@cached(ttl=CACHED_API_TTL)
async def get_newest_commit(cls, owner: str, repo: str, branch: str) -> str:
commit_url = GIT_API_COMMIT_FORMAT.format(owner=owner, repo=repo, branch=branch)
commit_url_proxy = GIT_API_PROXY_COMMIT_FORMAT.format(
owner=owner, repo=repo, branch=branch
)
resp = await AsyncHttpx().get([commit_url, commit_url_proxy])
return "" if resp.status_code != 200 else resp.json()["sha"]
class APIStrategy(Protocol):
"""API策略"""