-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformatter.py
165 lines (144 loc) · 5.26 KB
/
formatter.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
from datetime import datetime
def format_to_text(payload: dict) -> str:
"""
根据payload构建要发送的纯文本消息内容。
"""
repository = payload.get("repository", {})
full_name = repository.get("full_name", "未知仓库")
html_url = repository.get("html_url", "无链接")
ref = payload.get("ref", "未知分支")
branch = ref.replace("refs/heads/", "") if ref.startswith("refs/heads/") else ref
pusher = payload.get("pusher", {}).get("name", "未知推送者")
compare = payload.get("compare", "无比较链接")
commits = payload.get("commits", [])
commit_count = len(commits)
# 提取提交详情
commit_details = []
for commit in commits:
commit_id = commit.get("id", "")[:7]
message = commit.get("message", "").strip().replace("\n", " ")
url = commit.get("url", "无链接")
commit_details.append(
f" - 提交ID: {commit_id}\n 消息: {message}\n 链接: {url}"
f" - 提交ID: {commit_id}\n 消息: {message}"
)
commit_messages = "\n".join(commit_details) if commit_details else " 无提交详情"
# 提取推送时间
timestamp = payload.get("head_commit", {}).get("timestamp", "")
try:
push_time = datetime.strptime(timestamp, "%Y-%m-%dT%H:%M:%S%z").strftime(
"%Y-%m-%d %H:%M:%S %Z"
)
except (ValueError, TypeError):
push_time = "未知时间"
message = (
f"【GitHub Push 事件通知】\n"
f"仓库: {full_name}\n"
f"仓库链接: {html_url}\n"
f"分支: {branch}\n"
f"推送者: {pusher}\n"
f"推送时间: {push_time}\n"
f"提交数: {commit_count}\n"
f"提交详情:\n{commit_messages}\n"
f"比较链接: {compare}"
)
return message
def format_to_json(payload: dict) -> dict:
"""
根据 GitHub Webhook Payload 格式化推送事件为复杂 JSON 消息。
"""
repository = payload.get("repository", {})
repo_name = repository.get("full_name", "未知仓库")
repo_url = repository.get("html_url", "无链接")
ref = payload.get("ref", "未知分支")
branch = ref.replace("refs/heads/", "") if ref.startswith("refs/heads/") else ref
pusher = payload.get("pusher", {})
pusher_name = pusher.get("name", "未知推送者")
pusher_email = pusher.get("email", "未知邮箱")
compare_url = payload.get("compare", "无比较链接")
commits = payload.get("commits", [])
commit_count = len(commits)
# 获取推送时间
timestamp = payload.get("head_commit", {}).get("timestamp", "")
try:
push_time = datetime.strptime(timestamp, "%Y-%m-%dT%H:%M:%S%z").strftime(
"%Y-%m-%d %H:%M:%S %Z"
)
except (ValueError, TypeError):
push_time = "未知时间"
# 构建消息内容
messages = [
{
"type": "node",
"data": {
"user_id": "",
"nickname": "GitHub Bot",
"content": [
{
"type": "text",
"data": {
"text": (
f"仓库: {repo_name}\n"
f"分支: {branch}\n"
f"推送者: {pusher_name} ({pusher_email})\n"
f"推送时间: {push_time}\n"
f"提交数: {commit_count}\n"
)
},
}
],
},
}
]
news = []
for commit in commits:
commit_id = commit.get("id", "")[:7]
message = commit.get("message", "无提交信息").strip()
auther = commit.get("author", {})
auther_name = auther.get("name", "未知作者")
auther_email = auther.get("email", "未知邮箱")
url = commit.get("url", "无链接")
# 添加详细信息消息
messages.append(
{
"type": "node",
"data": {
"user_id": "",
"nickname": "GitHub Bot",
"content": [
{
"type": "text",
"data": {
"text": f"提交ID: {commit_id}\n消息: {message}\n提交者: {auther_name} ({auther_email})\n链接: {url}"
},
}
],
},
}
)
# 添加简要新闻
news.append({"text": f"{commit_id}: {message}"})
messages.append(
{
"type": "node",
"data": {
"user_id": "",
"nickname": "GitHub Bot",
"content": [
{
"type": "text",
"data": {
"text": f"仓库链接: {repo_url}\n比较链接: {compare_url}"
},
}
],
},
}
)
return {
"prompt": "GitHub Push 事件通知",
"summary": f"仓库: {repo_name}",
"source": "GitHub Push 事件通知",
"messages": messages,
"news": news,
}