mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-09-30 01:00:02 +08:00
🐛 fix(plugin-store): 优先获取commit号在进行插件下载 (#1853)
* 🐛 fix(plugin-store): 优先获取commit号在进行插件下载 * ✅ perf(github-utils): 使用sourcery建议,封装更新commit方法
This commit is contained in:
@@ -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地址格式 (代理)"""
|
||||
|
||||
@@ -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策略"""
|
||||
|
||||
Reference in New Issue
Block a user