55
66class Reply :
77 def __init__ (self , config ):
8+ """
9+ 初始化 Reply 实例
10+ Args:
11+ config (dict): 配置字典,包含初始化所需的配置信息
12+ """
813 if not isinstance (config , dict ):
914 raise Exception ('Reply config should be a dict.' )
1015 if 'type' not in config :
@@ -14,68 +19,98 @@ def __init__(self, config):
1419 self .lock = threading .Lock ()
1520
1621 def add_reply (self , reply_msg ):
17- # reply 格式检查:title, content 必选
22+ """
23+ 添加回复消息
24+ Args:
25+ reply_msg (dict): 回复消息字典,必须包含 'content' 字段
26+ """
1827 if 'content' not in reply_msg :
1928 raise Exception ('Reply format error, title and content are required.' )
20- if 'priority' in reply_msg :
21- if not isinstance (reply_msg ['priority' ], int ):
22- raise Exception ('Reply format error, priority should be an integer.' )
23- elif reply_msg ['priority' ] == 0 :
29+ if 'msg_type' in reply_msg :
30+ if not isinstance (reply_msg ['msg_type' ], str ):
31+ raise Exception ('Reply format error, msg_type should be a string.' )
32+ reply_msg ['msg_type' ] = [t .strip () for t in reply_msg ['msg_type' ].split (',' )]
33+ if 'SINGLE' in reply_msg ['msg_type' ]:
2434 self .send_single_message (reply_msg )
2535 return
26-
36+ else :
37+ reply_msg ['msg_type' ] = ['NORMAL' ]
38+ if 'target' not in reply_msg :
39+ reply_msg ['target' ] = 'all'
40+ if 'title' not in reply_msg :
41+ reply_msg ['title' ] = ''
42+ if 'group_id' not in reply_msg :
43+ reply_msg ['group_id' ] = 0
2744 with self .lock : # 加锁
2845 self .replies .append (reply_msg )
2946
3047 def send (self ):
31- msg_list = {}
32- main_msg = None
48+ """
49+ 实时发送单条消息
50+ Args:
51+ reply (dict): 单条回复消息字典,必须包含 'content' 字段
52+ Returns:
53+ bool: 表示发送是否成功
54+ """
55+ msg_groups = {}
56+ main_msg_group = []
3357 with self .lock : # 加锁
3458 # 发送所有消息的逻辑
3559 for msg in self .replies :
36- if msg ['title' ] == '__MAIN_REVIEW__' :
37- main_msg = msg
60+ if 'MAIN' in msg ['msg_type' ]:
61+ # msg 加入到 main_msg 中
62+ main_msg_group .append (msg )
3863 continue
39- self .__parse_msg (msg , msg_list )
64+ self .__parse_msg (msg , msg_groups )
4065
4166 self .replies = [] # 清空已发送的消息
42- if main_msg :
43- self .__parse_msg (main_msg , msg_list )
67+ for main_msg in main_msg_group :
68+ self .__parse_msg (main_msg , msg_groups )
4469 ret = True
45- for target , msg in msg_list .items ():
70+ for target , msg_group in msg_groups .items ():
4671 reply_target = ReplyFactory .get_reply_instance (target , self .config )
47- ret &= reply_target .send (msg )
72+ for msg in msg_group :
73+ ret &= reply_target .send (msg )
4874 return ret
4975
5076 def send_single_message (self , reply ):
5177 """
52- 实时发送消息
78+ 实时发送单条消息
79+
80+ Args:
81+ reply (dict): 单条回复消息字典,必须包含 'content' 字段
82+ Returns:
83+ bool: 表示发送是否成功
5384 """
5485 targets = [t .strip () for t in reply ['target' ].split (',' )]
5586 if 'all' in targets :
5687 targets = ReplyFactory .get_all_targets ()
5788 ret = True
5889 for target in targets :
5990 reply_target = ReplyFactory .get_reply_instance (target , self .config )
60- # 如果title不为__IGNORE__ or __MAIN_REVIEW__, 则发送带有标题的消息
61- if 'title' not in reply or reply ['title' ] not in ['__IGNORE__' , '__MAIN_REVIEW__' ]:
62- ret &= reply_target .send (f"## { reply ['title' ]} \n \n { reply ['content' ]} \n \n " )
63- else :
91+ if ('TITLE_IGNORE' in reply ['msg_type' ] or 'MAIN' in reply ['msg_type' ]
92+ or 'title' not in reply or not reply ['title' ]):
6493 ret &= reply_target .send (reply ['content' ])
94+ else :
95+ title = f"## { reply ['title' ]} \n \n " if 'title' in reply else ''
96+ ret &= reply_target .send (f"{ title } { reply ['content' ]} \n \n " )
6597 return ret
6698
67- def __parse_msg (self , msg , msg_list ):
99+ def __parse_msg (self , msg , msg_groups ):
68100 targets = [t .strip () for t in msg ['target' ].split (',' )]
69- if 'all' in targets :
101+ if 'target' not in msg or ' all' in targets :
70102 targets = ReplyFactory .get_all_targets ()
71103 for target in targets :
72- msg_list [target ] = msg_list .get (target , '' )
73- # 如果msg['title']不存在
74- if 'title' not in msg or msg ['title' ] in ['__IGNORE__' , '__MAIN_REVIEW__' ]:
75- msg_list [target ] = f"{ msg ['content' ]} \n \n " + msg_list [target ]
104+ if target not in msg_groups :
105+ msg_groups [target ] = {}
106+ if msg ['group_id' ] not in msg_groups [target ]:
107+ msg_groups [target ][msg ['group_id' ]] = []
108+ if ('TITLE_IGNORE' in msg ['msg_type' ] or 'MAIN' in msg ['msg_type' ]
109+ or 'title' not in msg or not msg ['title' ]):
110+ msg_groups [target ][msg ['group_id' ]].insert (0 , msg ['content' ])
76111 else :
77- msg_list [ target ] + = f"## { msg ['title' ]} \n \n { msg [ 'content' ] } \n \n "
78-
112+ title = f"## { msg ['title' ]} \n \n " if 'title' in msg else ''
113+ msg_groups [ target ][ msg [ 'group_id' ]]. append ( f" { title } { msg [ 'content' ] } \n \n " )
79114
80115if __name__ == '__main__' :
81116 reply = Reply ({'type' : 'merge_request' ,
0 commit comments