regarding user creation in jira 7.1 using groovy script runner .

rTrack Support March 30, 2016

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

0 votes
Answer accepted
rTrack Support March 30, 2016

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

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

It should be 

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

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 .

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

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...   

rTrack Support March 31, 2016

Hi Thanos ,

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

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

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)
}
rTrack Support March 31, 2016

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 .

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

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")
0 votes
Aleks Yenin (Polontech)
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

Suggest an answer

Log in or Sign up to answer