To do this exercise, we will use the famous plugin for Jira called Adaptavist Scriptrunner.

First step, we need to create a REST Endpoint in our Jira Server. This REST Endpoint will query the external REST API to obtain the values and options (In this example we will query GitHub to obtain a list of repositories).

Go to REST Endpoints section in your Jira

Captura de pantalla 2019-09-26 a las 19.57.14

Create a new REST Endpoint, something like the picture below (here the source code)

Captura de pantalla 2019-09-26 a las 20.01.37

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("https://api.github.com")
        def repos = httpBuilder.request(Method.GET, ContentType.JSON) {
            uri.path = "/search/repositories"
            uri.query = [q:"$query in:name", sort: "stars", order:"desc"]
            headers."User-Agent" = "My JIRA" 

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

        def repoNames = repos["items"]*."full_name"
        rt = [
            items: repoNames.collect { String repo ->
                [
                    value: repo,
                    html : repo.replaceAll(/(?i)$query/) { "${it}<span id="mce_SELREST_start" style="overflow:hidden;line-height:0;">&#65279;</span>" },
                    label: repo,
                ]
            },
            total: repos["total_count"],
            footer: "Choose repo... (${repoNames.size()} of ${repos["total_count"]} shown...)"
        ]
    }

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

Now you can try to Query your new REST Endpoint inside Jira, open in your browser this URL:

https://your_jira.domain.com/rest/scriptrunner/latest/custom/githubRepoQuery?query=tetris

Captura de pantalla 2019-09-26 a las 20.08.56

After that, you can create a new Customfield (of type Text Field Single line)

We have named as “GitHub Repos” (Remember to apply the custom field to the screens of the target project, and remember to restrict the context of the custom field to take care of the performance of your Jira)

Captura de pantalla 2019-09-26 a las 20.04.54

Now we are ready to do the last step! Create a new Behaviour!

Captura de pantalla 2019-09-26 a las 20.10.15

(Remember to map the new Behaviour to your Project and IssueTypes)

Captura de pantalla 2019-09-26 a las 20.12.48

Now, create the Initializer with a code like this (remember to fill the getFieldByName function with the correct name of the field, in this case is “GitHub Repos”)

Captura de pantalla 2019-09-26 a las 20.13.27

getFieldByName("GitHub Repos").convertToMultiSelect([
    ajaxOptions: [
        url : getBaseUrl() + "/rest/scriptrunner/latest/custom/githubRepoQuery",
        query: true, // keep going back to the sever for each keystroke
        minQueryLength: 4,
        keyInputPeriod: 500,
        formatResponse: "general",
    ]
])

That’s all! Now we can test it! It’s very very cool! Examples:

Captura de pantalla 2019-09-26 a las 20.15.21

And it’s multi-select!

View mode:

Captura de pantalla 2019-09-26 a las 20.16.27

Edit mode:

Captura de pantalla 2019-09-26 a las 20.16.38

By MrAddon

Posted by:.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s