Copy/paste from my Stack Overflow question:
I am trying to add a custom JIRA REST endpoint with Script Runner.
The REST endpoint needs to set user properties: a list of users is provided, and for each user a list of user properties.
This is the script I have cobbled together so far:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.bc.user.search.UserSearchService
import com.atlassian.sal.api.user.UserManager
import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.json.JsonBuilder
import groovy.json.JsonSlurper
import groovy.transform.BaseScript
import javax.servlet.http.HttpServletRequest
import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response
@BaseScript CustomEndpointDelegate delegate
setUserProperties(httpMethod: "POST", groups: ["jira-administrators"])
{ MultivaluedMap queryParams, String body, HttpServletRequest request ->
def userPropertyManager = ComponentAccessor.getUserPropertyManager()
def userManager = ComponentAccessor.getUserManager()
def userSearchService = ComponentAccessor.getComponent(UserSearchService.class)
def json = new JsonSlurper().parseText(body)
def propertyCount = 0
def customerList = new ArrayList<>()
json.each {
aCustomer ->
def user = userManager.getUserByName(aCustomer.name.toString())
aCustomer.properties.each { aProperty ->
userPropertyManager.getPropertySet(user).setString("jira.meta." + aProperty.key.toString(), aProperty.value.toString())
propertyCount++
}
customerList.add(user)
}
def response = new ArrayList<>()
response.add( new JsonSlurper().parseText('{ "properties_changed": ' + propertyCount + ' }') )
response.add( customerList )
return Response.ok( new JsonBuilder(response).toPrettyString() ).build();
}A previous (working) iteration of this script, running on another server, used email addresses, see http://stackoverflow.com/questions/36698657/how-to-get-a-user-by-email-in-jira-script-runner. But, as stated above, now I'm not getting users by email address but by username.
This is the userprops.json file I use to test:
[
{
"name": "felicity.smoak",
"email": "felicity.smoak@queenconsolidated.com",
"properties": {
"Company": "Queen Consolidated",
"Foo": "bar"
}
},
{
"name": "johnny.cash",
"email": "johnny.cash@nashville.com",
"properties": {
"Company": "Nashville",
"Nickname": "The Man In Black"
}
}
]I test with this curl command:
curl -X POST \
-i \
-H "X-Atlassian-Token: nocheck" \
-H "Content-type: text/json" \
--data "@userprops.json" \
-u "$JIRA_USER":"$JIRA_PASSWORD" \
http://"$JIRA_SERVER"/rest/scriptrunner/latest/custom/setUserProperties
This is the output:
HTTP/1.1 404 Not Found
Server: nginx
Date: Wed, 07 Sep 2016 14:55:38 GMT
Content-Type: application/json;charset=UTF-8
Content-Length: 0
Connection: keep-alive
X-AREQUESTID: 0123456789
X-ASEN: SEN-123456789
Set-Cookie: JSESSIONID=1A2B3C4D5E6F1A2B3C4D5E6F1A2B3C4D5E6F; Path=/; Secure; HttpOnly
X-Seraph-LoginReason: OK
Set-Cookie: atlassian.xsrf.token=ABCD-EFGH-IJKL-MNOP|abcdef0123456789abcdef0123456789abcdef0123456789|lin; Path=/; Secure
X-ASESSIONID: xyz123
X-AUSERNAME: amedee.vangasse
Cache-Control: no-cache, no-store, no-transform
X-Content-Type-Options: nosniff
So it's not finding the REST endpoint where I should find it according to the Scriptrunner documentation.
If I use any URL that I *know* to be wrong, then I get an expected "Oops, you've found a dead link." error page. Only with the correct URL I get a 404 + no other output.
Also, when I add the doSomething sample and try to GET that, I get a 404 and a content length of 0. Doing a GET on any other wrong URL gives me a 404 AND a content length of a few KiB.