如何使用JIRA REST API获取项目中的所有sprint

时间:2016-06-14 20:39:30

标签: jira jira-rest-api jira-agile

是否有类似“http://www.example.com/jira/rest/agile/1.0/sprint?project=XYZ”的内容,以检索项目中的所有冲刺。

JIRA平台api可以检索项目信息,JIRA软件API可以检索给定板的冲刺。但我需要针对给定项目的任何给定项目(组合)或至少板的冲刺,以便我可以在以后检索这些板中的冲刺

3 个答案:

答案 0 :(得分:12)

你可以用两种资源来做到这一点:

  • 首先,您可以通过以下方式获得项目的所有Scrum板:

https://docs.atlassian.com/jira-software/REST/cloud/#agile/1.0/board-getAllBoards

  • 使用查询参数 projectKeyOrId 输入来过滤它们。

  • 迭代所有元素并使用下面的url和每个棋盘的ID来获取它的冲刺:

https://docs.atlassian.com/jira-software/REST/cloud/#agile/1.0/board/{boardId}/sprint-getAllSprints

答案 1 :(得分:5)

这会为您返回任何结果吗?

http://jira-domain.com/rest/greenhopper/latest/rapidviews/list

如果是这样,找到包含该项目的视图的id,然后尝试这个url:

http://jira-domain.com/rest/greenhopper/latest/sprintquery/{view-id}?includeHistoricSprints=true&includeFutureSprints=true

答案 2 :(得分:4)

在编写代码之前,我们先做一些假设:

  1. 董事会名称与项目名称相同
  2. 只有一个名称为
  3. 的棋盘
  4. 我们有一个Sprint模型
  5. 我在这里使用Jersey客户端从JIRA检索数据。

    private Client jerseyClient = Client.create();
    jerseyClient.addFilter(new HTTPBasicAuthFilter("username", "password"));
    private Gson gson = new Gson();
    

    帮助方法

       /**
         * This method will a GET request to the URL supplied
         * @param url to request
         * @return String response from the GET request
         */
        public String makeGetRequest(String url){
            ClientResponse response = jerseyClient.resource(url).accept("application/json").get(ClientResponse.class);
            return response.getEntity(String.class);
        }
    
    
        /**
         * This method helps in extracting an array from a JSON string
         * @param response from which Array need to be extracted
         * @param arrayName
         * @return JsonArray extracted Array
         */
        public JsonArray extractArrayFromResponse(String response, String arrayName){
            JsonObject jsonObject = gson.fromJson(response, JsonObject.class);
            return jsonObject.get(arrayName).getAsJsonArray();
        }
    

    检索短跑的代码

        /**
         * This method will retrieve list of sprints in a given project
         * @param project for which we are requesting sprints 
         * @return List of sprints
         */
         public List<Sprint> getSprints(String project) {
            List<Sprint> sprintList = new ArrayList<>();
    
            try {
                //get board URL for the given 
                String boardUrl = "https://jira.company.com/rest/agile/1.0/board?name=" + URLEncoder.encode(project, "UTF-8");  //assumption 1
                String boardResponse = makeGetRequest(boardUrl);
                JsonArray boards = extractArrayFromResponse(boardResponse, "values");
                if(boards.size() > 0){
                   JsonObject board = boards.get(0).getAsJsonObject(); //assumption 2
    
                //get all sprints for above obtained board
                String sprintUrl = jsonHandler.extractString(board, "self")+"/sprint";
                String sprintsResponse = makeGetRequest(sprintUrl);
                JsonArray sprints = extractArrayFromResponse(sprintsResponse, "values");
    
                    //loop through all sprints
                    for (int i = 0; i < sprints.size(); i++) {
                        JsonElement sprint = sprints.get(i);
                        JsonObject sprintObj = sprint.getAsJsonObject();
    
                        String sprintName = sprintObj.get("name").getAsString();
                        Sprint sprint = new Sprint(sprintName);
                        sprintList.add(sprint);
                    }//end of for loop
    
                }
    
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
    
            return sprintList;
    
        }