How to get all sprints in project using JIRA REST API
Asked Answered
E

3

20

Is there anything something like "https://www.example.com/jira/rest/agile/1.0/sprint?project=XYZ", to retrieve all the sprints in a JIRA Software project.

The JIRA platform API can retrieve projects info and the JIRA Software API can retrieve sprints for a given board. But I need sprints for any given project (combination) or at least boards for a given software project so that I can retrieve sprints in those boards later.

Expect answered 14/6, 2016 at 20:39 Comment(1)
The script I posted can be tweaked to get this info if GET SPRINT rest api has all the fields you need. https://mcmap.net/q/663266/-what-is-the-easiest-way-to-extract-sprint-start-and-end-dates-from-the-jira-dbDagnah
G
21

You can do it but with two resources:

  • First you can get all the scrum boards of the project with this:

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

  • Use the query parameters projectKeyOrId and type to filter them.

  • Iterate all the elements and use the url below with the id of each board to get its sprints:

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

Globate answered 4/8, 2016 at 7:6 Comment(1)
This answer does not work for 2.0 or 3.0 REST APIs?Unfold
R
6

Does this return any results for you?

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

If so, find the id of the view that contains this project, then try this url:

http://jira-domain.com/rest/greenhopper/latest/sprintquery/{view-id}?includeHistoricSprints=true&includeFutureSprints=true
Ruckman answered 15/6, 2016 at 9:16 Comment(0)
E
4

Let's make some assumptions before writing code:

  1. Board Name same as project name
  2. There will be only one board with that name
  3. We have a Sprint model

I am using Jersey client here to retrieve data from JIRA.

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

Helper methods

   /**
     * 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();
    }

Code for retrieving sprints

    /**
     * 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;

    }
Expect answered 9/8, 2016 at 18:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.