I am Not sure about adding attachements with transition(I had never done), but I think can be clubbed. Here is the code to just add an attachment to JIRA, You can use Transition API of JRJC and apply/add some logic in below code to get work done.
Follow link [Listing All JIRA Transitions via API
for updating status based on transition, Hope you may get something from here too.
public void addAttachment(IssueRestClient issueRestClient,
VersionOne versionOne, Issue issue, Epics epic) {
try {
URI attachmentsUri = new URI(
applicationProperties.get(Constants.JIRAURL)
+ "/rest/api/2/issue/" + issue.getKey()
+ "/attachments");
Iterable<Attachment> attachments = issue.getAttachments();
Set<String> existingAttachments = new TreeSet<String>();
String _jiraUser = applicationProperties.get(Constants.JIRAUSER);
String _jiraPwd = applicationProperties.get(Constants.JIRAPWD);
String auth = new String(Base64.encode(_jiraUser + ":" + _jiraPwd));
Set<String> files = new TreeSet<String>();
for (Attachment attachment : attachments) {
for (VAttachements vAttachement : epic
.getAttachement()) {
files.add(vAttachement.getFileName());
}
existingAttachments.add(attachment.getFilename());
}
for (VAttachements vAttachement : epic.getvAttachement()) {
if (!(existingAttachments.contains(vAttachement.getFileName()))) {
Promise<Void> attachmentResult = issueRestClient
.addAttachment(attachmentsUri,
vAttachement.getInputStream(),
vAttachement.getFileName());
attachmentResult.claim();
Constants.REPORT.info(attachmentResult.isDone());
}
}
for (Attachment attachment : attachments) {
for (String checkAttachment : existingAttachments) {
if (!files.contains(checkAttachment))
deleteJiraAttachment(attachment, auth, issue,
checkAttachment);
}
}
} catch (Exception e) {
Constants.ERROR.info(Level.INFO, e);
}
}
-Here Epics is a POJO class which contains attachments to be added in Jira, through getter/ setter method.
private void deleteJiraAttachment(Attachment attachment, String auth,
Issue issue, String jiraFilename) {
URI attachmentURL = attachment.getSelf();
int status;
try {
if (jiraFilename.equalsIgnoreCase(attachment.getFilename())) {
status = invokeDeleteMethod(auth, String.valueOf(attachmentURL));
if (status == 204) {
Constants.REPORT.info("Attachment deleted from Issue"
+ issue.getKey());
} else if (status == 403) {
System.out
.println("attachments for Issue\t "
+ issue.getKey()
+ " is disabled or you don't have permission to remove");
} else if (status == 404) {
Constants.REPORT.info("No attachment is not found for"
+ issue.getKey());
}
}
} catch (AuthenticationException | ClientHandlerException e) {
Constants.ERROR.info(Level.INFO, e);
}
}
private static int invokeDeleteMethod(String auth, String url)
throws AuthenticationException, ClientHandlerException {
Client client = Client.create();
WebResource webResource = client.resource(url);
ClientResponse response = webResource
.header("Authorization", "Basic " + auth)
.type("application/json").accept("application/json")
.delete(ClientResponse.class);
int statusCode = response.getStatus();
if (statusCode == 401) {
throw new AuthenticationException("Invalid Username or Password");
}
return statusCode;