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,360,222
Community Members
 
Community Events
168
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

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 

 

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
Community showcase
Published in Jira

Online AMA this week: Your project management questions answered by Jira Design Lead James Rotanson

We know that great teams require amazing project management chops. It's no surprise that great teams who use Jira have strong project managers, effective workflows, and secrets that bring planning ...

190 views 1 6
Read article

Atlassian Community Events