Jira Scriptrunner - How to pull users and their details / groups from scriptrunner console

Michael
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.
July 10, 2024

Hi all,

We are trying to figure out a way to pull all users within multiple groups, and display their details via the scriptrunner console.

Example:

 

User 1 - Name, Email, All groups associated: (Group 1, Group 3, Group 5)
User 2 - Name, Email, All groups associated: (Group 2, Group 4, Group 5)
User 3 - Name, Email, All groups associated: (Group 1, Group 2, Group 4)
User 4 - Name, Email, All groups associated: (Group 1, Group 4, Group 5)
User 5 - Name, Email, All groups associated: (Group 2, Group 3, Group 4)

Goal:
If we run the script and want users details who are within both group 4 & group 5, we would hopefully get the following visual output:

User 2 - Name, Email, All groups associated: (Group 2, Group 4, Group 5)
User 4 - Name, Email, All groups associated: (Group 1, Group 4, Group 5)

 

Is this possible / does anyone know a script that can accomplish this?

Thanks,

Mike

3 answers

1 accepted

0 votes
Answer accepted
Michael
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.
July 11, 2024

For those who need to pull this type of reporting; we've figured it out, and the console prints our the user details in a nice HTML format so we can copy / paste the results into excel. (separated by";")

Please see below for the coding:

import com.adaptavist.hapi.jira.groups.Groups
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.login.LoginManager
import groovy.xml.MarkupBuilder

//Define groups to pull from
def Group1 = Groups.getByName('Group1 Users')
def Group2 = Groups.getByName('Group2 Users')
def jiraUserGroup = Groups.getByName('jira-software-users')

//Extract members from the above defined groups
def Group1Members = Group1.members
def Group2Members = Group2.members
def jiraUserGroupMembers = jiraUserGroup.members

//Define 'grouping' to start from (IE: Users in jira-software-users)
def users = jiraUserGroupMembers
users.unique()

def loginManager = ComponentAccessor.getComponentOfType(LoginManager)

//Only retain users who match the grouping logic below
users.retainAll {
    it in Group1Members || it in Group2Members
}

def stringWriter = new StringWriter()
def content = new MarkupBuilder(stringWriter)
def groupManager = ComponentAccessor.groupManager

//Extract the username, display name, and email address for remaining users & display in html format
users.each {
    def displayname = it.displayName
    def username = it.username
    def groups = groupManager.getGroupNamesForUser(username) as String[]
    def email = it.emailAddress
    def createdDate = it.createdDate
        content.html{
            p("${displayname};${createdDate};${username};${groups};${email}")
    }
}

stringWriter.toString()
Michael
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.
July 11, 2024

Hello again @Nicolas Grossi

That won't help us, as we aren't looking for reporters within tickets. We are looking for all active users within specific groups.

Any other suggestions would be greatly appreciated.

Thanks,

Mike

0 votes
Nicolas Grossi
Banned
July 10, 2024
Michael
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.
July 10, 2024

Thanks Nicolas,

we'll look at this and try to create some coding based on this. We'll reach back out if we still have questions.

Thanks again,

Mike

Michael
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.
July 10, 2024

Hi again @Nicolas Grossi,

We've put together the following code, but we can't seem to get it to work correctly:

import com.adaptavist.hapi.jira.groups.Groups
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.login.LoginManager

def Group1 = Groups.getByName('Group 1 Users')
def Group2 = Groups.getByName('Group 2 Users')
def jiraUserGroup = Groups.getByName('jira-software-users')

def Group1Members = Group1.members
def Group2Members = Group2.members
def jiraUserGroupMembers = jiraUserGroup.members

def users = jiraUserGroupMembers

users.unique()

def loginManager = ComponentAccessor.getComponentOfType(LoginManager)

//Only include users in these groups.
users.addAll {
    it in Group1Members || it in Group2Members
}

//take action on all remaining users
users.each {
    def username = it.username
    def fullname = it.displayName
    def email = it.emailAddress

    println("username: $username fullname: $fullname email: $email")
}

We are trying to pull the username, display name, and email for all members within the corresponding "group 1" and "group 2". Would you be able to point us in the correct direction?

Thanks,

Mike

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events