Python 统计Jira的bug 并发送邮件功能
作者:yappleorange 时间:2021-03-24 05:44:24
1.首先在pycharm上使用pip安装
pip install html-table
pip install jira
2.初始化发件人邮箱,账号,密码
# 发件人邮箱账号
my_sender = 'username@xxx.com.cn'
# user登录邮箱的用户名,password登录邮箱的密码(授权码,即客户端密码,非网页版登录密码),但用腾讯邮箱的登录密码也能登录成功
my_pass = 'xxxxx'
# 收件人邮箱账号
my_users=['username@xxx.com.cn']
3.登录Jira
class JiraTool:
#初始化
def __init__(self):
self.server = 'http://ip:5500' //连接Jira的Ip地址
self.basic_auth = ('username', 'password') //连接Jira的账户和密码
self.jiraClinet = None
4.登录Jira
def login(self):
self.jiraClinet = JIRA(server=self.server, basic_auth=self.basic_auth)
if self.jiraClinet != None:
print("登录成功!")
return True
else:
return False
5.获取Jira问题列表
def get_issue_list_by_jql(self, jql):
issue_list = []
issue_key_list = self.jiraClinet.search_issues(jql_str=jql,startAt=0,maxResults=1000) //Jira默认统计50条,maxResults设置大小
for key_list in issue_key_list:
issue = self.jiraClinet.issue(key_list.key)
issue_list.append(issue)
# print(issue.key) #关键字
# print(issue.fields.summary) #bug描述
# print(issue.fields.status) bug状态
# print(issue.fields.assignee) #经办人
# print(issue.fields.components[0].name) #模块
# print(issue.fields.priority) #优先级
return issue_list
6.创建一个表格
def gen_new_bug_caption_str(issue_list):
dict = {}
for issue in issue_list:
dict[issue.fields.status.name] = dict.get(issue.fields.status.name, 0) + 1
#dict[issue.key.split('-')[0]] = dict.get(issue.key.split('-')[0],0) + 1
caption_str = '近一周共计新增bug' + str(len(issue_list)) + '个。 已关闭:' + str(dict.get('已关闭')) + '个。 已解决待关闭:' + str(dict.get('已解决')) + '个。 待处理:' +str(dict.get('待处理')) + '个'
#print(caption_str)
return caption_str
7.生成html
#标题样式
# table.caption.set_style({'font-size':'15px','align':'left'})
table.caption.set_style({'font-size':'15px'})
# 表格样式,即<table>标签样式
table.set_style({
'border-collapse':'collapse',
'word-break':'keep-all',
'white-space':'nowrap',
'font-size':'14px'
})
#设置每个单元格的样式,主要是规定边框样式:
table.set_cell_style({
'border-color':'#000',
'border-width':'1px',
'border-style':'solid',
'padding':'5px',
})
#设置表头单元格样式,规定颜色,字体大小,以及填充大小:
#表头样式
table.set_header_row_style({
'color':'#fff',
'background-color':'#696969',
'font-size':'18px',
})
#覆盖表单单元格字体样式
table.set_header_cell_style({
'padding':'15px',
})
#遍历数据行,根据不同状态设置背景颜色
for row in table.iter_data_rows():
if row[1].value in "待处理":
row[1].set_style({
'background-color': '#FFB6C1',
})
if row[1].value in "已解决":
row[1].set_style({
'background-color': '#E1FFFF',
})
if row[1].value in "已关闭":
row[1].set_style({
'background-color': '#90EE99',
})
if row[1].value in "重新打开":
row[1].set_style({
'background-color': '#DC143C',
})
if row[1].value in "开发中":
row[1].set_style({
'background-color': '#f7d7a7',
})
#生成HTML文本:
html = table.to_html()
# print(html)
return html
8.发送邮件
def sendmail(html):
ret=True
try:
# 邮件内容
msg=MIMEText(html,'html','utf-8')
# 括号里的对应发件人邮箱昵称、发件人邮箱账号
msg['From']=formataddr(["张三",my_sender])
# 括号里的对应收件人邮箱昵称、收件人邮箱账号
#msg['To']=formataddr(["李四",my_user])
# 邮件的主题
msg['Subject']="bug情况统计"
server=smtplib.SMTP_SSL("smtp.exmail.qq.com", 465)
# 登录服务器,括号中对应的是发件人邮箱账号、邮箱密码
server.login(my_sender, my_pass)
# 发送邮件,括号中对应的是发件人邮箱账号、收件人邮箱账号、发送邮件
server.sendmail(my_sender, my_users, msg.as_string())
# 关闭连接
server.quit()
# 如果 try 中的语句没有执行,则会执行下面的 ret=False
except Exception:
ret=False
return ret
9.调试
new_bug_jql = "project in (AAA, BBB, CCC) AND issuetype in (Bug, 缺陷) AND created >= -1w ORDER BY component ASC, assignee ASC, priority DESC, updated DESC"
old_bug_jql = "project in (AAA, BBB, CCC) AND issuetype in (Bug, 缺陷) AND status in (待处理, 开发中, Reopened) AND created <= -1w ORDER BY component ASC, assignee ASC, priority DESC, updated DESC"
jiraTool = JiraTool()
jiraTool.login()
new_issue_list = jiraTool.get_issue_list_by_jql(new_bug_jql)
new_bug_caption_str = gen_new_bug_caption_str(new_issue_list)
new_bug_html = gen_html_table(new_issue_list,new_bug_caption_str)
# print(new_bug_html)
old_issue_list = jiraTool.get_issue_list_by_jql(old_bug_jql)
old_bug_html = gen_html_table(old_issue_list, "超过一周未关闭bug")
eamil_html = (new_bug_html + "<br/><br/><br/>" + old_bug_html).replace(">", ">").replace(""", "\"").replace("<", "<")
# print(eamil_html)
ret=sendmail(eamil_html)
if ret:
print("邮件发送成功")
else:
print("邮件发送失败")
来源:https://www.cnblogs.com/yappleorange/p/15763268.html
标签:Python,统计,Jira
0
投稿
猜你喜欢
python利用opencv实现SIFT特征提取与匹配
2021-07-22 19:08:46
aspjpeg组件安装问题
2008-09-27 17:52:00
301转向代码合集
2008-03-20 10:12:00
ASP平台如何打造金牌英文网站
2008-04-10 08:47:00
asp分段插入数据库
2010-07-02 13:13:00
Oracle 添加用户并赋权,修改密码,解锁,删除用户的方法
2009-10-14 11:37:00
PHP的mysqli_select_db()函数讲解
2023-06-07 18:59:44
php ajax异步读取rss文档数据
2023-10-17 19:59:02
基于Python实现本地音乐播放器的制作
2022-05-11 16:37:46
Flask框架通过Flask_login实现用户登录功能示例
2021-07-19 07:15:38
php生成随机数/生成随机字符串的方法小结【5种方法】
2023-09-05 20:23:21
ASP/php/aspx生成静态网页的方法
2007-10-25 12:01:00
Python 实现集合Set的示例
2021-10-04 06:27:19
Python装饰器的应用场景及实例用法
2022-06-24 16:09:03
JavaScript Length 字符长度函数
2008-12-12 12:29:00
Python实现将通信达.day文件读取为DataFrame
2022-07-31 23:33:20
python实现复制文件到指定目录
2022-09-25 20:53:12
DW中如何使用Library
2007-02-03 11:39:00
Python Tkinter Menu组件详解
2021-07-24 06:51:18
python3.x zip用法小结
2023-08-13 05:25:05