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 and preserve attachments in their categories.

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.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()

// Specifying the user and issue key
def currentUser = userManager.getUserByName("admin")
def sourceIssue = issueManager.getIssueObject("ONE-1")

// Copying an issue and attachments within the same project
def issueCopy = issueFactory.cloneIssue(sourceIssue)
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