Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How do I programatically set the attachment of an issue equal to the attachment of another issue?

tommy guo
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 10, 2013

I wish to programatically set an attachment to a newly created issue equal to the attachment that already exists on another issue. Any suggestion would be greatly appreciated!

3 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

2 votes
Answer accepted
RambanamP
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 11, 2013

try with the following to copy attachments from other issue

AttachmentManager attchMgr = ComponentAccessor.getAttachmentManager();
	AttachmentPathManager pathManager = ComponentAccessor.getAttachmentPathManager();
	List<Attachment> attchments = attchMgr.getAttachments(sourceIssue);
	if (!attchments.isEmpty()) {
for (Attachment attachment : attchments) {
		String filePath = PathUtils.joinPaths(pathManager.getAttachmentPath(), sourceIssue.getProjectObject().getKey(), sourceIssue.getKey(), attachment.getId().toString());
		File atFile = new File(filePath);
		if (atFile.exists()) {
		try {
			if (atFile.canRead()) {
			attchMgr.createAttachmentCopySourceFile(atFile, attachment.getFilename(),attachment.getMimetype(), attachment.getAuthor(), issue, null,														attachment.getCreated());
			}
		} 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.");
	}
}
}

ts from one issue to another

tommy guo
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 11, 2013

Rambanam you're a life saver!!! It works perfectly! I cant thank you enough =D

RambanamP
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 11, 2013

glad to hear it worked!!

cheers...

Özerk Dolanbay February 2, 2014

Hi all,

"createAttachmentCopySourceFile" method deprecated since jira 6.0 and the new method is "createAttachment(CreateAttachmentParamsBean createAttachmentParamsBean)". So how can I implement a CreateAttachmentParamsBean? Can anyone help me?

Thanks in advance.

fabio marcelo May 22, 2014

Hi Rambanam and Tommy,

Please could you share the complete code ? I tried use the code above , but it not worked.

Thanks in Advance

Best Regards

Fabio

Sachin Dhamale June 21, 2015

Hi Rambanam; I want to simple attach file to the issue without copying but when i attach the file using bellow code it will delete that file from directory. so for the next issue it give FileNotFoundException.

Sachin Dhamale June 21, 2015

File file = new File("C:\\Documents and Settings\\eQuest\\Desktop\\Geometric\\test.txt"); ChangeItemBean changeItemBean = attachmentManager.createAttachment(file, file.getName(), "text/plain", user, issue, new HashMap(), time);

0 votes
tommy guo
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 11, 2013

I've tried saving the attachments into a temporaryAttachmentMonitor but it's not working because I can't cast an attachment to a temporaryattachment:

AttachmentManager attachManager = ComponentManager.getInstance().getAttachmentManager();

List<Attachment> myAttachments = attachManager.getAttachments(issue);

TemporaryAttachmentsMonitor myTempAttachments =

new TemporaryAttachmentsMonitor();

for (Attachment attachment : myAttachments)

{

myTempAttachments.add( attachment);

}

0 votes
tommy guo
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 11, 2013

I know I can use AttachmentManager.getAttachments(Issue) to get all the attachments available on a particular issue, but how do I transfer these attachments onto another issue?

TAGS
AUG Leaders

Atlassian Community Events