Hello,
I want to add attachment and a user replies to a mail via script. Currently i am doing this via mail handler - script runner.
If issue key is present in ticket, a comment should be added . If no issue key is present, new ticket should be created. Please see below script . Kindly advice. Thanks Swarna
import com.atlassian.jira.component.ComponentAccessor
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.MessageUserProcessor
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.UserManager
import com.atlassian.mail.MailUtils
import org.apache.commons.io.FileUtils
//def reporterUser = messageUserProcessor.getAuthorFromSender(message) ?: user
def userManager = ComponentAccessor.getComponent(UserManager)
def projectManager = ComponentAccessor.getProjectManager()
def issueFactory = ComponentAccessor.getIssueFactory()
def messageUserProcessor = ComponentAccessor.getComponent(MessageUserProcessor)
JiraHome jiraHome = ComponentAccessor.getComponent(JiraHome)
def subject = message.getSubject() as String
def issue = ServiceUtils.findIssueObjectInString(subject)
if (issue) {
def comment = MailUtils.getBody(message)
messageHandlerContext.createComment(issue, reporter, nonQuotedMessageBody, false)
return
}
// need to attachment code
ApplicationUser user = userManager.getUserByName("admin")
ApplicationUser reporter = messageUserProcessor.getAuthorFromSender(message) ?: user
def project = projectManager.getProjectObjByKey("BA")
def issueObject = issueFactory.getIssue()
issueObject.setProjectObject(project)
issueObject.setSummary(subject)
issueObject.setDescription(MailUtils.getBody(message))
issueObject.setIssueTypeId(project.issueTypes.find { it.name == "Unsorted" }.id)
issueObject.setReporter(reporter)
issue = messageHandlerContext.createIssue(user, issueObject) // <1>
def attachments = MailUtils.getAttachments(message) // <2>
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) // <4>
}