Retrieve a users meta-data with Groovy

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

This widget could not be displayed.
Tarun Sapra Community Champion Jan 09, 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 Champion Jan 09, 2018

You can get currentUser using

ComponentAccessor.getJiraAuthenticationContext()getLoggedInUser()

Thank you very much!

Tarun Sapra Community Champion Jan 09, 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)"

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 Champion Jan 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
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted Wednesday in New to Jira

Are you planning to trial, or are currently trialling Jira Software? - We want to talk to you!

Hello! I'm Rayen, a product manager at Atlassian. My team and I are working hard to improve the trial experience for Jira Software Cloud. We are interested in   talking to 20 people planning t...

119 views 2 0
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you