How to copy the contents of an Apwide File Field to Jira's general attachment field

Marco Brundel
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 16, 2021

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

3 answers

2 accepted

2 votes
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 17, 2021

Hi @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:-

appwide.png

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:-

test1.png

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:-

attachment_storage.png

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:-

attachment_storage2.png

I hope this helps to answer your question. :)

Thank you and Kind Regards,

Ram

Marco Brundel
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 21, 2021

Hi @Ram Kumar Aravindakshan _Adaptavist_ ,

Thank you, with your tips I got it working. 

Marco

Venugopal Paladi November 16, 2021

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 

Guillaume _Apwide_
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
May 9, 2022

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

Like # people like this
0 votes
Answer accepted
David Berclaz
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 19, 2021

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

0 votes
Kamil Sobura
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
May 27, 2024

@Guillaume _Apwide_ 

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"?

David Berclaz
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 27, 2024

Hi @Kamil Sobura

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:

  1. Parse your Jira issues

  2. Retrieve the file IDs from the file field for each issue

  3. Download the files with the download function (Java Doc)

  4. Upload those files as Jira attachments

Please let me know if this helps.

Kind regards,

David

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events