Is it possible to create new JIRA users with Groovy

Scott Gilliland February 19, 2016

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

4 votes
Thanos Batagiannis _Adaptavist_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 19, 2016

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

rTrack Support March 30, 2016

hi,

IT THROWS ERROR for me .

could you please provide the example in other format.

Thanos Batagiannis _Adaptavist_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 30, 2016

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.

rTrack Support March 30, 2016

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 .

rTrack Support March 31, 2016

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 ?

 

Thanos Batagiannis _Adaptavist_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 31, 2016

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

rTrack Support March 31, 2016

Hi thanos,

already i created a new question in this blog .

fyr,

check this questions:

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

 

Scott Gilliland April 1, 2016

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

Thanos Batagiannis _Adaptavist_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 1, 2016

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. 

 

Scott Gilliland April 1, 2016

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

Thanos Batagiannis _Adaptavist_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 1, 2016

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

Regards 

0 votes
Bob Swift OSS (Bob Swift Atlassian Apps)
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 19, 2016

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

Scott Gilliland April 1, 2016

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