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
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.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.