Hello,
I am running JIRA version 6.3.15 with ScriptRunner version 3.0.16 and am trying to implement a groovy script that automatically copies file attachments of an issue when it is cloned. For example, suppose Issue B is a clone of Issue A, where Issue A has the attachments I want to copy. The link relationship between the two issues is defined as "Issue B clones Issue A." This script is meant to copy the attachments from Issue A into Issue B.
Unfortunately, the script repeatedly fails with the following error every time it attempts to create and write the copied attachment to disk:
It seems that everything works fine up to the point of writing the copied attachment to disk. Attachments are successfully added to the database and are even listed in the Issue View as 0KB attachments with the correct filename. I am running using the ScriptRunner Console, though I tried triggering this script on a workflow transition, which yielded the same results. Here is a copy of the groovy script I am running:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.util.PathUtils
import com.atlassian.jira.issue.attachment.Attachment
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.attachment.CreateAttachmentParamsBean
// ******** FOR TESTING ONLY: Reference a Test Issue Object ********
IssueManager im = ComponentAccessor.getIssueManager()
MutableIssue issue = im.getIssueObject("TEST-1150")
// ************************************************************************
// Instantiate the PathUtils library to get the paths associated with the attachments
pathUtils = new PathUtils();
// Instantiate Issue Link Manager required to iterate through linked JIRA's for a given issue
def linkManager = ComponentAccessor.getIssueLinkManager()
//Instantiate Attachment Manager required to iterate through and issue's attachments
def attachmentManager = ComponentAccessor.getAttachmentManager()
//Instantiate Path Manager required for copying attachments to new JIRA
def pathManager = ComponentAccessor.getAttachmentPathManager()
// Get the user object for the current user
def user = ComponentAccessor.getJiraAuthenticationContext()?.getUser()
// Specify the link-type we wish to iterate for attachments
def linkType = "Cloners"
// Iterate through each ["Cloners"] linked issue
for (IssueLink link in linkManager.getOutwardLinks(issue.id)) {
if (linkType.contains(link.issueLinkType.name)) {
linkedIssue = link.getDestinationObject()
// If linked issue contains attachments, iterate through and copy over each attachment
for(Attachment attachment in linkedIssue.getAttachments()) {
// Define path to which attachment is to be written (i.e. copied)
filePath = pathUtils.joinPaths(pathManager.attachmentPath, linkedIssue.projectObject.key, issue.key, attachment.id.toString())
File newFile = new File(filePath)
// Define and build Attachment bean
def bean = new CreateAttachmentParamsBean.Builder()
.file(newFile)
.filename(attachment.getFilename().toString())
.contentType(attachment.getMimetype().toString())
.author(attachment.getAuthorObject())
.issue(issue)
.zip (false)
.thumbnailable(true)
.createdTime(attachment.getCreated())
.copySourceFile(true)
.build()
attachmentManager.createAttachment (bean)
}
}
}I went through several troubleshooting steps I found elsewhere on Atlassian Answers, most which dealt with folder permissions. I went through the steps and am pretty certain that the folder permissions are correct:
JIRA Home Folder Permissions:
JIRA_HOME_FOLDER_PERMISSIONS.png
JIRA Install Folder Permissions:
JIRA_INSTALL_FOLDER_PERMISSIONS.png
I also checked to ensure that our JIRA instance was run by the correct user (i.e. jira):
JIRA Process Details:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
jira 30995 3.6 35.0 5298644 2686144 ? Sl Feb01 105:35 /opt/atlassian/jira/jre//bin/java -Djava
At this point I am pretty much stuck. I tried implementing the code with and without beans (i.e. deprecated methods) to no avail. I confirmed folder permissions, file permissions, host user permissions, my own JIRA user permissions and found nothing to explain this issue. Any help or insight you could offer would be very much appreciated. Thanks!