How to read Attachment by advanced scripting during the first transition "Create"

Originally asked by Anatoly Ekert on 27 May 2020 (original question)


I have the following problem:

I sync an issue with attachment into a new issue and I wish to be able to read the attachment by the first transition “create”, which means before the first status (Create Issue / Open / …) is set.

I understand from my readings (on the forums) that the attachment are not yet completely available by that first transition, but it is possible to access some equivalent temporary attachment.
However all my attempts have failed.

Any ideas how to do get the attachment in the temporary directory? Code snippets, pointers? Thank you.


Comments:

Juan Grases commented on 29 May 2020

Hi! Could you describe exactly the use case? Why do you need to have the attachment available before creating the issue and what would you like to do with it?

Anatoly Ekert commented on 02 June 2020

Hi. I do not need access to the attachment before the creation of the Issue, but in the process of creation. Because I have to fill in the fields of the Issue partially from this attachment.

Roman commented on 02 June 2020

hey Anatoly Ekert . Is this for Jira Cloud or Server?

Anatoly Ekert commented on 02 June 2020

hi. this is for Server

Roman commented on 02 June 2020

Anatoly Ekert Can you try

def attachmentId = replica.addedAttachments.find { it.filename == "Foo.pdf" }?.id
def filePath = blobMetadataList.find {it.blobId == attachmentId}?.blobFilePath
if (filePath) {
  def f = new java.io.File(filePath)
  // feel free to use any method to read in stuff into memory: https://stackoverflow.com/questions/326390/how-do-i-create-a-java-string-from-the-contents-of-a-file
}
  

Anatoly Ekert commented on 02 June 2020

Roman, I do not understand where it came from "blobMetadataList "? There is an error at this point.

Juan Grases commented on 02 June 2020

blobMetadataList should be an accessible variable in the context of the script (Jira Server incoming script), it represent the list of attachments you are receiving from the other side. Are you sure you are using the right name of the file on the first line of the proposed script? What is exactly the error?

Anatoly Ekert commented on 03 June 2020

I figured out blobMetadataList, now everything works, but unfortunately there is no result. I get Null. I checked the file name is correct and it exists in the original Issue.

2020-06-03 06:04:45,931+0000 pool-38-thread-1 INFO anonymous [com.exalate.script] mas filePath= null
2020-06-03 06:04:45,931+0000 pool-38-thread-1 INFO anonymous [com.exalate.script] mas attachmentId= null

Answer by Juan Grases on 19 June 2020

Hi again,

Looks like this script would help you on what you want:

//Incoming script
replica.addedAttachments.collect{it.getRemoteIdStr()}.each{ attachmentId ->
  if(attachmentId){
    def filePath = blobMetadataList.find{it.getBlobId() == attachmentId}?.getBlobFilePath()
    if (filePath) {
      def f = new java.io.File(filePath)
    // feel free to use any method to read in stuff into memory: https://stackoverflow.com/questions/326390/how-do-i-create-a-java-string-from-the-contents-of-a-file
    }else{
      throw new Exception("File Path not found")
    }
  }
}

Let me know if it helps,

Juan


Comments:

Laith Ata commented on 26 June 2020

Hi,

it worked. Thanks again!

Cheers,
Laith

Answer by Juan Grases on 02 June 2020

Hi!

From the incoming processor you have available metadata of the attachments coming from the other side such as mimetype, filename, filesize, created (date).

You can access it like this:

def remoteNewAttachments = replica.addedAttachments

def fileNames = remoteNewAttachments.collect{it.filename} // Results in a list of file names

Which info you need from the received attachments?

Best regards,

Juan


Comments:

Anatoly Ekert commented on 02 June 2020

Yes, I can get the file name, there is no problem with that. But I need to get the path to the file and read the file and get the data from it.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.