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

Add attachments via comment

Pourna Radha-Burhoo July 27, 2021

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 // <3>
FileUtils.writeByteArrayToFile(file, attachment.contents)
messageHandlerContext.createAttachment(file, attachment.filename, attachment.contentType, user, issue) // <4>
}

1 answer

1 accepted

0 votes
Answer accepted
Helmy Ibrahim _Adaptavist_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 29, 2021

Hi Swarna,

So when a user reply to the mail, this block of code will be executed:

if (issue) {
def comment = MailUtils.getBody(message)
messageHandlerContext.createComment(issue, reporter, nonQuotedMessageBody, fals

// Handle attachment here

return
}

And ultimately adding a comment to the issue.

So, you will need to handle the attachment inside here, just like you did at the end of the script.

I hope this helps.

Cheers,
Helmy

Pourna Radha-Burhoo July 29, 2021

@Helmy Ibrahim _Adaptavist_ , thanks a lot it has solved my issue.

 

I got another issue to add participants in the code above. If  user add "To" or "CC" add them as participants.

I am stuck there how to add them. Kindly help me

 

Thanks,

swarna

Helmy Ibrahim _Adaptavist_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 30, 2021

Hi Swarna,

I can't really find any example specific to this but we do have an example where you can add CC users as to Watchers: Add CC to Watchers

You can use the above to get the idea of it and convert it to add the users to Request Participants following this script: Add list of users to Request Participants 

Then you can call setCustomFieldValue on the issue object returned by this line of your script:

issue = messageHandlerContext.createIssue(user, issueObject) // <1>

And not on the issueObject. Perhaps you have to convert it to MutableIssue first before you can call setCustomFieldValue:

def newIssue = issue as MutableIssue
newIssue.setCustomFieldValue(requestParticipantsField, applicationUsers)

Then, since this is not a post function, you need to call IssueManager.updateIssue against the newIssue object above.

issueManager.updateIssue(user, newIssue, EventDispatchOption.DO_NOT_DISPATCH, false

I hope this helps!

Cheers,
Helmy

Pourna Radha-Burhoo August 2, 2021

Hello @Helmy Ibrahim _Adaptavist_ 

 

I have managed to create users but they are consuming licenses.

I want to check if users exist add to participant list if not add. Please see my codes below: It is not working

mport com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.service.util.ServiceUtils
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.event.type.EventDispatchOption
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
final targetProjectKey = 'BA'
final defaultReporterName = 'admin'
final defaultIssueType = 'Unsorted'
def messageUserProcessor = ComponentAccessor.getComponent(MessageUserProcessor) as MessageUserProcessor
def userManager = ComponentAccessor.getComponent(UserManager)
def projectManager = ComponentAccessor.projectManager
def issueFactory = ComponentAccessor.issueFactory
def customFieldManager = ComponentAccessor.customFieldManager

def subject = message.subject as String
def issue = ServiceUtils.findIssueObjectInString(subject)
def cf = customFieldManager.getCustomFieldObject("customfield_10101")
def project = projectManager.getProjectObjByKey(targetProjectKey)
def user = userManager.getUserByName(defaultReporterName)
def reporterUser = messageUserProcessor.getAuthorFromSender(message) ?: user
def allCcRecipients = message.getRecipients(Message.RecipientType.CC) as ArrayList<InternetAddress>
def participantList = [] as ArrayList<ApplicationUser>
def issueObject = issueFactory.issue
def body = MailUtils.getBody(message)
allCcRecipients.findAll {
allCcRecipients.each {
InternetAddress internetAddress = it as InternetAddress

def email = internetAddress.address
def fullName = internetAddress.getPersonal() ?: email
def mailparticipant = userManager.getUserByName(fullName)
if (!mailparticipant )

{ mailparticipant = messageHandlerContext.createUser(email, null, email, fullName, null) // def participant = messageUserProcessor.findUserByEmail(it.address) as ApplicationUser participantList.add(mailparticipant)}

else

{ def listOfExistingParticipants = issue.getCustomFieldValue(cf) as ArrayList<ApplicationUser> listOfExistingParticipants.add(mailparticipant) }

}
}
if (issue) {
def listOfExistingParticipants = issue.getCustomFieldValue(cf) as ArrayList<ApplicationUser>

if(participantList.containsAll(listOfExistingParticipants) ) {
def comment = "${subject} - ${body}"
messageHandlerContext.createComment(issue, reporterUser, comment, false)
}
} else {

issueObject.setProjectObject(project)
issueObject.setSummary(subject)
issueObject.setDescription(MailUtils.getBody(message))
issueObject.setIssueTypeId(project.issueTypes.find

{ it.name == defaultIssueType }

.id)
issueObject.setReporter(reporterUser)
issueObject.setCustomFieldValue(cf, participantList)
messageHandlerContext.createIssue(user, issueObject)
ComponentAccessor.getIssueManager().updateIssue(user, issueObject, EventDispatchOption.ISSUE_UPDATED, false)
}

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events