Forums

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

Is there a way to get the configuration of an insight customfield using the JAVA API?

Angel Peña February 24, 2022

Hi guys!

I'm having a problem.

For some reason, I need to get all the valid options of an insight customfield using the java API. As it's not a regular select customfield, I can not get the options using the `optionsManager`.

My idea was to get the configuration of the customfield, and from that, get the IQL for that customfield. Then I could proceed to execute that IQL using the insight java API and get all the options for that field.

The issue is that, after an exhaustive analysis of both the jira java API and the insight java API, I could not find a way to retrieve this IQL. Has anyone managed to accomplish this? (I'm open to use the rest API if i need to).

In the insight API i found this class, that seems to be exactly what i need:
https://insight-javadoc.riada.io/insight-javadoc-8.6/insight-core/com/riadalabs/jira/plugins/insight/services/model/customfield/AbstractCustomFieldConnectBean.html

But unfortunately, I did not find any Facade that retrieves an object of that class based on a Jira customfield ID.

Any ideas? Thank you very much in advance.

3 answers

1 vote
ITS TAB April 5, 2023

We have finally found the answer to this question. There is a way to obtain the configuration. Please see the following sample code:

import com.atlassian.jira.component.ComponentAccessor
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.*

def cf = ComponentAccessor.customFieldManager.getCustomFieldObject(12802)
def issue = ComponentAccessor.issueManager.getIssueObject('SR-8291')
def config = cf.getRelevantConfig(issue)

def customFieldFacade = ComponentAccessor.getOSGiComponentInstanceOfType(ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.CustomFieldFacade"));

customFieldFacade.findConfigurationFor(config.id).filterScopeIql

 The .filterScopeIql is only an example. You can also get the object schema ID and many more information. 

Hope this helps to avoid mixing up REST API and Groovy API.

Cheers!

0 votes
ITS TAB March 27, 2023

I actually found another way of getting the information without triggering the REST API or SQL:

import com.atlassian.jira.component.ComponentAccessor
import com.riadalabs.jira.plugins.insight.services.jira.customfield.ObjectFieldConfigItem
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
@WithPlugin('com.riadalabs.jira.plugins.insight')

def cf = ComponentAccessor.customFieldManager.getCustomFieldObject(12345)
def issue = ComponentAccessor.issueManager.getIssueObject('SW-1234')
def config = cf.getRelevantConfig(issue)
cf.getRelevantConfig(issue).configItems.each{
log.debug(it)
log.debug(" "+it.getType())
if(it.getType() instanceof ObjectFieldConfigItem){
ObjectFieldConfigItem ofci = it.getType() as ObjectFieldConfigItem
log.debug(" " + ofci.getViewHtml(it.fieldConfig,null))
}
}
You get the full HTML output that is also shown in the configuration page of your Jira instance. You just have to parse it according to your needs.

I have also seen the DefaultConnectBean that is used to obtain this in the Insight/Assets API is obtained from some CustomFieldDal object, but I did not find a way of obtaining this.

Maybe someone has another idea on how to deal with it.

0 votes
PD 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.
February 25, 2022

I've been hesitant to share my experience here in case others would choose to skip reviewing questions with at least one answer.

I've tried long and hard to find this answer as well.

I searched through all the insight javadocs for some way to instantiate a CustomFieldCOnnectBean without any luck.

I've also tried to get at it from the customField object:

 cf.configurationItemTypes[1].getConfigurationObject(issue, config)

But this returns a null.

So unless someone can point to some obscure internal api, I think you have 2 options

1) call the rest api: /rest/insight/1.0/customfield/default/<config id>/connect

2) query the db:

select * from AO_8542F1_IFJ_DCF_CONNECT where CUSTOM_FIELD_CONFIG_ID= <configId>;

 

Here is an example using rest endpoint (needs some error handling but should point you in the correct direction):

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.sal.api.net.Request
import com.atlassian.sal.api.net.Response
import com.atlassian.sal.api.net.TrustedRequest
import com.atlassian.sal.api.net.TrustedRequestFactory
import org.apache.http.client.utils.URIBuilder
import com.atlassian.jira.config.properties.APKeys
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import groovy.json.JsonSlurper
@PluginModule TrustedRequestFactory trustedRequestFactory

def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def baseUrl = ComponentAccessor.applicationProperties.getString(APKeys.JIRA_BASEURL)

//Insight Custom Field
def cf = ComponentAccessor.customFieldManager.getCustomFieldObject(19921)
//get a config for a sample issue for the insight custom field
def issue= ComponentAccessor.issueManager.getIssueObject('KEY-123')
def config = cf.getRelevantConfig(issue)

def insightConfigApiUrl = "$baseUrl/rest/insight/1.0/customfield/default/$config.id/connect"

//create a trusted request
def request = trustedRequestFactory.createTrustedRequest(Request.MethodType.GET, insightConfigApiUrl) as TrustedRequest
request.addTrustedTokenAuthentication(new URIBuilder(baseUrl).host, currentUser.username)
request.setHeader("Content-Type", 'application/json')

//execute the request and parse the response
def response = request.execute()
def insightConfig = new JsonSlurper().parseText(response)

//return the issueScopeIql
insightConfig.customFieldConnect.issueScopeIql
Yaoqi Huang February 14, 2023

Hello,

Currently I'm also struggling on getting the configuration of the asseb object type custom fields.

Do you have any ideas on retrieving this through Java API only now?

Regards,

Yaoqi Huang

PD 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.
February 14, 2023

No, I still haven't figured it out (haven't tried to much, because for me, the rest/sql methods are adequate).

I might check again after I upgrade to Jira 9.x / JSM 5.x (I'm still on 8.20/4.20)

Yaoqi Huang February 15, 2023

Hello,

About REST API, I can obtain the object schema info and the QL query info according to your groovy script.

But I am unable to find any PUT/POST REST API to update the object scheme info or QL query info. I used the developer tool of the browser and found that it sent the following request to JSM, but I cannot see why it uses 1 in the Request URL. I tested several asset object type custom fields, only found out that the digit in URL increased by 1 every time I opened a configuration of a custom field but this cannot help on automating the updating custom fields process.

Request URL: http://localhost:2990/jira/rest/insight/1.0/customfield/default/connect/1

Request body: {
    "objectSchemaId": 1
}

Could you please advise me on this?

Regards,

Yaoqi Huang

Suggest an answer

Log in or Sign up to answer