Skip to content

Commit

Permalink
🐛 fix context re-enter error (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
yanyongyu authored Sep 8, 2024
1 parent 6ed211e commit c784c16
Show file tree
Hide file tree
Showing 13 changed files with 42 additions and 36 deletions.
4 changes: 2 additions & 2 deletions src/plugins/github/dependencies/commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
@Author : yanyongyu
@Date : 2023-10-07 17:16:46
@LastEditors : yanyongyu
@LastEditTime : 2024-03-05 14:30:05
@LastEditTime : 2024-09-08 11:43:07
@Description : None
@GitHub : https://github.com/yanyongyu
"""
Expand Down Expand Up @@ -33,7 +33,7 @@ async def get_commit(
should_remind = event.is_tome()

try:
async with context as bot:
async with context() as bot:
resp = await bot.rest.repos.async_get_commit(
owner=owner, repo=repo, ref=ref
)
Expand Down
20 changes: 13 additions & 7 deletions src/plugins/github/dependencies/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@
@Author : yanyongyu
@Date : 2023-10-07 17:16:28
@LastEditors : yanyongyu
@LastEditTime : 2023-10-07 17:16:28
@LastEditTime : 2024-09-08 11:42:36
@Description : None
@GitHub : https://github.com/yanyongyu
"""

__author__ = "yanyongyu"

from typing import Annotated
from functools import partial
from contextlib import nullcontext
from collections.abc import AsyncGenerator
from collections.abc import Callable, AsyncGenerator

from nonebot import logger
from nonebot.params import Depends
Expand Down Expand Up @@ -46,7 +47,12 @@ async def github_user_context(token: str) -> AsyncGenerator[GitHubBot, None]:

async def get_github_public_context(
matcher: Matcher, state: T_State, user: USER
) -> AsyncGeneratorContextManager[GitHubBot] | nullcontext[OAuthBot]:
) -> Callable[[], AsyncGeneratorContextManager[GitHubBot] | nullcontext[OAuthBot]]:
"""Get current GitHub public context from event user.
Return a callable to make sure the context can be reused in dependency cache.
"""

bot = get_github_bot()

