Hi,
I want to add participants via mail handler script runner. Kindly provide any code.
Thanks,Swarna
Hi @Pourna Radha-Burhoo,
Request Participants is a custom field, you can update the same just like normal user picker fields
Below article would give you more idea and sample code for reference
KB Article
Regards,
Leo
@Leo ,
I am using in mail handler but the article is already defining the users in an array. I want to it from the mail address and add them as participants. If participants are not existed in jira, create them
How it is achievable?
Thanks,
Swarna
Hi @Pourna Radha-Burhoo
am I understanding you correctly you are referring to ScriptRunner Mail Handler?
Further you want to set Watchers (is it what you meant by Participants) by setting the users on CC on an email? If so, there is indeed documentation:
https://scriptrunner.adaptavist.com/latest/jira/mail-handler.html#_create_users_from_cc_and_add_to_watchers
In case this is not what you are looking for Community members might be able to say more and advise further if you could bring in some more details - how your configuration looks like, where you will take the users from that should go into Participants, as well as what you already have and what is not working.
Cheers,Daniel
@Daniel Ebers ,
Hello,
Yes I am using ScriptRunner Mail Handler? I want to add "CC" or "TO" as participants.
What i have achieved so far i am able to create user and add to request particpants. But same is consuming a licence and should not do so.
Please see my codes below:
import com.atlassian.jira.component.ComponentAccessorimport com.atlassian.jira.service.util.ServiceUtilsimport com.atlassian.jira.service.util.handler.MessageUserProcessorimport com.atlassian.jira.user.ApplicationUserimport com.atlassian.jira.user.util.UserManagerimport com.atlassian.mail.MailUtilsimport javax.mail.Messageimport javax.mail.internet.InternetAddressfinal targetProjectKey = 'BA'final defaultReporterName = 'admin'final defaultIssueType = 'Unsorted'def messageUserProcessor = ComponentAccessor.getComponent(MessageUserProcessor) as MessageUserProcessordef userManager = ComponentAccessor.getComponent(UserManager)def projectManager = ComponentAccessor.projectManagerdef issueFactory = ComponentAccessor.issueFactorydef customFieldManager = ComponentAccessor.customFieldManager
def subject = message.subject as Stringdef 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) ?: userdef allCcRecipients = message.getRecipients(Message.RecipientType.CC) as ArrayList<InternetAddress>def participantList = [] as ArrayList<ApplicationUser>def issueObject = issueFactory.issuedef body = MailUtils.getBody(message)allCcRecipients.findAll {allCcRecipients.each {InternetAddress internetAddress = it as InternetAddress
def email = internetAddress.addressdef fullName = internetAddress.getPersonal() ?: emaildef mailparticipant = userManager.getUserByName(fullName)mailparticipant = messageHandlerContext.createUser(email, null, email, fullName, null)// def participant = messageUserProcessor.findUserByEmail(it.address) as ApplicationUserparticipantList.add(mailparticipant)}}if (issue) {def listOfExistingParticipants = issue.getCustomFieldValue(cf) as ArrayList<ApplicationUser>
if(participantList.containsAll(listOfExistingParticipants) ) {def comment = "${subject} - ${body}"messageHandlerContext.createComment(issue, reporterUser, comment, false)}} else {issueObject.setProjectObject(project)issueObject.setSummary(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)}
When you say "Request participants" in terms of a Jira Service Management project it should not consume a license unless there is some configuration done wrong.https://confluence.atlassian.com/servicemanagementserver/adding-request-participants-939926441.html
Re-reading your requirement I cannot state 100% for sure if the code works with "Request Participants" also as the documentation is referring to "Watcher".
@Pourna Radha-Burhoo, since you already achieved most of the parts. and only problem is created user is consuming license( I assume)
Application access is managed through Group via application access configuration
if you set any group as default for new users, they'll be added to the same and that will consume license(s)
you can check that configuration here Admin --> Applications --> Application Access
workaround I can think of is removing the user from the added group through script(I never tried this though
import com.atlassian.jira.component.ComponentAccessor;def um = ComponentAccessor.getUserManager()def gm = ComponentAccessor.getGroupManager()def user = um.getUserByName("user-name")def group = gm.getGroup("group name user is getting added after creation")def uu = ComponentAccessor.userUtiluu.removeUserFromGroup(group, user)
Hope this helps,
I have managed to create the users but i am stuck how to add same in while replying an existing issue. Kindly guide me
import com.atlassian.jira.component.ComponentAccessorimport com.atlassian.jira.service.util.ServiceUtilsimport com.atlassian.jira.service.util.handler.MessageUserProcessorimport com.atlassian.jira.user.ApplicationUserimport com.atlassian.jira.user.util.UserManagerimport com.atlassian.mail.MailUtilsimport javax.mail.Messageimport com.atlassian.jira.issue.IssueManagerimport com.atlassian.jira.event.type.EventDispatchOptionimport javax.mail.internet.InternetAddressimport com.atlassian.jira.config.util.JiraHomeimport org.apache.commons.io.FileUtilsimport com.atlassian.jira.component.ComponentAccessorimport com.atlassian.jira.config.util.JiraHomeimport com.atlassian.jira.service.services.file.FileServiceimport com.atlassian.jira.service.util.ServiceUtilsimport com.atlassian.jira.service.util.handler.MessageUserProcessorimport com.atlassian.jira.user.ApplicationUserimport com.atlassian.jira.user.util.UserManagerfinal targetProjectKey = 'BA'final defaultReporterName = 'admin'final defaultIssueType = 'Unsorted'def messageUserProcessor = ComponentAccessor.getComponent(MessageUserProcessor) as MessageUserProcessordef userManager = ComponentAccessor.getComponent(UserManager)def projectManager = ComponentAccessor.projectManagerdef issueFactory = ComponentAccessor.issueFactorydef customFieldManager = ComponentAccessor.customFieldManagerJiraHome jiraHome = ComponentAccessor.getComponent(JiraHome)def subject = message.subject as Stringdef 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) ?: userdef 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.issuedef body = MailUtils.getBody(message)allCcRecipients.findAll {allCcRecipients.each {InternetAddress internetAddress = it as InternetAddressdef participant1 = messageUserProcessor.findUserByEmail(it.address)if (!participant1){
def email = internetAddress.addressdef fullName = internetAddress.getPersonal() ?: emaildef mailparticipant = userManager.getUserByName(fullName)participant1 = messageHandlerContext.createUser(email, null, email, fullName, null)}participantList.add(participant1)}}
allToRecipients.findAll {allToRecipients.each {InternetAddress internetAddress = it as InternetAddressdef participant1 = messageUserProcessor.findUserByEmail(it.address)if (!participant1){
//log.warn (">>>>> participantList: " + participantList)
if (issue) {def comment = MailUtils.getBody(message)issueObject.setCustomFieldValue(cf, participantList)messageHandlerContext.createComment(issue, reporterUser, comment, 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 // >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 // >FileUtils.writeByteArrayToFile(file, attachment.contents)messageHandlerContext.createAttachment(file, attachment.filename, attachment.contentType, user, issueObject) // <4>
It looks like you're new here. Sign in or register to get started.