Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to use Select List Conversions consuming any Jira REST-Service?

Stefan Bärthlein December 11, 2018

I'm wondering if it is possible to use Select List Conversions to consume any Jira REST service?

For example, How could I use this list convertion to show a list of issuetypes using this REST: https://docs.atlassian.com/software/jira/docs/api/REST/7.12.0/?_ga=2.178952807.2117906921.1544427662-1440400765.1527589455#api/2/issuetype-getIssueAllTypes

 

It seems that the convertion needs a special format, like the ScriptRunner example mentions in the CustomEndpointDelegate examples  - an item seems to have attributes like:

  • value
  • html
  • label
  • icon

The above mentioned REST-Service supplies the data for building options with avatar, but the format seems to be not recognized by the list conversion.

Is there a simple way to re-format the JSON of the ajax-call?

Do one have to write an endpoint for any Jira REST-Service in order to correct the format?

BTW: It seems the Endpoint-Examples do not work for me as the variables uri, response and reader are not declared (as mentioned by the REST endpoints ScriptRunner UI)

 

Any Hints?
Thanks a lot!

 

1 answer

0 votes
Stefan Bärthlein December 11, 2018

Here are my first hardcoded debugging steps in order to understand which format should be allplied and how server / client communication works, jfyi:

 

import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.json.JsonBuilder
import groovy.transform.BaseScript

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

@BaseScript CustomEndpointDelegate delegate

// /rest/scriptrunner/latest/custom/issuetype soll über GET nur der Gruppe jira-servicedesk-users zur Verfügung stehen
issuetype(httpMethod: "GET", groups: ["jira-servicedesk-users"]) { MultivaluedMap queryParams -> /*, String body ->*/           
    // Hier die Struktur für das Issue-Format (Select List Convertion) aufbauen zB:
    /*
        {
           "sections": [
              {
                 "label": "Wählen Sie den passenden Vorgangstypen",
                 "sub": "Showing 10 of 39 matching issues",
                 "id": "cs",
                 "issues": [
                    {
                       "key": "BD-1",
                       "keyHtml": "BD-1",
                       "img": "/secure/viewavatar?size=xsmall&avatarId=10548&avatarType=issuetype",
                       "summary": "Was ist das hier?",
                       "summaryText": "Was ist das hier?",
                       "id": 10041
                    },
                    {
                       "key": "BD-2",
                       "keyHtml": "BD-2",
                       "img": "/secure/viewavatar?size=xsmall&avatarId=10548&avatarType=issuetype",
                       "summary": "Bitte ein neues Schema auf XYZ mit dem Namen "SVCDESK_DEV02" erstellen",
                       "summaryText": "Bitte ein neues Schema auf XYZ mit dem Namen \"SVCDESK_DEV02\" erstellen",
                       "id": 10042
                    },
                    {
                       "key": "BD-3",
                       "keyHtml": "BD-3",
                       "img": "/secure/viewavatar?size=xsmall&avatarId=10548&avatarType=issuetype",
                       "summary": "Bitte neue DB "XYZ" erstellen und mir Zugriff geben",
                       "summaryText": "Bitte neue DB \"XYZ\" erstellen und mir Zugriff geben",
                       "id": 10043
                    },
                 ],
                 "totalIssues": 3
              }
           ]
        }    
    */
    /*def item1 = [key: "BD-1", keyHtml: "BD-1", img: "/secure/viewavatar?size=xsmall&avatarId=10548&avatarType=issuetype", summary: "Was ist das hier?", summaryText: "Was ist das hier?", id: 10041 ]
    def item2 = [key: "BD-2", keyHtml: "BD-2", img: "/secure/viewavatar?size=xsmall&avatarId=10548&avatarType=issuetype", summary: "Bitte ein neues Schema auf XYZ mit dem Namen "SVCDESK_DEV02" erstellen", summaryText: "Bitte ein neues Schema auf XYZ mit dem Namen \"SVCDESK_DEV02\" erstellen", id: 10042 ]
    def items = [item1, item2]
    def section = [
                    label: "section label",
                    sub: "10-100",
                    id: "cs",
                    issues: items,
                    totalIssues: 2,
                ]    
    def sections = [:]
    sections = [ section ]
    def resultJSON = [sections : sections]*/
    
    
    //Hier im "general" - Format Daten ausgeben zB:
    /*
        {
        "items": [
            {
                "value": "test",
                "html": "<strong>fetter text</strong>",
                "label": "label"
            },
            {
                "value": "test 2",
                "html": "<strong>fetter text 2</strong>",
                "label": "label 2"
            }
        ],
        "total": 2,
        "footer": "bitte checken..."
        }
    */
    def item1 = [value: "test", html: "<strong>fetter text</strong>", label: "label"]
    def item2 = [value: "test 2", html: "<strong>fetter text 2</strong>", label: "label 2"]
    def items = [item1, item2]
    def resultJSON = [items : items, total: 2, footer: "bitte checken..."]
    
    return Response.ok(new JsonBuilder(resultJSON).toString()).build();
}

 

