Hi Community,
I have written a script on mailhandler to perform the following functionlality - compare recipients and participents of an exisiting tickets. If it is true, create a new ticket with the recipients as participants. Find below part of my script:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.exception.CreateException
import com.atlassian.jira.service.util.handler.MessageHandlerContext
import com.atlassian.jira.service.util.handler.MessageUserProcessor
import com.atlassian.jira.service.util.ServiceUtils
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.UserManager
import com.atlassian.mail.MailUtils
import com.atlassian.jira.issue.comments.CommentManager
import com.opensymphony.util.TextUtils
import javax.mail.Address
import javax.mail.Message
import javax.mail.internet.InternetAddress
final targetProjectKey = 'TE'
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.getSubject() as String
def body = MailUtils.getBody(message) as String
ApplicationUser user = userManager.getUserByName("jason")
ApplicationUser reportermail = messageUserProcessor.getAuthorFromSender(message) ?: user
def issueFromSubject = ServiceUtils.findIssueObjectInString(subject)
def commentManager = ComponentAccessor.getCommentManager()
def project = projectManager.getProjectObjByKey(targetProjectKey)
if(issueFromSubject){
def cf = ComponentAccessor.customFieldManager.getCustomFieldObjectsByName("Request participants")[0]
def cfVal = issueFromSubject.getCustomFieldValue (cf)?.collect
{ApplicationUser userNow -> user?.getUsername()}
def addressesTO = message.getRecipients(Message.RecipientType.TO) as List<Address>
def addressesCC = message.getRecipients(Message.RecipientType.CC) as List<Address>
def addressesBCC = message.getRecipients(Message.RecipientType.TO) as List<Address>
List<Address> addresses = []
if (addressesTO)
{ addresses.addAll(addressesTO) }
if (addressesCC)
{ addresses.addAll(addressesCC) }
if (addressesBCC)
{ addresses.addAll(addressesBCC) }
addresses.each{
InternetAddress internetAddress = it as InternetAddress
def email = internetAddress.address
def fullName = internetAddress.getPersonal() ?: email
def mailparticipant = userManager.getUserByName(fullName)
cfVal.each{
if(!fullName || !mailparticipant){
def issueObject = issueFactory.getIssue()
issueObject.setProjectObject(project)
issueObject.setSummary(subject)
issueObject.setDescription(MailUtils.getBody(message))
issueObject.setIssueTypeId(project.issueTypes.find
{ it.name == "request participants"}
.id)
issueObject.setReporter(reportermail)
messageHandlerContext.createIssue(user, issueObject)
}
}
}
}
The problem is that when the result is true, nothing happen.
Can you provide support on the issue please.
Thank you advance for your great help and support.
Best regards,
Jason Li