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

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How to read a text file attached to an issue with scriptrunner?

Hi, 

I'm trying to read a text file attached to an issue, using a script.

Here below the code I'm trying to use:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.issue.AttachmentManager
import com.atlassian.jira.issue.attachment.Attachment

def issueManager = ComponentAccessor.getIssueManager()
def aM= ComponentAccessor.getAttachmentManager()
def pM = ComponentAccessor.getAttachmentPathManager()
def issue = issueManager.getIssueObject("TEST-129")

List attachments = aM.getAttachments(issue)

if (!attachments.isEmpty()) {
for (Attachment a in attachments) {
String fileName=a.getFilename()
log.warn(fileName)
String storePath=pM.getAttachmentPath()
def attachmentFilePath = new File(storePath).path
def attachmentFile= new File(attachmentFilePath,fileName)
log.warn(attachmentFile)
log.warn(attachmentFile.text)
}
}

 

Quite sure I'm missing something, because the logs I get are the following:

 

image.png

Thanks in advance for any hint.

Andrea

 

1 answer

1 accepted

0 votes
Answer accepted

The file is stored in a special path under the storePath.

storePath/projectKey/bucket/issueKey/attachmentId

There is a really easy way to get the correct file path using AttachmentUtils but it's deprecated.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.util.AttachmentUtils

def issueManager = ComponentAccessor.getIssueManager()
def aM= ComponentAccessor.getAttachmentManager()

def issue = issueManager.getIssueObject("TEST-129")

def attachments = aM.getAttachments(issue)

attachments.each{a ->
def fileName = a.fileName
log.warn(filename)
def filePath = AttachmentUtils.getAttachmentFile(a)
def attachmentFile = new File(filePath)
log.warn(attachmentFile)
log.warn(attachmentFile.text)
}
 The current way to access the content of a file is using AttachmentManager.steamAttachmentContent

There are some examples here: https://community.atlassian.com/t5/Answers-Developer-Questions/Accessing-jira-attachment-file-data-from-a-jira-plugin/qaq-p/564803

Thank you! I'll try to follow your suggestments and let you know.

Andrea

Hi @Peter-Dave Sheehan 

I tried to follow your hint, read the links, and spent some time on the StreamAttachmentContent. The file to be read has become an XML.

I did something like this

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.issue.AttachmentManager
import com.atlassian.jira.issue.attachment.Attachment
import com.atlassian.jira.issue.attachment.FileAttachments
import com.atlassian.jira.util.AttachmentUtils
import org.apache.commons.io.FilenameUtils
import com.atlassian.jira.util.io.InputStreamConsumer
import org.springframework.util.StreamUtils
import java.io.FileOutputStream
import java.io.InputStream
import java.io.IOException
import groovy.util.XmlParser
import groovy.util.XmlSlurper

def issueManager = ComponentAccessor.getIssueManager()
def aM= ComponentAccessor.getAttachmentManager()
def pM = ComponentAccessor.getAttachmentPathManager()
def issue = issueManager.getIssueObject("TEST-129")
def attachments = aM.getAttachments(issue)

String attachmentFileText=""

if (!attachments.isEmpty()) {
for (Attachment a in attachments) {
def fileName=FilenameUtils.getBaseName(a.getFilename())
//log.warn (fileName)
def fileExtension=FilenameUtils.getExtension(a.getFilename())
//log.warn (fileExtension)
def attachmentFile = aM.streamAttachmentContent(a, new FileInputStreamConsumer(a.getFilename()))

List attachmentFileLines = attachmentFile.text.split('\n')
def righeFile=1
attachmentFileLines.each(){
singleLine->
//log.warn(righeFile)
//log.warn(singleLine)
attachmentFileText=attachmentFileText+singleLine
righeFile=righeFile+1
}
}
}





def inputXML = new XmlParser().parseText(attachmentFileText)

inputXML.requirement.each{
req->
log.warn(req.Key.text())
}


/**

inputXML.requirements.requirement.findAll{
req ->
req.Key!=null
}.each { reqK ->
log.warn("${reqK.Summary.text()}")
}

**/

class FileInputStreamConsumer implements InputStreamConsumer{

private final String filename;
public FileInputStreamConsumer(String filename) {
this.filename = filename;
}
@Override
public File withInputStream(InputStream is) throws IOException {
final File f = File.createTempFile(FilenameUtils.getBaseName(filename), FilenameUtils.getExtension(filename));
StreamUtils.copy(is, new FileOutputStream(f));
return f;
}
}

 

and it seems it reads the file, even though with a lot of warnings.

The file I am reading is an XML exported from Excel.

It seems I'm reading it. Now I need some experience with XML parser.

image.png

Nice job.

Yeah I'm not a big fan of working with xml node and the xmlparser. Json is so much easier.

I always get tons of "static type checking" errors in the scriptrunner editor. That's because the editor tries to validate your code but is missing information about what the data type of various variable will be. Those types will be assigned only at runtime.

I mostly ignore those warnings. I only use them as clues to find when code actually fails to run. The "proof is in the pudding" as they say.

Like Andrea Boerio likes this

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Jira Software

How to create Jira issus from Excel file?

When to use CSV importer When managing your processes in Jira, there are many occasions where you need to create a lot of tasks. Creating them one by one will cost you a lot of time and effort and i...

4,326 views 22 31
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you