Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to check long attachment name in mail handler?

nihcet May 30, 2020

Hi,
There is 256 character limit for attachment in Jira. We create issue from scriptrunner mail handler. When e-mail have long than 256 character attachment, the email is not deleted from the inbox after creating a problem. And a e-mail is create a lot of issues.
How to check long attachment name in mail handler?

Thanks

Jira v7.12.1

Scriptrunner v5.6.1

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
import javax.mail.Message

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) {
return
}

ApplicationUser user = userManager.getUserByName("jiramail")
ApplicationUser reporter = messageUserProcessor.getAuthorFromSender(message) ?: user
def sender = message.getFrom() as String
def project = projectManager.getProjectObjByKey("AAA")
def issueObject = issueFactory.getIssue()
issueObject.setProjectObject(project)


def addressesTo = message.getRecipients(Message.RecipientType.TO) as String
def addressesCc = message.getRecipients(Message.RecipientType.CC) as String
def addressesBcc = message.getRecipients(Message.RecipientType.BCC) as String
def allAddresses = (addressesTo + addressesCc + addressesBcc) as String
// def addresses = addressesTo.take(254)
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def kab = customFieldManager.getCustomFieldObjectByName("BBB")
issueObject.setCustomFieldValue(kab, allAddresses)


def sortSubject = subject.take(255) as String
issueObject.setSummary(sortSubject)
// issueObject.setSummary(subject)

issueObject.setDescription(MailUtils.getBody(message))

issueObject.setIssueTypeId(project.issueTypes.find { it.name == "Mail" }.id)
issueObject.setReporter(reporter)

// add sender to custom field
def emailCf = customFieldManager.getCustomFieldObjectByName("CCC")
issueObject.setCustomFieldValue(emailCf, sender)
// add sender to custom field

issue = messageHandlerContext.createIssue(user, issueObject)

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)
}

 

0 answers

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
Atlassian, Team '25 Europe, Atlassian conference, AI-powered teamwork, Atlassian events, keynote speakers, Barcelona tech conference, Atlassian Williams Racing, Carlos Sainz, Jenson Button, Mike Cannon-Brookes, Anu Bharadwaj, Atlassian Rovo

Atlassian Williams Racing is coming to Team '25 Europe 🏎️

Discover the teamwork and precision behind success on and off the track. See how this high-performing team makes the impossible possible. Be one of the first 100 customers to register for a chance to win 2 tickets to an Atlassian Williams 2026 race.

Register now
AUG Leaders

Atlassian Community Events