Last active
April 11, 2019 01:14
-
-
Save jechlin/dfb80bb70b2fa5e5685975e19b450226 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This script should be added as a Custom Script Listener, listening for ServiceDeskCommentEvent | |
* | |
* It will send attachments to the reporter and participants by email. | |
*/ | |
import com.atlassian.jira.component.ComponentAccessor | |
import com.atlassian.jira.user.ApplicationUser | |
import com.atlassian.jira.util.AttachmentUtils | |
import com.atlassian.mail.Email | |
import com.atlassian.mail.queue.SingleMailQueueItem | |
import javax.activation.DataHandler | |
import javax.activation.FileDataSource | |
import javax.mail.BodyPart | |
import javax.mail.internet.MimeBodyPart | |
import javax.mail.internet.MimeMultipart | |
import javax.mail.internet.MimeUtility | |
def groupManager = ComponentAccessor.groupManager | |
def changeHistoryManager = ComponentAccessor.changeHistoryManager | |
def attachmentManager = ComponentAccessor.attachmentManager | |
def customFieldManager = ComponentAccessor.customFieldManager | |
def issue = event.issue | |
// todo: update the issue key | |
if (issue.projectObject.key != "AAA") { | |
// check project condition, return unless project key is AAA | |
return | |
} | |
// read attachments just added | |
def attachmentIds = changeHistoryManager.getChangeHistories(issue).last().getChangeItemBeans().findAll { | |
it.field == 'Attachment' | |
}.collect { | |
it.to as Long | |
} | |
// do we have any attachments under 2mb | |
def attachments = attachmentIds.findResults { attachmentId -> | |
def attachment = attachmentManager.getAttachment(attachmentId) | |
attachment.filesize < 2 * 1024**2 ? | |
attachment : | |
null | |
} | |
if (!attachments) { | |
// if all attachments over 2mb or no attachments, bail | |
return | |
} | |
def participants = customFieldManager.getCustomFieldObjectByName('Request participants') | |
List<ApplicationUser> recipients = [issue.reporter] | |
recipients.addAll(issue.getCustomFieldValue(participants) as List<ApplicationUser>) | |
// for each user not a member of jira-users send them the email | |
recipients.each { recipient -> | |
if (groupManager.isUserInGroup(recipient, "jira-users")) { | |
// ignore users who are in jira-users group | |
return | |
} | |
def email = new Email(recipient.emailAddress) | |
email.with { | |
subject = "Attachments for ${issue.key}" | |
body = "Here are the recently-added attachments for ${issue.key}" | |
} | |
def multipart = email.getMultipart() ?: new MimeMultipart() | |
attachmentIds.each { attachmentId -> | |
def attachment = attachmentManager.getAttachment(attachmentId) | |
File attFile = AttachmentUtils.getAttachmentFile(attachment) | |
BodyPart attPart = new MimeBodyPart() | |
FileDataSource attFds = new FileDataSource(attFile) | |
attPart.setDataHandler(new DataHandler(attFds)) | |
attPart.setFileName(MimeUtility.encodeText(attachment.filename)) | |
multipart.addBodyPart(attPart) | |
} | |
email.setMultipart(multipart) | |
ComponentAccessor.getMailQueue().addItem(new SingleMailQueueItem(email)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment