checking attachment in Jira ticket while changing the state

Omprakash Thamsetty
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.
September 8, 2015

Hi All,

 

When I am changing the state from "submitted" to "Need Information" then one window pop-up. In that window we have option to add attachment. If I add the attachment to the issue in pop-up windows then email should trigger with added attachment in popup after clicking on "update" button. Is there any method or class available to find it?

 

Right now emails are sending, what ever file is latest but we need only when file added in pop-up windows.

 

Any help much appreciated?

 

Thanks

4 answers

1 accepted

0 votes
Answer accepted
Taha Khanzada
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.
September 8, 2015

Hi Oprakash,

You might be interested in looking at below link where similar kind of requirement is answered

https://answers.atlassian.com/questions/98885

 Taha

0 votes
Omprakash Thamsetty
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.
September 14, 2015

Hi Taha khanzada,

 

Your solutions work for me. I have modified little in my above script and its worked for me. Now I am able to send files as an attachment if it is 0 or 1 or more than one.

 

We need to keep Bodypart attachBody = new MimeBodyPart() line within the loop. Here is the working code. Hope it will help others.

 

package com.custom
 
import com.atlassian.jira.event.issue.AbstractIssueEventListener
import com.atlassian.jira.event.issue.IssueEvent
 
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.component.ComponentAccessor
 
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.AttachmentManager;
import com.atlassian.jira.issue.attachment.Attachment;
import com.atlassian.mail.queue.SingleMailQueueItem
 
import com.atlassian.mail.Email
import com.atlassian.mail.server.MailServerManager
import com.atlassian.mail.server.SMTPMailServer
import org.apache.log4j.Category
import java.sql.Timestamp
 
import com.atlassian.jira.util.AttachmentUtils;
import com.atlassian.jira.util.PathUtils
 
import javax.mail.Multipart
import javax.mail.internet.MimeMultipart
import javax.mail.BodyPart
import javax.mail.internet.MimeBodyPart
import javax.activation.FileDataSource
import javax.activation.DataHandler
 
/ Logger
def Category log = Category.getInstance("com.onresolve.jira.groovy.PostFunction")
 
// Set the logging level to DEBUG
log.setLevel(org.apache.log4j.Level.DEBUG);
log.debug ("Event 400 : Sending email on ${issue.getStatusObject().name}")
 
ComponentManager componentManager = ComponentManager.getInstance()
CustomFieldManager customFieldManager = componentManager.getCustomFieldManager()
MailServerManager mailServerManager = componentManager.getMailServerManager()
SMTPMailServer mailServer = mailServerManager.getDefaultSMTPMailServer()
AttachmentManager attachmentManager = componentManager.getAttachmentManager()
pathManager = componentManager.getAttachmentPathManager()
 
 
if (mailServer) {
    //TO: Email Address//
 
      def  toemail = test@gmail.com
    Email email = new Email(toemail)
     
        
    ///////Email Subject////////
       email.setSubject("($issue.key) - $issue.summary")
   
    ///////Set the Email Mime Type///// 
    email.setMimeType("text/Plain");
     
    ///////Email Body////////  
 
 
String content
 
 
   if( issue.status.name == "Resolved"){
        
 
 ////Checking for attachment.
       Multipart multipart = new MimeMultipart("mixed");
        
     
 
           def changeItems = transientVars["changeItems"]
           def uploadChanges = changeItems.findAll { item -> item.getField() == "Attachment" && item.getFieldType() == "jira" }
            
           uploadChanges.each { uploadChange ->
           def Attachment attachment = attachmentManager.getAttachment(uploadChange.getTo()?.toLong())
 
              if (attachment) {
                addAttachmentsToMessage(multipart,attachment);
                
          }
            email.setMultipart(multipart);     
 
          content = content +
       "\nResolved:              ${issue.resolutionDate.format('MM/dd/yyyy hh:mm a')}" +
       "\nResolution:            $issue.resolution.name"
 
 
       //set the body
       email.setBody(content)
         
      ///////SEND Email ////////
       SingleMailQueueItem item = new SingleMailQueueItem(email);
       ComponentAccessor.getMailQueue().addItem(item);
           }
 
}else {
   log.error "No SMTP mail server defined"
}


private void addAttachmentsToMessage(Multipart multipart, Attachment attachment){

                BodyPart attachBody = new MimeBodyPart();
                def attachedFile = AttachmentUtils.getAttachmentFile(attachment);

                FileDataSource source = new FileDataSource(attachedFile);
                attachBody.setDataHandler(new DataHandler(source));
               attachBody.setFileName(attachment.filename);
                multipart.addBodyPart(attachBody);
}
0 votes
Omprakash Thamsetty
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.
September 11, 2015

Hi Taha and Jaime,

 

I have modified my script. Its just sending last file but I want to send all files, which are attached while "Resolve" transition.

 

Condition I checked

    1) If I attach one file ( file1.txt) then its working fine without any issue

    2) If I attach 2 files ( file2.txt and file3.txt ) then file3.txt sending 2 times in one email but I want to send file2.txt and file3.txt both . I am missing something in addBodypart  or adding files to email.

 

Can any one help me to modify my code. I think need to put some loop to add all files.

 

Here is the code that I have right now

 

package com.custom

import com.atlassian.jira.event.issue.AbstractIssueEventListener
import com.atlassian.jira.event.issue.IssueEvent

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.AttachmentManager;
import com.atlassian.jira.issue.attachment.Attachment;
import com.atlassian.mail.queue.SingleMailQueueItem

import com.atlassian.mail.Email
import com.atlassian.mail.server.MailServerManager
import com.atlassian.mail.server.SMTPMailServer
import org.apache.log4j.Category
import java.sql.Timestamp

