Been racking my brains for a couple days going through the forums looking for a way to do what I thought would be a simple task. That's to read a CSV file attached to a ticket then parse it down into something useable.
Finally came up with the following solution that I'm sure could be finessed but works. Thought I would share here incase anyone else ran into a similar need.
import com.mindprod.csv.CSVReader
import com.atlassian.jira.issue.attachment.Attachment
Map csvFromAttachment (Attachment attachment, Boolean hasHeaders = false, char delim = ',', char quote = '\"', Boolean allowMultiLine = true, Boolean trim = true) {
Map res = [headers: null, lines: []]
Integer cnt = 0
attachment.withInputStream { stream -> {
CSVReader reader = new CSVReader(stream.newReader(),delim, quote, allowMultiLine, trim)
try {
while (reader) {
if (cnt == 0 && hasHeaders) res.headers = reader.getAllFieldsInLine()
else (res.lines as List).add(reader.getAllFieldsInLine())
cnt++
}
} catch (EOFException e) {
reader.close()
} catch (e) {
// Handle later
}
}}
return res
}