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
|
from abc import ABC, abstractmethod
from enum import Enum
class NotificationType(Enum):
ISSUE_TRIAGED = "issue_triaged"
PR_REVIEWED = "pr_reviewed"
SECURITY_ALERT = "security_alert"
class NotificationService(ABC):
"""通知サービスの抽象基底クラス"""
@abstractmethod
def send(self, notification_type: NotificationType, data: dict) -> bool:
pass
class SlackNotificationService(NotificationService):
"""Slack通知サービス"""
def __init__(self, webhook_url: str):
self.webhook_url = webhook_url
def send(self, notification_type: NotificationType, data: dict) -> bool:
message = self._build_message(notification_type, data)
response = requests.post(self.webhook_url, json=message)
return response.status_code == 200
def _build_message(self, notification_type: NotificationType, data: dict) -> dict:
if notification_type == NotificationType.ISSUE_TRIAGED:
return self._build_triage_message(data)
elif notification_type == NotificationType.PR_REVIEWED:
return self._build_review_message(data)
elif notification_type == NotificationType.SECURITY_ALERT:
return self._build_security_alert_message(data)
def _build_triage_message(self, data: dict) -> dict:
issue = data['issue']
triage = data['triage_result']
repo = data['repository']
priority_emoji = {
'high': ':red_circle:',
'medium': ':large_orange_circle:',
'low': ':large_green_circle:'
}
return {
"blocks": [
{
"type": "header",
"text": {
"type": "plain_text",
"text": "New Issue Triaged"
}
},
{
"type": "section",
"fields": [
{
"type": "mrkdwn",
"text": f"*Repository:*\n{repo['full_name']}"
},
{
"type": "mrkdwn",
"text": f"*Issue:*\n<{issue['html_url']}|#{issue['number']}>"
},
{
"type": "mrkdwn",
"text": f"*Priority:*\n{priority_emoji.get(triage['priority'], '')} {triage['priority']}"
},
{
"type": "mrkdwn",
"text": f"*Labels:*\n{', '.join(triage['labels'])}"
}
]
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": f"*Summary:* {triage['summary']}"
}
}
]
}
def _build_review_message(self, data: dict) -> dict:
pr = data['pull_request']
review = data['review_result']
repo = data['repository']
status_emoji = ':white_check_mark:' if review.approved else ':x:'
blocks = [
{
"type": "header",
"text": {
"type": "plain_text",
"text": "PR Review Completed"
}
},
{
"type": "section",
"fields": [
{
"type": "mrkdwn",
"text": f"*Repository:*\n{repo['full_name']}"
},
{
"type": "mrkdwn",
"text": f"*PR:*\n<{pr['html_url']}|#{pr['number']}>"
},
{
"type": "mrkdwn",
"text": f"*Status:*\n{status_emoji} {'Approved' if review.approved else 'Changes Requested'}"
},
{
"type": "mrkdwn",
"text": f"*Comments:*\n{len(review.comments)} issues found"
}
]
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": f"*Summary:* {review.summary}"
}
}
]
# セキュリティ問題がある場合は警告を追加
if review.security_issues:
blocks.append({
"type": "section",
"text": {
"type": "mrkdwn",
"text": f":warning: *Security Issues:*\n" + '\n'.join(f"• {issue}" for issue in review.security_issues)
}
})
return {"blocks": blocks}
def _build_security_alert_message(self, data: dict) -> dict:
return {
"blocks": [
{
"type": "header",
"text": {
"type": "plain_text",
"text": ":rotating_light: Security Alert"
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": f"*{data['title']}*\n{data['description']}"
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": f"<{data['url']}|View Details>"
}
}
]
}
class DiscordNotificationService(NotificationService):
"""Discord通知サービス"""
def __init__(self, webhook_url: str):
self.webhook_url = webhook_url
def send(self, notification_type: NotificationType, data: dict) -> bool:
message = self._build_message(notification_type, data)
response = requests.post(self.webhook_url, json=message)
return response.status_code in [200, 204]
def _build_message(self, notification_type: NotificationType, data: dict) -> dict:
if notification_type == NotificationType.ISSUE_TRIAGED:
return self._build_triage_embed(data)
elif notification_type == NotificationType.PR_REVIEWED:
return self._build_review_embed(data)
elif notification_type == NotificationType.SECURITY_ALERT:
return self._build_security_alert_embed(data)
def _build_triage_embed(self, data: dict) -> dict:
issue = data['issue']
triage = data['triage_result']
repo = data['repository']
priority_color = {
'high': 0xFF0000,
'medium': 0xFFA500,
'low': 0x00FF00
}
return {
"embeds": [{
"title": f"Issue Triaged: {issue['title']}",
"url": issue['html_url'],
"color": priority_color.get(triage['priority'], 0x808080),
"fields": [
{"name": "Repository", "value": repo['full_name'], "inline": True},
{"name": "Priority", "value": triage['priority'], "inline": True},
{"name": "Labels", "value": ', '.join(triage['labels']), "inline": False},
{"name": "Summary", "value": triage['summary'], "inline": False}
],
"footer": {"text": "Codex Auto-Triage"}
}]
}
def _build_review_embed(self, data: dict) -> dict:
pr = data['pull_request']
review = data['review_result']
repo = data['repository']
color = 0x00FF00 if review.approved else 0xFF0000
return {
"embeds": [{
"title": f"PR Review: {pr['title']}",
"url": pr['html_url'],
"color": color,
"fields": [
{"name": "Repository", "value": repo['full_name'], "inline": True},
{"name": "Status", "value": "Approved" if review.approved else "Changes Requested", "inline": True},
{"name": "Issues Found", "value": str(len(review.comments)), "inline": True},
{"name": "Summary", "value": review.summary, "inline": False}
],
"footer": {"text": "Codex Auto-Review"}
}]
}
def _build_security_alert_embed(self, data: dict) -> dict:
return {
"embeds": [{
"title": f"Security Alert: {data['title']}",
"url": data['url'],
"color": 0xFF0000,
"description": data['description'],
"footer": {"text": "Codex Security Scanner"}
}]
}
# 通知マネージャー
class NotificationManager:
"""複数の通知サービスを管理"""
def __init__(self):
self.services: list[NotificationService] = []
def add_service(self, service: NotificationService):
self.services.append(service)
def notify_all(self, notification_type: NotificationType, data: dict):
results = []
for service in self.services:
try:
result = service.send(notification_type, data)
results.append(result)
except Exception as e:
print(f"Notification failed: {e}")
results.append(False)
return all(results)
# 通知ヘルパー関数
def send_triage_notification(issue: dict, triage_result: dict, repository: dict):
"""トリアージ結果の通知を送信"""
manager = NotificationManager()
slack_url = os.environ.get('SLACK_WEBHOOK_URL')
if slack_url:
manager.add_service(SlackNotificationService(slack_url))
discord_url = os.environ.get('DISCORD_WEBHOOK_URL')
if discord_url:
manager.add_service(DiscordNotificationService(discord_url))
manager.notify_all(NotificationType.ISSUE_TRIAGED, {
'issue': issue,
'triage_result': triage_result,
'repository': repository
})
def send_review_notification(pr: dict, review_result: ReviewResult, repository: dict):
"""レビュー結果の通知を送信"""
manager = NotificationManager()
slack_url = os.environ.get('SLACK_WEBHOOK_URL')
if slack_url:
manager.add_service(SlackNotificationService(slack_url))
discord_url = os.environ.get('DISCORD_WEBHOOK_URL')
if discord_url:
manager.add_service(DiscordNotificationService(discord_url))
manager.notify_all(NotificationType.PR_REVIEWED, {
'pull_request': pr,
'review_result': review_result,
'repository': repository
})
|