Bulk Upload Users into Jira

Jon Kocen February 2, 2022

Bulk uploading users into Jira can be confusing. I found a few scripts and put them together into one script. This does require ScriptRunner and access to your server's folders.

I started out using the Script Editor for the code and then used Console to execute the script. The Editor allows me to save the file for later use.

The users were loaded into a CSV with User ID, Password, Email Address and Display Name. We use single sign on so the password value was empty.

I uploaded the file to our server so the script could access it.

Hope this helps someone in the future.

 

/*
CSV Format
user ID, Email, full name


CSV parsing code copied from: https://stackoverflow.com/questions/49675423/read-csv-file-and-put-result-in-a-map-using-groovy-without-using-any-external-l
*/
import com.atlassian.jira.bc.user.UserService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.bc.user.UserService.CreateUserRequest
import com.atlassian.jira.bc.user.UserService
import com.atlassian.jira.component.ComponentAccessor


File file = new File("//homedev/isdev//Persons.csv")

def csvMapList = []

file.eachLine { line ->
def columns = line.split(",")

// the username of the new user - needs to be lowercase and unique - required
String userName = columns[0]
// The password for the new user - if empty a random password will be generated
String password = ""
// The email address for the new user - required
String emailAddress = columns[1]
// The display name for the new user - required
String displayName = columns[2]

// notifications are sent by default, set to false to not send a notification
boolean sendNotification = false
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def userService = ComponentAccessor.getComponent(UserService)
// see if this user already has an account
def user = ComponentAccessor.userManager.getUserByName(userName)
// new user
if (user == null) {
log.warn("add user "+columns[0]+ " "+ columns[1]+ " "+ columns[2])

def newCreateRequest = UserService.CreateUserRequest.withUserDetails(loggedInUser, userName, password, emailAddress, displayName)
.sendNotification(sendNotification)

def createValidationResult = userService.validateCreateUser(newCreateRequest)
assert createValidationResult.valid : createValidationResult.errorCollection

userService.createUser(createValidationResult)

}
}

2 comments

Comment

Log in or Sign up to comment
Taranjeet Singh
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 3, 2022

@Jon Kocen Thanks for sharing this useful method of bulk creating/uploading users in Jira!

This will definitely be useful for users using ScriptRunner in their Jira instances.

Todd Winton February 16, 2022

@Jon Kocen 

Will this work for Service Management users?

Jon Kocen February 16, 2022

It should work for Service Management users since the underlying user context is probably the same.

Like Todd Winton likes this
TAGS
AUG Leaders

Atlassian Community Events