Works with the behaviour like:

 

getFieldById("customfield_11002").convertToSingleSelect([ 
    ajaxOptions: [
        url : getBaseUrl() + "/rest/scriptrunner/latest/custom/issuetype",
        query: true, // keep going back to the sever for each keystroke
        // this information is passed to the server with each keystroke
        //formatResponse: "issue"
        formatResponse: "general"
    ],
    css: "max-width: 500px; width: 500px",
]);
Stefan Bärthlein December 11, 2018

And here the version consumeing the Jira REST using the webclient (HTTPBuilder) - feels somehow odd but it's working so far if I use

formatResponse: "general" 

in the behaviour

 

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

// /rest/scriptrunner/latest/custom/issuetype soll über GET nur der Gruppe jira-servicedesk-users zur Verfügung stehen
issuetype(httpMethod: "GET", groups: ["jira-servicedesk-users"]) { MultivaluedMap queryParams -> /*, String body ->*/               
    //Hier im "general" - Format Daten ausgeben zB:
    /*
        {
        "items": [
            {
                "value": "test",
                "html": "<strong>fetter text</strong>",
                "label": "label"
            },
            {
                "value": "test 2",
                "html": "<strong>fetter text 2</strong>",
                "label": "label 2"
            }
        ],
        "total": 2,
        "footer": "bitte checken..."
        }
    */
/*    def item1 = [value: "test", html: "<strong>fetter text</strong>", label: "label"]
    def item2 = [value: "test 2", html: "<strong>fetter text 2</strong>", label: "label 2"]
    def items = [item1, item2]
    def resultJSON = [items : items, total: 2, footer: "bitte checken..."]*/

    def httpBuilder = new HTTPBuilder("http://myserver.com:8080")   
    def response = httpBuilder.request(Method.GET, ContentType.JSON) {
        uri.path = "/rest/api/2/issuetype"
        uri.query = [os_authType:"basic", os_username:"admin", os_password:"svcdesk-admin-123"]
        response.failure = { resp, reader ->
            log.warn("REST Endpoint Failed to query JIRA API: " + reader.errorMessages)
            return
        }
    }

    def items = []
    response.each {  items.add( [ value : it.name, html : "<span style='display:block; font-weight:bold;'>" + it.name + "</span><span style='display:block;'>" + it.description + "</span>", label: it.name, icon: it.iconUrl ] ) }
    def resultJSON = [items : items, total: items.size(), footer: "Wählen Sie einen Vorgangstyp..."]
    
    return Response.ok(new JsonBuilder(resultJSON).toString()).build();
}

BTW I'm still getting error infos in the ScriptRunners UI not recognizing the some variables but the output / result in postman or the chrome webdevtools brings the supposed results...

Stefan Bärthlein December 11, 2018

Still wondering - there must be an easier solution, mine feels somehow hacky?!

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events