Skip to content

Commit

Permalink
✨ add deployment command
Browse files Browse the repository at this point in the history
  • Loading branch information
yanyongyu authored Nov 28, 2023
1 parent d1b4a1c commit 9347647
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 1 deletion.
3 changes: 2 additions & 1 deletion 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 14:15:38
* @LastEditTime : 2023-11-28 13:32:11
* @Description : README
* @GitHub : https://github.com/yanyongyu
-->
Expand Down Expand Up @@ -48,6 +48,7 @@ GitHub Bot for QQ
| `/readme` | 查看仓库 README |
| `/license` | 获取仓库许可证 |
| `/release [tag]` | 获取仓库最新 Release,或指定 tag 的 Release |
| `/deployment` | 获取仓库 Deployment 列表 |
| `/star`, `/unstar` | 快捷 star |
| `/diff` | 查看 PR diff |
| `/comment` | 评论 Issue/PR |
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/github/plugins/github_reply/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"/readme: 获取仓库 README\n"
"/license: 获取仓库 LICENSE\n"
"/release [tag]: 获取仓库最新或指定 Release\n"
"/deployment: 获取仓库 Deployment 列表\n"
"/star: star 仓库(仅仓库安装 APP 后有效)\n"
"/unstar: unstar 仓库(仅仓库安装 APP 后有效)\n"
"/content: 查看 Issue、PR 信息及事件\n"
Expand Down Expand Up @@ -51,3 +52,4 @@
from . import license as license
from . import release as release
from . import unlabel as unlabel
from . import deployment as deployment
96 changes: 96 additions & 0 deletions src/plugins/github/plugins/github_reply/deployment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
"""
@Author : yanyongyu
@Date : 2023-11-28 11:04:29
@LastEditors : yanyongyu
@LastEditTime : 2023-11-28 13:35:52
@Description : None
@GitHub : https://github.com/yanyongyu
"""

__author__ = "yanyongyu"

from githubkit.utils import UNSET
from nonebot import logger, on_command
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.cache.message_tag import RepoTag, create_message_tag
from src.plugins.github.dependencies import REPLY_TAG, GITHUB_PUBLIC_CONTEXT
from src.providers.platform import (
TARGET_INFO,
MESSAGE_INFO,
TargetType,
extract_sent_message,
)

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


@deployment.handle()
async def handle_content(
target_info: TARGET_INFO,
message_info: MESSAGE_INFO,
tag: REPLY_TAG,
context: GITHUB_PUBLIC_CONTEXT,
):
await create_message_tag(
message_info, RepoTag(owner=tag.owner, repo=tag.repo, is_receive=True)
)

try:
async with context as bot:
resp = await bot.rest.repos.async_list_deployments(
owner=tag.owner,
repo=tag.repo,
sha=UNSET,
ref=UNSET,
task=UNSET,
environment=UNSET,
per_page=3,
)
result = resp.parsed_data
except ActionTimeout:
await deployment.finish("GitHub API 超时,请稍后再试")
except ActionFailed as e:
logger.opt(exception=e).error(f"Failed while getting repo readme: {e}")
await deployment.finish("未知错误发生,请尝试重试或联系管理员")
except Exception as e:
logger.opt(exception=e).error(f"Failed while getting repo readme: {e}")
await deployment.finish("未知错误发生,请尝试重试或联系管理员")

if not result:
msg = "该仓库没有部署记录"
else:
msg = "\n\n".join(
f"Deploy ID: {d.id}"
+ f"\nDeployed to {d.environment}"
+ (f" by {d.creator.login}" if d.creator else "")
+ f"\n创建于 {d.created_at.isoformat()}"
+ (
f"\n更新于 {d.updated_at.isoformat()}"
if d.updated_at != d.created_at
else ""
)
for d in result
)

match target_info.type:
case TargetType.QQ_USER | TargetType.QQ_GROUP:
result = await deployment.send(msg)
case (
TargetType.QQ_OFFICIAL_USER
| TargetType.QQGUILD_USER
| TargetType.QQ_OFFICIAL_GROUP
| TargetType.QQGUILD_CHANNEL
):
result = await deployment.send(msg)

tag = RepoTag(owner=tag.owner, repo=tag.repo, 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 9347647

Please sign in to comment.