Create Jira bulk users using groovy and ScriptRunner

Datta
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!
April 27, 2020

I need help to parse CSV in Groovy. : JIRA Server

 

Hope you are doing well.

I am trying to create bulk jira users from CSV. I am using script runner console . However, failed to parse CSV.

https://library.adaptavist.com/entity/create-a-user-in-jira

I have added below code to above script for read csv file but don't know how to read particular column to pass value to variables.

 

File file = new File("<path_to_jira_home>/files/test.csv") // <-- Change this to your path...

if (file.exists() && file.isFile()) {
String[] lines = file.text.split('\n')
List<String[]> rows = lines.collect {
it.split(',')
}
} else {
"No file found at that location..."
}

Please share code if you already have.

Much appreciated....

Thank you,
Datta

3 answers

1 accepted

1 vote
Answer accepted
Ravi Sagar _Sparxsys_
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.
April 28, 2020

Hi @Datta 

Look at the code below to parse the csv file.

/*
CSV Format
userName,userEmail,userName
user1,user1@example.com,"User 1 Name"
user2,user2@example.com, "User 2 Name"
user3,user3@example.com, "User 3 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
*/


File file = new File("/opt/jira/home/scripts/file.csv")

def csvMapList = []

file.eachLine { line ->
def columns = line.split(",")
def tmpMap = [:]

tmpMap.putAt("userName", columns[0])
tmpMap.putAt("userEmail", columns[1])
tmpMap.putAt("userFullName", columns[2])

csvMapList.add(tmpMap)
}

// csvMapList.getAt("userName") //return all usernames
// return csvMapList.getAt("userEmail") //return all emails
//csvMapList.getAt("userFullName") //return all usernames

return csvMapList

Once you have a map built from the csv file. You can do wonderful things with it (like passing to the script you are using form the library.

I hope it helps.

Ravi

Datta
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!
April 28, 2020

Ho Ravi,

Thank you so much.

Let me try to merge this in my code. 

I will let you know if it work .

Thank you,

Datta Borude

Like # people like this
karim.belhadj September 13, 2020

Hi @Datta & @Ravi Sagar _Sparxsys_ 

 

im looking for the same thing , i succed in reading the csv file but i cant create users from my csv .

 

This is my code can someone help me please ? Thank you

 

/*
CSV Format
userName,userEmail,userName
user1,user1@example.com,"User 1 Name"
user2,user2@example.com, "User 2 Name"
user3,user3@example.com, "User 3 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("C:/Users/Ovyka/Desktop/mehdi/tryit.csv")

def csvMapList = []

file.eachLine { line ->
def columns = line.split(",")
def tmpMap = [:]

tmpMap.putAt("userName", columns[0])
tmpMap.putAt("userEmail", columns[1])
tmpMap.putAt("userFullName", columns[2])

def a = tmpMap.getAt("username", columns[0])
return a
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def userService = ComponentAccessor.getComponent(UserService)

UserService.CreateUserRequest createUserRequest = UserService.CreateUserRequest.
withUserDetails(user, a.toString(), "password", "user@examplele.com", "Test User: user")
UserService.CreateUserValidationResult result = userService.validateCreateUser(createUserRequest)
if(result.isValid())
userService.createUser(result)
else
result.getErrorCollection()
csvMapList.add(tmpMap)
}

 

 

 

Salwadi Edukondal October 3, 2020

@karim.belhadj 

I have one question here even i have tried same code but i could not able to read file.getting java.io.FileNotFoundException.is there any particular criteria for file creation and file path?

0 votes
Vijay Sv May 5, 2021

Can someone add a working solution, please...

0 votes
Yuval Maron February 24, 2021

Hey, did you manage to find a solution for that issue?

Suggest an answer

Log in or Sign up to answer