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.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")
}
}
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
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.
Edit to my answer is done with the if case condition checks. and also to the group too.
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.