当前位置: 首页 > 工具软件 > JIRA-Client > 使用案例 >

调用Jira API 获取Project的Board参数和Sprint参数

袁运锋
2023-12-01

每个jira项目都有sprint参数和board参数,关系为一对多的关系。

  • project 和 board > 1对n
  • board 和 sprint > 1对n


如果想要查询一个项目具有哪些正在进行的sprint,还需要费一番功夫。

因为目前jira -api的python库里并没有给出方法,不过我们可以通过下面的方法获得:

通过get请求,根据项目的key或者ID获得board信息,地址和参数如下

url+"/rest/agile/1.0/board?projectKeyOrId=" + projectKeyOrId

通过get请求,根据board的ID查询到sprint的信息,根据state进行筛选

url+"/rest/agile/1.0/board/" + str(item['id']) + "/sprint?state=future,active"

nodejs代码

// With ES5
var JiraApi = require('jira-client');
var jira = '';

describe('JIRA API', async() => {
	beforeEach('Initialize', async() => {
		// Initialize
		jira = new JiraApi({
			protocol: 'https',
			host: 'jira.xxxx.com', // jira域名
			username: 'liyc',
			password: '',
			apiVersion: '2',
			strictSSL: true
		});
	});

	/**
	 * ES6
	 * @description 获取指定issue状态(中文状态:已解决、已解决未部署、已关闭等)
	 * @param issueNumber Jira Number(IssueID)
	*/

	it.skip('findIssue issue.fields.status.name', async() => {
			try {
				var issueNumber = 'S5A-1116';
				var issue = await jira.findIssue(issueNumber);
				await console.log(`Status: ${issue.fields.status.name}`);
			} catch (error) {
				await console.error(error);
			}
	});

	/**
	 * ES6
	 * @description 获取指定issue状态信息
	 * @param issueNumber Jira Number(IssueID)
	*/
	it.skip('findIssue issue.fields.status', async () => {
		try {
			var issueNumber = 'S5A-1116';
			var issue = await jira.findIssue(issueNumber);
			await console.log(`Status: ${JSON.stringify(issue.fields.status)}`);
		} catch (error) {
			await console.error(error);
		}
	});

	/**
	 * ES6
	 * @description 获取指定issue所有相关字段
	 * @param issueNumber Jira Number(IssueID)
	*/
	it.skip('findIssue issue.fields', async () => {
		try {
			var issueNumber = 'S5A-1116';
			var issue = await jira.findIssue(issueNumber);
			await console.log(`fields: ${JSON.stringify(issue.fields)}`);
		} catch (error) {
			await console.error(error);
		}
	});
	
	/**
	 * ES6
	 * @description 获取jira api版本
	 * @param 
	*/
	it.skip('apiVersion', async () => {
		await console.log(`apiVersion: ${jira.apiVersion}`);
	});

});

python代码

# -- coding: UTF-8 --
import requests
from jira import JIRA
url = 'https://jira.atlassian.com'
jira = JIRA(server=url, basic_auth=('username', 'password'))
cookies = jira._session.cookies
projectKeyOrId = "project_key"
board_url = url+"/rest/agile/1.0/board?projectKeyOrId=" + projectKeyOrId
response = requests.get(url, cookies=cookies,
                        headers={"Accept": "application/json"})
# print(json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": ")))
qq = response.json()
sprint_result_list = []
for item in qq['values']:
    sprint_url = url+"/rest/agile/1.0/board/" + str(item['id']) + "/sprint?state=future,active"
    response = requests.get(url, cookies=cookies,
                            headers={"Accept": "application/json"})
    sprint_json = response.json()
    if 'values' in sprint_json:
        sprint_list = sprint_json['values']
        for sprint in sprint_list:
            element = {'sprint_id': sprint['id'], 'sprint_name': sprint['name']}
            if element not in sprint_result_list:
                sprint_result_list.append(element)
print(sprint_result_list)
 类似资料: