Working with attachments

In this recipe, we will see how to add attachments to an issue via REST and browse existing attachments.

Getting ready

Create a JIRA REST client as mentioned in the Writing Java client for REST API recipe. Make sure attachments are enabled on the JIRA instance by checking at Administration | System | Advanced | Attachments.

How to do it...

There are three different methods exposed by JRJC to add attachments to an issue. The following are the three options and how they are used.

Using input stream and a new filename

  1. Create an InputStream object from the filepath:
            InputStream in = new FileInputStream("/Users/jobinkk/Desktop/test.txt");
  2. Get the issue to attach the file. You can use the IssueRestClient object as explained previously:
            final Promise<Issue> issue = 
            jiraRestClient.getIssueClient().getIssue(key); 
            Issue browsedIssue = issue.get(); 
  3. Get the attachment URI from the issue:
            URI attachmentURI = browsedIssue.getAttachmentsUri();
  4. Add the attachment:
            issueClient.addAttachment(attachmentURI , in, "file1.txt");

    Here, the last argument, file1.txt, is the name with which the file appears on the issue.

Using the AttachmentInput object

This is very similar to the previous case. The only difference is that an AttachmentInput object is created before calling the addAttachments method. Note that the method name is addAttachments, with an "s" at the end:

  1. Create an InputStream object from the file path:
            InputStream in = new FileInputStream
            ("/Users/jobinkk/Desktop/test.txt");
  2. Get the issue to attach the file. You can use the IssueRestClient object as explained earlier:
            final Promise<Issue> issue = issueClient.getIssue(key); 
            Issue browsedIssue = issue.get();
  3. Get the attachment URI from the issue:
            URI attachmentURI = browsedIssue.getAttachmentsUri();
  4. Create the AttachmentInput object:
            AttachmentInputattachmentInput = new 
            AttachmentInput("file2.txt", in1);
  5. Add the attachment:
            issueClient.addAttachments(attachmentURI , attachmentInput);

Using file and a new filename

Here, we use a File object instead of creating InputStream and use it in the addAttachments method:

  1. Create a File object from the file path:
            File file = new File("/Users/jobinkk/Desktop/test.txt"));
  2. Get the issue to attach the file. You can use the IssueRestClient object as explained earlier:
            final Promise<Issue> issue = issueClient.getIssue(key);
            Issue browsedIssue = issue.get();
  3. Get the attachment URI from the issue:
            URI attachmentURI = browsedIssue.getAttachmentsUri();
  4. Add the attachment:
            issueClient.addAttachments(attachmentURI, file);

Browsing attachments

Browsing attachments on an issue is even easier. You just need to invoke the getAttachments method:

Iterable<Attachment> attachments = attachedIssue.get().getAttachments(); 
for (Attachment attachment : attachments) {
        System.out.println("Name:" + attachment.getFilename() 
        + ", added by:"        
        + attachment.getAuthor().getDisplayName() 
        + ", URI:" + attachment.getSelf()); 
} 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.69.106