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

Scriptrunner Mail Handler help

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

0 votes
Answer accepted

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)

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.

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)

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

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.

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Apps & Integrations

🍂📹 Apptoberfest demo contest roundup: vote for your favorite demos!

Hi Community! The submissions are in (and listed below) for the 🍻🍂Apptoberfest🍂🍻 Demo Competition and it’s time for you to place your votes for the best: Analytics & reporting app demo ...

232 views 6 15
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