🐛 修复检查更新

This commit is contained in:
CMHopeSunshine 2022-09-17 18:11:31 +08:00
parent 0224cd1d49
commit 7fd5394855

View File

@ -4,34 +4,32 @@ import git
from nonebot.utils import run_sync
from git.exc import GitCommandError, InvalidGitRepositoryError
from LittlePaimon import __version__, NICKNAME
from LittlePaimon.utils import aiorequests
def time_str(timestamp: int) -> str:
time_local = time.localtime(timestamp)
return time.strftime("%m-%d %H:%M:%S", time_local)
@run_sync
def check_update():
async def check_update():
resp = await aiorequests.get('https://api.github.com/repos/CMHopeSunshine/LittlePaimon/commits')
data = resp.json()
if not isinstance(data, list):
return '检查更新失败,可能是网络问题,请稍后再试'
try:
repo = git.Repo(Path().absolute())
except InvalidGitRepositoryError:
return '没有发现git仓库无法通过git检查更新'
local_commit = repo.head.commit
remote_commit = []
for commit in repo.iter_commits(max_count=15):
if local_commit == commit:
for commit in data:
if local_commit.binsha == commit['sha']:
break
remote_commit.append(commit)
if not remote_commit:
return f'当前已是最新版本:{__version__}'
i = 1
result = '检查到更新,日志如下:\n'
for commit in remote_commit:
if isinstance(commit.message, str):
result += f'{i}.{time_str(commit.committed_date)}\n' + commit.message.replace(':bug:', '🐛').replace(
':sparkles:', '').replace(':memo:', '📝') + '\n'
i += 1
for i, commit in enumerate(remote_commit, start=1):
result += f'{i}.{commit["commit"]["committer"]["date"].replace("T", " ").replace("Z", "")}\n' + commit['commit']['message'].replace(':bug:', '🐛').replace(
':sparkles:', '').replace(':memo:', '📝') + '\n'
if i >= 5:
break
return result