[Groovy]How to get value from an Insight object

JFA91 February 26, 2019

Hello everyone,

I'm currently working on a groovy script on JIRA, and I would like to get some attributes values from Insight base on an object name.

I have the name of the object who is referring to a custom field and I would like to have some of his attributes but I'm no able to success.

Could you help me to :

- Find object ID base on name (RelatedCIsCF)

- get attribute  value (ApplicationCF).

 

Could someone help me on this ?

 

thanks you.


Regards,

Julien

 

 

1 answer

0 votes
Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 26, 2019

Hi Julien,

I am not using Insight anymore, but I think this should work with the object key. If you need to make it work with the name, let me know I will look into it (although you need to keep in mind that several objects might have the same name).

import com.atlassian.jira.component.ComponentAccessor

Class objectFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade")
def objectFacade = ComponentAccessor.getOSGiComponentInstanceOfType(objectFacadeClass)
String objectKey = "KEY-123"
def insightObject = objectFacade.loadObjectBean(objectKey)
int objectId = insightObject.getId()

int attributeRef = attribute_id
String attributeRef = "Attribute name"

def objectAttribute = objectFacade.loadObjectAttributeBean(objectId, attributeRef)
if(objectAttribute){
def objectAttributeValues = objectAttribute.getObjectAttributeValueBeans()
def objectAttributeValue = objectAttributeValues[0]
}

As you can see you can use either the id or the name of the parameter.

You can browse the api if you need more information.

Regards,

Antoine

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 1, 2019

Hi @JFA91 did that help ?

JFA91 March 4, 2019

Hello Antoine,

 

for now no because first I need to find the object ID (ObjectKet on your script) based on the name and i'm stuck on this part.

I need to find the object ID from text and after that I will be able to get the other attribute.

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 4, 2019

I see two ways you could retrieve it by name : 

//First solution
Class objectFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade");
def objectFacade = ComponentAccessor.getOSGiComponentInstanceOfType(objectFacadeClass);
//ID of the "Name" attribute
def objectTypeAttributeId = 12345
def objects = objectFacade.findObjectBeansByAttributeValue(objectTypeAttributeId, "=", "Your object name")
if (objects != null && objects.size() > 0){
def insightObject = objects[0]
}

//Second solution
Class iqlFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.IQLFacade");
def iqlFacade = ComponentAccessor.getOSGiComponentInstanceOfType(iqlFacadeClass);
//id of your insight schema
int schemaId = 5

def objects = iqlFacade.findObjectsByIQLAndSchema(schemaId, "Name = \"" + "Your object name" + "\"");
if (objects != null && objects.size() > 0){
def insightObject = objects[0]
}

 Then you can work with the insightObject of the first script.

Let me know if that worked.

JFA91 March 4, 2019

Thanks you for quick replay.

Could you explain me what means objectTypeAttributeId ?

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 4, 2019

That would be the id of the attribute : 

image.png

Like Mark McCubbin likes this
JFA91 March 4, 2019

I'm missing something I think.

 

I have the error for the line  :

mi.setCustomFieldValue(applicationCF, [objectFacade.loadObjectBean(objects[0].getId())]);

java.lang.NullPointerException: Cannot invoke method getId() on null object

 

 Here is the code, but to explain :

 - Variable declaration (customfield, ...)

def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();

/* Get Insight Object Facade from plugin accessor */
Class objectFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade");
def objectFacade = ComponentAccessor.getOSGiComponentInstanceOfType(objectFacadeClass);

/* Get Insight IQL Facade from plugin accessor */
Class iqlFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.IQLFacade");
def iqlFacade = ComponentAccessor.getOSGiComponentInstanceOfType(iqlFacadeClass);

// 10201 : Id of CustomField "Application"
CustomField applicationCF = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(10201);
// 10205 : Id of CustomField "Environment"
CustomField environmentCF = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(10205);
// 10208 : Id of CustomField "Related CIs"
CustomField relatedCIsCF = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(10208);

MutableIssue mi = (MutableIssue)issue;
def updateFileds = 0;

def summary = issue.Summary;
def posEndApplication = summary.indexOf("]");
if (posEndApplication == 1) //Custom part for Appdynamics server incident

- getting server name (serverName, other are not important) from issue  :

def posEndEnvironment = summary.indexOf("]", posEndApplication + 1);
def posEndServer = summary.indexOf("]", posEndEnvironment + 1);
def serverName = summary.substring(posEndApplication + 4, posEndServer);
def posEndSummary = summary.indexOf("]", posEndServer + 1);
def summary2 = summary.substring(posEndServer + 5, posEndSummary);

 The part where i'm stuck  :

