Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,467,449
Community Members
 
Community Events
177
Community Groups

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

Edited

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

2 answers

2 accepted

1 vote
Answer accepted

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

Hi @Ram Kumar Aravindakshan _Adaptavist_ ,

Thank you, with your tips I got it working. 

Marco

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 

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://doc.apwide.com/apps/v/file-field/user-guide-section/java-groovy-api#copy-file-fields-to-jira-attachments-field

Like # people like this
0 votes
Answer accepted

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://doc.apwide.com/apps/v/file-field/user-guide-section/java-groovy-api

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events