Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to add participants via mail handler

Pourna Radha-Burhoo July 27, 2021

Hi,

 

I want to add participants via mail handler script runner. Kindly provide any code.

 

Thanks,
Swarna

2 answers

0 votes
Daniel Ebers
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.
July 31, 2021

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

Pourna Radha-Burhoo August 1, 2021

@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.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 javax.mail.internet.InternetAddress
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

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 participantList = [] as ArrayList<ApplicationUser>
def issueObject = issueFactory.issue
def body = MailUtils.getBody(message)
allCcRecipients.findAll {
allCcRecipients.each {
InternetAddress internetAddress = it as InternetAddress

def email = internetAddress.address
def fullName = internetAddress.getPersonal() ?: email
def mailparticipant = userManager.getUserByName(fullName)
mailparticipant = messageHandlerContext.createUser(email, null, email, fullName, null)
// def participant = messageUserProcessor.findUserByEmail(it.address) as ApplicationUser
participantList.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)
}

 

 

 

 

 

 

 

Thanks,

Swarna

Daniel Ebers
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.
August 1, 2021

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".

0 votes
Leo
Community Champion
July 29, 2021

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

Pourna Radha-Burhoo July 29, 2021

@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

Leo
Community Champion
August 4, 2021

@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.userUtil
uu.removeUserFromGroup(group, user)

 

Hope this helps,

 

Regards,

Leo

Pourna Radha-Burhoo August 4, 2021

@Leo ,

 

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.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)
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 // <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

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events