generated from actions/typescript-action
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
bot.py
98 lines (78 loc) · 2.97 KB
/
bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import asyncio
import os
from contextlib import contextmanager
from pathlib import Path
from typing import cast
import nonebot
from nonebot import logger
from nonebot.adapters.github import Adapter as GITHUBAdapter
from nonebot.adapters.github import Event
from nonebot.drivers.none import Driver
from nonebot.message import handle_event
@contextmanager
def ensure_cwd(cwd: Path):
current_cwd = Path.cwd()
try:
os.chdir(cwd)
yield
finally:
os.chdir(current_cwd)
async def handle_github_action_event():
"""处理 GitHub Action 事件"""
driver = cast(Driver, nonebot.get_driver())
try:
config = driver.config
# 从环境变量中获取事件信息
# 读取到的 gitub_run_id 会因为 nonebot 配置加载机制转成 int,需要转回 str
event_id = str(config.github_run_id)
event_name = config.github_event_name
github_event_path = Path(config.github_event_path)
# 生成事件
if event := Adapter.payload_to_event(
event_id, event_name, github_event_path.read_text(encoding="utf-8")
):
bot = nonebot.get_bot()
await handle_event(bot, event)
except Exception:
logger.exception("处理 GitHub Action 事件时出现异常")
handle_event_task = None
class Adapter(GITHUBAdapter):
def _setup(self):
self.driver.on_startup(self._startup)
async def _startup(self):
driver = cast(Driver, self.driver)
try:
await super()._startup()
except Exception:
logger.exception("启动 GitHub 适配器时出现异常")
driver.exit(True)
return
# 完成启动后创建任务处理 GitHub Action 事件
handle_event_task = asyncio.create_task(handle_github_action_event())
# 处理完成之后就退出
handle_event_task.add_done_callback(lambda _: driver.exit(True))
@classmethod
def payload_to_event(
cls, event_id: str, event_name: str, payload: str | bytes
) -> Event | None:
# webhook 事件中没有 pull_request_target,但是 actions 里有
# githubkit.exception.WebhookTypeNotFound: pull_request_target
if event_name == "pull_request_target":
event_name = "pull_request"
return super().payload_to_event(event_id, event_name, payload)
with ensure_cwd(Path(__file__).parent):
app_id = os.environ.get("APP_ID")
private_key = os.environ.get("PRIVATE_KEY")
# https://docs.github.com/en/actions/learn-github-actions/contexts#runner-context
# 如果设置时,值总是为 "1"
runner_debug = os.environ.get("RUNNER_DEBUG", "0")
nonebot.init(
driver="~none",
github_apps=[{"app_id": app_id, "private_key": private_key}],
log_level="DEBUG" if runner_debug == "1" else "INFO",
)
driver = nonebot.get_driver()
driver.register_adapter(Adapter)
nonebot.load_plugins("src/plugins")
if __name__ == "__main__":
nonebot.run()