Scriptrunners jobs for exporting Ticket Data

Taylor Chappell
Contributor
December 26, 2024

Hello all, trying to get a script going to export some data from couple fields onto a sheet. In the Jobs tab of the ScriptRunner settings, there is a template script for generating an email that I plan on basing this on. The issue I'm having is pulling the data from the fields. It doesn't seem like a Job pulls data from fields in the same way a Listener/Behaviour/etc does (obviously can't use event, current ID/form value, etc). So how can I achieve this?

1 answer

1 accepted

1 vote
Answer accepted
Taylor Chappell
Contributor
December 26, 2024
import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.mail.Email

import groovy.xml.MarkupBuilder

import org.jsoup.Jsoup

import javax.mail.internet.MimeBodyPart

import javax.mail.internet.MimeMultipart


final def filePath = 'tmp'

final def filename = 'Inv_Issue_Data'

final def subject = 'Inventory Data Export'

final def emailAddr = 'emailaddy'

final def language = 'English'

final def country = 'US'

final def location = 'America/Albequirque'

def emailBody = new StringWriter()

def html = new MarkupBuilder(emailBody)

html.html {

    head {

        style (type:'text/css', """

            table {

              border-collapse: collapse;

              width: 100%;

            }

            th, td {

              text-align: left;

              padding: 8px;

            }

            tr:nth-child(even){background-color: #f2f2f2}

            th {

              background-color: #04AA6D;

              color: white;

            }

        """)

    }

    body  {

        table  {

            thead  {

                tr  {

                    th 'Issue Key'

                    th 'Issue Summary'

                    th 'Assignee'

                    th 'Reporter'

                    th 'Status'

                }

                Issues.search("project = \"Help Desk\" and resolution != done").each { issues ->

                    def issueKey = issues.key

                    def username = issues.summary

                    def assignee = issues.assignee

                    def reporter = issues.reporter

                    def status = issues.resolution.name

                        tr {

                            td ( issueKey )

                            td ( username )

                            td ( assignee )

                            td ( reporter )

                            td ( status )

                        }

                    }

                }

            }

        }

    }

def dest = new File("${filePath}/${filename}.csv")

dest.createNewFile()

def fileWriter = new FileWriter("${filePath}/${filename}.csv")

fileWriter.write(generateCSV(emailBody.toString()))

fileWriter.close()

creatMessage(emailAddr, subject, emailBody.toString(), dest)

dest.delete()

final static String generateCSV(String tableDetails) {

    def stringBuilder = new StringBuilder()

    def doc = Jsoup.parseBodyFragment(tableDetails)

    def rows = doc.getElementsByTag('tr')

    rows.each {

        def header = it.getElementsByTag('th')

        def cells = it.getElementsByTag('td')

        header.each { headerCell ->

            stringBuilder.append(headerCell.text().concat(', '))

        }

        cells.each { cell ->

            stringBuilder.append(cell.text().concat(', '))

        }

        stringBuilder.append('\n')

    }

    //Remove empty line in CSV

    def last = stringBuilder.lastIndexOf('\n')

    if (last > 0) {

        stringBuilder.delete(last, stringBuilder.length())

    }

    stringBuilder.toString()

}

final static creatMessage(String to, String subject, String content, File file) {

    def mailServerManager = ComponentAccessor.mailServerManager

    def mailServer = mailServerManager.defaultSMTPMailServer

    def multipart = new MimeMultipart()

    def body = new MimeBodyPart()

    def mailAttachment = new MimeBodyPart()

    body.setContent(content, 'text/html; charset=utf-8')

    mailAttachment.attachFile(file)

    multipart.addBodyPart(body)

    multipart.addBodyPart(mailAttachment)

    def email = new Email(to)

    email.setSubject(subject)

    email.setMultipart(multipart)

    email.setMimeType("text/html")

    def threadClassLoader = Thread.currentThread().contextClassLoader

    Thread.currentThread().contextClassLoader = mailServer.class.classLoader

    mailServer.send(email)

    Thread.currentThread().contextClassLoader = threadClassLoader

}

Found the issue I was having. I needed to do the search and iterate on like 54 as a function rather than trying to ID/assign it earlier.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events