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

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

Here I have two issues, one of them will be the source issue, where user adds an attachment and another issue will be the target issue, where the user want 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 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);


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


                                                }catch (RemoveException e) {



                                                catch (SecurityException se) {

                                                            System.out.println(“Could not read attachment file. Not copying. (${se.message})”);

                                                } catch (AttachmentException e) {



                                    } else {

                                                System.out.println(“Attachment file does not exist where it should. Not copying.”);


Posted in J2EE, JAVA, JIRA, JIRA on Demand. Tagged with , , , , .

One Response

Leave a Reply

Your email address will not be published. Required fields are marked *