Source Instance

When sending attachments, you need to remember, that not all the attachments have to be sent over.
You can use the groovy collection methods, which allow for high grained control over the collection contents:

Data Filter

  • To send all the attachments you can use this code

replica.attachments    = issue.attachments
 

  • For example, to send only the *.pdf files add the following script 

replica.attachments = issue.attachments.findAll { attachment -> attachment.filename.endsWith(".pdf") }


Destination Instance

Create Processor

You can define how to create issue attachments,  received from the source side. It is possible to define how they will look like.

Check possible methods below:

  • Create new attachments, received from the source side

 issue.attachments.addAll(replica.addedAttachments)

or

 issue.attachments += replica.addedAttachments

 

//add all new attachments as listed in the replica and remove all attachments from the issue which have been removed in the remote issue
issue.attachments = attachmentHelper.mergeAttachments(issue, replica)
 

  • Manipulate the attachments via collection methods

issue.attachments.addAll(replica.addedAttachments)
issue.attachments = issue.attachments.collect {attachment -> attachment.filename = "node_A_" + attachment.filename}
 

  • Gather statistic from the attachments using this code:

 def numberOfAttachmentsPerTypes = issue.attachments.inject([:]) { result, attachment ->   
  def getFileExt = {
      filename -> def lastDotIdx = filename.lastIndexOf("."); (lastDotIdx > 0)? filename.substring(lastDotIdx) : null 
     }
  def fileExt = getFileExt(attachment.filename)
  def numberOfAttachmentsPerType = result[fileExt]
  numberOfAttachmentsPerType = numberOfAttachmentsPerType ?: 0
  result[fileExt] = numberOfCommentsPerAuthor + 1
  result
}
/*
for attachments:[
 [
   filename:"foo.pdf"
 ],
 [
   filename:"bar.txt"
 ],
 [
   filename:"baz.pdf"
 ],
 [
   filename:"hahaha"
 ]
]
 numberOfAttachmentsPerTypes would be [".pdf" : 2,".txt" : 1, null : 1]
*/

Change Processor

  • Add the code below to the Change processor to add the new attachments:

 issue.attachments.addAll(replica.addedAttachments)
or
 issue.attachments += replica.addedAttachments

 

  • You can also make the issue attachments the same as the ones, sent from the remote instance:

Attention!

This will remove all the attachments, which are not present on the remote issue replica. Use with caution.

issue.attachments = replica.attachments


//add all new attachments as listed in the replica and remove all attachments from the issue which have been removed in the remote issue
issue.attachments = attachmentHelper.mergeAttachments(issue, replica)
 

See Also