Let's make some assumptions before writing code:
- Board Name same as project name
- There will be only one board with that name
- 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
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
public List<Sprint> getSprints(String project) {
List<Sprint> sprintList = new ArrayList<>();
try {
String boardUrl = "https://jira.company.com/rest/agile/1.0/board?name=" + URLEncoder.encode(project, "UTF-8");
String boardResponse = makeGetRequest(boardUrl);
JsonArray boards = extractArrayFromResponse(boardResponse, "values");
if(boards.size() > 0){
JsonObject board = boards.get(0).getAsJsonObject();
String sprintUrl = jsonHandler.extractString(board, "self")+"/sprint";
String sprintsResponse = makeGetRequest(sprintUrl);
JsonArray sprints = extractArrayFromResponse(sprintsResponse, "values");
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);
}
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return sprintList;
}
GET SPRINT
rest api has all the fields you need. stackoverflow.com/a/60378905/1499296 - AKS