checking attachment in Jira ticket while changing the state

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

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 vote

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.

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.

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

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

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 -&gt; item.getField() == "Attachment" &amp;&amp; item.getFieldType() == "jira" }
           
           uploadChanges.each { uploadChange -&gt;
           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"
}

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 -&gt; item.getField() == "Attachment" &amp;&amp; item.getFieldType() == "jira" }
            
           uploadChanges.each { uploadChange -&gt;
           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);
}

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Julia Dillon
Posted Tuesday in Jira

Tell us how your team runs on Jira!

Hey Atlassian Community! Today we are launching a bunch of customer stories about the amazing work teams, like Dropbox and Twilio, are doing with Jira. You can check out the stories here. The thi...

187 views 1 17
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you