Check out our new apps for Jira Cloud and start free trial.
Skip to end of metadata
Go to start of metadata

This example illustrates the way to copy an issue with attachments in categories to another project with preserving files in categories as in the source issue.

This script will work only in projects that are associated with the same scheme.


import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.AttachmentManager
import com.atlassian.jira.issue.IssueFactory
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.attachment.Attachment
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.user.util.UserManager
import com.onresolve.scriptrunner.runner.ScriptRunnerImpl
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.stiltsoft.jira.attachcategory.facade.SmartAttachmentsFacade

// Initializing the app components
@WithPlugin("com.stiltsoft.jira.smart-attachments")
SmartAttachmentsFacade facade = ScriptRunnerImpl.getPluginComponent(SmartAttachmentsFacade)
UserManager userManager = ComponentAccessor.getUserManager()
IssueFactory issueFactory = ComponentAccessor.getIssueFactory()
IssueManager issueManager = ComponentAccessor.getIssueManager()
AttachmentManager attachmentManager = ComponentAccessor.getAttachmentManager()
ProjectManager projectManager = ComponentAccessor.getProjectManager()

// Specifying the user, issue key and key of the target project
def currentUser = userManager.getUserByName("admin")
def sourceIssue = issueManager.getIssueObject("ONE-1")
def targetProject = projectManager.getProjectByCurrentKey("TWO")

// Copying an issue with attachments to another project
def issueCopy = issueFactory.cloneIssue(sourceIssue)
issueCopy.setProjectObject(targetProject)
Map<String, Object> newIssueParams = ["issue": issueCopy] as Map<String, Object>
def clonedIssue = issueManager.createIssueObject(currentUser, newIssueParams)
attachmentManager.copyAttachments(sourceIssue, currentUser, clonedIssue.key)


def sourceAttachmentCategories = facade.getAttachmentCategories(sourceIssue)

List<Attachment> attachmentsFromClonedIssue = new ArrayList<>(attachmentManager.getAttachments(clonedIssue))

sourceAttachmentCategories.categories.each({
    category -> category.getAttachments(false).each({
        attachment ->
            def attachmentCopy = attachmentsFromClonedIssue.find({clonedAttachment -> clonedAttachment.filename.equalsIgnoreCase(attachment.filename)})
            facade.addAttachmentsToCategory(clonedIssue, category.id, Collections.singletonList(attachmentCopy), currentUser)
            attachmentsFromClonedIssue.remove(attachmentCopy)
    })
})
  • No labels