Problems resolving/importing custom groovy classes with Insight automation script

Jörg Hoffmann August 11, 2022

Hi.

I have a custom class: "com.onresolve.jira.groovy.methods.IssueSearch":

package com.onresolve.jira.groovy.methods
. . .

public class IssueSearch{

.
.
.

}

 

Importing it works fine in other groovy scripts used in scheduler jobs, workflow post-functions etc., like as you would expect:

import com.onresolve.jira.groovy.methods.IssueSearch 

 

yet when using the script for a Jira Insight automation (THEN "execute groovy script")
I get this error:

unable to resolve class com.onresolve.jira.groovy.methods.IssueSearch

 

Also getting the same error when importing:

import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin 

unable to resolve class com.onresolve.scriptrunner.runner.customisers.WithPlugin
unable to resolve class com.onresolve.scriptrunner.runner.customisers.PluginModule

 

So what gives? Why is this happening?

1 answer

1 vote
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 21, 2022

That's normal.

The insight groovy implementation is different and separate from the scriptrunner one.

As far as I know, unless they are explicitly exported, plugin classes are only available within the plugin itself.

Scriptrunner does some special things (all a bit esoteric to me) with script root etc to make your custom class available within the context of that plugin.

The only way I know to load a custom class such as that is with code like this:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.config.util.JiraHome

def jiraHome = ComponentAccessor.getComponentOfType(JiraHome).home
def classLoader = new GroovyClassLoader(getClass().classLoader)
def classFile = new File("$jiraHome/scripts/com/onresolve/jira/groovy/methods/IssueSearch.groovy")
Class IssueSearch = classLoader.parseClass(classFile)

If your script home is different, adjust accordingly.

Similarly, the PluginModule and WithPlugin classes and anything else in the com.onresolve.scriptrunner package will not be available by default to the Insight plugin or any insight groovy scripts.

So if your custom IssueSearch class uses PluginModule or WithPluggin, that won't work, the Insight groovy compiler will not be able to access those.

You'll need to use native jira ways to access those components.

For example, this works int he insight console to get JSM request Types for service desk with id=1:

import com.atlassian.jira.component.ComponentAccessor
Class RequestTypeService = ComponentAccessor.pluginAccessor.classLoader.findClass("com.atlassian.servicedesk.api.requesttype.RequestTypeService");
def requestTypeService = ComponentAccessor.getOSGiComponentInstanceOfType(RequestTypeService)

def queryBuilder = requestTypeService.newQueryBuilder()
queryBuilder.requestOverrideSecurity(true)
queryBuilder.serviceDesk(1)

requestTypeService.getRequestTypes(null, queryBuilder.build()).results

 The same in scriptrunner would look like this:

import com.atlassian.servicedesk.api.requesttype.RequestTypeService
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin

@WithPlugin("com.atlassian.servicedesk") sdPlugin
@PluginModule RequestTypeService requestTypeService

def queryBuilder = requestTypeService.newQueryBuilder()
queryBuilder.requestOverrideSecurity(true)
queryBuilder.serviceDesk(1)

requestTypeService.getRequestTypes(null, queryBuilder.build()).results

Suggest an answer

Log in or Sign up to answer