Hi,
In our Jira Server we work with the apps Apwide File Field, Automation for Jira and ScriptRunner.
I want to copy the contents of an Apwide File Field (customFieldId=14795) to Jira's general attachment field.
I think a solution with Scriptrunner and Groovy will work.
I just can't figure out how to get the correct content of the groovy script in my use case. Can anyone help me with that?
I want to copy the contents of an Apwide File Field (customFieldId=14795) to Jira's general attachment field :-)
Regards,
Marco Brundel
For your requirement, you could try using a Listener.
The IssueCreate event / IssueUpdated event can be used to copy the attachment from the Apwide File Field to the Attachment field either during the creation of the Issue or when the Issue is being updated.
Below is a print screen of the Listener configuration:-
Below is a working sample code for your reference:-
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.attachment.CreateAttachmentParamsBean
import java.text.SimpleDateFormat
def issue = event.issue as MutableIssue
def customFieldManager = ComponentAccessor.customFieldManager
def attachmentManager = ComponentAccessor.attachmentManager
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def appwide = customFieldManager.getCustomFieldObjectsByName("Appwide Attachment")[0]
def appwideValue = appwide.getValue(issue).toString().split(",") as List<String>
//The path where the apwide attachments are stored
def folderPath = "/media/ram/Linux_Disk_Space/atlassian/application-data/jira8/data/attachments/00_apwide_file_field_files"
def formatYear = new SimpleDateFormat("yyyy")
def formatCurrentDate = new SimpleDateFormat("yyyy-MM-dd")
def currentYear = formatYear.format(new Date())
def currentDate = formatCurrentDate.format(new Date())
def latestFile = "${folderPath}/${currentYear}/${currentDate}/${appwideValue.last()}"
def sourceFile = new File(latestFile)
def attachmentParams = new CreateAttachmentParamsBean(sourceFile,sourceFile.name,null,loggedInUser,issue,null,false,null,new Date(),true)
attachmentManager.createAttachment(attachmentParams)
Please note, this sample code is not 100% exact to your environment. Hence, you will need to make the required modifications.
Below are some test print screens:-
1) When the Appwide attachment is added, it is automatically added to Jira's attachment, as well as shown in the image below:-
2) Although the attachment displays as the Actual file name added when it is stored in the Folder, it will display a timestamp as shown in the image below:-
which is why the Jira attachment appears to be so.
3) However, in the Jira attachments folder, it displays as a regular attachment file as shown below:-
I hope this helps to answer your question. :)
Thank you and Kind Regards,
Ram
Hi @Ram Kumar Aravindakshan _Adaptavist_ ,
Thank you, with your tips I got it working.
Marco
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Ram Kumar Aravindakshan _Adaptavist_
Since I am new to Script. I have tried the same script but its not working. Can u suggest me the edits (What are the edits I should do for successful run of the script).
Thanks
Venu
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Here is an improved version of the script to copy Apwide file fields to Jira attachments:
import com.atlassian.jira.issue.attachment.Attachment
import com.atlassian.renderer.util.FileTypeUtil
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.onresolve.scriptrunner.runner.customisers.PluginModule
@WithPlugin("com.apwide.document.file-field")
import com.apwide.file.api.FileManager;
import com.atlassian.jira.component.*;
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.attachment.CreateAttachmentParamsBean
import com.apwide.document.file.FileDescriptor
import org.apache.commons.io.FilenameUtils
@PluginModule
FileManager fileManager
def issue = event.issue as MutableIssue
log.warn "Current Issue: ${issue}"
def final APWIDE_FILE_FIELD_NAME = "Resume" // set you own Apwide File Field name here
def customFieldManager = ComponentAccessor.customFieldManager
def attachmentManager = ComponentAccessor.attachmentManager
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def customField = customFieldManager.getCustomFieldObjectsByName(APWIDE_FILE_FIELD_NAME)[0]
log.warn "Apwide customField: ${customField}"
def customFieldValue = customField.getValue(issue).toString().split(",") as List<String>
log.warn "Apwide customField value: ${customFieldValue}"
if (customFieldValue.isEmpty())
return
for (fileId in customFieldValue){
log.warn "Current file id: ${fileId}"
List<Attachment> existingAttachments = attachmentManager.getAttachments(issue)
log.warn("Existing attachments: ${existingAttachments}(number of attachments: ${existingAttachments.size()})")
if (fileManager.exists(fileId)){
def filePath = fileManager.getPath(fileId)
log.warn "File path: ${filePath}"
def fileDescriptor = new FileDescriptor(fileId)
log.warn "File descriptor: ${fileDescriptor}"
log.warn "File name: ${fileDescriptor.fileName}"
log.warn "File type: ${fileDescriptor.fileType}"
log.warn "File extension: ${fileDescriptor.fileExtension}"
log.warn "Thumbnailable: ${fileDescriptor.thumbnailable}"
log.warn "User id: ${fileDescriptor.userId}"
log.warn "Timestamp: ${fileDescriptor.timestamp}"
def sourceFile = new File(filePath)
def attachmentParams = new CreateAttachmentParamsBean(sourceFile,fileDescriptor.fileName,null,loggedInUser,issue,null,fileDescriptor.thumbnailable,null,new Date(fileDescriptor.timestamp),true)
Attachment existingAttachmentWithSameName = existingAttachments.find({Attachment it -> it.getFilename() == fileDescriptor.fileName} )
log.warn "Existing attachment with same name: ${existingAttachmentWithSameName}"
if (existingAttachmentWithSameName != null){
log.warn "Already existing attachment with same name"
if (existingAttachmentWithSameName.getCreated().getTime() == fileDescriptor.timestamp){
log.warn "Exact same file already exists, do nothing"
}
else{
log.warn "Delete the existing file with the same name"
attachmentManager.deleteAttachment(existingAttachmentWithSameName)
log.warn "Attach the new file with the same name"
attachmentManager.createAttachment(attachmentParams)
}
}
else{
attachmentManager.createAttachment(attachmentParams)
log.warn "File successfully added to attachments!"
}
}
}
You can find it up to date version of this script in our documentation here: https://file-field.apwide.com/doc/latest/server-data-center/java-groovy-api
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks for your question @Marco Brundel and for your great support @Ram Kumar Aravindakshan _Adaptavist_
Additional examples of integration between File Field and ScriptRunner:
https://file-field.apwide.com/doc/latest/server-data-center/rest-api
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Could you please confirm that mentioned listener works also for history issues?
It means that a lot of attachments in file field have been uploaded a few years ago, but now I am going to copy all stored files to system field as "attachment"?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks for your question.
This example is effective when the file is uploaded while the script is being executed (either during issue creation or update). However, it will not function if the file was added in the past.
Based on my understanding, your script should perform the following tasks:
Parse your Jira issues
Retrieve the file IDs from the file field for each issue
Download the files with the download function (Java Doc)
Upload those files as Jira attachments
Please let me know if this helps.
Kind regards,
David
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.