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

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Script Runner Mail Handler Edited



i use a custom mailhandler with groovy script to add comments generated from email bodies. i use the sender address which is a customer of the service desk as author. But all comments are marked as internal. After some testing i found out that only users with agent rights can create public comments. If you are no agent, comments are always internal.

Here is some code


import com.atlassian.jira.project.UpdateProjectParameters
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.scriptrunner.canned.jira.admin.CopyProject
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.atlassian.jira.issue.Issue
//import com.onresolve.jira.groovy.user.FormField
import org.apache.log4j.Logger
import org.apache.log4j.Level
import com.atlassian.crowd.embedded.impl.ImmutableGroup
import com.atlassian.crowd.embedded.api.Group;
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.plugin.webfragment.model.JiraHelper;
import com.atlassian.jira.bc.projectroles.ProjectRoleService
import com.atlassian.jira.util.SimpleErrorCollection
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.project.Project
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.project.ProjectCategory
import com.atlassian.jira.bc.user.UserService
import com.atlassian.jira.user.util.UserUtil
import com.atlassian.jira.service.util.ServiceUtils
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.UserManager
import com.atlassian.mail.MailUtils
import com.atlassian.jira.user.UserUtils
import com.atlassian.jira.service.util.handler.MessageUserProcessor
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.bc.issue.IssueService.CreateValidationResult
import com.atlassian.jira.bc.issue.IssueService.IssueResult
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.jira.config.util.JiraHome
import com.atlassian.jira.util.json.JSONObject

// get log in user to proof that you can create user
def user = ComponentAccessor.getUserManager().getUserByName('admin')

// get Manager classes
def userService = ComponentAccessor.getComponent(UserService)
def userManager = ComponentAccessor.getComponent(UserManager)
def projectManager = ComponentAccessor.getProjectManager()
def issueFactory = ComponentAccessor.getIssueFactory()
def messageUserProcessor = ComponentAccessor.getComponent(MessageUserProcessor)
def commentManager = ComponentAccessor.getCommentManager()
def groupManager = ComponentAccessor.getGroupManager()

final SD_PUBLIC_COMMENT = "sd.public.comment"
//def properties = [(SD_PUBLIC_COMMENT): new JSONObject(["internal": false])]
//def properties = [(SD_PUBLIC_COMMENT): new JSONObject().put("internal", false).toString()]
def properties = [(SD_PUBLIC_COMMENT): new JSONObject(["internal": false] as Map)]
// get email sender
//def reporter = MailUtils.getSenders(message)
def reporter = ""
final List<String> senders = MailUtils.getSenders(message);
for (final String emailAddress : senders)
reporter = emailAddress
//check if user is in Whitelist

// check if sender is already a known user
ApplicationUser ReporterFrom = messageUserProcessor.getAuthorFromSender(message)

Project project = projectManager.getProjectByCurrentKey("ACSD")

if(ReporterFrom == null){
//user unknown - create user

// create issue or comment
log.debug("create the issue")
JiraHome jiraHome = ComponentAccessor.getComponent(JiraHome)
def subject = message.getSubject() as String
def issue = ServiceUtils.findIssueObjectInString(subject)
if (issue) {
log.debug("Add comment to existing issue")
def comment = commentManager.create(issue, user, MailUtils.getBody(message), null, null, new Date(), properties, true)
else {

//ApplicationUser user = userManager.getUserByName("admin")
//ApplicationUser reporter = messageUserProcessor.getAuthorFromSender(message) ?: user

def issueObject = issueFactory.getIssue()
issueObject.setIssueTypeId(project.issueTypes.find { == "Incident" }.id)
issue = messageHandlerContext.createIssue(user, issueObject)

// Get Attachments out of the email
def attachments = MailUtils.getAttachments(message)

attachments.each { MailUtils.Attachment attachment ->
def destination = new File(jiraHome.home, FileService.MAIL_DIR).getCanonicalFile()
def file = FileUtils.getFile(destination, attachment.filename) as File
FileUtils.writeByteArrayToFile(file, attachment.contents)
messageHandlerContext.createAttachment(file, attachment.filename, attachment.contentType, user, issue)

return 'finished'


Did anybody runs into the same issue?


best regards 

2 answers

Hi @Andre Schmidt ,


def issue = ServiceUtils.findIssueObjectInString(subject)

 worked to find the existing issue as my mail handler is not able to get it and creating a new issue every time with the same summary?



Hi @Eshwar Palem ,

yes, the function scans the subject to find an existing issue ID. If so the issue object will be returned. So he checks the issue id, not the summary


best regards

Hi @Andre Schmidt


The method is returning null when I printed in the logs and it is creating duplicate issues when I am replying to the existing email thread.

Can you provide some suggestions on this?



When you create the issue,  issue id need to be included in the issue subject. Because findIssueObjectsInString(String) use string tokenizing to search existing issue key. 

That will avoid new issue creation when commenting via email.

Suggest an answer

Log in or Sign up to answer
Community showcase
Published in Jira

⏰ Day in the life of a Jira Admin!

Hello Community! We thoroughly enjoyed this just-for-fun conversation in the Jira Admin Group about what it's like to be a Jira Admin. For #JiraJuly, our talented designers created these graphics t...

478 views 2 17
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you