Think. Build. Salesforce Solutions.

Salesforce Consulting Services across Salesforce Clouds & Across App Lifecycles

Blog

How to programmatically move attachments from one JIRA issue to another JIRA issue?

By |2020-07-16T10:06:44+00:00May 24th, 2016|Tags: , , , , |

The following piece of code when executed, helps us move attachments between two issues of the same project or different projects in Jira.

Here I have two issues, one of them will be the source issue, where the user adds an attachment and another issue will be the target issue, where the user wants to move the attachment to.

This functionality is working fine in JIRA in its UI, however, I have tried to accomplish the same functionality through Java code. The following code executes the task of moving an attachment between the issues of both the same or different projects.

public void attachment(){
AttachmentManager attchMgr = ComponentAccessor.getAttachmentManager();
AttachmentPathManager pathManager = ComponentAccessor.getAttachmentPathManager();
List<Attachment> attchments = attchMgr.getAttachments(sourceIssue);
 IssueManager issueManager = ComponentAccessor.getIssueManager();
 MutableIssue targetIssue=issueManager.getIssueObject(targetIssueKey);

 if (!attchments.isEmpty()) {

for (Attachment attachment : attchments) {

String filePath = PathUtils.joinPaths(pathManager.getAttachmentPath(), sourceIssue.getProjectObject().getKey(),”10000″ ,sourceIssue.getKey(), attachment.getId().toString());

File f=new File(filePath);
if (f.exists()) {
System.out.println(“file exists in file system========”);

try {

  if (f.canRead()) {

attchMgr.createAttachmentCopySourceFile(f, attachment.getFilename(),attachment.getMimetype(), attachment.getAuthor(), targetIssue, null, attachment.getCreated());
List<Attachment> attchments1 = attchMgr.getAttachments(targetIssue);

 for(Attachment atc1:attchments1)
  {

System.out.println(“target file name——“+atc1.getFilename()+”-get attachment id of target–“+atc1.getId()+”get properties of target issue”+atc1.getProperties());

String filePathTarget = PathUtils.joinPaths(pathManager.getAttachmentPath(), targetIssue.getProjectObject().getKey(),”10000″ ,targetIssue.getKey(), atc1.getId().toString());

File fTarget = new File(filePathTarget);
 if(!fTarget.exists()){  

System.out.println(“delete file attachment of source issue “);
attchMgr.deleteAttachment(attachment);

  }
}
 }

}catch (RemoveException e) { e.printStackTrace();  } catch (SecurityException se) {
 System.out.println(“Could not read attachment file. Not copying. (${se.message})”);
  } catch (AttachmentException e) {
e.printStackTrace();  }
  } else {System.out.println(“Attachment file does not exist where it should. Not copying.”);
}
  }
 }
}

Leave A Comment