Hey!
I use the ScriptRunner Mail Handler to create new issue from incoming emails:
The script looks like this:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.exception.CreateException
import com.atlassian.jira.config.util.JiraHome
import com.atlassian.jira.service.services.file.FileService
import com.atlassian.jira.service.util.ServiceUtils
import com.atlassian.jira.service.util.handler.MessageHandlerContext
import com.atlassian.jira.service.util.handler.MessageUserProcessor
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.UserManager
import com.atlassian.mail.MailUtils
import javax.mail.Message
import javax.mail.internet.InternetAddress
import org.apache.commons.io.FileUtils
final defaultIssueType = 'Task'
def messageUserProcessor = ComponentAccessor.getComponent(MessageUserProcessor) as MessageUserProcessor
def userManager = ComponentAccessor.getComponent(UserManager)
def projectManager = ComponentAccessor.projectManager
def issueFactory = ComponentAccessor.issueFactory
JiraHome jiraHome = ComponentAccessor.getComponent(JiraHome)
def subject = message.subject as String
def getProject = subject.split(' ')[0]
log.warn("Проект пойманный в обработчике - ${getProject}")
def getBody = MailUtils.getBody(message).split("С уважением")
def issue = ServiceUtils.findIssueObjectInString(subject)
if (issue) {
log.warn("Данная задача уже имеется, ничего не делаем!")
return
}
def project = projectManager.getProjectObjByKey(getProject)
//def user = userManager.getUserByName(defaultReporterName)
def reporterUser = messageUserProcessor.getAuthorFromSender(message) //?: user
def allCcRecipients = message.getRecipients(Message.RecipientType.CC) as List<InternetAddress>
def firstValidUser = allCcRecipients.findResult {
messageUserProcessor.findUserByEmail(it.address)
}
def issueObject = issueFactory.issue
issueObject.setProjectObject(project)
issueObject.setSummary(subject.split("${getProject}")[1].trim())
issueObject.setDescription(getBody[0].trim())
issueObject.setIssueTypeId(project.issueTypes.find {it.name == defaultIssueType}.id)
issueObject.setReporter(reporterUser)
firstValidUser ?
issueObject.setAssignee(firstValidUser as ApplicationUser) :
log.error('Unable to retrieve valid user from message CC list, issue will be created without assignee')
try {
issue = messageHandlerContext.createIssue(reporterUser, issueObject)
def attachments = MailUtils.getAttachments(message)
def attachmentFileName = ""
attachments.each { MailUtils.Attachment attachment ->
if (attachment.filename.contains("utf_8")) {
attachmentFileName = "MsgAttachment.eml"
} else {
attachmentFileName = attachment.filename
}
def destination = new File(jiraHome.home, FileService.MAIL_DIR).getCanonicalFile()
def file = FileUtils.getFile(destination, attachmentFileName) as File
FileUtils.writeByteArrayToFile(file, attachment.contents)
messageHandlerContext.createAttachment(file, attachment.filename, attachment.contentType, reporterUser, issue)
}
log.warn("Задача ${issue} была успешно создана из обработчика jiratask@ntsmail.ru")
} catch (CreateException e) {
log.error('Error creating issue: ', e)
} catch (NullPointerException npe) {
log.error('Error adding attachments issue: ', npe)
} catch (FileNotFoundException fnfe) {
log.error('The filename, directory name, or volume label syntax is incorrect: ', fnfe)
}
And when creating new issue from emails, I would like to display the attached screenshots in the issue description, since this is displayed in the body of the email.
And it turns out that screenshots are added only to the attachments of the issue, and they disappear from the description itself, which has a bad effect on the context of the issue. Please tell me if there is any way to display screenshots in the task description?
Here is an example of an email being sent:
And here's what the issue looks like:
For your requirement, you could try something like this:-
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.config.util.JiraHome
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.service.services.file.FileService
import com.atlassian.jira.service.util.ServiceUtils
import com.atlassian.jira.service.util.handler.MessageUserProcessor
import com.atlassian.mail.MailUtils
import org.apache.commons.io.FileUtils
def userManager = ComponentAccessor.userManager
def projectManager = ComponentAccessor.projectManager
def issueManager = ComponentAccessor.issueManager
def issueFactory = ComponentAccessor.issueFactory
def messageUserProcessor = ComponentAccessor.getComponent(MessageUserProcessor)
def jiraHome = ComponentAccessor.getComponent(JiraHome)
def subject = message.subject as String
def issue = ServiceUtils.findIssueObjectInString(subject) as MutableIssue
if (issue) {
return
}
def user = userManager.getUserByName("admin")
def reporter1 = messageUserProcessor.getAuthorFromSender(message) ?: user
def project = projectManager.getProjectObjByKey("MOCK")
def issueObject = issueFactory.issue
issueObject.setProjectObject(project)
issueObject.setSummary(subject)
issueObject.setDescription(MailUtils.getBody(message))
issueObject.setIssueTypeId(project.issueTypes.find { it.name == "Bug" }.id)
issueObject.setReporter(reporter1)
issue = messageHandlerContext.createIssue(user, issueObject) as MutableIssue
def attachments = MailUtils.getAttachments(message)
def issueDescription = new StringBuilder(issue.description).append("\n")
def attachmentFileNames = [] as ArrayList<String>
def destination = new File(jiraHome.home, FileService.MAIL_DIR).canonicalFile
attachments.each { it ->
def file = FileUtils.getFile(destination, it.filename) as File
FileUtils.writeByteArrayToFile(file, it.contents)
messageHandlerContext.createAttachment(file, it.filename, it.contentType, user, issue)
attachmentFileNames.add(it.filename)
}
FileUtils.forceDelete(destination.listFiles().first())
attachmentFileNames.eachWithIndex { item, index ->
issueDescription.append("Step ${index+1}: \n").append("!${item}|thumbnail!").append("\n")
}
issue.setDescription(issueDescription.toString())
issueManager.updateIssue(user,issue, EventDispatchOption.DO_NOT_DISPATCH,false)
Please note this sample code is not 100% exact to your environment. Hence, you will need to make the required modifications.
Below is sample test result:-
I hope this helps to answer your question. :)
Thank you and Kind Regards,
Ram
Hey @Ram Kumar Aravindakshan _Adaptavist_
Thank you very much for the answer you provided.
Yes, screenshots are really added to the task description, this is cool, which is what I wanted to achieve.
But the description of the task will not necessarily be the same as I indicated in my example. The structure of the description may be completely different from this example and in this case such a change in the description will not work)
attachmentFileNames.eachWithIndex { item, index ->
issueDescription.append("Step ${index+1}: \n").append("!${item}|thumbnail!").append("\n")
}
Do you know how to get the content of an incoming email, and not its text? If you get the content of the email, then I think it will be possible to determine exactly where you need to insert a screenshot.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Glad to hear the solution worked for you. :)
If your question is answered, please accept the solution provided.
Regarding your latest question, if you want to get the content of the email, you will have to play with the MailUtils and maybe try to add if/else conditions to determine what is returned by it.
Also, you can add additional if/else conditions to determine how you want to format the content. You could modify the section:-
attachmentFileNames.eachWithIndex { item, index ->
issueDescription.append("Step ${index+1}: \n").append("!${item}|thumbnail!").append("\n")
}
and include if / else conditions like below:-
attachmentFileNames.eachWithIndex { item, index ->
if(item.contains("<Some Value>") {
issueDescription.append("!${item}|thumbnail!").append("\n")
} else {
issueDescription.append("Step ${index+1}: \n").append("!${item}|thumbnail!").append("\n")
}
}
Please note, this sample code is not 100% exact to your environment. Hence, you will need to make the required modifications.
I hope this helps to answer your question. :)
Thank you and Kind Regards,
Ram
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.