Hello,
from diverse questions and answers found here, I tried to solve my question with the following script. But no attachment gets attached. I guess, the way to address the newly created issue is dead wrong, but what shall I change here? We use 6.4.12 ...
Thanks in advance,
Maud
import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Category
import com.atlassian.jira.issue.Issue;
def Category log = Category.getInstance("com.onresolve.jira.groovy.PostFunction");
log.setLevel(org.apache.log4j.Level.DEBUG);
// Key of old issue with attachment
Issue issueKey = issue
def id = issueKey.getId()
// Key of new issue (which shall get attachment)
// !!! NOT OK.
def linkedIssueKeyList = ComponentAccessor.getIssueLinkManager().getOutwardLinks(issue.getID())
def linkcounter = linkedIssueKeyList.size()
def lastlinkedIssueKey = linkedIssueKeyList[linkcounter].getId()
// User
def user = ComponentAccessor.getJiraAuthenticationContext().getUser().name
// Managerservice for attachements
def attachmentManager = ComponentAccessor.getAttachmentManager()
// today
def today = new Date()
// Last attachement
def attachmentPathManager = ComponentAccessor.getAttachmentPathManager().attachmentPath.toString()
def fileName = issueKey.attachments.last()?.filename.toString()
def fileType = issueKey.attachments.last()?.mimetype.toString()
def fileId = issueKey.attachments.last()?.id
// Construct path for attachement
def currentIssue = issue.toString()
def projectKey = currentIssue.replaceAll("[^A-Z]", "").toString();
def path = attachmentPathManager + "/" + projectKey + "/" + currentIssue + "/" + fileId + "/"
File filePath = new File(path.toString())
// issue that shall get attachement
Issue newIssue = ComponentAccessor.getIssueManager().getIssueObject(lastlinkedIssueKey)
// Any attachment there? Then use it
if (fileName != null) {
attachmentManager.createAttachmentCopySourceFile(filePath, fileName, fileType, user, newIssue, null, today)
}