Skip to content

Commit f0dd907

Browse files
author
quanbisen
committed
feat: 添加自定webhook通知支持
1 parent 9e7ba46 commit f0dd907

File tree

7 files changed

+77
-12
lines changed

7 files changed

+77
-12
lines changed

biz/entity/review_entity.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class MergeRequestReviewEntity:
22
def __init__(self, project_name: str, author: str, source_branch: str, target_branch: str, updated_at: int,
3-
commits: list, score: float, url: str, review_result: str, url_slug: str):
3+
commits: list, score: float, url: str, review_result: str, url_slug: str, webhook_data: dict):
44
self.project_name = project_name
55
self.author = author
66
self.source_branch = source_branch
@@ -11,6 +11,7 @@ def __init__(self, project_name: str, author: str, source_branch: str, target_br
1111
self.url = url
1212
self.review_result = review_result
1313
self.url_slug = url_slug
14+
self.webhook_data = webhook_data
1415

1516
@property
1617
def commit_messages(self):
@@ -20,7 +21,7 @@ def commit_messages(self):
2021

2122
class PushReviewEntity:
2223
def __init__(self, project_name: str, author: str, branch: str, updated_at: int, commits: list, score: float,
23-
review_result: str, url_slug: str):
24+
review_result: str, url_slug: str, webhook_data: dict):
2425
self.project_name = project_name
2526
self.author = author
2627
self.branch = branch
@@ -29,6 +30,7 @@ def __init__(self, project_name: str, author: str, branch: str, updated_at: int,
2930
self.score = score
3031
self.review_result = review_result
3132
self.url_slug = url_slug
33+
self.webhook_data = webhook_data
3234

3335
@property
3436
def commit_messages(self):

biz/event/event_manager.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ def on_merge_request_reviewed(mr_review_entity: MergeRequestReviewEntity):
3232
{mr_review_entity.review_result}
3333
"""
3434
notifier.send_notification(content=im_msg, msg_type='markdown', title='Merge Request Review',
35-
project_name=mr_review_entity.project_name,
36-
url_slug=mr_review_entity.url_slug)
35+
project_name=mr_review_entity.project_name, url_slug=mr_review_entity.url_slug,
36+
webhook_data=mr_review_entity.webhook_data)
3737

3838
# 记录到数据库
3939
ReviewService().insert_mr_review_log(mr_review_entity)
@@ -58,9 +58,9 @@ def on_push_reviewed(entity: PushReviewEntity):
5858

5959
if entity.review_result:
6060
im_msg += f"#### AI Review 结果: \n {entity.review_result}\n\n"
61-
notifier.send_notification(content=im_msg, msg_type='markdown',
62-
title=f"{entity.project_name} Push Event", project_name=entity.project_name,
63-
url_slug=entity.url_slug)
61+
notifier.send_notification(content=im_msg, msg_type='markdown',title=f"{entity.project_name} Push Event",
62+
project_name=entity.project_name, url_slug=entity.url_slug,
63+
webhook_data=entity.webhook_data)
6464

6565
# 记录到数据库
6666
ReviewService().insert_push_review_log(entity)

biz/queue/worker.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ def handle_push_event(webhook_data: dict, gitlab_token: str, gitlab_url: str, gi
4949
score=score,
5050
review_result=review_result,
5151
url_slug=gitlab_url_slug,
52+
webhook_data=webhook_data,
5253
))
5354

5455
except Exception as e:
@@ -115,6 +116,7 @@ def handle_merge_request_event(webhook_data: dict, gitlab_token: str, gitlab_url
115116
url=webhook_data['object_attributes']['url'],
116117
review_result=review_result,
117118
url_slug=gitlab_url_slug,
119+
webhook_data=webhook_data,
118120
)
119121
)
120122

@@ -160,6 +162,7 @@ def handle_github_push_event(webhook_data: dict, github_token: str, github_url:
160162
score=score,
161163
review_result=review_result,
162164
url_slug=github_url_slug,
165+
webhook_data=webhook_data,
163166
))
164167

165168
except Exception as e:
@@ -225,7 +228,8 @@ def handle_github_pull_request_event(webhook_data: dict, github_token: str, gith
225228
score=CodeReviewer.parse_review_score(review_text=review_result),
226229
url=webhook_data['pull_request']['html_url'],
227230
review_result=review_result,
228-
url_slug=github_url_slug
231+
url_slug=github_url_slug,
232+
webhook_data=webhook_data,
229233
))
230234

231235
except Exception as e:

biz/utils/im/feishu.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import json
21
import requests
32
import os
4-
import re
53
from biz.utils.log import logger
64

75

biz/utils/im/notifier.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
from biz.utils.im.dingtalk import DingTalkNotifier
22
from biz.utils.im.feishu import FeishuNotifier
3+
from biz.utils.im.webhook import ExtraWebhookNotifier
34
from biz.utils.im.wecom import WeComNotifier
45

56

6-
def send_notification(content, msg_type='text', title="通知", is_at_all=False, project_name=None, url_slug=None):
7+
def send_notification(content, msg_type='text', title="通知", is_at_all=False, project_name=None, url_slug=None,
8+
webhook_data: dict={}):
79
"""
810
发送通知消息到配置的平台(钉钉和企业微信)
911
:param content: 消息内容
1012
:param msg_type: 消息类型,支持text和markdown
1113
:param title: 消息标题(markdown类型时使用)
1214
:param is_at_all: 是否@所有人
1315
:param url_slug: 由gitlab服务器的url地址(如:http://www.gitlab.com)转换成的slug格式,如: www_gitlab_com
16+
:param webhook_data: push event、merge event的数据内容
1417
"""
1518
# 钉钉推送
1619
dingtalk_notifier = DingTalkNotifier()
1720
dingtalk_notifier.send_message(content=content, msg_type=msg_type, title=title, is_at_all=is_at_all,
18-
project_name=project_name, url_slug=url_slug)
21+
project_name=project_name, url_slug=url_slug)
1922

2023
# 企业微信推送
2124
wecom_notifier = WeComNotifier()
@@ -26,3 +29,15 @@ def send_notification(content, msg_type='text', title="通知", is_at_all=False,
2629
feishu_notifier = FeishuNotifier()
2730
feishu_notifier.send_message(content=content, msg_type=msg_type, title=title, is_at_all=is_at_all,
2831
project_name=project_name, url_slug=url_slug)
32+
33+
# 额外自定义webhook通知
34+
extra_webhook_notifier = ExtraWebhookNotifier()
35+
system_data = {
36+
"content": content,
37+
"msg_type": msg_type,
38+
"title": title,
39+
"is_at_all": is_at_all,
40+
"project_name": project_name,
41+
"url_slug": url_slug
42+
}
43+
extra_webhook_notifier.send_message(system_data=system_data, webhook_data=webhook_data)

biz/utils/im/webhook.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import os
2+
from biz.utils.log import logger
3+
import requests
4+
5+
6+
class ExtraWebhookNotifier:
7+
def __init__(self, webhook_url=None):
8+
"""
9+
初始化ExtraWebhook通知器
10+
:param webhook_url: 自定义webhook地址
11+
"""
12+
self.default_webhook_url = webhook_url or os.environ.get('EXTRA_WEBHOOK_URL', '')
13+
self.enabled = os.environ.get('EXTRA_WEBHOOK_ENABLED', '0') == '1'
14+
15+
def send_message(self, system_data: dict, webhook_data: dict):
16+
"""
17+
发送额外自定义webhook消息
18+
:param system_data: 系统消息内容
19+
:param webhook_data: github、gitlab的push event、merge event的原始数据
20+
"""
21+
if not self.enabled:
22+
logger.info("ExtraWebhook推送未启用")
23+
return
24+
25+
try:
26+
data = {
27+
"ai_codereview_data": system_data,
28+
"webhook_data": webhook_data
29+
}
30+
response = requests.post(
31+
url=self.default_webhook_url,
32+
json=data,
33+
headers={'Content-Type': 'application/json'}
34+
)
35+
36+
if response.status_code != 200:
37+
logger.error(f"ExtraWebhook消息发送失败! webhook_url:{self.default_webhook_url}, error_msg:{response.text}")
38+
return
39+
40+
except Exception as e:
41+
logger.error(f"ExtraWebhook消息发送失败! ", e)

conf/.env.dist

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ WECOM_WEBHOOK_URL=https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxx
5050
FEISHU_ENABLED=0
5151
FEISHU_WEBHOOK_URL=https://open.feishu.cn/open-apis/bot/v2/hook/xxx
5252

53+
#自定义webhook配置,使用场景:通过飞书发送应用消息可以实现Push评审通知到提交人,在自定义webhook里可以实现各种定制通知功能
54+
#参数EXTRA_WEBHOOK_URL接收POST请求,data={ai_codereview_data: {}, webhook_data: {}},ai_codereview_data为本系统通知的数据,webhook_data为原github、gitlab hook触发的数据
55+
EXTRA_WEBHOOK_ENABLED=0
56+
EXTRA_WEBHOOK_URL=https://xxx/xxx
57+
5358
#日志配置
5459
LOG_FILE=log/app.log
5560
LOG_MAX_BYTES=10485760

0 commit comments

Comments
 (0)