Skip to content

Commit

Permalink
✨ add release command
Browse files Browse the repository at this point in the history
  • Loading branch information
yanyongyu authored Nov 27, 2023
1 parent 016e3c3 commit 80e0e4a
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 14 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author : yanyongyu
* @Date : 2020-09-10 17:11:45
* @LastEditors : yanyongyu
* @LastEditTime : 2023-11-27 13:39:43
* @LastEditTime : 2023-11-27 14:15:38
* @Description : README
* @GitHub : https://github.com/yanyongyu
-->
Expand Down Expand Up @@ -44,8 +44,9 @@ GitHub Bot for QQ
| `/subscribe owner/repo event[/action]` | 订阅指定仓库的指定事件(支持多事件订阅) |
| `owner/repo[#number]` 或 GitHub 链接 | 快捷查看仓库 Issue/PR, PR diff 等 |
| `/link`, `/repo` | 获取 Issue/PR、仓库链接 |
| `/license` | 获取仓库许可证 |
| `/readme` | 查看仓库 README |
| `/license` | 获取仓库许可证 |
| `/release [tag]` | 获取仓库最新 Release,或指定 tag 的 Release |
| `/star`, `/unstar` | 快捷 star |
| `/diff` | 查看 PR diff |
| `/comment` | 评论 Issue/PR |
Expand Down
10 changes: 9 additions & 1 deletion src/plugins/github/cache/message_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,16 @@ class CommitTag(RepoTag):
commit: str


class ReleaseTag(RepoTag):
"""Release tag"""

type: Literal["release"] = "release"
tag: str


Tag = Annotated[
RepoTag | IssueTag | PullRequestTag | CommitTag, Field(discriminator="type")
RepoTag | IssueTag | PullRequestTag | CommitTag | ReleaseTag,
Field(discriminator="type"),
]
"""Tag types"""

