How to copy an Attachment from a screen in subtask to the main Issue

SergioCourtois January 16, 2018

 

Hello everyone! I'm trying to copy an attachment from a screen ,
Who is associate to the closure of  a subtask .

I succeeded to do it with comment :

def commentManager = ComponentAccessor.getCommentManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def originalComment = transientVars.comment as String
def comments = originalComment
if (comments!=null)
{
    commentManager.create(parentIssue, user, comments, false)         
}

I'm trying to do an equivalent with the attachement ( 
copyAttachments(Issue issue, ApplicationUser author, String newIssueKey)
) but

transientVars.attachment

Does not seem to exist
How am I supposed to do ?

2 answers

1 accepted

0 votes
Answer accepted
Ivan Tovbin
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 2, 2018

Hi Sergio,

Based on your description, the attachment added during the 'Close issue' transition is always the last one (unless you have any post functions/automation that adds more attachments after that), the code to copy it to the parent issue is as follows:

import com.atlassian.jira.component.ComponentAccessor

def attMgr = ComponentAccessor.getAttachmentManager()
def attachments = attMgr.getAttachments(issue)
def lastAtt = attachments[0]
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

if (attachments.size() > 1){
for (int i = 1; i < attachments.size(); i++){
if (lastAtt.getCreated().getTime() < attachments[i].getCreated().getTime()){
lastAtt = attachments[i]
}
}
}

attMgr.copyAttachment(lastAtt, currentUser, issue.getParentObject().getKey())

 Also, make sure that you place this post function AFTER the "Re-index an issue to keep indexes in sync with the database" post function

0 votes
Germain Vincent January 18, 2018

Hi Sergio,

 

I would like to do the exact same thing but have no idea on how to manage that.

Hope someone could help :)

 

Best regards,

 

Germain.

Rgpnunes Gmail February 6, 2019

I am also trying to copy the last attachment during a post function by groovy. You did it?

Suggest an answer

Log in or Sign up to answer