RelatedCisCF is the custom field for servers name.

ApplicationCF is the custom field for application liked to servers name.

so my issue here is to find the application name (ApplicationCF) based on the server name.

objects = iqlFacade.findObjectsByIQLAndSchema(1, "objectType = \"Server\" AND Name=\"" + serverName + "\"");
def insightObject = objects[0]
if (objects != null)
{
updateFileds += 1;
mi.setCustomFieldValue(relatedCIsCF, [objectFacade.loadObjectBean(objects[0].getId())]);
}

int objectId = insightObject.getId()
String ApplicationAttr = "Application"
def objectApplicationAttr = objectFacade.loadObjectAttributeBean(objectId, ApplicationAttr)

objects = iqlFacade.findObjectsByIQLAndSchema(1, "objectType = \"Application\" AND Name=\"" + objectApplicationAttr + "\"");
if (objects != null)
{
updateFileds += 1;
mi.setCustomFieldValue(applicationCF, [objectFacade.loadObjectBean(objects[0].getId())]);
}
mi.setSummary(summary2);
}
}

 

btw, if you have an idea how I can debug more easily because when i'm testing, I'm not able to have output log to print my variable.

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 4, 2019

Where are you using this code ? In a post function ?

You can debug by using

log.error("Hello world")

 So in your case, I would 

log.error("serverName : " + serverName)

before trying to retrieve the insight object to make sure you get the right name. 

I guess this works anyway since your error is further in the code.

Try replacing the last part of your code by : 

log.error("serverName : " + serverName)
def objects = iqlFacade.findObjectsByIQLAndSchema(1, "objectType = \"Server\" AND Name=\"" + serverName + "\"");
def insightObject
if (objects != null && objects.size() > 0){
insightObject = objects[0]
updateFileds += 1
mi.setCustomFieldValue(relatedCIsCF, [objectFacade.loadObjectBean(insightObject.getId())]);
}

int objectId = insightObject.getId()
String ApplicationAttr = "Application"
def objectApplicationAttr = objectFacade.loadObjectAttributeBean(objectId, ApplicationAttr)
log.error("objectApplicationAttr : " + objectApplicationAttr)
String applicationName
if (objectApplicationAttr){
applicationName = objectAttribute.getObjectAttributeValueBeans()[0].getTextValue()
}
log.error("applicationName : " + applicationName)

objects = iqlFacade.findObjectsByIQLAndSchema(1, "objectType = \"Application\" AND Name=\"" + applicationName + "\"");
if (objects != null && objects.size() > 0){
updateFileds += 1;
mi.setCustomFieldValue(applicationCF, [objectFacade.loadObjectBean(objects[0].getId())]);
}
JFA91 March 4, 2019

result is :

Logs:
ERROR: serverName : SGEFRFRA0FSP01
ERROR: objectApplicationAttr : null
ERROR: applicationName : null

so it means this part doesn't work :

int objectId = insightObject.getId()
String ApplicationAttr = "Application"
def objectApplicationAttr = objectFacade.loadObjectAttributeBean(objectId, ApplicationAttr)

 thanks for the tips, it will help me to debug

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 4, 2019

If you log 

objectId 

what do you get ? Also did the line  

mi.setCustomFieldValue(relatedCIsCF, [objectFacade.loadObjectBean(insightObject.getId())]);

work ? 

JFA91 March 4, 2019

Object ID return "912" so it works.

the other line is working too, if i removed the application part the ticket is updated with the relatedCIs value (name of the server).

String ApplicationAttr = "Application"
def objectApplicationAttr = objectFacade.loadObjectAttributeBean(objectId, ApplicationAttr)

 I think I didn't understand well this part and the value "Application" is wrong. what should I set in the ApplicationAttr variable ?

The thing is, i'm not able to access Insight anymore as the trial period just expire last week and the billing is still not validated by the management.

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 4, 2019

Ahah, Insight is one hell of an expensive add-on. You can renew the free trial license, this is totally fine since you gotta pay it anyway, and the end date is set by the provider.

Once this is done, select that the object with id 912. Check that the attribute name is "Application" (care for case and spaces).

If this is the case or if you update it and it is still not working, try 

def objectApplicationAttr = objectFacade.loadObjectAttributeBean(objectId, 12345)

12345 being the id of the attribute "Application" 

