JIRA - Call external webservice and display data on select list custom field Edited

Hello,

We have an external webservice that returns data in XML format. I need to use the URL to get the data and only display values from a certain element as a list of drop down values on a JIRA custom field.

Can ScriptRunner "Select List Conversion" do this? Or is there another way to do this? 

 

I would appreciate if someone can provide some lead on this.

 

Thanks,

Venkat.

1 answer

Yup! There's a documented example of hitting the GitHub api at https://scriptrunner.adaptavist.com/latest/jira/behaviours-conversions.html#_walkthrough_external_rest_service.

The Github API returns JSON, but the HTTPBuilder library can parse XML into a nice navigable Groovy object just as well. Indeed, it should do that for you automagically, so that you can simply do something like:

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 -> // <1>

    def rt = [:]
    if (query) {
        def httpBuilder = new HTTPBuilder("https://my.endpoint.com")
        def response = httpBuilder.request(Method.GET, ContentType.JSON) {
            uri.path = "/path/to/call"
            uri.query = [parameter:"value", otherQueryParam: "otherValue"]
            headers."User-Agent" = "My JIRA" // replace with any headers needed by your external API

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

        def neededElements = response["rootElement"]["desiredChildElement"]
        rt = [
            items: neededElements.collect { element ->
                [
                    value: element.property,
                    html : "<b>${element.'someFriendlyLookingProperty'}</b>"
                    label: element."someOtherProperty",
                ]
            },
            total: response["total_count"],
            footer: "Choose thing... (${neededElements.size()} of ${response["total_count"]} shown...)"
        ]
    }

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

The specifics of what child elements to get and how to get them will vary based on the structure of the XML your external webservice returns. For help figuring out how to navigate the parsed response object, see the HTTPBuilder documentation at https://github.com/jgritman/httpbuilder/wiki. The https://github.com/jgritman/httpbuilder/wiki/Using-XML page may be of particular interest.

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Thursday in Jira Service Desk

How the Telegram Integration for Jira helps Sergey's team take their support efficiency to the bank

...+ reading Fantasy). The same is true for him at the bank he works for: Efficiency is key when time literally equals money. Read on to learn how Sergey makes most of the time he has by...

350 views 2 4
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you