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

Scriptrunner Mail Handler help

Justin Buchanan January 20, 2021

Hi folks,

I'm attempting to setup a ScriptRunner Mail Handler that does the below:

1) Creates an issue

2) Adds attachments from the email to the issue

3) Adds users to 2 different custom fields

4) Adds a comment to the issue

5) Transitions the issue to a new status (not necessary but would be nice)

I'm new to Scriptrunner and Groovy but cobbled together something that half works from examples on the internet. Parts 3 and 4 work in the Console but not when added to the Mail Handler script and issues are created using the Mail Handler. I couldn't figure out transitions at all. Attachments also do not work. Here is my script. Apologies if there is anything redundant in there. Is anyone able to point me in the right direction here? Any help is appreciated.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.service.util.ServiceUtils
import com.atlassian.jira.service.util.handler.MessageUserProcessor
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.UserManager
import com.atlassian.mail.MailUtils
import org.apache.commons.io.FileUtils
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.comments.CommentManager

def subject = message.getSubject() as String
def issue = ServiceUtils.findIssueObjectInString(subject)

def projectManager = ComponentAccessor.getProjectManager()
def issueFactory = ComponentAccessor.getIssueFactory()
def messageUserProcessor = ComponentAccessor.getComponent(MessageUserProcessor)
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueManager = ComponentAccessor.getIssueManager()
def userManager = ComponentAccessor.getUserManager()
def user = userManager.getUserByName("jira-admin")
ApplicationUser reporter = messageUserProcessor.getAuthorFromSender(message) ?: user
def project = projectManager.getProjectObjByKey("TP")

def issueObject = issueFactory.getIssue()
issueObject.setProjectObject(project)
issueObject.setSummary(subject)
issueObject.setDescription(MailUtils.getBody(message))
issueObject.setIssueTypeId(project.issueTypes.find { it.name == "Task" }.id)
issueObject.setReporter(reporter)

messageHandlerContext.createIssue(user, issueObject)

def attachments = MailUtils.getAttachments(message)

attachments.each { MailUtils.Attachment attachment ->
def destination = new File(jiraHome.home, FileService.MAIL_DIR).getCanonicalFile()
def file = FileUtils.getFile(destination, attachment.filename) as File
FileUtils.writeByteArrayToFile(file, attachment.contents)
messageHandlerContext.createAttachment(file, attachment.filename, attachment.contentType, user, issue)
}

def purWatchers = userManager.getUserByName("jdoe")
def purCustom = customFieldManager.getCustomFieldObjectByName("Purchasing Watcher Users")

def accWatcher1 = userManager.getUserByName("jdoe")
def accWatcher2 = userManager.getUserByName("jsmith")
def accCustom = customFieldManager.getCustomFieldObjectByName("Accounting Watchers")

issue.setCustomFieldValue(purCustom, [purWatchers])
issue.setCustomFieldValue(accCustom, [accWatcher1,accWatcher2])

String userName = "jira-admin"
def userComment = userManager.getUserByName(userName)
CommentManager commentManager = ComponentAccessor.getCommentManager()
commentManager.create(issueObject,userComment,"[~jmith] can you approve this?", true)

issueManager.updateIssue(user, issueObject, EventDispatchOption.DO_NOT_DISPATCH, false)

 

2 answers

1 accepted

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
John Chin
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 20, 2021

Hi Justin,

 

Welcome to the Atlassian Community. Could you please try replace the issue object by doing this?

from:

messageHandlerContext.createIssue(user, issueObject)

..
..
..

issueManager.updateIssue(user, issueObject,EventDispatchOption.DO_NOT_DISPATCH, false)

to:

def issue = messageHandlerContext.createIssue(user, issueObject)

..
..
..
..
commentManager.create(issue,userComment,"[~jmith] can you approve this?", true)
issueManager.updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
Justin Buchanan January 21, 2021

Accidentally posted this as an answer...

Hi @John Chin 

Thanks for the welcome and thanks for the help!

I tried to make the change you suggested, but get "the current scope already contains a variable of the name issue" as it is defined higher up. I tried using a different variable name, but the end result was the same as before. Only the basic ticket is created with no attachments and none of the other changes.

John Chin
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 21, 2021

Hi Justin,

After tested using the script, it seems like some variables are missing. I did the comparison from our Script Runner mail handler here .

I did some changes on your script and confirmed is working on my local instance.

Please give a try:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.config.util.JiraHome
import com.atlassian.jira.service.services.file.FileService
import com.atlassian.jira.service.util.ServiceUtils
import com.atlassian.jira.service.util.handler.MessageUserProcessor
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.UserManager
import com.atlassian.mail.MailUtils
import org.apache.commons.io.FileUtils
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.event.type.EventDispatchOption

def userManager = ComponentAccessor.getComponent(UserManager)
def projectManager = ComponentAccessor.getProjectManager()
def issueFactory = ComponentAccessor.getIssueFactory()
def messageUserProcessor = ComponentAccessor.getComponent(MessageUserProcessor)
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueManager = ComponentAccessor.getIssueManager()

JiraHome jiraHome = ComponentAccessor.getComponent(JiraHome)

def subject = message.getSubject() as String
def issue = ServiceUtils.findIssueObjectInString(subject)

if (issue) {
return
}

ApplicationUser user = userManager.getUserByName("admin")
ApplicationUser reporter = messageUserProcessor.getAuthorFromSender(message) ?: user
def project = projectManager.getProjectObjByKey("AA")

