Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

File path on the Monthly report on All Active, Inactive, and Never Logged-In Users groovy script

Lakshmi CH
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.
March 14, 2025

Hi Team,

I am reviewing the documentation and trying to obtain the reports. What file path should we provide? Please advise on this.

Document: https://www.scriptrunnerhq.com/help/example-scripts/send-current-active-and-inactive-user-details-in-email-body-and-csv-attachment-onPrem

//Set the file path final def filePath = ' '

//Set the filename final def filename = 'Monthly_Report'

//Set the email subject final def subject = 'Monthly report on All Active, Inactive, and Never Logged-In Users'

1 answer

1 accepted

1 vote
Answer accepted
Tuncay Senturk
Community Champion
March 16, 2025

Hi @Lakshmi CH 

You can create your "monthly-reports" folder under your JIRA_HOME and store the report files in this folder. Here is the code that may be helpful for this. 

 

def jiraHome = ComponentAccessor.getComponent(JiraHome.class);
String localHome = jiraHome.getLocalHomePath();
String folderPath = localHome + File.separatorChar + "monthly-reports";
Lakshmi CH
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.
March 17, 2025 edited


Hi @Tuncay Senturk , 

I am getting this error when executing this script.

aused by: java.io.IOException: Permission denied at java_io_File$createNewFile$2.call(Unknown Source) at Script4.run(Script4.groovy:107)




import com.atlassian.jira.config.util.JiraHome
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.login.LoginManager
import com.atlassian.mail.Email
import groovy.xml.MarkupBuilder
import org.jsoup.Jsoup

import javax.mail.internet.MimeBodyPart
import javax.mail.internet.MimeMultipart
import java.text.DateFormat

def loginManager = ComponentAccessor.getComponent(LoginManager)
def groupManager = ComponentAccessor.groupManager
def userUtil = ComponentAccessor.userUtil


//Create your "monthly-reports" folder under your JIRA_HOME and store the report files in this folder.
def jiraHome = ComponentAccessor.getComponent(JiraHome.class);
String localHome = jiraHome.getLocalHomePath();
String folderPath = localHome + File.separatorChar + "monthly-reports";

def adminGroup = groupManager.getUsersInGroup('site-admins')
def softwareGroup = groupManager.getUsersInGroup('jira-users')
def serviceDeskGroup = groupManager.getUsersInGroup('jira-servicedesk-users')

def users = adminGroup + softwareGroup + serviceDeskGroup
users.unique()

//Set the file path
final def filePath = ''
//Set the filename
final def filename = 'Monthly_Report'
//Set the email subject
final def subject = 'Monthly report on All Active, Inactive, and Never Logged-In Users'
//Set the email address
final def emailAddr = 'abc@xyz.com'
//You need to set your preferred language, e.g. 'en' for English
final def language = 'en'
//You need to set the country for the language format, e.g. UK for UK English, US for US English
final def country = 'US'
//You need to set your location, e.g. 'America/New_York'
final def location = 'America/Chicago'

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 'User Name'
                    th 'Full Name'
                    th 'Email Address'
                    th 'Last Login'
                    th 'Status'
                }
                users.each {
                    def lastLoginTime = loginManager.getLoginInfo(it.username).lastLoginTime
                    def username = it.username
                    def displayName = it.displayName
                    def emailAddress = it.emailAddress
                    def active = it.active
                    if (userUtil.getGroupsForUser(it.name).size() > 0) {
                        tr {
                            td ( username )
                            td ( displayName )
                            td ( emailAddress )
                            if (!active) {
                                td ( 'Inactive User' )
                            } else if (!lastLoginTime) {
                                td ('Logon not found' )
                            } else {
                                def now = Calendar.instance
                                def locale = new Locale(language, country)
                                def dateFormatter = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, locale)
                                def timeZone = TimeZone.getTimeZone(location)
                                dateFormatter.setTimeZone(timeZone)
                                def dateText = dateFormatter.format(now.time)
                                td ( dateText )
                            }
                            td ( active )
                        }
                    }
                }
            }
        }
    }
}
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()

//Generate CSV File
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
}
Tuncay Senturk
Community Champion
March 17, 2025

You must grant the user write permission. Otherwise it can't create folder/file.

Lakshmi CH
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.
March 17, 2025

ok, Understand. @Tuncay Senturk , Thank you so much for your quick response.

Tuncay Senturk
Community Champion
March 17, 2025

You're very welcome!

Suggest an answer

Log in or Sign up to answer