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)