script or sql query to find 400 users associated date like assigned issue, reported issues

Dayanand
Contributor
November 22, 2023

Team,

we have a requirement to find  400 users associated date like no of projects user has access, assigned issue, reported issues, account active or not, user last login 

 

1 answer

1 accepted

0 votes
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 22, 2023

Hi @Dayanand

Could you please provide more details on what exactly you are trying to retrieve?

I am asking this so I can try to provide an example.

Thank you and Kind regards,

Ram

Dayanand
Contributor
November 22, 2023

@Ram Kumar Aravindakshan _Adaptavist_ ,

We have a around 400 users which are less active... before we de-active them or remove their account from JIRA managements wants to know user data like below 

No of issue assigned for these 400 users?

No of issue reported by these 400 users?

Comments on JIRAs by these 400 users?

These 400 users last login data and time?

All project access these 400 users having in the whole Jira system?

These 400 users are active in the system or not ?

 

Please let me know if you need any further info

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 22, 2023

Hi @Dayanand

I have previously given a solution for a similar requirement to yours in this Community Post. In this example, the code is added to an Escalation Service, which is scheduled to run at a specific time to identify inactive users.

In your case, if you intend to run this as a one-off execution, you can use the same code and execute it on the ScriptRunner console upon making the required modifications.

Please take a look and let me know.

Thank you and Kind regards,

Ram

Dayanand
Contributor
November 22, 2023

@Ram Kumar Aravindakshan _Adaptavist_ ,

 Community Post is about user active or inactive and last activity along with that how we can find below info of each users

No of issue assigned for these 400 users?

No of issue reported by these 400 users?

Comments on JIRAs by these 400 users?

All project access these 400 users having in the whole Jira system?

Dayanand
Contributor
November 27, 2023

@Ram Kumar Aravindakshan _Adaptavist_  ,

 Community Post is about user active or inactive and last activity along with that how we can find below info of each users

No of issue assigned for these 400 users?

No of issue reported by these 400 users?

Comments on JIRAs by these 400 users?

All project access these 400 users having in the whole Jira system?

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 3, 2023

Hi @Dayanand

I am reusing the solution that I have provided in the  Community Post and adding some additional queries for your requirements, i.e.:-

No of issue assigned for these 400 users?

No of issue reported by these 400 users?

Comments on JIRAs by these 400 users?

All project access these 400 users having in the whole Jira system?

Regarding your 4th requirement i.e.:-

 All project access these 400 users having in the whole Jira system?

There is no straightforward approach to getting this. You can use a basic condition to check if or not the user has permission to, for example, Create an Issue in the project or Transition an Issue in the project.

In the sample code below, I have used this condition:-

def allPermission = (Projects.allProjects.collect { project ->
permissionManager.hasPermission(new ProjectPermissionKey('CREATE_ISSUES'), project, it)
} - false).size()

This will give the number of projects the user has permission to Create an Issue. So, if you are doing a count of 400 projects unless the user has permission to do all 400 projects, it will not return a total of 400.

Below is the updated working code for your reference:-

import com.adaptavist.hapi.jira.groups.Groups
import com.adaptavist.hapi.jira.issues.Issues
import com.adaptavist.hapi.jira.mail.Mail
import com.adaptavist.hapi.jira.projects.Projects
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.login.LoginManager
import com.atlassian.jira.security.plugin.ProjectPermissionKey
import groovy.xml.MarkupBuilder
import org.jsoup.Jsoup
import java.text.SimpleDateFormat

def loginManager = ComponentAccessor.getComponentOfType(LoginManager)
def userUtil = ComponentAccessor.userUtil
def permissionManager = ComponentAccessor.permissionManager
def issueManager = ComponentAccessor.issueManager

def adminGroup = Groups.getByName('jira-administrators').members
def softwareGroup = Groups.getByName('jira-software-users').members
def serviceDeskGroup = Groups.getByName('jira-servicedesk-users').members

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

final def filePath = '/tmp'
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; }""".toString())
}
body {
table {
thead {
tr {
th 'User Name'
th 'Full Name'
th 'Email Address'
th 'Last Login'
th 'Is Active?'
th 'Total Issues Assigned'
th 'Total Issues Reported'
th 'Total Comments Added'
th 'Total Projects with Permission'
}
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

def totalReported = Issues.search("assignee = ${username}").collect {it.key }.size()
def totalAssigned = Issues.search("reporter = ${username}").collect {it.key }.size()
def totalComment = Issues.search("issueFunction in commented('by ${username}')").size()

def allPermission = (Projects.allProjects.collect { project ->
permissionManager.hasPermission(new ProjectPermissionKey('CREATE_ISSUES'), project, it)
} - false).size()

if (userUtil.getGroupsForUser(it.name).size() > 0) {

tr {
if (!active) {
td ( username )
td ( displayName )
td ( emailAddress )
td ('Inactive User' )
td ( active )
td ( totalAssigned )
td ( totalReported )
td ( totalComment )
td ( allPermission)
} else if (!lastLoginTime) {
td ( username )
td ( displayName )
td ( emailAddress )
td ('Logon not found' )
td ( active )
td ( totalAssigned )
td ( totalReported )
td ( totalComment )
td ( allPermission )
} else {
def date = new Date(lastLoginTime)
def df2 = new SimpleDateFormat("dd/MM/yy hh:mm")
def dateText = df2.format(date)
td ( username )
td ( displayName )
td ( emailAddress )
td ( dateText )
td ( active )
td ( totalAssigned )
td ( totalReported )
td ( totalComment )
td ( allPermission )
}
}
}
}
}
}
}
}

def dest = new File("${filePath}/output.csv")
if (dest.exists()) {
dest.delete()
}
dest.createNewFile()

def subject = 'User data'
def emailAddr = 'sample@user.com'

def fileWriter = new FileWriter("${filePath}/output.csv")
fileWriter.write(generateCSV(emailBody.toString()))
fileWriter.close()

final static String generateCSV(String tableDetails) {
def stringWriter = new StringWriter()
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 ->
stringWriter.append(headerCell.text().concat(', '))
}
cells.each { cell ->
stringWriter.append(cell.text().concat(', '))
}
stringWriter.append('\n')
}
stringWriter.toString()
}

Mail.send {
setTo(emailAddr)
setHtml()
setSubject(subject)
setBody(emailBody.toString())
addAttachment(dest)
}

emailBody.toString()

Please modify the working code above according to your requirements and not just copy and paste it into your environment. Do not expect it to work as it is not 100% exact to your environment.

Below is a screenshot of the configuration for your reference:-

console_config.png

Since you are doing a check for 400 over users, the results will be truncated if you try to get them via the ScriptRunner console.

The best approach would be to use the email to add the result to the email body and as a CSV attachment.

I tested with 5 projects and 5 users in the test case I used. Of the 5 users, only one has access to all 5 projects, i.e. the Admin user. The others have access to only 4 projects.

Below is a screenshot of  the example output that you should receive via email:-

test1.png

 

I hope this helps to solve your issue. 

Thank you and Kind regards,

Ram

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 7, 2023

Hi @Dayanand

Has your question been answered?

If yes, please accept the answer provided.

Thank you and Kind regards,

Ram

Suggest an answer

Log in or Sign up to answer