44
55
66class Reply :
7- def __init__ (self , project_id , merge_request_id ):
7+ def __init__ (self , project_id , merge_request_iid ):
88 self .replies = []
99 self .lock = threading .Lock ()
1010 self .project_id = project_id
11- self .merge_request_id = merge_request_id
11+ self .merge_request_iid = merge_request_iid
1212
1313 def add_reply (self , reply ):
1414 # reply 格式检查:title, content 必选
@@ -25,29 +25,44 @@ def add_reply(self, reply):
2525 self .replies .append (reply )
2626
2727 def send (self ):
28- markdown_message = ""
28+ msg_list = {}
2929 with self .lock : # 加锁
3030 # 发送所有消息的逻辑
3131 for reply in self .replies :
32- markdown_message += f"## { reply ['title' ]} \n \n { reply ['content' ]} \n \n "
32+ targets = [t .strip () for t in reply ['target' ].split (',' )]
33+ if 'all' in targets :
34+ targets = ReplyFactory .get_all_targets ()
35+ for target in targets :
36+ msg_list [target ] = msg_list .get (target , '' )
37+ msg_list [target ] += f"## { reply ['title' ]} \n \n { reply ['content' ]} \n \n "
3338 self .replies = [] # 清空已发送的消息
34- reply_target = ReplyFactory .get_reply_instance (reply ['target' ], self .project_id , self .merge_request_id )
35- return reply_target .send (markdown_message )
39+ ret = True
40+ for target , msg in msg_list .items ():
41+ reply_target = ReplyFactory .get_reply_instance (target , self .project_id , self .merge_request_iid )
42+ ret &= reply_target .send (msg )
43+ return ret
3644
3745 def send_single_message (self , reply ):
3846 """
3947 实时发送消息
4048 """
41- reply_target = ReplyFactory .get_reply_instance (reply ['target' ], self .project_id , self .merge_request_id )
42- return reply_target .send (reply ['content' ])
49+ targets = [t .strip () for t in reply ['target' ].split (',' )]
50+ if 'all' in targets :
51+ targets = ReplyFactory .get_all_targets ()
52+ ret = True
53+ for target in targets :
54+ reply_target = ReplyFactory .get_reply_instance (target , self .project_id , self .merge_request_iid )
55+ ret &= reply_target .send (f"## { reply ['title' ]} \n \n { reply ['content' ]} \n \n " )
56+ return ret
4357
4458
4559if __name__ == '__main__' :
4660 reply = Reply (9885 , 18 )
4761 threads = []
4862 for i in range (10 ):
4963 threads .append (threading .Thread (target = reply .add_reply , args = (
50- {'title' : f'title{ i } ' , 'content' : f'content{ i } ' , 'target' : 'gitlab' , 'priority' : i % 3 },)))
64+ {'title' : f'title{ i } ' , 'content' : f'content{ i } ' , 'target' : 'gitlab, dingtalk' , 'priority' : i % 3 },)))
65+
5166 for thread in threads :
5267 thread .start ()
5368 for thread in threads :
0 commit comments