regarding user creation in jira 7.1 using groovy script runner .

Hi,

i found some luck in creating user in JIRA using groovy runner .here i mention my code .can you pls tel me whether it is correct or need to modify .because it throws error  while running the script .

 

code:

-----------

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

def userService = ComponentAccessor.getComponent(UserService)
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
UserService.CreateUserValidationResult newValidUser =UserService.validateCreateUserForAdmin("p1247", "12025", "12025", "12025", "12025@ramco.com", "vijaya bhaskar v")

 

another code:

----------------

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

def userService = ComponentAccessor.getComponent(UserService)
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
UserService.CreateUserValidationResult newValidUser =UserService.validateCreateUserForAdmin(currentUser, "12025", "12025", "12025", "12025@ramco.com", "vijaya bhaskar v")

 

error throws:

 

groovy.lang.MissingMethodException: No signature of method: static com.atlassian.jira.bc.user.UserService.validateCreateUserForAdmin() is applicable for argument types: (com.atlassian.jira.user.DelegatingApplicationUser, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String) values: [P1247(p1247), 12025, 12025, 12025, 12025@ramco.com, vijaya bhaskar v] at Script250.run(Script250.groovy:6)

 

 

2 answers

1 accepted

hi ,

I used that also . i found error with that .

code:

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

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

final CreateUserRequest createUserRequest = CreateUserRequest.withUserDetails("abc","abc", "abc", "abc@gmail.com", abc)
result = userService.validateCreateUser(createUserRequest);
if(result.isValid())
{
userService.createUser(createUserRequest);
}

 error:

 

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script270.groovy: 7: unable to resolve class CreateUserRequest @ line 7, column 25. final CreateUserRequest createUserRequest = CreateUserRequest.withUserDetails("abc","abc", "abc", "abc@gmail.com", abc) ^ 1 error

It should be 

UserService.CreateUserRequest createUserRequest = UserService.CreateUserRequest.
        withUserDetails(currentUser, "username", "password", "user@example.com", "Test User: user")
UserService.CreateUserValidationResult result = userService.validateCreateUser(createUserRequest)

Thanks Thanos ..

Its working fine now..

can you please guide me to learn groovy script for JIRA .can you provide any documents to learn groovy for JIRA .

Learning groovy (a programming language very similar to Java) is one thing and writing groovy-scripts (using SR) for JIRA is another. For the latter, SR documentation has a lot of example scripts and is a great starting point, and of course Atlassian Answers...   

Hi Thanos ,

is there any possibility to map a new user into groups while creating the new user login .

on top of my head, it should add something like that

if(result.isValid()) {
    def newUser = userService.createUser(result)
	def groupManager = ComponentAccessor.getGroupManager()
    def developersGroup = groupManager.getGroup("group-name")
    groupManager.addUserToGroup(newUser, developersGroup)
}

Here is my code :

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, "ASDASDF", "AASDSDFA", "ASDASAFA@gmail.com", "ASDASAA")
UserService.CreateUserValidationResult result = userService.validateCreateUser(createUserRequest)
if(result.isValid())
{
def newUser = userService.createUser(result)
def groupManager = ComponentAccessor.getGroupManager()
def developersGroup = groupManager.getGroup("IMG USERS")
groupManager.addUserToGroup(newUser, developersGroup)
print newUser

print developersGroup
}
else
result.getErrorCollection()

 

 

note :it creates the user and also map to the group also .but in result tab shows as null .

how can i print the username 

while creating the new user before it results with username .but now it shows as null while creating and mapping a new user to a group .

i used print statement also .but no luck in this .

can you help me to short out of it .

you should add

return newUser.getDisplayName()

just after the groupManager.addUserToGroup(newUser, developersGroup). Also notice that in order to get debug (or error, or info) messages in your logs the way is

log.debug("This is a debug message that will appear in you atlassian-jira.log file")

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published yesterday in Jira

5 ways you can make the most of Jira Software and Bitbucket Cloud

As part of the Bitbucket product team I'm always interested in better understanding what kind of impact the use of our tools have on the way you work. In a recent study we conducted of software devel...

57 views 0 5
Read article

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