Python 统计Jira的bug 并发送邮件
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("邮件发送失败")