How retrieve a list of attachments JIRA rest api via java? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How retrieve a list of attachments JIRA rest api via java? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

How do I get the list of attachments using JIRA API with Java? I don’t need the attachments I just want to know what attachments are there, and their ID. I followed basics of using a query to get issues and I am getting issues but not their attachments. My code:

[String searchQuery = "(created >= '2015/12/1' ) AND (created < '2016/01/1')" 
JiraAuthentication jiraAuth;
JiraRestClient arc;
// Get JiraAuthentication instance to create basic HTTP authentication
// string
jiraAuth = JiraAuthentication.getInstance();

// Make sure you use a new instance
jiraAuth.initInstance();
jiraAuth = JiraAuthentication.getInstance();

// Get the JIRA Rest Client from the basic HTTP authentication
jrc = jiraAuth.getJrc();

// Receive search results based on query
SearchRestClient searchClient = jrc.getSearchClient();
// limit results to 50
SearchResult result = searchClient.searchJql(searchQuery, 50,0, null);
Iterator<BasicIssue> itIssue = result.getIssues().iterator();

// pull information on individual issues.
while (itIssue.hasNext()) {
   BasicIssue lBasicIssue = (BasicIssue) itIssue.next();
   String lIssueKey = lBasicIssue.getKey();
   Issue issue = jrc.getIssueClient().getIssue(lIssueKey, null);
   //This line should print the list of attachments. but everything is always null.
   System.out.println(issue.getAttachements());
}][1]

One example where I am trying to get information is from HBASE-15062, which has the api link. In the api link there doesn’t show any attachments but in the actual link there are 4 attachments.

I know about the Jira api call api/2/issue/{issueIdOrKey}/attachments.

I tried : api/2/issue/12925031/attachments I get the response HTTP Status 405 - Method Not Allowed Does this mean I cannot get attachment list or am I just doing the request wrong?

How can I change my java code to get the attachments? Is getting attachments possible from the APACHE JIRA?

Answer

I would like to say jannis answer lead me to the answer for this question.

jannis method that adds the parameter ?expand=changelog is the only way to get attachments. The java call for getIssue() needs to change I found how to add parameters here, the api info is here.

getIssue(String issueKey, ProgressMonitor progressMonitor) was used:

Issue issue = jrc.getIssueClient().getIssue(lIssueKey, null);

Issue getIssue(String issueKey, Iterable<IssueRestClient.Expandos> expand, ProgressMonitor progressMonitor) should be used:

   Issue issue = jrc.getIssueClient().getIssue(lIssueKey, Arrays.asList(IssueRestClient.Expandos.CHANGELOG), null);

With the changed call to getIssue the changelog field should be populated. You can then use this field to look at attachments by iterating through the change log. I did this:

private null findAttachments(Issue issue) {
    String FieldName = "Attachment";
    Iterable<ChangelogGroup> changes = issue.getChangelog();
    StringBuilder attachments = new StringBuilder();
    StringBuilder attachmentsIDs = new StringBuilder();

    for (ChangelogGroup change: changes){
        //Multiple change items per group. 
        for(ChangelogItem item: change.getItems()){
            if(item.getField().equals(FieldName)){
                //Gets attachment name.
                attachments.append((String) item.getToString());
                //Gets attachment ID to download if needed.
                attachmentsIDs.append((String) item.getTo());
            }
        }
    }
    //Do something with attachments here..

}
We are here to answer your question about How retrieve a list of attachments JIRA rest api via java? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji