How to find latest attachment in an jira issue and attach to the email notification

Hemanth Kumar September 11, 2022

I am using email this issue script runner post function. I need to find last/latest attachment in an issue and email that as part of closure notification. I found below script and its working for pdf format, but i tried deriving for all format but it couldn't.

Can any one help ?

----------------

import com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.mail.MailAttachment
{
MailAttachment a ->

def issue = a.issue

def allAttachments = issue.attachments

def latest = allAttachments.findAll {

it.filename.endsWith(".docx")
it.filename.endsWith(".jpg")
it.filename.endsWith(".png")
it.filename.endsWith(".xlsx")

it.filename.endsWith(".pdf")

}.sort { it.created }.last()

return latest.id == a.id

2 answers

1 accepted

1 vote
Answer accepted
Hemanth Kumar September 12, 2022

My requirement was to fetch issue details, latest comments and latest attachment and send a closure notification to the issue filed "email". With help of Ram Kumar (atlassian admin) was able to fetch few helps and i finally composed a complete solution.

I have used Script runner (email this issue) post function.

 

Custom attachment callback: (to fetch latest comments in listed formats)

mport com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.mail.MailAttachment
{
MailAttachment a ->
def issue = a.issue
def issue a.issue
def allAttachments = issue.attachments
def fileExtensions = ['.pdf', '.jpg', '.jpeg', '.docx', '.xlsx', '.png']
def latest = allAttachments.findAll {
fileExtensions.each { String fileType ->
it.filename.endsWith(fileType)
}
}.sort { it.created }.last()
return latest.id == a.id
}

0 votes
Hemanth Kumar September 12, 2022

But when i use this along with my other template and try completing the ticket - its throwing an error. 

Done-Transitions.JPG

i am using Email Issue Post-function  where i have configured subject, HTML Body and under attachment options chosen - Attachments returned by a Groovy script as shown below. Kindly advise where i made a mistake.

attachment groovy.JPG

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events