def issueObject = issueFactory.getIssue()
issueObject.setProjectObject(project)
issueObject.setSummary(subject)
issueObject.setDescription(MailUtils.getBody(message))
issueObject.setIssueTypeId(project.issueTypes.find { it.name == "Task" }.id)
issueObject.setReporter(reporter)
issue = messageHandlerContext.createIssue(user, issueObject)

def attachments = MailUtils.getAttachments(message)

attachments.each { MailUtils.Attachment attachment ->
def destination = new File(jiraHome.home, FileService.MAIL_DIR).getCanonicalFile()
def file = FileUtils.getFile(destination, attachment.filename) as File
FileUtils.writeByteArrayToFile(file, attachment.contents)
messageHandlerContext.createAttachment(file, attachment.filename, attachment.contentType, user, issue)
}

def purWatchers = userManager.getUserByName("jdoe")
def purCustom = customFieldManager.getCustomFieldObjectByName("Purchasing Watcher Users")

def accWatcher1 = userManager.getUserByName("jdoe")
def accWatcher2 = userManager.getUserByName("jsmith")
def accCustom = customFieldManager.getCustomFieldObjectByName("Accounting Watchers")

issue.setCustomFieldValue(purCustom, [purWatchers])
issue.setCustomFieldValue(accCustom, [accWatcher1,accWatcher2])

String userName = "admin"
def userComment = userManager.getUserByName(userName)
CommentManager commentManager = ComponentAccessor.getCommentManager()
commentManager.create(issue,userComment,"[~jsmith] can you approve this?", true)

issueManager.updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
Justin Buchanan January 22, 2021

Thanks @John Chin ! That worked! Thanks so much for your help!

Omprakash Thamsetty
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 22, 2021

@John Chin https://community.atlassian.com/t5/Adaptavist-questions/Scriptrunner-Mail-Handler-help/qaq-p/1585805

Mail-handler.png

 

def project = projectManager.getProjectObjByKey("DFRHD")
IssueType issueType = issueTypeManager.getIssueType("Helpdesk")



def issueServiceobj = ComponentAccessor.getIssueService()
def issueInputParametersobj = issueServiceobj.newIssueInputParameters()

def subject = message.getSubject() as String
def body = MailUtils.getBody(message)
def fromAddr = MailUtils.getSenders(message)
def cfvalue = fromAddr as String
def EmailID = cfvalue?.replaceAll(/^\[([\s\S]*)]$/,'$1')
//Set up for project/users/inbox
UserManager userManager = ComponentAccessor.getUserManager()
//Change lines for the correct email inbox to test
ApplicationUser user = userManager.getUserByName("dfrmoserhelpdesk")
//If the subject line has an issue id, see if it exists
def issueMatcher = /DFRHD-[0-9]*/
def issueResult = subject =~ /$issueMatcher/

def issue = ServiceUtils.findIssueObjectInString(subject)
if (issue) {
return
}

def issueObject = issueFactory.getIssue()
def ContactEmailField = customFieldManager.getCustomFieldObjectsByName("Customer Email").first()

issueObject.setProjectObject(project)
issueObject.setSummary(subject)
issueObject.setDescription(body)
//issueObject.setPriority("Normal")
issueObject.setPriorityId(ComponentAccessor.getConstantsManager().getPriorities().find{it.getName().equals("4 Low")}.getId())
issueObject.setIssueTypeId(project.issueTypes.find { it.name == "Helpdesk" }.id)
issueObject.setReporter(user)
def projectComponentManager = ComponentAccessor.getProjectComponentManager()
def componentA = projectComponentManager.findByComponentNameCaseInSensitive('1st Level Support')
issueObject.setComponent(componentA)
issueObject.setCustomFieldValue(ContactEmailField, EmailID)

issue = messageHandlerContext.createIssue(user, issueObject)

def attachments = MailUtils.getAttachments(message)
attachments.each { MailUtils.Attachment attachment ->
def destination = new File(jiraHome.home, FileService.MAIL_DIR).getCanonicalFile()
def file = FileUtils.getFile(destination, attachment.filename) as File
FileUtils.writeByteArrayToFile(file, attachment.contents)
messageHandlerContext.createAttachment(file, attachment.filename, attachment.contentType, user, issue)
}

def cfConfig = sevFld.getRelevantConfig(issue)
def value = ComponentAccessor.optionsManager.getOptions(cfConfig)?.find {
it.toString() == '4-Low'
}

issue.setCustomFieldValue(sevFld, value)

def reqsrcfld = customFieldManager.getCustomFieldObjectsByName("Request Source Type").first()
def cfreqsrcConfig = reqsrcfld.getRelevantConfig(issue)
def reqsrcvalue = ComponentAccessor.optionsManager.getOptions(cfreqsrcConfig)?.find {
it.toString() == 'Email'
}
issue.setCustomFieldValue(reqsrcfld, reqsrcvalue)
issueManager.updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH, false)

}
0 votes
Justin Buchanan January 21, 2021

Hi @John Chin 

Thanks for the welcome and thanks for the help!

I tried to make the change you suggested, but get "the current scope already contains a variable of the name issue" as it is defined higher up. I tried using a different variable name, but the end result was the same as before. Only the basic ticket is created with no attachments and none of the other changes.

TAGS
AUG Leaders

Atlassian Community Events