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.