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.
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.ComponentAccessordef userUtil = ComponentAccessor.userUtildef userManager = ComponentAccessor.userManagerfinal String groupName = "GROUPA" // the group you want to add usersdef group = ComponentAccessor.groupManager.getGroup(groupName) // user names of the users to addfinal List<String> userToAdd = ["USR1", "USR2", "USR3"] //LOWER CASEuserToAdd.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")}}
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
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
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.
It looks like you're new here. Sign in or register to get started.