Retrieve a users meta-data with Groovy

Daniel Burke January 9, 2018

So far I have got a script which returns all of the users in my test instance of JIRA. 

I have also added to the script so that it is able to get all of the groups too.

However, this is quite basic and I would like to know how people use Groovy in order to retrieve more information (for example: Email, Fullname, login time, project roles and groups they're in etc).

I've been looking through the documentation but I'm feeling somewhat overwhelemed with the different classes and libraries to do different asks.

If anybody has any ideas, or could point my code in the right direction, that would be good.

Thanks

My script:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.bc.user.search.UserSearchService
import com.atlassian.jira.bc.user.search.UserSearchParams
import com.atlassian.jira.web.action.admin.roles.AbstractProjectRole
import com.atlassian.jira.bc.group.search.GroupPickerSearchService
import com.atlassian.crowd.embedded.api.Group

def userSearchService = ComponentAccessor.getComponent(UserSearchService.class);
UserSearchParams userSearchParams = (new UserSearchParams.Builder()).allowEmptyQuery(true).includeActive(true).includeInactive(true).maxResults(100000).build();

userSearchService.findUsers("", userSearchParams).each{ it->
log.error(it.getKey())
}

def groupSearchService = ComponentAccessor.getComponent(GroupPickerSearchService);
def groupArray = groupSearchService.findGroups("")
def groupManager = ComponentAccessor.getGroupManager()

for(Group group in groupArray){
log.error group.getName().toString()
}

1 answer

0 votes
Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 9, 2018

You can use ComponentAccessor

def groupManger = ComponentAccessor.getGroupManager()

The for role you can use - ProjectRoleManager

def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager.class) 

Group Manager has plenty of amazing methods to get deep insights into user group mapping. 

https://docs.atlassian.com/DAC/javadoc/jira/7.1.0-m01/reference/com/atlassian/jira/security/groups/GroupManager.html

 

https://docs.atlassian.com/DAC/javadoc/jira/7.1.0-m01/reference/com/atlassian/jira/security/roles/ProjectRoleManager.html#getProjectRoles(com.atlassian.jira.user.ApplicationUser, com.atlassian.jira.project.Project)

 

Each user is represented by "ApplicationUser" object which has methods like getEmailAddress() and getDisplayName()

https://docs.atlassian.com/DAC/javadoc/jira/7.1.0-m01/reference/com/atlassian/jira/user/ApplicationUser.html

Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 9, 2018

You can get currentUser using

ComponentAccessor.getJiraAuthenticationContext()getLoggedInUser()
Daniel Burke January 9, 2018

Thank you very much!

Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 9, 2018

For login related info for the user you can use the "LoginManager"

https://docs.atlassian.com/DAC/javadoc/jira/7.1.0-m01/reference/com/atlassian/jira/security/login/LoginManager.html

You can fetch an instance of this class using "ComponentAccessor.getComponent(LoginManager.class)"

Daniel Burke January 10, 2018

Thank you! Are you aware of how you can get the users that are in each project?

I currently am using:

def projectSearchService = ComponentAccessor.getProjectManager()
def projectArray = projectSearchService.getProjectObjects()

log.error projectArray + " " + projectRoles

This provides me with the project codes, and the available roles in my JIRA project, however I would like a list of the users in each role within the project. 

Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 10, 2018

Please see here

https://community.atlassian.com/t5/Answers-Developer-Questions/How-do-I-create-a-script-field-in-script-runner-that-lists-all/qaq-p/502999

You will need to use ProjectRoleManager to get users for a specific role in a project.  So you can create a for loop which goes over the list of roles and for each ieration fetch ProjecctRoleActors and then do getUsers()

Suggest an answer

Log in or Sign up to answer