Expand Down
18 changes: 12 additions & 6 deletions src/plugins/github/plugins/github_opengraph/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
@Author : yanyongyu
@Date : 2021-04-26 18:19:15
@LastEditors : yanyongyu
@LastEditTime : 2023-10-07 17:34:02
@LastEditTime : 2023-11-27 14:12:45
@Description : None
@GitHub : https://github.com/yanyongyu
"""
Expand All @@ -18,7 +18,6 @@
from nonebot.adapters.qq import MessageSegment as QQOfficialMS

from src.plugins.github import config
from src.plugins.github.cache.message_tag import RepoTag, CommitTag, create_message_tag
from src.plugins.github.dependencies import (
COMMIT,
RELEASE,
Expand All @@ -31,6 +30,12 @@
TargetType,
extract_sent_message,
)
from src.plugins.github.cache.message_tag import (
RepoTag,
CommitTag,
ReleaseTag,
create_message_tag,
)
from src.plugins.github.helpers import (
FULLREPO_REGEX,
NO_GITHUB_EVENT,
Expand Down Expand Up @@ -163,9 +168,10 @@ async def handle_release(

await create_message_tag(
message_info,
RepoTag(
ReleaseTag(
owner=owner,
repo=repo,
tag=release.tag_name,
is_receive=True,
),
)
Expand All @@ -176,15 +182,15 @@ async def handle_release(
)
match target_info.type:
case TargetType.QQ_USER | TargetType.QQ_GROUP:
result = await commit_graph.send(QQMS.image(image_url))
result = await release_graph.send(QQMS.image(image_url))
case (
TargetType.QQ_OFFICIAL_USER
| TargetType.QQGUILD_USER
| TargetType.QQ_OFFICIAL_GROUP
| TargetType.QQGUILD_CHANNEL
):
result = await commit_graph.send(QQOfficialMS.image(image_url))
result = await release_graph.send(QQOfficialMS.image(image_url))

tag = RepoTag(owner=owner, repo=repo, is_receive=False)
tag = ReleaseTag(owner=owner, repo=repo, tag=release.tag_name, is_receive=False)
if sent_message_info := extract_sent_message(target_info, result):
await create_message_tag(sent_message_info, tag)
4 changes: 3 additions & 1 deletion src/plugins/github/plugins/github_reply/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
@Author : yanyongyu
@Date : 2021-03-25 15:20:47
@LastEditors : yanyongyu
@LastEditTime : 2023-11-27 13:39:02
@LastEditTime : 2023-11-27 14:13:59
@Description : None
@GitHub : https://github.com/yanyongyu
"""
Expand All @@ -18,6 +18,7 @@
"/repo: 获取仓库链接\n"
"/readme: 获取仓库 README\n"
"/license: 获取仓库 LICENSE\n"
"/release [tag]: 获取仓库最新或指定 Release\n"
"/star: star 仓库(仅仓库安装 APP 后有效)\n"
"/unstar: unstar 仓库(仅仓库安装 APP 后有效)\n"
"/content: 查看 Issue、PR 信息及事件\n"
Expand Down Expand Up @@ -48,4 +49,5 @@
from . import comment as comment
from . import content as content
from . import license as license
from . import release as release
from . import unlabel as unlabel
6 changes: 3 additions & 3 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-11-27 13:38:26
@LastEditTime : 2023-11-27 14:09:07
@Description : None
@GitHub : https://github.com/yanyongyu
"""
Expand Down Expand Up @@ -54,10 +54,10 @@ async def handle_content(
except ActionFailed as e:
if e.response.status_code == 404:
await license.finish("未找到该仓库的 README")
logger.opt(exception=e).error(f"Failed while getting repo readme: {e}")
logger.opt(exception=e).error(f"Failed while getting repo license: {e}")
await license.finish("未知错误发生,请尝试重试或联系管理员")
except Exception as e:
logger.opt(exception=e).error(f"Failed while getting repo readme: {e}")
logger.opt(exception=e).error(f"Failed while getting repo license: {e}")
await license.finish("未知错误发生,请尝试重试或联系管理员")

msg = (
Expand Down
5 changes: 4 additions & 1 deletion src/plugins/github/plugins/github_reply/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
@Author : yanyongyu
@Date : 2021-03-26 14:31:37
@LastEditors : yanyongyu
@LastEditTime : 2023-11-25 17:14:27
@LastEditTime : 2023-11-27 14:11:03
@Description : None
@GitHub : https://github.com/yanyongyu
"""
Expand All @@ -23,6 +23,7 @@
from src.plugins.github.cache.message_tag import (
IssueTag,
CommitTag,
ReleaseTag,
PullRequestTag,
create_message_tag,
)
Expand Down Expand Up @@ -55,6 +56,8 @@ async def handle_link(
url += f"/pull/{tag.number}"
case CommitTag():
url += f"/commit/{tag.commit}"
case ReleaseTag():
url += f"/releases/tag/{tag.tag}"

match target_info.type:
case TargetType.QQ_USER | TargetType.QQ_GROUP:
Expand Down
106 changes: 106 additions & 0 deletions src/plugins/github/plugins/github_reply/release.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
"""
@Author : yanyongyu
@Date : 2023-11-27 13:46:17
@LastEditors : yanyongyu
@LastEditTime : 2023-11-27 14:13:09
@Description : None
@GitHub : https://github.com/yanyongyu
"""

__author__ = "yanyongyu"

import secrets

from nonebot.adapters import Message
from nonebot.params import CommandArg
from nonebot import logger, on_command
from nonebot.adapters.onebot.v11 import MessageSegment as QQMS
from nonebot.adapters.qq import MessageSegment as QQOfficialMS
from nonebot.adapters.github import ActionFailed, ActionTimeout

from src.plugins.github import config
from src.plugins.github.helpers import REPLY_ANY, NO_GITHUB_EVENT
from src.plugins.github.dependencies import REPLY_TAG, GITHUB_PUBLIC_CONTEXT
from src.plugins.github.cache.message_tag import ReleaseTag, create_message_tag
from src.providers.platform import (
TARGET_INFO,
MESSAGE_INFO,
TargetType,
extract_sent_message,
)

release = on_command(
"release",
rule=NO_GITHUB_EVENT & REPLY_ANY,
priority=config.github_command_priority,
block=True,
)


@release.handle()
async def handle_content(
target_info: TARGET_INFO,
message_info: MESSAGE_INFO,
tag: REPLY_TAG,
context: GITHUB_PUBLIC_CONTEXT,
args: Message = CommandArg(),
):
cmd_arg = args.extract_plain_text().strip()
target_tag = cmd_arg and (tag.tag if isinstance(tag, ReleaseTag) else None)
try:
async with context as bot:
if target_tag:
resp = await bot.rest.repos.async_get_release_by_tag(
owner=tag.owner, repo=tag.repo, tag=target_tag
)
else:
resp = await bot.rest.repos.async_get_latest_release(
owner=tag.owner, repo=tag.repo
)
release_data = resp.parsed_data
except ActionTimeout:
await release.finish("GitHub API 超时,请稍后再试")
except ActionFailed as e:
if e.response.status_code == 404:
if target_tag:
await release.finish(
f"找不到 {tag.owner}/{tag.repo}{target_tag} 发布"
)
else:
await release.finish(f"找不到 {tag.owner}/{tag.repo} 的最新发布")
logger.opt(exception=e).error(f"Failed while getting repo release: {e}")
await release.finish("未知错误发生,请尝试重试或联系管理员")
except Exception as e:
logger.opt(exception=e).error(f"Failed while getting repo release: {e}")
await release.finish("未知错误发生,请尝试重试或联系管理员")

await create_message_tag(
message_info,
ReleaseTag(
owner=tag.owner,
repo=tag.repo,
tag=release_data.tag_name,
is_receive=True,
),
)

image_url = (
f"https://opengraph.githubassets.com/{secrets.token_urlsafe(16)}/"
f"{tag.owner}/{tag.repo}/releases/tag/{release_data.tag_name}"
)
match target_info.type:
case TargetType.QQ_USER | TargetType.QQ_GROUP:
result = await release.send(QQMS.image(image_url))
case (
TargetType.QQ_OFFICIAL_USER
| TargetType.QQGUILD_USER
| TargetType.QQ_OFFICIAL_GROUP
| TargetType.QQGUILD_CHANNEL
):
result = await release.send(QQOfficialMS.image(image_url))

tag = ReleaseTag(
owner=tag.owner, repo=tag.repo, tag=release_data.tag_name, is_receive=False
)
if sent_message_info := extract_sent_message(target_info, result):
await create_message_tag(sent_message_info, tag)

0 comments on commit 80e0e4a

Please sign in to comment.