Need script runner code to find all the groups a user "A" is associated.

mohit panwar August 11, 2021

I have a requirement to find all the group of user "A" and align user "B" with all those groups.  Basically want to do mirror.

Please help what could be the best solution for this or if any scriptrunner code that would be very helpful.

 

Thanks in Advance. Appreciate for your quick response.

1 answer

1 accepted

2 votes
Answer accepted
Nic Brough -Adaptavist-
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 11, 2021

A combination of the examples at https://library.adaptavist.com/entity/identify-the-groups-of-a-user and https://library.adaptavist.com/entity/remove-specified-users-from-a-group (tweaked from "delete" to "add") should do the job.

mohit panwar August 11, 2021

Thanks Nic for this. It is very helpful. I am very new to scriprunner and sure i am doing some mistake. In below code i am able to get the group of user "mpan" but not able to add to user "mpan1". Could you please help me with the code. Thanks in advance.

import com.atlassian.jira.component.ComponentAccessor
def userUtil = ComponentAccessor.userUtil
def userManager = ComponentAccessor.userManager
def groupManager = ComponentAccessor.groupManager

def username = "mpan"
def groupNames = groupManager.getGroupNamesForUser(username) as String[]

"The groups for user '${username}' are: ${groupNames.join(' ')}"

def usertoadd1= "mpan1"

groupNames.each {
def usertoadd = userManager.getUserByName(it)
userUtil.addUserToGroup(usertoadd1, groupNames)
}

Like Udayakumar Ragupathy likes this
Nic Brough -Adaptavist-
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 12, 2021

Sorry for the delay!

I think there are two problems:

You've converted the groups to strings (which is fine for your log message) and then tried to use those strings instead of the group objects the addUserToGroup expects

Similarly, usertoadd1 contains a string, but the add to group call needs an ApplicationUser

So you're not giving the last line any data it can work with.  Check https://docs.atlassian.com/software/jira/docs/api/8.18.1/com/atlassian/jira/user/util/UserUtil.html for the docs on what it expects

The group one can be fixed by dumping the "as String" from the end.  The user bit needs to be fixed by working out what ApplicationUser object "mpan1" is.

mohit panwar August 19, 2021

Thanks Nic this worked perfectly now. Thank you so much.

Do we have any similar code for Bitbucket also ?

Thanks for your time.

Nic Brough -Adaptavist-
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 19, 2021

I've not tried user management in Bitbucket.  I suspect the principle of "there is a user object" is the same but I don't know how to use it.  I'd start at https://docs.atlassian.com/bitbucket-server/javadoc/7.15.1/api/

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events