Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Bitbucket Server: How to list users from groups using Java API Edited

Hello,

In Bitbucket Server, I have a list of user groups and would like to iterate through these groups with the output being the list of users that belong to each group. I want to use the Java API.

For example:

  • Group 1
    • User 1
    • User 2
    • User 3
  • Group 2
    • User 1
    • User 2

 

I can find the groups, but I am having trouble listing the users that belong to the groups.

I am running this in Adaptivist ScriptRunner Script Console. Here is the code I have so far:

import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.crowd.manager.directory.DirectoryManager
import com.atlassian.crowd.search.query.entity.GroupQuery
import com.atlassian.crowd.model.group.GroupType
import com.atlassian.crowd.search.query.entity.restriction.TermRestriction
import com.atlassian.crowd.search.query.entity.restriction.PropertyImpl
import com.atlassian.crowd.search.query.entity.restriction.MatchMode
import com.atlassian.crowd.search.query.membership.MembershipQuery
import com.atlassian.crowd.search.builder.QueryBuilder
import com.atlassian.crowd.search.query.entity.UserQuery
import com.atlassian.crowd.search.EntityDescriptor
import com.atlassian.crowd.model.user.Users
import com.atlassian.crowd.model.group.Membership
import com.atlassian.bitbucket.user.UserSearchRequest

def dm = ComponentLocator.getComponent(DirectoryManager)
def dir = dm.findAllDirectories().find {it.name == "Bitbucket Internal Directory"}?.id

// THIS WORKS
def groups = dm.searchGroups(dir,

                new GroupQuery(String, GroupType.GROUP, new TermRestriction(

                    new PropertyImpl<String>("name", String),

                    MatchMode.CONTAINS,

                    "partial-group-name-"

                ), 0, Integer.MAX_VALUE))


// QueryBuilder seems to build the query, but can't figure out how to use the query and get results
/*
def qry = QueryBuilder.queryFor(Users.class,EntityDescriptor.user()).childrenOf(EntityDescriptor.group()).withName("group-name")

def usrs = crowdService.search(qry)
*/



// DOES NOT WORK
def usrs = dm.searchUsers(dir,

               new UserQuery(String, new TermRestriction(

                    new PropertyImpl<String>("name", String),

                    MatchMode.CONTAINS,

                    "group-name"

                ), 0, Integer.MAX_VALUE))

 

Please let me know if I can provide any details that are more useful.

Thanks in advance for any nudge in the right direction.

1 answer

1 accepted

Hi @RJ,

Is it possible for you to use the UserService class of Bitbucket instead?

https://docs.atlassian.com/bitbucket-server/javadoc/5.16.0/api/reference/com/atlassian/bitbucket/user/UserService.html

That Service class has the following methods on it:

findGroups()

and

findUsersByGroup()

 

Robert Giddings,

Product Manager for ScriptRunner for Bitbucket

Hi @Robert Giddings _Adaptavist_ ,

Thanks for the reply. The UserService class is not yet working for me. Do you see anything I need to change? Here is the code, new code is at the bottom:

import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.crowd.manager.directory.DirectoryManager
import com.atlassian.crowd.search.query.entity.GroupQuery
import com.atlassian.crowd.model.group.GroupType
import com.atlassian.crowd.search.query.entity.restriction.TermRestriction
import com.atlassian.crowd.search.query.entity.restriction.PropertyImpl
import com.atlassian.crowd.search.query.entity.restriction.MatchMode
import com.atlassian.crowd.search.query.membership.MembershipQuery
import com.atlassian.crowd.search.builder.QueryBuilder
import com.atlassian.crowd.search.query.entity.UserQuery
import com.atlassian.crowd.search.EntityDescriptor
import com.atlassian.crowd.model.user.Users
import com.atlassian.crowd.model.group.Membership
import com.atlassian.bitbucket.user.UserSearchRequest
import com.atlassian.bitbucket.util.PageRequest
import com.atlassian.bitbucket.util.PageRequestImpl

def dm = ComponentLocator.getComponent(DirectoryManager)
def dir = dm.findAllDirectories().find {it.name == "Bitbucket Internal Directory"}?.id

// THIS WORKS
def groups = dm.searchGroups(dir,
new GroupQuery(String,
GroupType.GROUP, new TermRestriction(
new PropertyImpl<String>("name", String),
MatchMode.CONTAINS,
"partial-group-name-"
), 0, Integer.MAX_VALUE))

// NEW CODE
def us = ComponentLocator.getComponent(UserService)
PageRequest pagerq = new PageRequestImpl(0,20)
us.findUsersByGroup("group-name", pagerq)

I would expect the output to be a list of users, but instead the output I get is this:

 com.atlassian.bitbucket.util.PageImpl@2dd5f06

 

Any suggestions are welcome.

 

Thanks.

@Robert Giddings _Adaptavist_

Thanks a lot for pointing me in the right direction. After working with it a little bit more, I have something that is pretty close to what I am looking for. I am able to get the user details by the group name. Here is the code, still a work in progress, but it works:

import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.crowd.manager.directory.DirectoryManager
import com.atlassian.crowd.search.query.entity.GroupQuery
import com.atlassian.crowd.model.group.GroupType
import com.atlassian.crowd.search.query.entity.restriction.TermRestriction
import com.atlassian.crowd.search.query.entity.restriction.PropertyImpl
import com.atlassian.crowd.search.query.entity.restriction.MatchMode
import com.atlassian.crowd.search.query.membership.MembershipQuery
import com.atlassian.crowd.search.builder.QueryBuilder
import com.atlassian.crowd.search.query.entity.UserQuery
import com.atlassian.crowd.search.EntityDescriptor
import com.atlassian.crowd.model.user.Users
import com.atlassian.crowd.model.group.Membership
import com.atlassian.bitbucket.user.UserSearchRequest
import com.atlassian.bitbucket.util.PageRequest
import com.atlassian.bitbucket.util.PageRequestImpl
import static com.onresolve.scriptrunner.canned.bitbucket.util.BitbucketCannedScriptUtils.unlimitedPager

def dm = ComponentLocator.getComponent(DirectoryManager)
def dir = dm.findAllDirectories().find {it.name == "Bitbucket Internal Directory"}?.id

def groups = dm.searchGroups(dir,
new GroupQuery(String,
GroupType.GROUP, new TermRestriction(
new PropertyImpl<String>("name", String),
MatchMode.CONTAINS,
"partial-group-name-"
), 0, Integer.MAX_VALUE))

def users = "", userdetails, usrdtil
def us = ComponentLocator.getComponent(UserService)

for(group in groups){
users = us.findUsersByGroup(group, getUnlimitedPager()).values
userdetails = users.collect { user ->
usrdtl = usrdtl + group + user.slug + "," + user.displayName + "," + user.emailAddress + ","
}
}
return userdetails

Thanks again! 

Hi @RJ ,

I'm glad this is now working for you. :)

Regards,

Robert

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Bitbucket

Powering DevOps with Bitbucket Server & Data Center

Hi everyone, The Cloud team recently announced 12 new DevOps features that help developers ship better code, faster   ! While we’re all excited about the new improvements to Bitbucket ...

1,886 views 0 7
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you