JFA91 March 4, 2019

Many thanks Antoine for your help.

I will wait until insight is back to check the value and update you.

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 5, 2019

Hi @JFA91 ,

Did you collect the budget to buy new licenses ? :)

JFA91 April 5, 2019

Hello Antoine,

 

unfortunately no. We plan to merge two Insight CMDB this year and I don't have dates. But to do that they will have to pay the licenses.

i'm trying to continue my works use REST API to see CMDB information but it tooks timessssss compare to the graphical view and I have other project to continue.

 

But I still have my reminder to update this topic once i have finish.

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 5, 2019

Oh, definitely an interesting topic ! Good luck with that, I wished my company used Insight that extensively. :)

Natalie Al-Delemi September 13, 2019

Hi guys, 

Is it possible to get the attribute ID from Insight by Name? I've been stuck with a post-function for 4 days and getting no where. The script works if I specify the ID 

def objectTypeAttributeBean = objectTypeAttributeFacade.loadObjectTypeAttributeBean(328);

 

Since the ID is changing based on the users selection it is not possible to use the previous solution. I need a way of either using Name instead of ID or get the ID in a variable through the selected object. I will put my code here for reference and grateful if anyone know how to solve it.

 

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.CustomFieldManager;
import java.sql.Timestamp;
import java.text.DateFormat
import java.util.Date

/* Get Insight Object Facade from plugin accessor */
Class objectFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade");
def objectFacade = ComponentAccessor.getOSGiComponentInstanceOfType(objectFacadeClass);

/* Get Insight Object Attribute Facade from plugin accessor */
Class objectTypeAttributeFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectTypeAttributeFacade");
def objectTypeAttributeFacade = ComponentAccessor.getOSGiComponentInstanceOfType(objectTypeAttributeFacadeClass);


/* Get the factory that creates Insight Attributes */
Class objectAttributeBeanFactoryClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.services.model.factory.ObjectAttributeBeanFactory");
def objectAttributeBeanFactory = ComponentAccessor.getOSGiComponentInstanceOfType(objectAttributeBeanFactoryClass);

/* This is the custom field with the value you want to add to an object attribute */
CustomField jiraCustomField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(10802); //This is the Jira custom field called "Updated gate date"

/* This is the custom field where the object/s you want to set the value */
CustomField insightCustomField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(10208); // This is the Jira custom field containing the gate
def insightObjects = issue.getCustomFieldValue(insightCustomField); // "issue" variable is always accessible in post function scripts.

/* This is the priority object type attribute and the one we want to modify */
def objectTypeAttributeBean = objectTypeAttributeFacade.loadObjectTypeAttributeBean(328);

if (insightObjects != null) {
insightObjects.each{insightObject ->
/* Create the new attribute bean based on the value */
def newValue = issue.getCustomFieldValue(jiraCustomField);

my = DateFormat.getDateInstance().format(newValue);

def newObjectAttributeBean = objectAttributeBeanFactory.createObjectAttributeBeanForObject(insightObject, objectTypeAttributeBean, my);
/* Load the attribute bean */
def objectAttributeBean = objectFacade.loadObjectAttributeBean(insightObject.getId(), objectTypeAttributeBean.getId());
if (objectAttributeBean != null) {
/* If attribute exist reuse the old id for the new attribute */
newObjectAttributeBean.setId(objectAttributeBean.getId());
}
/* Store the object attribute into Insight. */
try {
objectAttributeBean = objectFacade.storeObjectAttributeBean(newObjectAttributeBean);
} catch (Exception vie) {
log.warn("Could not update object attribute due to validation exception:" + vie.getMessage());
}
}
}

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 13, 2019

Hi @Natalie Al-Delemi , 

You should open a new question so more people will be able to answer and come back to it in the future. I might have time next week to take a look but I am not sure yet.

Natalie Al-Delemi September 13, 2019

thanks for the quick reply @Antoine Berry  I'm new to Atlassian community to still figuring it out. I will take your advice and add a new post with the issue.

Really appreciate if you  or anyone could help me solve it. Thanks!

Marina Veselić October 21, 2019

Did you post it? :D

I was wndering could this be helpful in developing scripts on different environments. Dev, TEST, PROD...

 

Cheers,

Marina

Roberto Marquez Bittencourt September 27, 2021

That's the reason i'd prefer to find names than id's! The first try to move a script done in test enviroment to production and i have to rewrite everything because the id's in such enviroment's are differents!

 

Cheers,

Roberto Bittencourt

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events