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

Next challenges

Recent achievements

Recognition

  • Give kudos
  • My kudos

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

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

Hi,

 

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.security.roles.ProjectRole
import com.atlassian.jira.util.SimpleErrorCollection
import com.atlassian.jira.security.roles.ProjectRoleManager
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.project.Project
import com.atlassian.jira.security.roles.ProjectRoleActor
import com.atlassian.jira.security.roles.ProjectRoleActors
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.security.roles.RoleActor
import com.atlassian.jira.project.ProjectCategory
import com.atlassian.jira.bc.user.UserService
import com.atlassian.jira.security.groups.GroupManager
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.service.services.file.FileService
import org.apache.commons.io.FileUtils
import com.atlassian.jira.util.json.JSONObject
import com.atlassian.jira.bc.issue.comment.property.CommentPropertyService





// 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.setProjectObject(project)
issueObject.setSummary(subject)
issueObject.setDescription(MailUtils.getBody(message))
issueObject.setIssueTypeId(project.issueTypes.find { it.name == "Incident" }.id)
issueObject.setReporter(ReporterFrom)
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 

1 answer

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?

Regards,

Eshwar.

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?

Regards,

Eshwar.

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Confluence Cloud

Share your Confluence Cloud experience for a chance to win $500!

Take our 5-minute survey to win a $500 Visa gift card! Are you currently using Confluence Cloud? We want to hear from you! Fill out this quick survey about your Confluence Cloud experience so we ...

240 views 5 13
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