# First use user auth, allow user to access private repo if authorized
Expand All @@ -57,7 +63,7 @@ async def get_github_public_context(
client_id=config.github_app.client_id,
access_token=user.access_token,
)
return github_user_context(user.access_token)
return partial(github_user_context, user.access_token)
except ActionTimeout:
await matcher.finish("GitHub API 超时,请稍后再试")
except ActionFailed as e:
Expand Down Expand Up @@ -87,7 +93,7 @@ async def get_github_public_context(
async with bot.as_installation(installation_id):
resp = await bot.rest.repos.async_get(owner=owner, repo=repo)
if not resp.parsed_data.private:
return github_installation_context(installation_id)
return partial(github_installation_context, installation_id)
except ActionTimeout:
await matcher.finish("GitHub API 超时,请稍后再试")
except ActionFailed as e:
Expand All @@ -102,14 +108,14 @@ async def get_github_public_context(

# Finally use oauth bot if available
if config.oauth_app:
return nullcontext(get_oauth_bot())
return partial(nullcontext, get_oauth_bot())

# no bot available, prompt user to install
await matcher.finish("你还没有绑定 GitHub 帐号,请私聊使用 /install 进行安装")


GITHUB_PUBLIC_CONTEXT = Annotated[
AsyncGeneratorContextManager[GitHubBot] | nullcontext[OAuthBot],
Callable[[], AsyncGeneratorContextManager[GitHubBot] | nullcontext[OAuthBot]],
Depends(get_github_public_context),
]
"""Current GitHub bot context from event.
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/github/dependencies/issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
@Author : yanyongyu
@Date : 2023-10-07 17:16:46
@LastEditors : yanyongyu
@LastEditTime : 2024-05-16 18:19:54
@LastEditTime : 2024-09-08 11:43:21
@Description : None
@GitHub : https://github.com/yanyongyu
"""
Expand All @@ -29,7 +29,7 @@ async def get_issue(
number = int(state["issue"])

try:
async with context as bot:
async with context() as bot:
resp = await bot.rest.issues.async_get(
owner=owner, repo=repo, issue_number=number
)
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/github/dependencies/release.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
@Author : yanyongyu
@Date : 2023-10-07 17:16:46
@LastEditors : yanyongyu
@LastEditTime : 2024-03-05 14:31:05
@LastEditTime : 2024-09-08 11:43:39
@Description : None
@GitHub : https://github.com/yanyongyu
"""
Expand Down Expand Up @@ -33,7 +33,7 @@ async def get_release(
should_remind = event.is_tome()

try:
async with context as bot:
async with context() as bot:
resp = await bot.rest.repos.async_get_release_by_tag(
owner=owner, repo=repo, tag=tag
)
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/github/dependencies/repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
@Author : yanyongyu
@Date : 2023-10-07 17:16:55
@LastEditors : yanyongyu
@LastEditTime : 2024-08-18 17:41:16
@LastEditTime : 2024-09-08 11:43:48
@Description : None
@GitHub : https://github.com/yanyongyu
"""
Expand Down Expand Up @@ -32,7 +32,7 @@ async def get_repo(
should_remind = event.is_tome()

try:
async with context as bot:
async with context() as bot:
resp = await bot.rest.repos.async_get(owner=owner, repo=repo)
return resp.parsed_data
except ActionTimeout:
Expand Down
8 changes: 4 additions & 4 deletions src/plugins/github/plugins/github_issue/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
@Author : yanyongyu
@Date : 2021-03-09 15:15:02
@LastEditors : yanyongyu
@LastEditTime : 2024-06-02 16:49:23
@LastEditTime : 2024-09-08 11:44:07
@Description : None
@GitHub : https://github.com/yanyongyu
"""
Expand Down Expand Up @@ -96,7 +96,7 @@ async def handle_issue(
)

try:
async with context as bot:
async with context() as bot:
img = await issue_to_image(bot, issue_, highlight_comment=comment)
except ActionTimeout:
await issue.finish("GitHub API 超时,请稍后再试")
Expand Down Expand Up @@ -150,7 +150,7 @@ async def handle_pr_diff(
)

try:
async with context as bot:
async with context() as bot:
img = await pr_diff_to_image(bot, issue_)
except ActionTimeout:
await pr_diff_link.finish("GitHub API 超时,请稍后再试")
Expand Down Expand Up @@ -209,7 +209,7 @@ async def handle_short(
)

try:
async with context as bot:
async with context() as bot:
img = await issue_to_image(bot, issue_)
except TimeoutError:
await issue_short.finish("生成图片超时!请尝试重试")
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/github/plugins/github_reply/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
@Author : yanyongyu
@Date : 2021-03-26 14:45:05
@LastEditors : yanyongyu
@LastEditTime : 2024-06-02 16:50:38
@LastEditTime : 2024-09-08 11:44:18
@Description : None
@GitHub : https://github.com/yanyongyu
"""
Expand Down Expand Up @@ -106,7 +106,7 @@ async def handle_content(
)

try:
async with context as bot:
async with context() as bot:
img = await issue_to_image(bot, issue_info)
except ActionTimeout:
await content.finish("GitHub API 超时,请稍后再试")
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/github/plugins/github_reply/deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
@Author : yanyongyu
@Date : 2023-11-28 11:04:29
@LastEditors : yanyongyu
@LastEditTime : 2023-12-11 18:11:40
@LastEditTime : 2024-09-08 11:44:27
@Description : None
@GitHub : https://github.com/yanyongyu
"""
Expand Down Expand Up @@ -86,7 +86,7 @@ async def handle_content(
)

try:
async with context as bot:
async with context() as bot:
resp = await bot.rest.repos.async_list_deployments(
owner=owner,
repo=repo,
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/github/plugins/github_reply/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
@Author : yanyongyu
@Date : 2021-03-26 14:59:59
@LastEditors : yanyongyu
@LastEditTime : 2024-06-02 16:51:03
@LastEditTime : 2024-09-08 11:44:34
@Description : None
@GitHub : https://github.com/yanyongyu
"""
Expand Down Expand Up @@ -98,7 +98,7 @@ async def handle_diff(
)

try:
async with context as bot:
async with context() as bot:
img = await pr_diff_to_image(bot, issue_info)
except ActionTimeout:
await diff.finish("GitHub API 超时,请稍后再试")
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/github/plugins/github_reply/license.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
@Author : yanyongyu
@Date : 2023-11-27 13:32:08
@LastEditors : yanyongyu
@LastEditTime : 2023-12-11 16:01:46
@LastEditTime : 2024-09-08 11:44:44
@Description : None
@GitHub : https://github.com/yanyongyu
"""
Expand Down Expand Up @@ -85,7 +85,7 @@ async def handle_content(
)

try:
async with context as bot:
async with context() as bot:
resp = await bot.rest.repos.async_get(
owner=owner,
repo=repo,
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/github/plugins/github_reply/readme.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
@Author : yanyongyu
@Date : 2023-10-18 17:08:37
@LastEditors : yanyongyu
@LastEditTime : 2024-06-02 16:51:25
@LastEditTime : 2024-09-08 11:44:57
@Description : None
@GitHub : https://github.com/yanyongyu
"""
Expand Down Expand Up @@ -88,7 +88,7 @@ async def handle_content(
)

try:
async with context as bot:
async with context() as bot:
resp = await bot.rest.repos.async_get_readme(
owner=owner,
repo=repo,
Expand Down Expand Up @@ -120,7 +120,7 @@ async def render_content(
content: str = state["content"]

try:
async with context as bot:
async with context() as bot:
img = await readme_to_image(bot, repo_info, content)
except TimeoutError:
await readme.finish("生成图片超时!请稍后再试")
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/github/plugins/github_reply/release.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
@Author : yanyongyu
@Date : 2023-11-27 13:46:17
@LastEditors : yanyongyu
@LastEditTime : 2023-12-11 14:52:29
@LastEditTime : 2024-09-08 11:45:06
@Description : None
@GitHub : https://github.com/yanyongyu
"""
Expand Down Expand Up @@ -93,7 +93,7 @@ async def handle_content(
target_tag: str | None = state["target_tag"]

try:
async with context as bot:
async with context() as bot:
if target_tag:
resp = await bot.rest.repos.async_get_release_by_tag(
owner=owner, repo=repo, tag=target_tag
Expand Down
8 changes: 4 additions & 4 deletions src/plugins/github/plugins/github_search/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
@Author : yanyongyu
@Date : 2023-11-27 14:31:21
@LastEditors : yanyongyu
@LastEditTime : 2023-12-14 17:16:31
@LastEditTime : 2024-09-08 11:45:23
@Description : None
@GitHub : https://github.com/yanyongyu
"""
Expand Down Expand Up @@ -64,7 +64,7 @@ async def do_code_search(context: GITHUB_PUBLIC_CONTEXT, query: str = ArgPlainTe
)

try:
async with context as bot:
async with context() as bot:
resp = await bot.rest.search.async_code(q=query, per_page=5)
result = resp.parsed_data
except ActionTimeout:
Expand Down Expand Up @@ -118,7 +118,7 @@ async def do_repo_search(context: GITHUB_PUBLIC_CONTEXT, query: str = ArgPlainTe
)

try:
async with context as bot:
async with context() as bot:
resp = await bot.rest.search.async_repos(q=query, per_page=5)
result = resp.parsed_data
except ActionTimeout:
Expand Down Expand Up @@ -173,7 +173,7 @@ async def do_user_search(context: GITHUB_PUBLIC_CONTEXT, query: str = ArgPlainTe
)

try:
async with context as bot:
async with context() as bot:
resp = await bot.rest.search.async_users(q=query, per_page=5)
result = resp.parsed_data
except ActionTimeout:
Expand Down

0 comments on commit c784c16

Please sign in to comment.