2
votes

Does anyone know about how to access JIRA Agile sprints with JIRA java REST Client api or in some other way with java.

1) I am trying to retrieve JIRA data (projects, sprints, issues etc). i need a way to query the projects, sprints with different filters as well. i guess JRJC cannot get agile data. Is there any one stop solution for entire JIRA data?

2) JIRA gave 2 types api in their docs: REST Api and Java Api. when to use each of those

2
Are you using greenhopper?Somaiah Kumbera
Can you please provide a link to some documentationlch

2 Answers

2
votes
  1. All of the info you want is available here:

https://docs.atlassian.com/jira/REST/latest/

Note that you can get project info with this: https://docs.atlassian.com/jira/REST/latest/#api/2/project

a) As for sprint info - can you get the info you need with JQL? Example - can you go to Issues > Search for issues and use the basic search? Here, under the "More" option, you will see that you can specify the Sprint to filter for.

As you add/remove sprints, you will see the URL changing. For example if I chose to see all issue types from Sprint 1 and Sprint 6 my JQL looks like this:

?jql=Sprint%20in%20(227%2C%20229)

Note that these are the sprint ids.

Now you can just call the rest api with search like this:

https://jira.domain.com/rest/api/2/search?jql=Sprint%20in%20(227%2C%20229)

More details here: https://docs.atlassian.com/jira/REST/latest/#api/2/search

b) If you can get the info you want with a filter you can simply save the filter and call the filter with the rest api

https://jira.domain.com/rest/api/2/filter/{filter-id}

More details here: https://docs.atlassian.com/jira/REST/latest/#api/2/filter-getFilter

  1. The second part of your question is really another stackoverflow question: The jira REST API is simply a REST API - which are HTTP method calls to get data. You can call these with PHP, Java(with Jersey, for example), bash (using curl) or even just using a browser with - Chrome postman, for example.

The Java API, or the REST Java Client for Jira is an unsupported set of libs that you can use if you specifically want to use Java to makes these rest calls. Of course you can use Jersey or whatever else you want to make the Rest calls, but the Java Client provides some libraries that might be helpful.

0
votes

Try this one.. I used below code,

    private int setSprint(String sprint, String key, String boardId) {
    String _jiraUser = applicationProperties.get(Constants.JIRAUSER);
    String _jiraPwd = applicationProperties.get(Constants.JIRAPWD);
    String auth = new String(Base64.encode(_jiraUser + ":" + _jiraPwd));

    String agileURL = applicationProperties.get(Constants.JIRAURL)
            + "/rest/agile/1.0/board/" + boardId + "/sprint";

    int sprintId = invokeGetMethod(auth, agileURL, sprint);
    // Constants.REPORT.info(sprintId);

    String issueURL = applicationProperties.get(Constants.JIRAURL)
            + "/rest/agile/1.0/issue/" + key;

    int issueId = getIssueId(auth, issueURL, key);
    // Constants.REPORT.info(issueId);
    return invokePostMethod(auth, sprintId, key, issueId);
    // Constants.REPORT.info(statusCode);

}

private static int invokeGetMethod(String auth, String agileURL,
        String sprint) {

    int sprintId = 0;
    int startAt = 0;
    agileURL = agileURL + "?startAt=" + startAt;
    sprintId = getSprintResult(agileURL, auth, sprint, startAt);
    // sprintId = getSprintId(content, sprint, agileURL);

    return sprintId;
}

private static int getSprintResult(String agileURL, String auth,
        String sprint, int startAt) {
    HttpGet post = new HttpGet(agileURL);
    org.apache.http.impl.client.DefaultHttpClient httpClient = new org.apache.http.impl.client.DefaultHttpClient();
    post.setHeader("Content-type", "application/json");
    post.setHeader("Authorization", "Basic " + auth);
    HttpResponse response = null;
    String content = null;
    try {
        response = httpClient.execute(post);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        content = CharStreams.toString(new InputStreamReader(response
                .getEntity().getContent(), Charsets.UTF_8));
    } catch (IllegalStateException | IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    int SprintId = getSprintId(content, sprint, agileURL, startAt, auth);
    if (SprintId != 0)
        return SprintId;
    return SprintId;
}

static int getSprintId(String content, String sprint, String agileURL,
        int startAt, String auth) {
    boolean flag = false;

    int sprintId = 0;
    Gson gson = new Gson(); // Class<String> data=new Class<String>();
    Data values = gson.fromJson(content, Data.class);
    for (Values data : values.getValues()) { // data.get
        if (data.getName().equalsIgnoreCase(sprint))
        // Constants.REPORT.info(sprintId=data.getId());
        {
            flag = true;
            // Constants.REPORT.info(data);
            // statusCode = response.getEntityInputStream().;
            return sprintId = data.getId();
        }

    }
    if (!flag) {
    //Pagination
        agileURL = agileURL.replaceAll(
                "startAt=" + String.valueOf(startAt),
                "startAt=" + String.valueOf(startAt += 50));
        sprintId = getSprintResult(agileURL, auth, sprint, startAt);
        if (sprintId != 0)
            return sprintId;

    }
    return sprintId;

}

private static int getIssueId(String auth, String agileURL, String key) {

    int issueId = 0;
    try {

        org.apache.http.impl.client.DefaultHttpClient httpClient = new org.apache.http.impl.client.DefaultHttpClient();
        HttpGet post = new HttpGet(agileURL);

        post.setHeader("Content-type", "application/json");
        post.setHeader("Authorization", "Basic " + auth);
        HttpResponse response = httpClient.execute(post);
        String content = CharStreams.toString(new InputStreamReader(
                response.getEntity().getContent(), Charsets.UTF_8));

        Gson gson = new Gson(); // Class<String> data=new Class<String>();

        IssueData issueValues = gson.fromJson(content, IssueData.class);
        if (issueValues.getKey().equalsIgnoreCase(key))
            issueId = issueValues.getId();
        // Constants.REPORT.info(issueValues);
    } catch (Exception e) {
        Constants.ERROR.info(Level.INFO, e);
        // vjErrorLog.info(Level.INFO, e);
    }
    return issueId;
}
private static int invokePostMethod(String auth, int sprintId, String key,
        int issueId) {

    int statusCode = 0;
    try {
        String postUrl = applicationProperties.get(Constants.JIRAURL)
                + "/rest/agile/1.0/sprint/" + sprintId + "/issue";
        String str = "{\"issues\": [\"" + key + "\",\"" + issueId + "\"]}";
        statusCode = invokePostMethod(auth, postUrl, str);

        return statusCode;
    } catch (Exception e) {
        Constants.ERROR.info(Level.INFO, e);
        // vjErrorLog.info(Level.INFO, e);
    }
    return statusCode;
}

Create a IssueData class with below attribute. and create getter/setter also overeride toString() method.

private int id;
private String self;
private String key;