Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How to update component while creating issue through scripted mail handler

Sudheer Kumar Vempala August 10, 2022

Hello Community,

We have a requirement where components has to be updated while Issue created via mail handler.

Mail reached Mail_Q1 ---> Create Issue --> set the component as "Com1"  

Mail reached Mail_Q2 ---> Create Issue --> set the component as "Com2"  

Mail reached Mail_Q3 ---> Create Issue --> set the component as "Com3"  

So that we can differentiate Issues created from different mail queues. 

I have updated the script but getting error 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.exception.CreateException
import com.atlassian.jira.service.util.ServiceUtils
import com.atlassian.jira.service.util.handler.MessageHandlerContext
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 = 'TEST'
final defaultReporterName = 'admin'
final defaultIssueType = 'Task'

def messageUserProcessor = ComponentAccessor.getComponent(MessageUserProcessor) as MessageUserProcessor
def userManager = ComponentAccessor.getComponent(UserManager)
def projectManager = ComponentAccessor.projectManager
def issueFactory = ComponentAccessor.issueFactory

def subject = message.subject as String
def issue = ServiceUtils.findIssueObjectInString(subject)

if (issue) {
// issue already exists so do nothing
return
}

def project = projectManager.getProjectObjByKey(targetProjectKey)

def user = userManager.getUserByName(defaultReporterName)
def reporterUser = messageUserProcessor.getAuthorFromSender(message) ?: user

def allCcRecipients = message.getRecipients(Message.RecipientType.CC) as List<InternetAddress>
def firstValidUser = allCcRecipients.findResult {
messageUserProcessor.findUserByEmail(it.address)
}

def issueObject = issueFactory.issue
def issueManager = ComponentAccessor.issueManager
def component = ComponentAccessor.projectComponentManager.findByComponentName(issue.projectObject.id, "COM1")
issueObject.setProjectObject(project)
issueObject.setSummary(subject)
issueObject.setDescription(MailUtils.getBody(message))
issueObject.setIssueTypeId(project.issueTypes.find { it.name == defaultIssueType }.id)
issueObject.setReporter(reporterUser)
issue.setComponent([component])
issueManager.updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
firstValidUser ?
issueObject.setAssignee(firstValidUser as ApplicationUser) :
log.error('Unable to retrieve valid user from message CC list, issue will be created without assignee')

try {
(messageHandlerContext as MessageHandlerContext).createIssue(user, issueObject)
} catch (CreateException e) {
log.error('Error creating issue: ', e)
}

---error lines-------------

issue.setComponent([component])
issueManager.updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH, false)



Can some help me to set the component also while creating an Issues through mail handler.

-sudheer

1 answer

Suggest an answer

Log in or Sign up to answer
0 votes
Nic Brough -Adaptavist-
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 11, 2022

Welcome to the Atlassian Community!

Ploughing through someone else's code is hard work.  It is clear what the intention is, but could you tell us what the error messages are and what line they are pointing to in your code?

TAGS
AUG Leaders

Atlassian Community Events