🐛 fix github model and redis lib

This commit is contained in:
yanyongyu 2021-03-14 11:25:38 +08:00
parent a06dab91ec
commit cc2ed9d1de
4 changed files with 57 additions and 21 deletions

View File

@ -4,14 +4,14 @@
@Author : yanyongyu
@Date : 2021-03-11 01:34:31
@LastEditors : yanyongyu
@LastEditTime : 2021-03-12 13:36:57
@LastEditTime : 2021-03-14 11:16:32
@Description : None
@GitHub : https://github.com/yanyongyu
"""
__author__ = "yanyongyu"
import inspect
from typing import Any, Type, TypeVar
from typing import Any, Type, Union, TypeVar
from pydantic import BaseModel as _BaseModel, root_validator
@ -31,8 +31,15 @@ class BaseModel(_BaseModel):
def apply_requester(cls, values: dict) -> dict:
assert "requester" in values, "requester needed"
for name, info in cls.__fields__.items():
if name in values and inspect.isclass(info.type_) and issubclass(
info.type_, BaseModel):
is_model = inspect.isclass(info.type_) and issubclass(
info.type_, BaseModel)
is_generic = (
hasattr(info.type_, "__origin__") and
info.type_.__origin__ is Union and # type: ignore
any(issubclass(x, BaseModel)
for x in info.type_.__args__) # type: ignore
)
if name in values and (is_model or is_generic):
if isinstance(values[name], dict):
values[name]["requester"] = values["requester"]
elif isinstance(values[name], list):

View File

@ -0,0 +1,34 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@Author : yanyongyu
@Date : 2021-03-14 10:53:42
@LastEditors : yanyongyu
@LastEditTime : 2021-03-14 11:21:05
@Description : None
@GitHub : https://github.com/yanyongyu
"""
__author__ = "yanyongyu"
from typing import Optional
from .. import redis
REDIS_KEY_FORMAT = "github_bind_{group_id}"
def set_group_bind_repo(group_id: int, full_name: str) -> Optional[bool]:
return redis.set(REDIS_KEY_FORMAT.format(group_id=group_id), full_name)
def set_group_unbind_repo(group_id: int) -> int:
return redis.delete(REDIS_KEY_FORMAT.format(group_id=group_id))
def exists_group_bind_repo(group_id: int) -> int:
return redis.exists(REDIS_KEY_FORMAT.format(group_id=group_id))
def get_group_bind_repo(group_id: int) -> Optional[str]:
value = redis.get(REDIS_KEY_FORMAT.format(group_id=group_id))
return value if value is None else value.decode()

View File

@ -4,7 +4,7 @@
@Author : yanyongyu
@Date : 2021-03-12 15:03:23
@LastEditors : yanyongyu
@LastEditTime : 2021-03-13 16:11:26
@LastEditTime : 2021-03-14 11:01:36
@Description : None
@GitHub : https://github.com/yanyongyu
"""
@ -20,12 +20,10 @@ from nonebot.adapters.cqhttp import GROUP_ADMIN, GROUP_OWNER
from nonebot.adapters.cqhttp import Bot, GroupMessageEvent
from ...libs.repo import get_repo
from ... import redis, github_config as config
from ... import github_config as config
from src.libs.utils import allow_cancel, only_group
from ...libs.redis import set_group_bind_repo, set_group_unbind_repo, exists_group_bind_repo
print(__name__)
REDIS_KEY_FORMAT = "github_bind_{group_id}"
REPO_REGEX: str = r"^(?P<owner>[a-zA-Z0-9][a-zA-Z0-9\-]*)/(?P<repo>[a-zA-Z0-9_\-]+)$"
bind = on_command("bind",
@ -61,7 +59,7 @@ async def process_repo(bot: Bot, event: GroupMessageEvent, state: T_State):
await bind.reject(f"仓库名 {owner}/{repo_name} 不存在!请重新发送或取消")
return
redis.set(REDIS_KEY_FORMAT.format(group_id=event.group_id), repo.full_name)
set_group_bind_repo(event.group_id, repo.full_name)
await bind.finish(f"本群成功绑定仓库 {repo.full_name} ")
@ -77,8 +75,8 @@ unbind.__doc__ = """
@unbind.handle()
async def process_unbind(bot: Bot, event: GroupMessageEvent):
if redis.exists(REDIS_KEY_FORMAT.format(group_id=event.group_id)):
redis.delete(REDIS_KEY_FORMAT.format(group_id=event.group_id))
if exists_group_bind_repo(event.group_id):
set_group_unbind_repo(event.group_id)
await unbind.finish("成功解绑仓库!")
else:
await unbind.finish("尚未绑定仓库!")

View File

@ -4,7 +4,7 @@
@Author : yanyongyu
@Date : 2021-03-09 15:15:02
@LastEditors : yanyongyu
@LastEditTime : 2021-03-13 16:10:12
@LastEditTime : 2021-03-14 11:03:44
@Description : None
@GitHub : https://github.com/yanyongyu
"""
@ -20,13 +20,9 @@ from nonebot.typing import T_State
from nonebot.adapters.cqhttp import Bot, MessageEvent, MessageSegment, GroupMessageEvent
from src.libs.utils import only_group
from ... import redis, github_config as config
from ... import github_config as config
from ...libs.issue import get_issue, issue_to_image
from ..github_bind import REDIS_KEY_FORMAT, REPO_REGEX
from .. import github_bind
print(__name__, github_bind.__name__)
from ...libs.redis import get_group_bind_repo
# allow using api without token
try:
@ -35,6 +31,7 @@ except ImportError:
get_user_token = None
ISSUE_REGEX = r"^#(?P<number>\d+)$"
REPO_REGEX: str = r"^(?P<owner>[a-zA-Z0-9][a-zA-Z0-9\-]*)/(?P<repo>[a-zA-Z0-9_\-]+)$"
REPO_ISSUE_REGEX = r"^(?P<owner>[a-zA-Z0-9][a-zA-Z0-9\-]*)/(?P<repo>[a-zA-Z0-9_\-]+)#(?P<number>\d+)$"
issue = on_regex(REPO_ISSUE_REGEX, priority=config.github_command_priority)
@ -77,11 +74,11 @@ issue_short.__doc__ = """
async def handle_short(bot: Bot, event: GroupMessageEvent, state: T_State):
group = state["_matched_dict"]
number = int(group["number"])
full_name = redis.get(REDIS_KEY_FORMAT.format(group_id=event.group_id))
full_name = get_group_bind_repo(event.group_id)
if not full_name:
await issue_short.finish("此群尚未与仓库绑定!")
return
match = re.match(REPO_REGEX, full_name.decode())
match = re.match(REPO_REGEX, full_name)
if not match:
await issue_short.finish("绑定的仓库名不合法!请重新尝试绑定~")
owner = match.group("owner")