How use HAPI in ScriptRunner Script Editor

Alexandra Kolesova January 23, 2024

I write script for Automation in Asset. Script i save in Script Editor ScriptRunner.

But today i red about HAPI and try use it in my realization, but this don't work. I see error about "No such property: Assets for class", i try import 

com.adaptavist.hapi.jira.assets.Assets and get "unable to resolve class com.adaptavist.hapi.jira.assets.Assets
@ line 4, column 1.
import com.adaptavist.hapi.jira.assets.Assets"
Can i use com.adaptavist.hapi.jira.assets.Assets when i write script for "then" Asset Automation?

3 answers

1 accepted

0 votes
Answer accepted
Peter-Dave Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 23, 2024

You don't need to import any of the HAPI classes anywhere within the whole Scriptrunner functionality. Including script editor as you can see here:

2024-01-23 08_27_16-Script Editor.png

The only time an explicit import is useful will be if you want to edit code outside of scriptrunner (like an IDE such as intellij). Then importing will help with some autocomplete.

Note that autocomplete will not work in Intellij for injected methods. See https://productsupport.adaptavist.com/browse/SRJIRA-6649 for more details

Alexandra Kolesova January 25, 2024

Yes, when u write script it's correct. But when you use this script to automation Assets in “then” you get the errors that I described

Peter-Dave Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 25, 2024

You cannot use HAPI or anything from the scriptrunner framework in either the Assets Console or Assets automation.

If you want to use scriptrunner script with Assets, then create a Script Listener in scriptrunner and use the appropriate InsightObjectXXX Event.

If you want to use native Asset script environment, then you have to use pure Assets api

Nic Brough -Adaptavist-
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 25, 2024

This generalisation is not true, Scriptrunner's HAPI does work with Assests.  See https://docs.adaptavist.com/sr4js/latest/hapi/work-with-assets-insight for the things HAPI does support with Assets.

Peter-Dave Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 25, 2024

You cannot use HAPI or anything from the scriptrunner framework in either the Assets Console or Assets automation

I think I could have been clearer about that line.

But in Asset Script Console (<baseurl>/secure/admin/InsightPluginInitScriptConsole.jspa) HAPI, and other classes provided by scriptrunner (like PluginModule and WithPlugin) are not available.

The same is true if you create a groovy script that you want to link to the native Assets Automation module (under Configure Object Schema):

2024-01-25 14_54_00-Assets - JIRA.png

The user was trying to use a script created with HAPI in the script editor and point to it from the Assets Automation module.

The groovy implementation built into the Assets plugin actually uses a pretty old version of groovy. 2.4.15 from what I can tell:

2024-01-25 14_58_42-Assets script console - JIRA.png

As opposed to Scriptrunner:

2024-01-25 14_59_16-Script Console.png

Asset Groovy framework scripts won't have the ability to import classes stored in the script root with simple import statements.
You have to use the Groovy class loader and load the groovy file (which causes re-compilation with every execution).

So I'm moving away from that Asset's groovy implementation and automation feature in favor of Scriptrunner listeners. Much more powerful and efficient.

Like Alexandra Kolesova likes this
Alexandra Kolesova January 29, 2024

Thank you @Peter-Dave Sheehan 
Then maybe you know why when I use Insight Automation when getting object.getClass() I get type LegacyObjectBean instead of ObjectBean?
I was able to write changing the attribute values through ScriptRunner Listeners, but I’m also interested in how to implement it through “then” in Insight Automation, but because I have all the LegacyObjectBeans, I can’t update the values.

Peter-Dave Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 29, 2024

You can convert a LegacyObjectBean into a MutableObjectBean like this:

import com.atlassian.jira.component.ComponentAccessor
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade
import com.riadalabs.jira.plugins.insight.services.model.MutableObjectBean

ObjectFacade objectFacade = ComponentAccessor.getOSGiComponentInstanceOfType(ObjectFacade)

def object = objectFacade.loadObjectBean('XXX-890')
def mutableObject = MutableObjectBean.create(object)
mutableObject.getClass()
Like Alexandra Kolesova likes this
Alexandra Kolesova January 29, 2024

I have my code for example here, i already tried in last each getting from foundObject MutableObjectBean, but still the same createObjectAttributeBeanForObject() doesn't work, because first parameter should be with ObjectBean type.

Alexandra Kolesova January 29, 2024

This is my first attempt at creating/updating objects and there are some things I still don't fully understand. HAPI is much friendlier in terms of automation :)

But unfortunately, in my own plugin later I will need to figure out how to do all this without HAPI...

0 votes
Bobby Bailey
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 25, 2024

Hi @Alexandra Kolesova , 

 

Could you show a few screenshots of what you are trying to achieve and the error you are getting please?

Kind regards, 

Bobby

Alexandra Kolesova January 25, 2024

Hi!

  • I have script, i can edit in Script Editor

Снимок экрана 2024-01-25 в 14.03.08.png

  • That script i use in Insight Automation

Снимок экрана 2024-01-25 в 14.04.10.png

  • I try create object, my automation triggered, but script don't work if i use HAPI. I see error in insight_automation.log

Снимок экрана 2024-01-25 в 14.14.29.png

  • If i import com.adaptivist.hapi.assets.Assets, i get this error

Снимок экрана 2024-01-25 в 14.20.04.png

Leonie C_ Breitmoser
Contributor
July 23, 2024

Hi there,

I have run into into the same issue. Did you find a solution how to use the HAPI Code in the Scripts for Asset Automation?

Cheers,

Leonie

0 votes
Jeroen Poismans
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 23, 2024

Hi there!

HAPI is only included from Scriptrunner version 7.11.0+. Might want't to check your version installed?

Regards

Alexandra Kolesova January 25, 2024

I have HAPI in my version, but it not work when i use script in Automation Assets

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events