How to use "addUserToGroup" with Script Runner

Nikolaus Madalinski February 4, 2015

Hello,
i want to use the script runner to add users to group.
I found this script for showing the users who are not in a group:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.util.UserUtilUserUtil userUtil = ComponentAccessor.getUserUtil()
result = ''

userUtil.getUsers().findAll {
	userUtil.getGroupsForUser(it.getName()).size() == 0}
	.each { u -> result += u.name + "<br>" 
	}
result

 

Here (https://docs.atlassian.com/jira/latest/com/atlassian/jira/security/groups/GroupManager.html) is see the method "addUserToGroup".
Is it possible to help me for programming the additional code, to add users to an existing group?

Would me help very much.
Thanks

2 answers

Suggest an answer

Log in or Sign up to answer
4 votes
Cesare Jacopo Corzani
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.
February 4, 2015

It should be something like that:

import com.atlassian.jira.component.ComponentAccessor

def userManager = ComponentAccessor.getUserManager()
def groupManager = ComponentAccessor.getGroupManager()


def group = groupManager.getGroup("Group1")

userManager.getAllUsers().each {
    user -> groupManager.addUserToGroup(user,group)
}

This code add all users to Group1

Danilo Lima September 12, 2018

I needed to add more than 1000 users to a group, with a conditional before the function it worked like magic for me, Thanks!

Brad Butchers March 19, 2020

Didn't work for me, it get the error below. ScriptRunner version 5.6.15.1-p5 

 

scripterror.png

Danilo Lima March 20, 2020

Hi Brad, Replace

>

to

>

All codes we post here have this kind of character scape... 

user -> groupManager.addUserToGroup(user,group)

Like Brad Butchers likes this
Brad Butchers March 22, 2020

Thanks Danilo_Lima :)

0 votes
Nikolaus Madalinski February 5, 2015

Thanks for your answer. It was to define the group before :)

 

 

TAGS
AUG Leaders

Atlassian Community Events