Working with Script mail handler

Pooja Singh September 16, 2020

Hello Community,

I am currently writing a script for using the script mail handler for achieving the following use case.

  1. We are already using script runner for sending out email to non jira users.
  2. When those non-jira users reply to the email, it should come back to Jira and transition the ticket as well as add comment onto it

As per what I have read in the documentations, it is possible to do so using the script mail handler in the incoming mail handler settings.

In the document it shows how to get the commits from 3rd party app like bitbucket etc.

But in my case it will be coming from email. Also, I am not sure how to set the comment author since the actual author will be a non jira user.

I managed to write the following code it does seem to be working. So far it does not give any errors.

import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.service.util.ServiceUtils
import com.atlassian.mail.MailUtils
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.service.util.handler.MessageUserProcessor

CommentManager commentManager = ComponentAccessor.getCommentManager()
def messageUserProcessor = ComponentAccessor.getComponent(MessageUserProcessor)
def subject = message.getSubject() as String
def issue = ServiceUtils.findIssueObjectInString(subject)
def body = MailUtils.getBody(message)

def sender = messageUserProcessor.getAuthorFromSender(message) as String
if (issue) {
def comment = sender + subject + " - " + body
commentManager.create(issue, reporter, comment, false)
}

If anyone has worked in this before? It would be a big help.

Thanks in advance,

Pooja

1 answer

0 votes
Iresh Rupasinghe January 1, 2021

You can get external reporter identity using following code:

// get email sender
def reporterEmail = ""
final List<String> senders = MailUtils.getSenders(message);
for (final String emailAddress : senders)
{
   reporterEmail = emailAddress
}

 

However, use default user to create issues for non jira users. That will help to reduce number of licenses. 

Suggest an answer

Log in or Sign up to answer