import com.atlassian.jira.util.AttachmentUtils;
import com.atlassian.jira.util.PathUtils

import javax.mail.Multipart
import javax.mail.internet.MimeMultipart
import javax.mail.BodyPart
import javax.mail.internet.MimeBodyPart
import javax.activation.FileDataSource
import javax.activation.DataHandler

/ Logger
def Category log = Category.getInstance("com.onresolve.jira.groovy.PostFunction")

// Set the logging level to DEBUG
log.setLevel(org.apache.log4j.Level.DEBUG);
log.debug ("Event 400 : Sending email on ${issue.getStatusObject().name}")

ComponentManager componentManager = ComponentManager.getInstance()
CustomFieldManager customFieldManager = componentManager.getCustomFieldManager()
MailServerManager mailServerManager = componentManager.getMailServerManager()
SMTPMailServer mailServer = mailServerManager.getDefaultSMTPMailServer()
AttachmentManager attachmentManager = componentManager.getAttachmentManager()
pathManager = componentManager.getAttachmentPathManager()


if (mailServer) {
    //TO: Email Address//

      def  toemail = test@gmail.com
    Email email = new Email(toemail)
    
       
    ///////Email Subject////////
       email.setSubject("($issue.key) - $issue.summary")
  
    ///////Set the Email Mime Type/////  
    email.setMimeType("text/Plain");
    
    ///////Email Body////////   


String content


   if( issue.status.name == "Resolved"){
       

 ////Checking for attachment.
       Multipart multipart = new MimeMultipart("mixed");
        BodyPart attachBody = new MimeBodyPart();
    

           def changeItems = transientVars["changeItems"]
           def uploadChanges = changeItems.findAll { item -> item.getField() == "Attachment" && item.getFieldType() == "jira" }
           
           uploadChanges.each { uploadChange ->
           def Attachment attachment = attachmentManager.getAttachment(uploadChange.getTo()?.toLong())

              if (attachment) {
                def attachedFile = AttachmentUtils.getAttachmentFile(attachment);

                FileDataSource source = new FileDataSource(attachedFile);
                attachBody.setDataHandler(new DataHandler(source));
                attachBody.setFileName(attachment.filename);
                multipart.addBodyPart(attachBody);
                email.setMultipart(multipart);
          }
                

          content = content +
       "\nResolved:              ${issue.resolutionDate.format('MM/dd/yyyy hh:mm a')}" +
       "\nResolution:            $issue.resolution.name"


       //set the body
       email.setBody(content)
        
      ///////SEND Email ////////
       SingleMailQueueItem item = new SingleMailQueueItem(email);
       ComponentAccessor.getMailQueue().addItem(item);
           }

}else {
   log.error "No SMTP mail server defined"
}
0 votes
JamieA
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.
September 8, 2015

I'm not sure what you mean really. When you say pop-up windows I think you are talking about a workflow transition?

> Right now emails are sending, what ever file is latest but we need only when file added in pop-up windows.

How are you currently sending an email including the attachment? If you are using the "send email" post-function that comes with ScriptRunner, there is an option in the attachments section to only include "new in this transition" or something like that, which sounds like what you want.

Omprakash Thamsetty
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.
September 9, 2015

Hi Jamie, We have script to send an email with an attachment when the state change. I am not using script runner to send email. Yes, I do see attachment option new , custome and all but that is not my requirement. When transition the state we are having some mandatory fields to fill so there is one screen popup for them to fill. In that screen added "attachment" system filed to add file. When ever we add the file then file should go as an attachment. Taha suggested one in above in this thread. I may need to try that to achieve it. Is our script runner will do this by choosing custom option in attachment? or any method we have to get the attached when in transition the state.

JamieA
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.
September 9, 2015

If you've written your own code can you attach it? I would say you just want the most recent attachment.

Omprakash Thamsetty
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.
September 10, 2015

Jamie, Attached the code. It is sending the most recent attached file from the issue but we need only attached file while transition ( When I click on "Resolve" transition from top button for particular issue then resolve screen popup. Some times we will add the file as an attachment in the screen which popup and then click submit then issue states will change to resolve and then send email.) If file is not attached then email notification should send without attachment. If multiple files are added in transition then add all of them. Can we do with "send a custom email" plugin will do this? Any other solutions also acceptable on it. We have currently post-function with below code to send latest file. ////Checking for attachment. Multipart multipart = new MimeMultipart("mixed"); BodyPart attachBody = new MimeBodyPart(); List<Attachment> attachments = attachmentManager.getAttachments(issue); NumOfAttachedfiles = attachments.size(); if(!attachments.isEmpty()) { def fileCreatedDate, fileCreatedDateFinal; Attachment attachmentFinal; for (int i=0; i< NumOfAttachedfiles; i++) { Attachment attachment = attachments[i]; fileCreatedDate = attachment.getCreated(); if(i==0){ fileCreatedDateFinal = fileCreatedDate; attachedFile = AttachmentUtils.getAttachmentFile(attachment); attachedFileName = attachment.getFilename(); }else{ if(fileCreatedDate.after(fileCreatedDateFinal)){ fileCreatedDateFinal = fileCreatedDate; attachedFile = AttachmentUtils.getAttachmentFile(attachment); attachedFileName = attachment.getFilename(); } }//else } //for FileDataSource source = new FileDataSource(attachedFile); attachBody.setDataHandler(new DataHandler(source)); attachBody.setFileName(attachedFileName); multipart.addBodyPart(attachBody); email.setMultipart(multipart);

Suggest an answer

Log in or Sign up to answer