Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Create Jira bulk users using groovy and ScriptRunner Edited

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

2 answers

1 accepted

1 vote
Answer accepted

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

Like # people like this

Hi @Datta & @Ravi Sagar _Adaptavist_ 

 

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)
}

 

 

 

@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?

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

Suggest an answer

Log in or Sign up to answer
TAGS

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you