Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,558,393
Community Members
 
Community Events
184
Community Groups

Rest API Groovy in a loop

Edited

Hello, 

Have a post function which gets a custom field containing email addresses separated by semicolons. Able to parse them and ideally wanted to do a POST to create users. 

Only the first user gets created, then it errors out 

Here is the code: 

//Parse Emails
def users = customFieldManager.getCustomFieldObject("customfield_10100")
def userlist = issue.getCustomFieldValue(users).toString()
String[] emails = userlist.split(";")
String out = ""

//Iterate Emails and Create Users via REST
for(String email in emails){out += email.split("@")[0]

def http3 = new HTTPBuilder("http://localhost:8080/rest/api/2/user")
http3.request(POST, ContentType.JSON){
requestContentType = ContentType.JSON
request.addHeader("Authorization: Basic YWRtaW46YWRtaW4=", "ContentType: application/json")
body = [
name: email,
password:"password",
emailAddress: email,
displayName: email.split("@")[0],
applicationKeys:"jira-core"
]

response.success = { resp, JSON -> return JSON }
response.failure = { resp, JSON -> return JSON }
}
}

Here is the error I'm getting:

2018-05-07 14:07:21,000 http-nio-8080-exec-10 ERROR admin 847x502x2 1xxytla 172.17.0.1 /secure/CommentAssignIssue.jspa [c.o.s.jira.workflow.ScriptWorkflowFunction] Script function failed on issue: PROYEC-1, actionId: 91, file: <inline script>

groovyx.net.http.HttpResponseException: 

at groovyx.net.http.HTTPBuilder.defaultFailureHandler(HTTPBuilder.java:651)

at groovyx.net.http.HTTPBuilder$1.handleResponse(HTTPBuilder.java:503)

at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:223)

at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:165)

at groovyx.net.http.HTTPBuilder.doRequest(HTTPBuilder.java:515)

at groovyx.net.http.HTTPBuilder.doRequest(HTTPBuilder.java:434)

at groovyx.net.http.HTTPBuilder.request(HTTPBuilder.java:383)

at groovyx.net.http.HTTPBuilder$request.call(Unknown Source)

at Script4.run(Script4.groovy:24)

 

Wondering if there's some sort of pause I should do, as logs show the first user gets created, the POST is made but there's something making subsequent calls fail... 

 

1 answer

1 accepted

1 vote
Answer accepted
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.
May 25, 2018

Hi Patrice, 

Instead of collecting and iterating through emails why not iterating through the list of the applications users, where each Application user holds the email address and the display name. 

Or even collect in a map the email address (as a key) and the display name (as a value) and the iterate through this map. 

Example code

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.user.ApplicationUser

def issue = issue as MutableIssue
def users = ComponentAccessor.customFieldManager.getCustomFieldObject("customfield_10100")
def userlist = issue.getCustomFieldValue(users) as List <ApplicationUser>

def emailAndNameMap = userlist?.collectEntries {
[(it.emailAddress) : it.displayName]
}

userlist?.each {
log.debug "Email address $it.emailAddress"
log.debug "Display name $it.displayName"
//do some rest calls
}

One last thing why you use the REST api to create the users and not the Java API ? 

Hi Thanos, 

Many thanks for this and apologies for the delay, was away. 

Part of the 'brief' is that I only have a custom field with email addresses separated by semicolons. Users do not exist yet. I have to grab each email, parse first part and use it as DisplayName and create users on a remote Jira instance... 

Tried your method to map and .collectEntries as it.emailAddress and it.displayName but it fails as it does not know it is an email address 

groovy.lang.MissingPropertyException: No such property: emailAddress for class: java.lang.String

EDIT: I found my original method to be working, I hadn't accounted for trailing white spaces... 

Thanks 

 

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.
Jun 05, 2018

Hi Patrice, 

Oh ok, I got the use case wrong (I thought the custom field was a MultiUserPicker).

Glad that you sorted this out. 

Regards, Thanos

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events