-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathformatters.py
More file actions
602 lines (472 loc) · 17 KB
/
Copy pathformatters.py
File metadata and controls
602 lines (472 loc) · 17 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
from datetime import datetime
from typing import Any
def truncate_text(text: str, limit: int = 200) -> str:
value = (text or "").strip()
if len(value) <= limit:
return value
return value[: limit - 3] + "..."
def format_webhook_issue_message(
repo: str,
action: str,
issue: dict[str, Any],
sender: dict[str, Any] | None,
) -> str | None:
action_labels = {
"opened": "新建",
"closed": "关闭",
"reopened": "重新打开",
}
if action == "closed" and issue.get("state") == "closed":
action_labels["closed"] = "关闭"
if action not in action_labels:
return None
actor = (sender or {}).get("login") or issue.get("user", {}).get("login")
actor = actor or "未知"
message_lines = [
f"[GitHub Webhook] 仓库 {repo} 的 Issue 更新",
f"#{issue['number']} {issue['title']}",
f"事件: {action_labels[action]}",
f"触发人: {actor}",
]
if issue.get("html_url"):
message_lines.append(f"链接: {issue['html_url']}")
return "\n".join(message_lines)
def format_webhook_pr_message(
repo: str,
action: str,
pull_request: dict[str, Any],
sender: dict[str, Any] | None,
) -> str | None:
action_labels = {
"opened": "新建",
"closed": "关闭",
"reopened": "重新打开",
}
if action == "closed" and pull_request.get("merged"):
action_labels["closed"] = "合并"
if action not in action_labels:
return None
actor = (sender or {}).get("login") or pull_request.get("user", {}).get("login")
actor = actor or "未知"
base_label = pull_request.get("base", {}).get("label", "?")
head_label = pull_request.get("head", {}).get("label", "?")
message_lines = [
f"[GitHub Webhook] 仓库 {repo} 的 PR 更新",
f"#{pull_request['number']} {pull_request['title']}",
f"事件: {action_labels[action]}",
f"触发人: {actor}",
f"分支: {head_label} → {base_label}",
]
if pull_request.get("html_url"):
message_lines.append(f"链接: {pull_request['html_url']}")
return "\n".join(message_lines)
def format_webhook_issue_comment_message(
repo: str,
action: str,
issue: dict[str, Any],
comment: dict[str, Any],
sender: dict[str, Any] | None,
) -> str | None:
action_labels = {
"created": "新增评论",
"edited": "编辑评论",
"deleted": "删除评论",
}
label = action_labels.get(action)
if not label:
return None
actor = (sender or {}).get("login")
actor = actor or comment.get("user", {}).get("login") or "未知"
message_lines = [
f"[GitHub Webhook] 仓库 {repo} 的 Issue 评论更新",
f"Issue #{issue.get('number', '?')} {issue.get('title', '')}",
f"事件: {label}",
f"触发人: {actor}",
]
if action != "deleted":
body = comment.get("body", "")
if body:
message_lines.append("评论内容:")
message_lines.append(truncate_text(body))
url = comment.get("html_url") or issue.get("html_url")
if url:
message_lines.append(f"链接: {url}")
return "\n".join(line for line in message_lines if line)
def format_webhook_commit_comment_message(
repo: str,
action: str,
comment: dict[str, Any],
sender: dict[str, Any] | None,
) -> str | None:
if action and action != "created":
return None
actor = (sender or {}).get("login")
actor = actor or comment.get("user", {}).get("login") or "未知"
commit_id = comment.get("commit_id", "")
short_commit = commit_id[:7] if commit_id else "未知"
message_lines = [
f"[GitHub Webhook] 仓库 {repo} 的提交评论",
f"提交: {short_commit}",
f"触发人: {actor}",
]
body = comment.get("body", "")
if body:
message_lines.append("评论内容:")
message_lines.append(truncate_text(body))
if comment.get("html_url"):
message_lines.append(f"链接: {comment['html_url']}")
return "\n".join(line for line in message_lines if line)
def format_webhook_discussion_message(
repo: str,
action: str,
discussion: dict[str, Any],
sender: dict[str, Any] | None,
) -> str | None:
action_labels = {
"created": "新建讨论",
"edited": "更新讨论",
"answered": "标记为已回答",
"unanswered": "取消回答",
"labeled": "添加标签",
"unlabeled": "移除标签",
}
label = action_labels.get(action)
if not label:
return None
actor = (sender or {}).get("login") or discussion.get("user", {}).get("login")
actor = actor or "未知"
message_lines = [
f"[GitHub Webhook] 仓库 {repo} 的 Discussion 更新",
f"Discussion #{discussion.get('number', '?')} {discussion.get('title', '')}",
f"事件: {label}",
f"触发人: {actor}",
]
if discussion.get("html_url"):
message_lines.append(f"链接: {discussion['html_url']}")
return "\n".join(line for line in message_lines if line)
def format_webhook_discussion_comment_message(
repo: str,
action: str,
discussion: dict[str, Any],
comment: dict[str, Any],
sender: dict[str, Any] | None,
) -> str | None:
action_labels = {
"created": "新增讨论评论",
"edited": "编辑讨论评论",
"deleted": "删除讨论评论",
}
label = action_labels.get(action)
if not label:
return None
actor = (sender or {}).get("login")
actor = actor or comment.get("user", {}).get("login") or "未知"
message_lines = [
f"[GitHub Webhook] 仓库 {repo} 的 Discussion 评论更新",
f"Discussion #{discussion.get('number', '?')} {discussion.get('title', '')}",
f"事件: {label}",
f"触发人: {actor}",
]
if action != "deleted":
body = comment.get("body", "")
if body:
message_lines.append("评论内容:")
message_lines.append(truncate_text(body))
url = comment.get("html_url") or discussion.get("html_url")
if url:
message_lines.append(f"链接: {url}")
return "\n".join(line for line in message_lines if line)
def format_webhook_fork_message(
repo: str,
forkee: Any,
sender: dict[str, Any] | None,
) -> str | None:
if not isinstance(forkee, dict):
return None
actor = (sender or {}).get("login") or "未知"
new_repo = forkee.get("full_name") or forkee.get("name") or "未知"
html_url = forkee.get("html_url")
message_lines = [
f"[GitHub Webhook] 仓库 {repo} 被 Fork",
f"新仓库: {new_repo}",
f"触发人: {actor}",
]
if html_url:
message_lines.append(f"链接: {html_url}")
return "\n".join(message_lines)
def format_webhook_pr_review_comment_message(
repo: str,
action: str,
pull_request: dict[str, Any],
comment: dict[str, Any],
sender: dict[str, Any] | None,
) -> str | None:
action_labels = {
"created": "新增审查评论",
"edited": "编辑审查评论",
"deleted": "删除审查评论",
}
label = action_labels.get(action)
if not label:
return None
actor = (sender or {}).get("login")
actor = actor or comment.get("user", {}).get("login") or "未知"
message_lines = [
f"[GitHub Webhook] 仓库 {repo} 的 PR 审查评论",
f"PR #{pull_request.get('number', '?')} {pull_request.get('title', '')}",
f"事件: {label}",
f"触发人: {actor}",
]
if action != "deleted":
body = comment.get("body", "")
if body:
message_lines.append("评论内容:")
message_lines.append(truncate_text(body))
url = comment.get("html_url") or pull_request.get("html_url")
if url:
message_lines.append(f"链接: {url}")
return "\n".join(line for line in message_lines if line)
def format_webhook_pr_review_message(
repo: str,
action: str,
pull_request: dict[str, Any],
review: dict[str, Any],
sender: dict[str, Any] | None,
) -> str | None:
action_labels = {
"submitted": "提交审查",
"edited": "编辑审查",
"dismissed": "撤销审查",
}
label = action_labels.get(action)
if not label:
return None
actor = (sender or {}).get("login")
actor = actor or review.get("user", {}).get("login") or "未知"
review_state = review.get("state", "").upper()
message_lines = [
f"[GitHub Webhook] 仓库 {repo} 的 PR 审查",
f"PR #{pull_request.get('number', '?')} {pull_request.get('title', '')}",
f"事件: {label}",
f"审查状态: {review_state or 'N/A'}",
f"触发人: {actor}",
]
body = review.get("body", "")
if body:
message_lines.append("审查内容:")
message_lines.append(truncate_text(body))
url = review.get("html_url") or pull_request.get("html_url")
if url:
message_lines.append(f"链接: {url}")
return "\n".join(line for line in message_lines if line)
def format_webhook_pr_review_thread_message(
repo: str,
action: str,
pull_request: dict[str, Any],
thread: dict[str, Any],
sender: dict[str, Any] | None,
) -> str | None:
action_labels = {
"created": "创建审查线程",
"resolved": "已解决审查线程",
"unresolved": "重新打开审查线程",
}
label = action_labels.get(action)
if not label:
return None
actor = (sender or {}).get("login") or "未知"
comments = thread.get("comments")
first_comment = comments[0] if isinstance(comments, list) and comments else {}
body = first_comment.get("body", "")
message_lines = [
f"[GitHub Webhook] 仓库 {repo} 的 PR 审查线程",
f"PR #{pull_request.get('number', '?')} {pull_request.get('title', '')}",
f"事件: {label}",
f"触发人: {actor}",
]
if body:
message_lines.append("讨论内容:")
message_lines.append(truncate_text(body))
url = thread.get("html_url") or pull_request.get("html_url")
if url:
message_lines.append(f"链接: {url}")
return "\n".join(line for line in message_lines if line)
def format_webhook_star_message(
repo: str,
action: str,
sender: dict[str, Any] | None,
) -> str | None:
action_labels = {
"created": "收藏了仓库",
"deleted": "取消收藏仓库",
}
label = action_labels.get(action)
if not label:
return None
actor = (sender or {}).get("login") or "未知"
message_lines = [
"[GitHub Webhook] Star 事件",
f"仓库: {repo}",
f"触发人: {actor}",
f"事件: {label}",
]
return "\n".join(message_lines)
def format_webhook_create_message(
repo: str,
payload: dict[str, Any],
sender: dict[str, Any] | None,
) -> str | None:
ref_type = payload.get("ref_type")
if not ref_type:
return None
ref = payload.get("ref") or ""
actor = (sender or {}).get("login") or "未知"
message_lines = [
"[GitHub Webhook] 创建事件",
f"仓库: {repo}",
f"触发人: {actor}",
]
if ref_type == "repository":
message_lines.append("创建了新的仓库版本")
elif ref_type == "branch":
message_lines.append(f"创建分支: {ref}")
elif ref_type == "tag":
message_lines.append(f"创建标签: {ref}")
else:
message_lines.append(f"创建 {ref_type}: {ref}")
return "\n".join(message_lines)
def format_issue_details(repo: str, issue_data: dict[str, Any]) -> str:
if "pull_request" in issue_data:
return f"#{issue_data['number']} 是一个 PR,请使用 /ghpr 命令查看详情"
created_str = issue_data["created_at"].replace("Z", "+00:00")
updated_str = issue_data["updated_at"].replace("Z", "+00:00")
created_at = datetime.fromisoformat(created_str)
updated_at = datetime.fromisoformat(updated_str)
status = "开启" if issue_data["state"] == "open" else "已关闭"
labels = ", ".join([label["name"] for label in issue_data.get("labels", [])])
result = (
f"🔍 Issue 详情 | {repo}#{issue_data['number']}\n"
f"标题: {issue_data['title']}\n"
f"状态: {status}\n"
f"创建者: {issue_data['user']['login']}\n"
f"创建时间: {created_at.strftime('%Y-%m-%d %H:%M:%S')}\n"
f"更新时间: {updated_at.strftime('%Y-%m-%d %H:%M:%S')}\n"
)
if labels:
result += f"标签: {labels}\n"
if issue_data.get("assignees") and len(issue_data["assignees"]) > 0:
assignees = ", ".join(
[assignee["login"] for assignee in issue_data["assignees"]]
)
result += f"指派给: {assignees}\n"
if issue_data.get("body"):
body = issue_data["body"]
if len(body) > 200:
body = body[:197] + "..."
result += f"\n内容概要:\n{body}\n"
result += f"\n链接: {issue_data['html_url']}"
return result
def format_pr_details(repo: str, pr_data: dict[str, Any]) -> str:
created_str = pr_data["created_at"].replace("Z", "+00:00")
updated_str = pr_data["updated_at"].replace("Z", "+00:00")
created_at = datetime.fromisoformat(created_str)
updated_at = datetime.fromisoformat(updated_str)
status = pr_data["state"]
if status == "open":
status = "开启"
elif status == "closed":
status = "已关闭" if not pr_data.get("merged") else "已合并"
labels = ", ".join([label["name"] for label in pr_data.get("labels", [])])
result = (
f"🔀 PR 详情 | {repo}#{pr_data['number']}\n"
f"标题: {pr_data['title']}\n"
f"状态: {status}\n"
f"创建者: {pr_data['user']['login']}\n"
f"创建时间: {created_at.strftime('%Y-%m-%d %H:%M:%S')}\n"
f"更新时间: {updated_at.strftime('%Y-%m-%d %H:%M:%S')}\n"
f"分支: {pr_data['head']['label']} → {pr_data['base']['label']}\n"
)
if labels:
result += f"标签: {labels}\n"
if pr_data.get("requested_reviewers") and len(pr_data["requested_reviewers"]) > 0:
reviewers = ", ".join(
[reviewer["login"] for reviewer in pr_data["requested_reviewers"]]
)
result += f"审阅者: {reviewers}\n"
if pr_data.get("assignees") and len(pr_data["assignees"]) > 0:
assignees = ", ".join(
[assignee["login"] for assignee in pr_data["assignees"]]
)
result += f"指派给: {assignees}\n"
result += (
f"增加: +{pr_data.get('additions', 0)} 行\n"
f"删除: -{pr_data.get('deletions', 0)} 行\n"
f"文件变更: {pr_data.get('changed_files', 0)} 个\n"
)
if pr_data.get("body"):
body = pr_data["body"]
if len(body) > 200:
body = body[:197] + "..."
result += f"\n内容概要:\n{body}\n"
result += f"\n链接: {pr_data['html_url']}"
return result
def format_webhook_push_message(
repo: str,
payload: dict[str, Any],
sender: dict[str, Any] | None,
) -> str | None:
ref = payload.get("ref", "")
branch_or_tag = ref.split("/")[-1] if "/" in ref else ref
commits = payload.get("commits", [])
if not commits:
return None
actor = (sender or {}).get("login") or "未知"
message_lines = [
f"[GitHub Webhook] 仓库 {repo} 代码推送",
f"分支: {branch_or_tag}",
f"触发人: {actor}",
f"新增 {len(commits)} 个提交:"
]
for commit in commits[:3]:
msg = commit.get("message", "").split("\n")[0]
sha = commit.get("id", "")[:7]
message_lines.append(f"- {sha} {msg}")
if len(commits) > 3:
message_lines.append(f"... 等共 {len(commits)} 个提交")
compare_url = payload.get("compare")
if compare_url:
message_lines.append(f"链接: {compare_url}")
return "\n".join(message_lines)
def format_webhook_release_message(
repo: str,
action: str,
release: dict[str, Any],
sender: dict[str, Any] | None,
) -> str | None:
action_labels = {
"published": "发布了新版本",
"created": "创建了版本",
"edited": "更新了版本",
"deleted": "删除了版本",
"prereleased": "发布了预发布版本",
"released": "发布了正式版本",
}
label = action_labels.get(action)
if not label:
return None
actor = (sender or {}).get("login") or release.get("author", {}).get("login") or "未知"
tag_name = release.get("tag_name", "未知版本")
name = release.get("name") or tag_name
message_lines = [
f"[GitHub Webhook] 仓库 {repo} Release 更新",
f"版本: {name} ({tag_name})",
f"事件: {label}",
f"触发人: {actor}",
]
body = release.get("body", "")
if body and action in ["published", "released", "prereleased"]:
message_lines.append("发布说明:")
message_lines.append(truncate_text(body, limit=200))
if release.get("html_url"):
message_lines.append(f"链接: {release['html_url']}")
return "\n".join(message_lines)