You're on your way to the next level! Join the Kudos program to earn points and save your progress.
Level 1: Seed
25 / 150 points
Next: Root
1 badge earned
Challenges come and go, but your rewards stay with you. Do more to earn more!
What goes around comes around! Share the love by gifting kudos to your peers.
Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!
Join now to unlock these features and more
The Atlassian Community can help you and your team get more value out of Atlassian products and practices.
Hello,
I am stuck with a piece of code how to add participant or newly created user as participants while a customer replies an existing ticket. Please see my code below: I am using script runner mail handler. Kindly help me.
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 javax.mail.Message
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.event.type.EventDispatchOption
import javax.mail.internet.InternetAddress
import com.atlassian.jira.config.util.JiraHome
import org.apache.commons.io.FileUtils
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
final targetProjectKey = 'BA'
final defaultReporterName = 'admin'
final defaultIssueType = 'Unsorted'
def messageUserProcessor = ComponentAccessor.getComponent(MessageUserProcessor) as MessageUserProcessor
def userManager = ComponentAccessor.getComponent(UserManager)
def projectManager = ComponentAccessor.projectManager
def issueFactory = ComponentAccessor.issueFactory
def customFieldManager = ComponentAccessor.customFieldManager
JiraHome jiraHome = ComponentAccessor.getComponent(JiraHome)
def subject = message.subject as String
def issue = ServiceUtils.findIssueObjectInString(subject)
def cf = customFieldManager.getCustomFieldObject("customfield_10101")
def project = projectManager.getProjectObjByKey(targetProjectKey)
def user = userManager.getUserByName(defaultReporterName)
def reporterUser = messageUserProcessor.getAuthorFromSender(message) ?: user
def allCcRecipients = message.getRecipients(Message.RecipientType.CC) as ArrayList<InternetAddress>
def allToRecipients = message.getRecipients(Message.RecipientType.TO) as ArrayList<InternetAddress>
log.warn ("allCcRecipients: " + allCcRecipients)
def participantList = [] as ArrayList<ApplicationUser>
def issueObject = issueFactory.issue
def body = MailUtils.getBody(message)
allCcRecipients.findAll {
allCcRecipients.each {
InternetAddress internetAddress = it as InternetAddress
def participant1 = messageUserProcessor.findUserByEmail(it.address)
if (!participant1){
def email = internetAddress.address
def fullName = internetAddress.getPersonal() ?: email
def mailparticipant = userManager.getUserByName(fullName)
participant1 = messageHandlerContext.createUser(email, null, email, fullName, null)
}
participantList.add(participant1)
}
}
allToRecipients.findAll {
allToRecipients.each {
InternetAddress internetAddress = it as InternetAddress
def participant1 = messageUserProcessor.findUserByEmail(it.address)
if (!participant1){
def email = internetAddress.address
def fullName = internetAddress.getPersonal() ?: email
def mailparticipant = userManager.getUserByName(fullName)
participant1 = messageHandlerContext.createUser(email, null, email, fullName, null)
}
participantList.add(participant1)
}
}
//log.warn (">>>>> participantList: " + participantList)
if (issue) {
def comment = MailUtils.getBody(message)
messageHandlerContext.createComment(issue, reporterUser, comment, false)
issueObject.setCustomFieldValue(cf, participantList)
def attachments = MailUtils.getAttachments(message) // <2>
attachments.each { MailUtils.Attachment attachment ->
def destination = new File(jiraHome.home, FileService.MAIL_DIR).getCanonicalFile()
def file = FileUtils.getFile(destination, attachment.filename) as File // <3>
FileUtils.writeByteArrayToFile(file, attachment.contents)
messageHandlerContext.createAttachment(file, attachment.filename, attachment.contentType, user, issue) // <4>
}
return
}
issueObject.setProjectObject(project)
issueObject.setSummary(subject)
log.warn (">>>>> subject: " + subject)
issueObject.setDescription(MailUtils.getBody(message))
issueObject.setIssueTypeId(project.issueTypes.find{ it.name == defaultIssueType }.id)
issueObject.setReporter(reporterUser)
issueObject.setCustomFieldValue(cf, participantList)
messageHandlerContext.createIssue(user, issueObject)
ComponentAccessor.getIssueManager().updateIssue(user, issueObject, EventDispatchOption.ISSUE_UPDATED, false)
def attachments = MailUtils.getAttachments(message) // <2>
attachments.each { MailUtils.Attachment attachment ->
def destination = new File(jiraHome.home, FileService.MAIL_DIR).getCanonicalFile()
def file = FileUtils.getFile(destination, attachment.filename) as File // <3>
FileUtils.writeByteArrayToFile(file, attachment.contents)
messageHandlerContext.createAttachment(file, attachment.filename, attachment.contentType, user, issueObject) // <4>
}
Thanks,
Swarna