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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Copy attachment from task to linked issue during a transition

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

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

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

can you please  include below at the top and try again

import com.atlassian.jira.issue.Issue

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.
Aug 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 

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.
Aug 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.

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.
Aug 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");

}

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

Hi,

Is there any solution?

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");

}

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
TAGS
AUG Leaders

Atlassian Community Events