上一篇介紹了如何利用python的jira庫操作jira,我們能通過項目的key獲取項目的相關信息,同時也能利用jql查詢一些信息,例如查詢bug、故事、子任務等等,但是有些屬性卻不能通過這種方式直接拿到
比如這樣一個功能:針對某個項目,篩選不同sprint下有多少個bug
一種方式是可以寫死sprint,這樣前端頁面也能正常選擇,但這樣未免太笨,而且當項目或者sprint多起來以后,也不好維護;
另一種方式是動態獲取一個項目的sprint
不過python的jira庫並未直接提供相關的方法來獲得sprint信息
通過【網上沖浪🏄♀️】發現可以借助jira的REST API來獲取
具體步驟如下:
(1) 先拿到項目下的board(borad是指一個項目下的面板)
(2) 通過board獲取項目的sprint
1、獲取項目的board
from jira import JIRA import requests jira = JIRA(server='http://jira.xxx/', basic_auth=('user', 'password')) cookies = jira._session.cookies # 獲取jira對象中的cookie headers = { "Accept": "application/json", } base_url = "http://jira.xxx.com" # jira服務的域名 board_url = base_url + "/rest/agile/1.0/board/?projectKeyOrId=project_key" # 獲取board的api接口 # projectKeyOrId這個字段需要填寫項目的key res = requests.get(board_url, headers=headers, cookies=cookies) print(res.json())
結果如下
{ 'maxResults': 50, 'startAt': 0, 'total': 6, 'isLast': True, 'values': [{ 'id': id1, 'self': 'http://xxx', 'name': 'xxx子任務看板', 'type': 'kanban' }, { 'id': id2, 'self': 'http://xxx', 'name': 'xxx故事看板', 'type': 'scrum' }, { 'id': id3, 'self': 'http://xxx', 'name': 'xxx缺陷看板', 'type': 'kanban' }] }
可以看到這個接口返回了這個項目下的所有面板,這里我需要故事看板對應的id(不過其實任取一個看板中的id也行,后面都能拿到sprint),后續調用獲取sprint接口時,需要傳這個值
官方接口文檔:Get all boards
2、獲取sprint
sprint_url = base_url + "/rest/agile/1.0/board/" + str(id3) + "/sprint?state=future,active,closed" """ 這是獲取sprint的api接口 id3是剛才獲取的board_id, state參數指定提取哪種狀態sprint,它有3個值可選:future(未來),active(激活的),closed(已關閉的) "" res = requests.get(sprint_url, headers=headers, cookies=cookies) print(res.json())
結果如下
{ 'maxResults': 50, 'startAt': 0, 'isLast': True, 'values': [{ 'id': id1, 'self': 'http://jira.xxx', 'state': 'closed', 'name': '故事看板-xx項目 Sprint 0', 'startDate': '2021-08-23T13:47:00.000+08:00', 'endDate': '2021-08-30T01:47:00.000+08:00', 'completeDate': '2021-08-31T09:30:07.273+08:00', 'activatedDate': '2021-08-23T13:47:47.796+08:00', 'originBoardId': id }, { 'id': id2, 'self': 'http://jira.xxx', 'state': 'closed', 'name': 'xxx', 'startDate': '2021-08-31T10:16:00.000+08:00', 'endDate': '2021-09-14T20:16:00.000+08:00', 'completeDate': '2021-09-13T11:44:07.732+08:00', 'activatedDate': '2021-08-31T09:31:27.177+08:00', 'originBoardId': id }] }
這個接口會返回項目下的所有沖刺,其中的id就是sprint_id
官方接口文檔如下:Get all sprints
綜上我們就通過接口拿到了一個項目的sprint
整理下上述代碼,新建文件jira_get_sprint.py
# coding: utf-8 """ author: hmk detail: create_time: """ from jira import JIRA import requests class JiraSprint: def __init__(self): self.jira = JIRA(auth=('user', 'password'), options={'server': 'http://jira.xxx/'}) self.cookies = self.jira._session.cookies # 獲取登錄jira后的cookie self.base_url = "http://jira.xxx" self.headers = { "Accept": "application/json", } def get_sprint_board(self, project): """ 獲取項目的故事看板 :param project: :return: """ board_url = self.base_url + "/rest/agile/1.0/board/?projectKeyOrId="+project try: res = requests.get(board_url, headers=self.headers, cookies=self.cookies) # print(res.json()) values = res.json()["values"] # 提取返回結果中的values列表 if values: for i in values: if "故事" in i["name"]: # 取故事卡看板,如果name中包含"故事"2個字就認為這是故事看板 story_board_id = i["id"] return story_board_id # else: # continue return values[0]["id"] # 如果不包含故事看板,則取第一個board_id else: return None except Exception as e: raise e def get_sprint_id(self, project): """ 獲取sprint_id :param project: :return: """ story_board_id = self.get_sprint_board(project) if story_board_id is not None: sprint_url = self.base_url + "/rest/agile/1.0/board/" \ + str(story_board_id) \ + "/sprint?state=future,active,closed" # state = future,active,closed,表示篩選指定sprint的狀態,未來、活動、關閉 try: res = requests.get(sprint_url, headers=self.headers, cookies=self.cookies) # print(res.json()) values = res.json()["values"][::-1] # 提取各個沖刺的數據,並倒序排列 if values: sprints = [{"id": sprint["id"], "name": sprint["name"], "state": sprint["state"]} for sprint in values] # print(sprints) return sprints else: print("該項目下面沒有沖刺記錄") return None except Exception as e: raise e else: print("當前沒有故事看板") if __name__ == '__main__': test = JiraSprint() # story_board_id = test.get_sprint_board("xxx") # print(story_board_id) sprint_id = test.get_sprint_id("xxx") # print(sprint_id)
參考博客:
https://blog.csdn.net/qq_19696631/article/details/116952066
https://blog.csdn.net/u013302168/article/details/121894843