前言:“JIRA是一款优秀的问题跟踪管理软件工具,可以对各种类型的问题进行跟踪管理,包括缺陷、任务、子任务、需求、sprint等。公司为了提高测试效率都会测试平台化,那么就会出现跨平台使用jira数据的问题。最近一段时间我们就来讨论下如何通过jira api达到我们的目的”
想要获取到jira上的数据,首先我们需要知道是那个project、project下的那个board以及那一个sprint。本文就介绍如何获取project、board和sprint.
一、获取project
jira上project一般都会比较多,如下图,我们需要知道每个project的key或者ID,这是操作project api的基础。
public static JSONArray getJiraProjects() { //httpClient 自己封装的http接口调用 HttpClientResponse clientResponse = httpClient("get", "http://you jira address:port/rest/api/2/project", ""); if (clientResponse != null) { logger.info("获取jira上project成功"); //已json数组的方式返回 return JSONArray.fromObject(clientResponse.getResponseBody()); } return null; } //url 可使用参数如下expand扩充字段、recent返回的个数、properties返回的属性 //http://jira.timevale.cn:8081/rest/api/2/project?expand=projectKeys,description,url,lead&recent=2&properties=key,id
//返回的数据结构如下,其中id和key会在后面的api中使用,需要保存 [ { "expand": "description,lead,url,projectKeys", "self": "http://you jira address:port/rest/api/2/project/12345", "id": "12345",//后续需要使用 "key": "AI",//后续需要使用 "name": "AI项目组", "avatarUrls": { "48x48": "http://you jira address:port/secure/projectavatar?avatarId=10324", "24x24": "http://you jira address:port/secure/projectavatar?size=small&avatarId=10324", "16x16": "http://you jira address:port/secure/projectavatar?size=xsmall&avatarId=10324", "32x32": "http://you jira address:port/secure/projectavatar?size=medium&avatarId=10324" }, "projectTypeKey": "software" } ]
二、获取board
有时候一个project下面可能存在多个board,如下图,那么我们就需要知道具体是那个board.
//入参projectKey是步骤一中返回的id或者key public static String getBoardId(String projectKey) { String boarId = ""; HttpClientResponse boardResponse = httpClient("get", "http://you jira address:port/rest/agile/1.0/board?projectKeyOrId=" + projectKey, ""); if (boardResponse != null && "200".equals(boardResponse.getStateCode()) && boardResponse.getResponseBody() != null) { JSONObject jsonObject = JSONObject.fromObject(boardResponse.getResponseBody().toString()); //获取项目看板数组 JSONArray boardArray = jsonObject.getJSONArray("values"); if (boardArray != null && boardArray.size() > 0) { for (int i = 0; i < boardArray.size(); i++) { //默认使用项目名称+ board作为项目看板 JSONObject object = boardArray.getJSONObject(i); if ((projectKey + " board").equals(object.getString("name")) || (projectKey + " Board").equals(object.getString("name"))) { //返回看板ID boarId = object.getString("id"); } } //没有满足要求的,设置第一个 if (StringUtils.isBlank(boarId)) { boarId = boardArray.getJSONObject(0).getString("id"); } } } return boarId; } //URL 中可使用参数startAt表示开始的位置、maxResults表示返回的数量 //http://you jira address:port/rest/agile/1.0/board?projectKeyOrId={projectKeyOrId}&startAt=1&maxResults=1
//返回结果如下,其中的id在获取sprint时需要使用,需要保存 { "maxResults": 50, "startAt": 0, "isLast": true, "values": [ { "id": num1,//后续需要使用 "self": "http://you jira address:port/rest/agile/1.0/board/num1", "name": "测试", "type": "kanban" }, { "id": num2, "self": "http://you jira address:port/rest/agile/1.0/board/num2", "name": "医签宝", "type": "scrum" } ] }
三、获取sprint
项目的一个迭代对应到jira一般就是一个sprint,如下图,获取到sprint id就可以获取到sprint关联的需求、任务、缺陷等
根据board id 获取sprint代码如下
//入参boardId就是第二步中获取的id public static List<TmsJiraSpringModel> getSpring(String boardId) { List<TmsJiraSpringModel> result = new ArrayList<>(); HttpClientResponse boardResponse = httpClient("get", "http://you jira address:port/rest/agile/1.0/board/" + boardId + "/sprint?state=future,active", ""); if (boardResponse != null && "200".equals(boardResponse.getStateCode()) && boardResponse.getResponseBody() != null) { JSONObject jsonObject = JSONObject.fromObject(boardResponse.getResponseBody().toString()); //获取Spring JSONArray boardArray = jsonObject.getJSONArray("values"); if (boardArray != null && boardArray.size() > 0) { for (int i = 0; i < boardArray.size(); i++) { //自己封装的对象 TmsJiraSpringModel springModel = new TmsJiraSpringModel(); JSONObject object = boardArray.getJSONObject(i); if (object != null) { springModel.setSpringId(object.getString("id")); springModel.setSpringName(object.getString("name")); result.add(springModel); } } } } return result; } //url 可使用参数startAt表示开始的位置、maxResults表示返回的数量、state表示sprint的状态 //http://you jira address:port/rest/agile/1.0/board/{boardId}/sprint?state=future,active&startAt=1&maxResults=1
//返回结果如下, 其中id为sprint id后面会使用 { "maxResults": 2, "startAt": 1, "isLast": false, "values": [ { "id": num1,//后续需要使用 "self": "http://you jira address:port/rest/agile/1.0/sprint/num1", "state": "active", "name": "【智能人事】一期项目", "startDate": "2020-12-07T14:10:58.582+08:00", "endDate": "2021-01-15T02:10:00.000+08:00", "originBoardId": xxx }, { "id": num2, "self": "http://you jira address:port/rest/agile/1.0/sprint/num2", "state": "active", "name": "企业合同权限迭代", "startDate": "2020-12-16T10:11:24.631+08:00", "endDate": "2020-12-29T10:11:00.000+08:00", "originBoardId": xxx } ] }
附录:
jira api地址:
1、https://developer.atlassian.com/cloud/jira/platform/rest/v2/intro/#about
2、https://developer.atlassian.com/cloud/jira/software/rest/intro/#introduction
更多文章请关注公共号