In this recipe, we will see how to add attachments to an issue via REST and browse existing attachments.
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.
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.
InputStream
object from the filepath:InputStream in = new FileInputStream("/Users/jobinkk/Desktop/test.txt");
IssueRestClient
object as explained previously:final Promise<Issue> issue = jiraRestClient.getIssueClient().getIssue(key); Issue browsedIssue = issue.get();
URI attachmentURI = browsedIssue.getAttachmentsUri();
issueClient.addAttachment(attachmentURI , in, "file1.txt");
Here, the last argument, file1.txt
, is the name with which the file appears on the issue.
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:
InputStream
object from the file path:InputStream in = new FileInputStream ("/Users/jobinkk/Desktop/test.txt");
IssueRestClient
object as explained earlier:final Promise<Issue> issue = issueClient.getIssue(key); Issue browsedIssue = issue.get();
URI attachmentURI = browsedIssue.getAttachmentsUri();
AttachmentInput
object:AttachmentInputattachmentInput = new AttachmentInput("file2.txt", in1);
issueClient.addAttachments(attachmentURI , attachmentInput);
Here, we use a File
object instead of creating InputStream
and use it in the addAttachments
method:
File file = new File("/Users/jobinkk/Desktop/test.txt"));
final Promise<Issue> issue = issueClient.getIssue(key); Issue browsedIssue = issue.get();
URI attachmentURI = browsedIssue.getAttachmentsUri();
issueClient.addAttachments(attachmentURI, file);
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()); }
3.16.69.106