官方文檔:https://jira.readthedocs.io/en/master/
官方文檔:https://docs.atlassian.com/DAC/rest/jira/6.1.html#d2e864
優秀博客:https://blog.csdn.net/weixin_43790276/article/details/89892699
優秀博客:https://www.cnblogs.com/superhin/p/11693280.html
一、先安裝jira庫,並測試獲取所有項目信息
pip install jira
from jira import JIRA jira = JIRA(auth=("username", "pwd"), options={'server': 'https://**.**.**.**'}) projects = jira.projects() print(projects)
注意:
jira = JIRA(auth=("username", "pwd"), options={'server': 'https://**.**.**.**'})
第一行中的參數是auth不是basic_auth,連接方式請參考文首最新的官方文檔,其他文章均為basic_auth,導致連不上
輸出:
是一個數組,每個項目由<>包圍,key是關鍵字,name是項目名
[<JIRA Project: key='NEW', name='***項目', id='10433'>, <JIRA Project: key='**', name='**', id='10467'>, <JIRA Project: key='***', name='***重構', id='10501'>, <JIRA Project: key='P2020021451', name='**', id='10502'>, <JIRA Project: key='***', name='**工作', id='10481'>, <JIRA Project: key='***', name='**工作', id='10490'>, <JIRA Project: key='**', name='**申請', id='10446'>, <JIRA Project: key='**', name='**', id='10613'>]
可以和jira頁面項目列表對照一下,是完整且正確的。
二、查詢某個項目的bug數據:總數,嚴重bug數。。。。。
from jira import JIRA jira = JIRA(auth=("username", "pwd"), options={'server': 'https://jira.**.net.cn'}) # 查詢問題,支持JQL語句 issue = jira.search_issues('project = P2020021451 AND component = **管理模塊', maxResults=-1) print(len(issue))
輸出:顯示77條bug總數
注意:如果不加maxResults=-1參數,則實際總數大於50時只能查出50條數據。
實例2:
查詢每個bug的詳細信息,需要加上fields參數
可使用的字段:項目project; 模塊名稱components; 標題summary; 缺陷類型issuetype; 具體描述內容description; 經辦人assignee; 報告人reporter; 解決結果resolution; bug狀態status; 優先級priority; 創建時間created; 更新時間updated; 評論comments
# 查詢問題,支持JQL語句,一定要增加json_result參數,會返回bug詳細信息,fields為指定顯示的字段 issues = jira.search_issues('project = P2020021451 AND component = 銷售管理模塊', fields="summary, priority, status", maxResults=-1, json_result='true') print(issues)
輸出:
{ 'expand': 'schema,names', 'startAt': 0, 'maxResults': 1000, 'total': 77, 'issues': [{ 'expand': 'operations,versionedRepresentations,editmeta,changelog,renderedFields', 'id': '66559', 'self': 'https://jira.**.net.cn/rest/api/2/issue/66559', 'key': 'P2020021451-173', 'fields': { 'summary': '***為非必填項', 'priority': { 'self': 'https://jira.**.net.cn/rest/api/2/priority/3', 'iconUrl': 'https://jira.**.net.cn/images/icons/priorities/medium.svg', 'name': '中', 'id': '3' }, 'status': { 'self': 'https://jira.**.net.cn/rest/api/2/status/10540', 'description': '', 'iconUrl': 'https://jira.**.net.cn/images/icons/statuses/generic.png', 'name': '開發處理中', 'id': '10540', 'statusCategory': { 'self': 'https://jira.**.net.cn/rest/api/2/statuscategory/4', 'id': 4, 'key': 'indeterminate', 'colorName': 'yellow', 'name': '處理中' } } } }, { 'expand': 'operations,versionedRepresentations,editmeta,changelog,renderedFields', 'id': '57443', 'self': 'https://jira.**.net.cn/rest/api/2/issue/57443', 'key': 'P2020021451-4', 'fields': { 'summary': '**開發功能', 'priority': { 'self': 'https://jira.**.net.cn/rest/api/2/priority/2', 'iconUrl': 'https://jira.**.net.cn/images/icons/priorities/high.svg', 'name': '高', 'id': '2' }, 'status': { 'self': 'https://jira.**.net.cn/rest/api/2/status/10332', 'description': '', 'iconUrl': 'https://jira.**.net.cn/images/icons/statuses/generic.png', 'name': '需求完成', 'id': '10332', 'statusCategory': { 'self': 'https://jira.**.net.cn/rest/api/2/statuscategory/3', 'id': 3, 'key': 'done', 'colorName': 'green', 'name': '完成' } } } }] }
可以看見返回為json格式,我們指定要返回的fields字段都在json里面