Copy attachments from one issue to another linked issue in another project

Filip Škledar September 1, 2021

Is there some kind of post function or listener that enables copying of attachments form one issue to his linked issue in another project? I would like to do it with ScriptRunner for cloud.

3 answers

1 accepted

3 votes
Answer accepted
Jack Brickey
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 1, 2021

OOTB I don’t believe there is a solution for this. I would suggest Automation but there is an open request for this - AUT-1030 . There are addon solutions, e.g. Exlate and probably scriptrunner.

Filip Škledar September 1, 2021

I have scriptrunner and that is the way I would like to implement solution. Do you know how to implement it?

 

Kr,

Filip

Jack Brickey
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 1, 2021

A search in the Community search bar returns this - https://community.atlassian.com/t5/forums/searchpage/tab/message?advanced=false&allow_punctuation=false&q=Copy+attachments+scriptrunner+ 

if you find the right solution for you please add to the post for future searches.

Laura Campbell _Seibert Group_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 1, 2021

Hi @Filip Škledar ,

If you're open to trying another app, Elements Copy & Sync allows you to copy attachments between linked issues.

For example, if you use the app to clone a JSM ticket into a software project (or just create a linked ticket), you can have attachments from the JSM ticket copied over to the new issue. This works even with new attachments, like when a customer provides something new on the JSM ticket.

If you have any trouble setting it up, don't hesitate to contact the Elements support team.

Laura

Like # people like this
0 votes
sahil kamble November 20, 2023

 

How to copy all Jira ticket issue attachments and file content as it is in another Jira cloud without any issues?

Ans:- 

(go to your project ) box tracking >- project setting >- permissions >- actions >- edit permissions > Browse Projects > give public access to that project
here you will be good to go with export and import issues without any error

note:-- You're in a team-managed project and can't see this option for that we need a company-managed project

 

https://confluence.atlassian.com/jirakb/cant-access-view-the-attachment-after-importing-csv-file-1115677396.html#:~:text=Cause,redirected%20to%20the%20login%20page.

0 votes
Lukasz Grobelny
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 4, 2022

If still needed, I can share code for this :) 

chris.m March 28, 2022

Lukasz Grobelny, Yes, Please share your code for this. Thank you so much.

Like Ziyarat Mahmudzadeh likes this
Nivesh Sharma October 4, 2022

@Lukasz Grobelny , can you please share the code.

Lukasz Grobelny
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.
October 10, 2022

Cloud or server?

Nivesh Sharma October 11, 2022

Cloud

Lukasz Grobelny
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.
October 11, 2022

So I added a custom field (select list) where the user was able to select if he want to copy the attachment as well, if he selected attachments the script would go like this:

if(selectedFields.contains("Załączniki")){
for(int i = 0; i < attachment.size(); i++ ){
def attId = attachment.id[i]
def att = Unirest.get('/rest/api/2/attachment/' + attId).asObject(Map).body
def downloadUrl = attachment.content[i]
def readFileResp = Unirest.get(downloadUrl).asBinary()
log.warn(downloadUrl)
def response = Unirest.post('/rest/api/2/issue/' + createdIssueKey + '/attachments')
.header("Accept", "application/json")
.header("X-Atlassian-Token", "nocheck")
.field("file", readFileResp.body, att.title.toString())
.asObject(Map)

}
}
Like Thuy Phan likes this
Soporte Inlogiq January 26, 2023

Hi Lukasz could you share a code you have for server Jira version? Thank you in advans

Lukasz Grobelny
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.
March 26, 2023

Hi, sorry, missed this... do you still need it?

chris.m March 26, 2023

Yes, please.

Lukasz Grobelny
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.
March 27, 2023
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.context.IssueContext
import com.atlassian.jira.issue.context.IssueContextImpl
import com.atlassian.jira.issue.fields.config.manager.PrioritySchemeManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.AttachmentManager
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
import org.apache.log4j.Logger
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.link.IssueLinkTypeManager

