Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Adding users from csv file script runner

karim.belhadj
Contributor
September 13, 2020

Hi team

 

Im trying to add users from csv file , i find a code i added some line , connection with csv it's ok also adding user it's okay , but always it add only the user number 1 . other list no .

 

Could some one 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)
}

 

 

 

 

 

 

 

 

1 answer

0 votes
Michael March
Contributor
September 13, 2020

 


from jira import JIRA
import csv
import time

def create_user(row):
   try:
      jira.add_user(row[0], row[1], directoryId=dirnum, password=None, fullname=row[2], notify=False, active=True ignore_existing=True)
      print("Added User: "+curuser)
   except:
      print("---- CAN'T ADD USER: "+curuser)

csvfile=sys.argv[1]
host=sys.argv[2]
uid=sys.argv[3]
passwd =sys.argv[4]

jira=JIRA(server=host, basic_auth=(uid, passwd))

f = open(csvfile, 'rb')
reader = csv.reader(f)

for user in users:
   print('Creating ->', user[2])
  create_user(user)









Suggest an answer

Log in or Sign up to answer