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
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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.