/Puling the CF
/
def summary = issue.getSummary()
def assignee = issue.getAssignee().getDisplayName()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def commentManager = ComponentAccessor.getCommentManager()
def existngComments = commentManager.getComments(issue);
def originalIssueType = issue.getIssueType().getName()
def originalDescription = issue.getDescription()
def priority = issue.getPriority().getName()

//Project picker - we need to select a destination project
def cField = customFieldManager.getCustomFieldObject("customfield_10201")
def projectKeyValue = (String) issue.getCustomFieldValue(cField)
//Cleaning the value
def projectKeyFromPicker = projectKeyValue.minus("Project: ")

//Selector of what will be moved - Checklist field on which users can elect what they want to mowe - comments etc
def additionalFieldsSelector = customFieldManager.getCustomFieldObject("customfield_10300")
def additionalFields = issue.getCustomFieldValue(additionalFieldsSelector) as String

// Reporter
final reporterKey = issue.getReporter().getKey()

//Priority
final priorityName = 'Major'

//Variales
def subTasks = issue.getSubTaskObjects()
def issueService = ComponentAccessor.issueService
def constantsManager = ComponentAccessor.constantsManager
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def prioritySchemeManager = ComponentAccessor.getComponent(PrioritySchemeManager)
def project = ComponentAccessor.projectManager.getProjectObjByKey(projectKeyFromPicker)
def issueType = constantsManager.allIssueTypeObjects.findByName(originalIssueType)

//Logged in user will be used
def reporter = ComponentAccessor.userManager.getUserByKey(reporterKey) ?: loggedInUser
def assignableUser = ComponentAccessor.userManager.getUserByName(assignee) ?: loggedInUser

// Setting up the priority
def issueContext = new IssueContextImpl(project, issueType) as IssueContext
def priorityId = constantsManager.priorities.findByName(priority)?.id ?: prioritySchemeManager.getDefaultOption(issueContext)

//creating the issue
def issueInputParameters = issueService.newIssueInputParameters().with {
setProjectId(project.id)
setIssueTypeId(issueType.id)
setReporterId(reporter.name)
setSummary(summary)
setPriorityId(priorityId)
setAssigneeId(assignableUser.name)
setDescription(originalDescription)
}

def validationResult = issueService.validateCreate(loggedInUser, issueInputParameters)
assert validationResult.valid : validationResult.errorCollection

def result = issueService.create(loggedInUser, validationResult)
assert result.valid : result.errorCollection
def newIssue = result.getIssue()

log.warn("${result.getIssue()} + was created" )

//Atthachment copy
def issueObject = issue.getAttachments()
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
AttachmentManager attachmentManager = ComponentAccessor.getAttachmentManager();
log.warn(issueObject.getAt(0))
attachmentManager.copyAttachments(issue, currentUser,result.getIssue().key)

//Comment Copy
if(additionalFields.contains("Komentarze")){
for(int i = 0; i < existngComments.size(); i++){
def commentAuthor = existngComments.getAt(i)getAuthorApplicationUser()
def newBody = existngComments.getAt(i).getBody()
commentManager.create(newIssue,commentAuthor,newBody,true)
log.warn(existngComments.getAt(i).getBody())
}
}
//Issue link

// Link type
final String issueLinkName = "Duplicate"

//sequence
final Long sequence = 1L

def issueLinkTypeManager = ComponentAccessor.getComponent(IssueLinkTypeManager)

def availableIssueLinkTypes = issueLinkTypeManager.issueLinkTypes
def linkType = availableIssueLinkTypes.findByName(issueLinkName)

ComponentAccessor.issueLinkManager.createIssueLink(newIssue.id, issue.id, linkType.id, sequence, loggedInUser)
Like # people like this
Lukasz Grobelny
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.
March 27, 2023

The way it works - the user selects Copy on the issue.

On a Copy screen, he can select the Destination project; on the checklist, he can choose what will be moved.

 

You can trim it down just to copy the issue without fireworks

Suggest an answer

Log in or Sign up to answer