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:
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.
Hi @RJ,
Is it possible for you to use the UserService class of Bitbucket instead?
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.