Snippets

Adaptavist Automatically Send Email to Users mentioned in an Issue's Comment for Jira DC

Created by Ram Kumar Aravindakshan last modified
/*
 * All right, title and interest in this code snippet shall remain the exclusive intellectual property of Adaptavist Group Ltd and its affiliates. Customers with a valid ScriptRunner
 * license shall be granted a  non-exclusive, non-transferable, freely revocable right to use this code snippet only within their own instance of Atlassian products. This licensing notice cannot be removed
 * or amended and must be included in any circumstances where the code snippet is shared by You or a third party."
 */
 
import com.adaptavist.hapi.jira.mail.Mail
import com.adaptavist.hapi.jira.users.Users

def issue = event.issue

def latestComment = issue.comments.last().body

if (latestComment.contains('~')) {

      def usernames = latestComment.split(' ').collect {
            if (it.contains('[') && it.contains(']') && it.contains('~')) {
                  it.replace('[','').replace(']','').replace('~','').trim().replace(',','').trim()
            }
      } - [null]

      def displayNames = usernames.collect { filtered ->
            Users.getByName(filtered).displayName
      }

      def emails = usernames.collect { filtered ->
            Users.getByName(filtered).emailAddress
      }

      def body = """
      Hi ${displayNames.join(',')},
     
      Reporter ${issue.reporter} has mentioned you on this issue ${issue.summary}, ${issue.key} for further assistance. Please find the below comment:
      
      ${latestComment}
      """

      Mail.send {
            setFrom('mail@mailbox.com')
            setSubject('Auto Notification')
            setTo(emails.join(','))
            setBody(body.toString())
            setHtml()
      }
}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.