python实现禅道BUG推送到钉钉
钉钉通知封装
pip install DingtalkChatbot --安装钉钉通知包
webhook,sign是钉钉机器人的标识
import time
import hmac
import hashlib
import base64
import urllib.parse
from dingtalkchatbot.chatbot import DingtalkChatbot
from utils.datas import data
class DingTalkSendmsg(object):
def __init__(self):
self.timestamp = str(round(time.time() * 1000))
self.sign = self.get_sign()
self.webhook = 'https://oapi.dingtalk.com/robot/send?access_token='+data['金华出租token']
self.webhook = self.webhook +'×tamp='+self.timestamp+'&sign='+self.sign
self.xiaoding = DingtalkChatbot(self.webhook)
def get_sign(self):
secret = data['金华出租屋sign']
secret_enc = secret.encode('utf-8')
string_to_sign = '{}\n{}'.format(self.timestamp, secret)
string_to_sign_enc = string_to_sign.encode('utf-8')
hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))
return sign
def send_markdown(self, title, msg, mobiles=None):
if not mobiles:
self.xiaoding.send_markdown(title=title, text=msg, is_at_all=True)
# self.xiaoding.send_markdown(title=title, text=msg, at_mobiles=['@all'])
elif isinstance(mobiles, list):
self.xiaoding.send_markdown(title=title, text=msg, at_mobiles=mobiles)
else:
raise TypeError("mobiles类型错误 不是list类型.")
消息推送
获取禅道数据,然后通过上面创建的钉钉类推送消息到钉钉
from utils.mysql_db import MysqlDB
from utils.DingTalk import DingTalkSendmsg
class Bugs():
""" bug 日清通知 """
def get_bugs(self) -> list:
BUGS = []
"""查询所有项目中未关闭的bug(不包含延期处理)"""
sql = "select count(a.id) as bugCount, b.realName , c.name, b.mobile from zt_bug as a " \
"inner join zt_user as b inner join zt_product as c where a.assignedTo = b.account " \
"and a.product = c.id and a.product = '9' and a.deleted = '0' and a.status != 'closed' " \
"and resolution != 'postponed'GROUP BY a.assignedTo"
# 统计出该项目中未关闭的bug对应的负责人,以及未关闭的总数
bugsCount = MysqlDB().pyodbcSql(sql)
BUGS.append(bugsCount)
print(BUGS)
return BUGS
def send_Notice_Msg(self):
"""发送钉钉通知@对应的负责人"""
for bugs in self.get_bugs():
for i in bugs:
print(i)
print(i[0])
text1 = "#### **禅道处理通知**\n 执 行 人: {0} \n\n 未处理bug数量: {1} 个 \n\n 所 属 项 目: {2} " \
"\n\n[点我查看](http://自己本地url/zentao/bug-browse-9-all-assigntome.html)".format(i[1], i[0], i[2])
if i[3] != "":
# 判断如果禅道中的用户有手机号码,才会@对方
DingTalkSendmsg().send_markdown(title="禅道处理通知", msg=text1, mobiles=[i[3]])
else:
DingTalkSendmsg().send_markdown(title="禅道处理通知", msg=text1)
if __name__ == '__main__':
Bugs().send_Notice_Msg()
实际效果

浙公网安备 33010602011771号