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

Scriptrunner - JIRA REST Endpoint for Select List

I am trying to configure a rest endpoint in JIRA that will make a GET call to an external rest API for a list of selectable options (retailers). I will then use a behaviour to populate a custom field with a value selected from the list. I understand the behaviour part but I am having trouble with the REST API.

Based on examples on here, this is my script currently. I am seeing the following errors...

line 31 - no such property : text for class

line 35 no such property : full name for class

line 40 ambigious prototypes for closure

I am brand new to Groovy so I am wondering where i am going wrong if anybody can shed any light? thanks in advance!

 

import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.json.JsonBuilder
import groovy.transform.BaseScript
import groovyx.net.http.ContentType
import groovyx.net.http.HTTPBuilder
import groovyx.net.http.Method

import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response

@BaseScript CustomEndpointDelegate delegate

githubRepoQuery(httpMethod: "GET") { MultivaluedMap queryParams ->

def query = queryParams.getFirst("query") as String

def rt = [:]

if (query) {
def httpBuilder = new HTTPBuilder("http:myurl")
def repos = httpBuilder.request(Method.GET, ContentType.JSON) {
def uri = [:]
uri.path = "mypath"

def headers = [:]
headers."User-Agent" = "My JIRA"

def response = [:]

response.failure = { resp, reader ->
log.warn("Failed to get Retailer: " + reader.text)
}
}

def repoNames = repos["items"]*."full_name"
rt = [
items: repoNames.collect { String repo ->
[
value: repo,
html : repo.replaceAll(/(?i)$query/) { "<b>${it}</b>" },
label: repo,
]
},
total: repos["total_count"],
footer: "Choose repo... (${repoNames.size()} of ${repos["total_count"]} shown...)"
]
}

return Response.ok(new JsonBuilder(rt).toString()).build()
}

2 answers

Hi Stephen,

I've had a look at your code and it looks fine. It sounds like the errors you are getting are coming from static type checking (see documentation). These errors can sometimes be disregarded. In this case your properties should be present at runtime.

What happens when you make a request to the endpoint??

Thanks,

Steve

1 of 2 Hi Stephen,

Thanks for your response, yes I have followed your point and my script does compile and run but doesnt return any values. I am expecting the below JSON to be returned but when i execute the end point, nothing is coming back, any ideas?

expected JSON response

{"retailerList":[{"RetailerID": "1234","RetailerName": "RetailerTest1","status": "Active"},{"RetailerID": "5678","RetailerName": "RetailerTest2","status": "Active"}]}

current response

// 20171214135133
// http://ruuujraa03:8080/rest/scriptrunner/latest/custom/githubRepoQuery

{

}

 

2 of 2 Here's the full version of my code

 

import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.json.JsonBuilder
import groovy.transform.BaseScript
import groovyx.net.http.ContentType
import groovyx.net.http.HTTPBuilder
import groovyx.net.http.Method

import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response

@BaseScript CustomEndpointDelegate delegate

githubRepoQuery(httpMethod: "GET") { MultivaluedMap queryParams ->

def query = queryParams.getFirst("query") as String

def rt = [:]
if (query) {
def httpBuilder = new HTTPBuilder("http://ruudwmpa01:5555")
def repos = httpBuilder.request(Method.GET, ContentType.JSON) {
uri.path = "rest/FLam/getRetailerList"
uri.query = [q:"$query in:name", sort: "retailerID", order:"desc"]
headers."User-Agent" = "My JIRA"

response.failure = { resp, reader ->
log.warn("Failed to get retailer: " + reader.text)
}
}

def repoNames = repos["retailer"]*.""
rt = [
items: repoNames.collect { String repo ->
[
value: repo,
html : repo.replaceAll(/(?i)$query/) { "<b>${it}</b>" },
label: repo,
]
},
total: repos["total_count"],
footer: "Choose repo... (${repoNames.size()} of ${repos["total_count"]} shown...)"
]
}

return Response.ok(new JsonBuilder(rt).toString()).build()
}

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase

The benefits of using Jira in different departments

Jira is a great tool to use across different departments. Forget that paperwork – switch to Jira and get that tasks done smoothly. Marketing Jira allows for a complete digital transformation of you...

97 views 0 5
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