Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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 Marketplace Apps & Integrations

Staying organized with Jira: best practices for a better project management

Project managers know this problem: A “mountain of work” lays in front of you, and you don’t know how and where to tackle them. Different to-dos lie ahead, but just one task after the other can be ha...

178 views 2 1
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you