Forums

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

How can i add all 100 user in a script to create their account in jira

Vikrant Yadav
Community Champion
May 2, 2020

Is their any way to add all 100 users in a below script, so that all users created in one go OR i need to run script everytime for each users.Please suggest 

 

import com.atlassian.jira.bc.user.UserService
import com.atlassian.jira.component.ComponentAccessor

// the username of the new user - needs to be lowercase and unique - required
final String userName = "user"

// The password for the new user - if empty a random password will be generated
final String password = "password"

// The email address for the new user - required
final String emailAddress = "user@jira.com"

// The display name for the new user - required
final String displayName = "New User"

// notifications are sent by default, set to false to not send a notification
final boolean sendNotification = false

def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def userService = ComponentAccessor.getComponent(UserService)

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

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

userService.createUser(createValidationResult)

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

 

@Stephen_Wright  @Nic_Brough__Adaptavist_  @Kristian Walker (Adaptavist) @Adrian_Stephen @Derek_Fields @Ravi_Sagar__Adaptavist_ 

 

Please help

 

Thanks

Vikrant yadav

2 answers

2 accepted

0 votes
Answer accepted
Martin Bayer [MoroSystems, s.r.o.]
Community Champion
May 4, 2020

@Vikrant Yadav I tried to adjust my answer and it dissapeared :(

The fix is:

def users = [
[userName: "superman", displayName: "Superman", password: "superman", emailAddress: "superman@gmail.com"],
[userName: "batman", displayName: "Batman", password: null, emailAddress: "batman@gmail.com"]
]

I used "{" / "}" instead of "[" / "]". Give it a try and let me know :)

Martin Bayer [MoroSystems, s.r.o.]
Community Champion
May 4, 2020

For others:

It can be simply done using "for" loop in groovy / Java:

import com.atlassian.jira.bc.user.UserService
import com.atlassian.jira.component.ComponentAccessor

def users = [
[userName: "superman", displayName: "Superman", password: "superman", emailAddress: "superman@gmail.com"],
[userName: "batman", displayName: "Batman", password: null, emailAddress: "batman@gmail.com"]
]

// notifications are sent by default, set to false to not send a notification
final boolean sendNotification = false

def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def userService = ComponentAccessor.getComponent(UserService)

for(def user: users){

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

def createValidationResult = userService.validateCreateUser(newCreateRequest)
if(createValidationResult.isValid()){
userService.createUser(createValidationResult)
} else {
log.error createValidationResult.errorCollection
}
}
Vikrant Yadav
Community Champion
May 4, 2020

Hi @Martin Bayer [MoroSystems, s.r.o.]  Now showing lots of error. Error.PNG

Martin Bayer [MoroSystems, s.r.o.]
Community Champion
May 4, 2020

hi @Vikrant Yadav these errors are "ok", you are not using TYPES in groovy, but "def" so console for example does not know property "errorCollection" on "createValidationResult" as property "createValidationResult" does not have TYPE defined.

Real errors will appear when you try to execute the script.

Vikrant Yadav
Community Champion
May 4, 2020

Hey @Martin Bayer [MoroSystems, s.r.o.]  still not working, getting errorError User.PNG

Martin Bayer [MoroSystems, s.r.o.]
Community Champion
May 4, 2020

@Vikrant Yadav  my first answer appeared again, try the code in the first example, I will delete the second one to clean this theme up

Vikrant Yadav
Community Champion
May 4, 2020

Hi @Martin Bayer [MoroSystems, s.r.o.]  first response also giving error. I already attached the screenshot

Martin Bayer [MoroSystems, s.r.o.]
Community Champion
May 4, 2020

Hi @Vikrant Yadav I fixed braces after you reported error { -> [ and } -> ] as there was error with this. Can you check it once again, please?

Vikrant Yadav
Community Champion
May 5, 2020

Hi @Martin Bayer [MoroSystems, s.r.o.]  in this script where braces do i need to changes, can you please share the correct script. 

 

import com.atlassian.jira.bc.user.UserService
import com.atlassian.jira.component.ComponentAccessor

def users = [
[userName: "superman", displayName: "Superman", password: "superman", emailAddress: "superman@gmail.com"],
[userName: "batman", displayName: "Batman", password: null, emailAddress: "batman@gmail.com"]
]

// notifications are sent by default, set to false to not send a notification
final boolean sendNotification = false

def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def userService = ComponentAccessor.getComponent(UserService)

for(def user: users){

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

def createValidationResult = userService.validateCreateUser(newCreateRequest)
if(createValidationResult.isValid()){
userService.createUser(createValidationResult)
} else {
log.error createValidationResult.errorCollection
}
}


 
Martin Bayer [MoroSystems, s.r.o.]
Community Champion
May 5, 2020

@Vikrant Yadav this is correct script, I already fixed braces in it. Just give it a try.

Vikrant Yadav
Community Champion
May 5, 2020

Hi @Martin Bayer [MoroSystems, s.r.o.]  still this script showing error , but it's working. creating users. Thanks Mate

Martin Bayer [MoroSystems, s.r.o.]
Community Champion
May 5, 2020

Great to hear it's working for you, it will show errors until you will not used TYPED variables. But I guess it is one time operation so it will not affect your Jira configuration experience :)

0 votes
Answer accepted
Martin Bayer [MoroSystems, s.r.o.]
Community Champion
May 4, 2020

Hi, yes you can, you need collection of users data:

  • username
  • displayname
  • password
  • email address

It can be for example

def users = [[username: "superman", displayname: "Superman", password: "superman", email: "superman@gmail.com"], [username: "batman", displayname: "Batman", password: null, email: "batman@gmail.com"]]

Then you need to adjust script little bit: 

import com.atlassian.jira.bc.user.UserService
import com.atlassian.jira.component.ComponentAccessor


def users = [
[userName: "superman", displayName: "Superman", password: "superman", emailAddress: "superman@gmail.com"],
[userName: "batman", displayName: "Batman", password: null, emailAddress: "batman@gmail.com"]
]

// notifications are sent by default, set to false to not send a notification
final boolean sendNotification = false

def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def userService = ComponentAccessor.getComponent(UserService)

for(def user: users){
def newCreateRequest = UserService.CreateUserRequest.withUserDetails(loggedInUser, user.userName, user.password, user.emailAddress, user.displayName).sendNotification(sendNotification)

def createValidationResult = userService.validateCreateUser(newCreateRequest)

def valid = createValidationResult.isValid()
if(valid){
userService.createUser(createValidationResult)
}else{
log.error createValidationResult.errorCollection
}
}

I didn't test the script so give it a try and let me know what is the result.

Note:

  • password can be set to null, in such case some random default password is generated for the user
  • notification can be set of you change final boolean sendNotification = false to final boolean sendNotification = true, I think the notification means "welcome" email in this case but I'm not sure so just test it on some testing user
Vikrant Yadav
Community Champion
May 4, 2020

Hey @Martin Bayer [MoroSystems, s.r.o.]  thanks for the response. But getting an error , i used the script shared by you :- 

 

User Error.PNG

Suggest an answer

Log in or Sign up to answer