Missed Team ’24? Catch up on announcements here.

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

How can i copy attachment of one jira ticket to other jira ticket

Sreenivasaraju P
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.
February 19, 2013

hi,

I want to copy attachment of one jira to other jira ticket. can i do this by code.

3 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

4 votes
Answer accepted
Henning Tietgens
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.
February 19, 2013

You could copy attachments from one issue to another like this

attachmentManager = componentManager.getAttachmentManager()
pathManager = componentManager.getAttachmentPathManager()
attachmentManager.getAttachments(issue).each {attachment ->
	filePath = PathUtils.joinPaths(pathManager.attachmentPath, issue.projectObject.key, issue.key, attachment.id.toString())
	atFile = new File(filePath)
	if (atFile.exists()) {
		try {
			if (atFile.canRead()) {
				attachmentManager.createAttachmentCopySourceFile(atFile, attachment.filename,
						attachment.mimetype, attachment.author, newIssue, [:], attachment.created)
			}
		}
		catch (SecurityException se) {
			log.warn("Could not read attachment file. Not copying. (${se.message})")
		}
	}
	else {
		log.warn("Attachment file does not exist where it should. Not copying.")
	}
}

issue is the source, newIssue is the target issue. This is Groovy, similar to Java. Hope it helps.

Henning

Sreenivasaraju P
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.
February 20, 2013

thanks.

this answer helped to write my java code.

Warren McInnes
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.
June 24, 2014

Hi Henning,

With your above code, Is there a way to copy the attachment from a relative location on the server where Jira is being hosted from? Thanks

See issue: https://answers.atlassian.com/questions/306787/is-there-a-way-to-add-an-attachment-with-automation-on-subtask?page=1#comment-309412

Henning Tietgens
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.
June 24, 2014

I don't know. It's a "normal" file operation which should work if attachments in general are working in JIRA.

Warren McInnes
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.
June 24, 2014

Alright, no worries. Thanks anyhow

Daniel Ray July 9, 2015

Would it work as a post function to copy to a sub-task?

Henning Tietgens
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 9, 2015

I think it should work for sub-tasks, too.

Maria Thoma July 13, 2016

I added your code in my groovy script but I'm getting error

No such property: PathUtils for class:........

Henning Tietgens
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 13, 2016

You have to import it first.

import com.atlassian.jira.util.PathUtils
3 votes
Kevin Draai January 5, 2018

For the new Atlassian AttachmentManager API you can use:

attachmentManager.copyAttachment(attachment, user, newIssue.getKey().toString())
2 votes
MJ
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
February 19, 2013

If you only have a handfull of issues in which you need to perform thins, I would suggest doing this manually. If you only want to use code, and/or have a large amount of attachments that need to be moved over, you might want to have a look at the JIRA CLI plugin. Example commands can be found here, i thin kit should be possible to make something with those. Within JIRA itself, it is not possible to perform through code, unless you want to hack around the database, but I would advice against doing that.
Cheers,

TAGS
AUG Leaders

Atlassian Community Events