Riada-AB / InsightManager

A simplified wrapper for the Insight Java API
Apache License 2.0
25 stars 13 forks source link

How to call from Insight automation? #33

Closed CTichy closed 2 years ago

CTichy commented 2 years ago

The tool is great! Could someone, for God's sake, tell me how I can call ScriptRunner scripts from Insight automation?

To create 100+ line scripts in pure Groovy for Insight, defining Facades, Beans, etc., etc. it took me only about 10/15 lines to perform the same logic. I just don't know how I can call the scripts created with the InsightManager and ScriptRunner from the Insight automation options.

Thank you for coding it and thank you for your help!!!

CTichy commented 2 years ago

Create a Custom REST Endpoint in ScriptRunner with some structure like this:

import customRiadaLibraries.insightmanager.InsightManagerForScriptrunner import com.atlassian.jira.component.ComponentAccessor import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate import groovy.json.JsonSlurper import groovy.transform.BaseScript import org.apache.log4j.Level

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

//Setup InsightManager InsightManagerForScriptrunner im = new InsightManagerForScriptrunner()

im.dispatchEvents(false)//After this, this specific instance of InsightManager (im) wont dispatch events. //im.readOnly = true //false is default

@BaseScript CustomEndpointDelegate delegate

// Set log level to INFO (default for REST endpoints is WARN) log.setLevel(Level.INFO)

// The requesting user must be in one of these groups final allowedGroups = ['jira-administrators'] myFunction(httpMethod: "PUT", groups: allowedGroups) { MultivaluedMap queryParams, String body -> def objectData = new JsonSlurper().parseText(body) as Map<String, List>

def anyObjectBean = im.getObjectBean(objectData.objectKey as String)

/*

Do as you please with your Insight Object!

*/

Response.ok("Object Modified".toString()).type(MediaType.TEXT_HTML).build() }

In Insight Automation create an HTTP request to the Endpoint like this:

Action: Http Request Url: https://"JIRA-HOME"/rest/scriptrunner/latest/custom/myFunction
note the final path name and the name of the function in the Endpoint

User name & Password: Has to be a Jira-Administrator Http method: PUT Post data: { "objectKey":"${Key}" }

note the placeholder ${Key} contains the Key of the object and the tag "objectKey" is the one parsed in the function with the JsonSlurper()