Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How can I remove user from group using groovy/scriptrunner?

jsurbhi06
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
March 16, 2022

I’ve a task wherein every week I need to remove some set of users (user id) to be removed from groups. I do have scriptrunner installed. I saw we can run some commands there or script. 
now I’m manually removing them. 

1 answer

1 accepted

1 vote
Answer accepted
Piyush A (STR)
Community Champion
March 16, 2022
Hi & welcome to the community !!!

 

yes, using scriptrunner you can do great automation that can ease your day to day job. 

for the required, I guess you’ve user id to be removed based on some criteria. I do have a code wherein you can just mention the user ids in the array and group name 

 

run the below in script console: replace the group a and user id with correct. 

 

import com.atlassian.jira.component.ComponentAccessor

def userUtil = ComponentAccessor.userUtil
def userManager = ComponentAccessor.userManager
final String groupName = "GROUPA" // the group you want to add users
def group = ComponentAccessor.groupManager.getGroup(groupName) // user names of the users to add
final List<String> userToAdd = ["USR1", "USR2", "USR3"] //LOWER CASE


userToAdd.each {
def usertoadd = userManager.getUserByName(it)
if (!usertoadd) {
log.warn("User: $userToAdd doesn't exist")
return
}

if (ComponentAccessor.getGroupManager().getGroupsForUser(usertoadd).contains(group)) {
log.warn("User: $usertoadd.username already in the group: $groupName")
return
}

if(!group) {
log.warn("Group: $groupName doesn't exist")
}

else {
userUtil.addUserToGroup(group, usertoadd)
log.warn("User: $usertoadd.username added to the $groupName")
}
}
jsurbhi06
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
March 16, 2022

Thanks @Piyush A (STR)   
This is what the exact I was looking for and just tested with a dummy user and group and it worked. Thanks

Like Piyush A (STR) likes this
Marian Gebauer April 11, 2022

Hello @Piyush A (STR) 

 

Great solution but i was wondering if there is possibility to add IF to this. Reason behind it is that when user is in user directory all is working fine but as long as user is not there it will crash. If you have 10 users to remove and it crushes then you have to leave this ID out and start again. 

 

Thank you 

Like Paula Suarez likes this
Piyush A (STR)
Community Champion
April 11, 2022

Thanks for the suggestion @Marian Gebauer . You're correct here, since that's the old code of mine, I've added 'if' condition for the user and group to exists or skip for those with an message.

Like Paula Suarez likes this
Marian Gebauer April 11, 2022

Hi @Piyush A (STR) 

Do you have example code that you can post here?

 

Thank you 

Piyush A (STR)
Community Champion
April 12, 2022

Edit to my answer is done with the if case condition checks. and also to the group too.

Suggest an answer

Log in or Sign up to answer