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
Just to be on the safe side, you might want to try this:
cfVal.each {
if(!fullName || !mailparticipant) {
// log something out first
}
}
This is to make sure the script is executing the codes within the if that we have.
Next, to create an issue using Scriptrunner, we can refer to this example. It should be something like this in your case:
cfVal.each {
if(!fullName || !mailparticipant) {
def issueInputParameters = issueService.newIssueInputParameters().with {
setProjectId(project.id)
setIssueTypeId(issueType.id)
setReporterId(reporter.key)
setSummary(summary)
}
def validationResult = issueService.validateCreate(loggedInUser, issueInputParameters)
assert validationResult.isValid() : validationResult.errorCollection
def result = issueService.create(loggedInUser, validationResult)
assert result.isValid() : result.errorCollection
}
}
You want to make sure the values that we are setting for the Project ID, Issue type ID and etc are correct before passing them to issueInputParameters varaible.
I hope that this helps.
Thanks,
Moga
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.