Copy attachment from task to linked issue during a transition

Swarna Radha August 19, 2019

Hi,

 

I want to copy all attachment from task to linked issue during transition. I am using script runner to create the linked issue and i want to add the attachments from the task.

 

Please provide me with a script.

 

Thanks,

Swarna

1 answer

0 votes
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.
August 19, 2019
Swarna Radha August 19, 2019

Hi,

 

I am getting error :

//iterate over the issues linked to the current issue through the "blocks" link type - change the link type as needed
for (Issue linkedIssue : issue.getLinkedIssues("relates to")) {
//add new attachments from linked issue
def linkedAttachments = linkedIssue.get("attachment")
for (Attachment linkedAttachment : linkedAttachments) {
//test if this attachment from the linked issue already exists on the current issue
if (!attachments.find{it.filename == linkedAttachment.filename && it.filesize == linkedAttachment.filesize && it.mimetype == linkedAttachment.mimetype})
//this attachment is new, copy it to the current issue
ComponentAccessor.attachmentManager.copyAttachment(linkedAttachment, currentUser, issue.key)
}
}

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.
August 19, 2019

Can you please let me know the error. what you are getting

Swarna Radha August 19, 2019

Hi,

Capture.PNGPlease see screenshot.

Thanks

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.
August 19, 2019

can you please  include below at the top and try again

import com.atlassian.jira.issue.Issue

Swarna Radha August 19, 2019

Still the same

 

Capture 2.PNG

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.
August 19, 2019
import com.atlassian.jira.issue.attachment.Attachment
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor

def linkedIssues = ComponentAccessor.issueLinkManager.getOutwardLinks(issue.id);
def attachmentManager = ComponentAccessor.getAttachmentManager();

String commentAuthor = "currentUser"
def authorUser = (commentAuthor == "currentUser") ? ComponentAccessor.jiraAuthenticationContext.loggedInUser : ComponentAccessor.userManager.getUserByName(commentAuthor)
linkedIssues.each {

def linkedIssue = it.destinationObject

attachmentManager.copyAttachments(issue, authorUser, linkedIssue.key);

}

 

try above code, it will help 

Swarna Radha August 19, 2019

Hi,

 

I have tested the above code. No attachments are being copied. 

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.
August 19, 2019

Please check whether you have add attachment permission on linked issues or not.

 

You can check logs for any error. or Add log statements to the code, you can find whether the issue is.

Swarna Radha August 19, 2019

Hi,

I have the permission to add attachments on linked issues. The script runs successfully.  There is no error in the logs.

Thanks

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.
August 19, 2019

In this code , i am checking only outward links. can please check the links are outward or inward.

Other you can add log or system. out. println statement to check execution.

 

import com.atlassian.jira.issue.attachment.Attachment
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor

def linkedIssues = ComponentAccessor.issueLinkManager.getOutwardLinks(issue.id);
def attachmentManager = ComponentAccessor.getAttachmentManager();

String commentAuthor = "currentUser"
def authorUser = (commentAuthor == "currentUser") ? ComponentAccessor.jiraAuthenticationContext.loggedInUser : ComponentAccessor.userManager.getUserByName(commentAuthor)
System.out.println("Current issue" + issue.getKey());
System.out.println("linked issues size " + linkedIssues.size());

linkedIssues.each {

def linkedIssue = it.destinationObject
System.out.println("linked issue >>>>" + linkedIssue.getKey());

attachmentManager.copyAttachments(issue, authorUser, linkedIssue.key);

System.out.println("done");

}

Swarna Radha August 19, 2019

Hi,

Time (on server): Tue Aug 20 2019 10:52:04 GMT+0400 (Gulf Standard Time)

The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.

No logs were found for this execution.

 

No logs are being found even I have changed Inwards and Outward linked

Swarna Radha August 20, 2019

Hi,

Is there any solution?

Swarna Radha August 20, 2019

Hi, I have added logs:

 

Time (on server): Wed Aug 21 2019 09:51:42 GMT+0400 (Gulf Standard Time)

The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.

2019-08-21 09:51:42,719 DEBUG [workflow.ScriptWorkflowFunction]: Current issue: CM-197
2019-08-21 09:51:42,720 DEBUG [workflow.ScriptWorkflowFunction]: linked issues size : 0

 

 

The linked issues size is 0 and for linkedIssue.getKey() there is no logs.

import com.atlassian.jira.issue.attachment.Attachment
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Category

def linkedIssues = ComponentAccessor.issueLinkManager.getOutwardLinks(issue.id);
def attachmentManager = ComponentAccessor.getAttachmentManager();

String commentAuthor = "currentUser"
def authorUser = (commentAuthor == "currentUser") ? ComponentAccessor.jiraAuthenticationContext.loggedInUser : ComponentAccessor.userManager.getUserByName(commentAuthor)
System.out.println("Current issue" + issue.getKey());

log.debug "Current issue: ${issue.getKey()}"
System.out.println("linked issues size " + linkedIssues.size());

log.debug "linked issues size : ${linkedIssues.size()}"

linkedIssues.each {

def linkedIssue = it.destinationObject
System.out.println("linked issue >>>>" + linkedIssue.getKey());
log.debug "linked issue >>>>: ${linkedIssue.getKey()}"
attachmentManager.copyAttachments(issue, authorUser, linkedIssue.key);

System.out.println("done");

}

Daphnis Hessling June 9, 2020

The reason why you get 0 for the size is because  of getOutwardLinks. Try getInwardLinks instead.

This is how we are copying the attachments from the source issue to a newly created linked issue:

import com.atlassian.jira.issue.attachment.Attachment
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Level

def linkedIssuesOutward = ComponentAccessor.issueLinkManager.getOutwardLinks(issue.id);
def linkedIssuesInward = ComponentAccessor.issueLinkManager.getInwardLinks(issue.id);

def attachmentManager = ComponentAccessor.getAttachmentManager();
log.setLevel(Level.DEBUG)

log.info "Current issue: " + issue.key
log.info "linked issues outward size: " + linkedIssuesOutward.size()
log.info "linked issues inward size: " + linkedIssuesInward.size()

linkedIssuesInward.each {

def linkedIssue = it.sourceObject

log.info "linked issue >>>>: ${linkedIssue.getKey()}"

attachmentManager.copyAttachments(linkedIssue, issue.creator, issue.key);

}

I hope this helps.

Kind regards,

Daphnis

Suggest an answer

Log in or Sign up to answer