Is it possible to create new JIRA users with Groovy

Hello Atlassian forum members,

Is it possible to add new users to JIRA using Groovy?  I have looked through the UserManager and ApplicationUser but could not find anything related to new user creation.  If anyone has experience with this and could assist, or could point me to the documentation should I have simply missed it I would appreciate it.


Thanks, and I hope this finds everyone doing well.

 

Regards,

Scott

2 answers

This widget could not be displayed.

Or if you decide to use Script Runner (and for JIRA v7.*) you can execute the script below to your script console

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

def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def userService = ComponentAccessor.getComponent(UserService)

UserService.CreateUserRequest createUserRequest = UserService.CreateUserRequest.
        withUserDetails(user, "username", "password", "user@example.com", "Test User: user")
UserService.CreateUserValidationResult result = userService.validateCreateUser(createUserRequest)
if(result.isValid())
    userService.createUser(result)
else
    result.getErrorCollection()

For JIRA v6.*

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

def user = ComponentAccessor.getJiraAuthenticationContext().getUser()
def userService = ComponentAccessor.getComponent(UserService)

UserService.CreateUserValidationResult createUserValidationResult =
        userService.validateCreateUserForSetup(
                user.directoryUser,
                "username",
                "password",
                "password",
                "userName@example.com",
                "User Full Name")

if(createUserValidationResult.isValid()) {
    def newUser = userService.createUserWithNotification(createUserValidationResult)
    return "User ${newUser.displayName} succesfully created"
} else
    createUserValidationResult.getErrorCollection()

There are several methods you can call and create users with different rights see the UserService API, for example if you want a user with admin rights use 

userService.validateCreateUserForAdmin(...)

and if you do not want to receive "user created" notifications use

userService.createUserWithNoNotification(...)

 

Two options always better than one....

I edited the params to make them more clear.

Kind regards

hi,

IT THROWS ERROR for me .

could you please provide the example in other format.

Hi, I improved the script above, let me know if this works for you. And if not please provide the error you get, if any, in the logs.

hi thanos,

Thanks .its working fine now ...

Great !!! I am looking answer for the past two days .i am searching for the groovy tutorials .But no luck ...

Good Job Thanos .

Hi thanos,

i need a help from you ... i need to create a custom field with multi level cascading select . do u know about that .

i need to create 2 levels ,3 levels and 4 levels and also need to insert the n number of values in to that custom field  at the time of the creation itself . it is possible in JIRA jelly runner . but in groovy i am not aware the process and the coding part too.

can you help on this ?

 

Hi,

Could you please create a new question, because is different than the original one, and I am sure someone (or even me when I find some time) will answer.

Cheers

Hi thanos,

already i created a new question in this blog .

fyr,

check this questions:

https://answers.atlassian.com/questions/38063913

 

Hello Thanos,

Thank you very much for your answer.  We are still running JIRA 6.4.11 (something I should have stated in my question, apologies) but perhaps your suggestion can still work in the older API with some modification.  I appreciate you taking the time to provide your thoughts, thank you again.

Regards,

Scott

Hi Scott

Apologies for the confusion, I thought rTrack Support was you and I modified the original script to be compatible with JIRA7 (the original one was comp with JIRA 6.4), I will repost the JIRA6 compatible one. 

 

Any confusion was my fault, not your smile  I appreciate any help you are willing to provide, and thanks again!

I added in the answer a script compatible with JIRA v6.* Please let me know if this works for you.

Regards 

This widget could not be displayed.

addUser and related actions are available in the JIRA Command Line Interface (CLI) and can be used in almost any scripting language.

I have seen some of the Bob Swift tools and they look quite powerful.  Perhaps in the future we will have a chance to work with them.  Thank you for your suggestion!

Suggest an answer

Log in or Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted yesterday in New to Jira

Are you planning to trial, or are currently trialling Jira Software? - We want to talk to you!

Hello! I'm Rayen, a product manager at Atlassian. My team and I are working hard to improve the trial experience for Jira Software Cloud. We are interested in   talking to 20 people planning t...

79 views 1 0
Join discussion

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you