环境:python2.7
1、安装jira库
pip install jira
2、python连接jira服务器
#coding: UTF-8
from jira import JIRA
username = raw_input('请输入用户名: ')
password = raw_input('输入密码: ')
options = {
'verify': False,
'server': 'https://jira.XXXXXXX.com'}
jira = JIRA(options, basic_auth=(username, password))
3、查询issue
Jira 通过 JQL(JIRA Query Language,Jira 查询语言)进行搜索,语法类似于 SQL。可以通过 JQL 搜索符合条件的 issues,如通过
assignee = currentUser()
查找经办人为当前用户的 issues。
def searchIssues(jql, max_results=100):
''' Search issues
@param jql: JQL, str
@param max_results: max results, int, default 100
@return issues: result, list
'''
try:
issues = jira.search_issues(jql, maxResults=max_results)
return issues
except Exception as e:
print(e)
jql = '''
assignee = currentUser()
'''
issues = searchIssues(jql)
for issue in issues:
print('{0}: {1}'.format(issue.key, issue.fields.summary))
4、获取jira待修复缺陷数据
issues = jira.search_issues('project = XXX AND status = 待修复', maxResults=1000,
fields='assignee,summary,customfield_10121')
# 获取缺陷信息
for u in issues:
# print u #打印缺陷的key值
issue = jira.issue(u) # 根据key获取缺陷
ass = issue.fields.assignee # 获取缺陷经办人
print ass
sec = issue.fields.customfield_10121 # 获取缺陷严重性
print sec
sum = issue.fields.summary # 获取缺陷摘要
print sum
获取的待修复缺陷数据可以保存到excel文件中
#coding: UTF-8
from jira import JIRA
import xlwt
import os
path=os.getcwd()
#输入jira账号和密码
def input_user():
# 主流程开始
username = raw_input('请输入用户名: ')
password = raw_input('输入密码: ')
if (username == '' or password == ''):
print('用户名和密码不能为空')
exit
return username,password
#########################获取jira待修复缺陷数据
def jira_data_repair():
options = {
'verify': False,
'server': 'https://jira.XXXXX.com'}
jira = JIRA(options, basic_auth=(username, password))
issues = jira.search_issues('project = XXXXX AND status = 待修复', maxResults=1000,
fields='assignee,summary,customfield_10121')
# 写excel
workbook = xlwt.Workbook(encoding='utf-8')
worksheet = workbook.add_sheet('test')
total = 0
# 获取缺陷信息
for u in issues:
# print u #打印缺陷的key值
issue = jira.issue(u) # 根据key获取缺陷
ass = issue.fields.assignee # 获取缺陷经办人
sec = issue.fields.customfield_10121 # 获取缺陷严重性
sum = issue.fields.summary # 获取缺陷摘要
# 写入内容
worksheet.write(total, 0, str(u))
worksheet.write(total, 1, str(ass))
worksheet.write(total, 2, str(sec))
total = total + 1
# 保存excel
workbook.save(path+'/output_data\\repair.xls')
if __name__ == '__main__':
username,password=input_user()
jira_data_repair()