REST Endpoint scaning

David Vyslouzil March 11, 2022

Hello community, 
We just started using Scriptrunner REST Endpoints to integrate with another system. Basically, another system calls that REST API and it contains issues from JQL filter with info. 
The problem we are now facing is, that it is not updated every time it is called. 
I was expecting that every time I call that REST Endpoint it runs through code, gets new list of issues and returns it, but nowadays REST calls returns issues which are in JQL filter anymore. 
If I update code (like delete empty row etc.) or hit "scan" button, it returns what it should. 

Is there any way to make these data actual everytime REST API is called? Or at least make any scheduler which would trigger scan every XX minutes?

I believe I have all necessary things in code which starts: 

import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.json.JsonBuilder
import groovy.transform.BaseScript
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter

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

@BaseScript CustomEndpointDelegate delegate

 
Thank you :) 
David

2 answers

1 accepted

0 votes
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
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 13, 2022

Hi @David Vyslouzil

Could you please share the complete code you are currently using for the REST Endpoint?

Thank you and Kind Regards,

Ram

David Vyslouzil March 14, 2022

Hi Ram, 
thank you for your response, sure I am adding one of codes bellow. 
One more thing we found out, isn't there any option of caching? We turned on logging on that code and it is logging only when I update REST Endpoint but there is no log when I call that API. 

Thank you. 
David


import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.json.JsonBuilder
import groovy.transform.BaseScript
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter

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

@BaseScript CustomEndpointDelegate delegate

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def versionManager = ComponentAccessor.getVersionManager()
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchService = ComponentAccessor.getComponent(SearchService)
def issueManager = ComponentAccessor.getIssueManager()

def customfield_INIKey = customFieldManager.getCustomFieldObject(15920)
def customfield_Cluster = customFieldManager.getCustomFieldObjectsByName("Cluster - Squad")
def customfield_Readiness = customFieldManager.getCustomFieldObject(15312)

def ClSq
def INIKey_Val
def Readiness_Val

def LatestFixV
String result

def user = ComponentAccessor.userManager.getUserByName("addon_com.codebarrel.addons.automation")
def IssKey
def MapIssue = [:]
def Map = [:]
def Map_Final = [:]
def List = []
def cluster
def squad
def iniIssue
def releaseDate

def query = jqlQueryParser.parseQuery("type = Epic AND FixVersion is not EMPTY AND issueFunction in linkedIssuesOf('type = Initiative AND Roadmap = 2022') AND 'Cluster - Squad' is not EMPTY AND fixVersion in releaseDate('before 2022-12-31 after 2022-01-01')")
def search = searchService.search(user, query, PagerFilter.getUnlimitedFilter())


search.results.each{ currentIssue ->
LatestFixV = null
ClSq = currentIssue.getCustomFieldValue(customfield_Cluster)
IssKey = currentIssue.key
cluster = ClSq.getAt(null) as String
squad = ClSq.getAt("1") as String
if (squad == null) squad = cluster
log.warn("currentIssue")
currentIssue.fixVersions.each { currentFixV ->
if (LatestFixV == null) {
LatestFixV = currentFixV
}else{
if (currentFixV.getReleaseDate() > LatestFixV.getReleaseDate()) LatestFixV = currentFixV
}
}
if(LatestFixV.getReleaseDate() == null) releaseDate = null
else releaseDate = LatestFixV.getReleaseDate().format("dd.MM.yyyy")
INIKey_Val = currentIssue.getCustomFieldValue(customfield_INIKey)

Readiness_Val = currentIssue.getCustomFieldValue(customfield_Readiness) as String
log.warn("${IssKey} ${Readiness_Val}")
if(Readiness_Val != null){
if(Readiness_Val.contains("Production business rollout")) Readiness_Val = "business"
else if(Readiness_Val.contains("Small improvements or Continuous development")) Readiness_Val = "development"
else if(Readiness_Val.contains("Pilot")) Readiness_Val = "Pilot"
} else Readiness_Val = "blank"

iniIssue = issueManager.getIssueObject(INIKey_Val)
MapIssue.Group = cluster
MapIssue.AxisY = squad
MapIssue.Details = iniIssue.getSummary()
MapIssue.StartDate = releaseDate
MapIssue.EndDate = releaseDate
MapIssue.Description = currentIssue.getSummary()
MapIssue.Category = Readiness_Val
MapIssue.JiraKey = INIKey_Val
MapIssue.Type = "Milestone"
MapIssue.value = "0.0"
Map.Key = IssKey
Map_Final += Map << MapIssue
List.add(Map_Final)
}

Milestones(httpMethod: "GET", groups: admins) { MultivaluedMap queryParams, String body ->
return Response.ok(new JsonBuilder([List]).toString()).build();
}
Ram Kumar Aravindakshan _Adaptavist_
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 14, 2022

Hi @David Vyslouzil

I have reviewed your code, and from what I see, this code is not going to work as expected.

Firstly, you have declared the code outside the REST Endpoint parameter, i.e.

Milestones(httpMethod: "GET", groups: admins) { MultivaluedMap queryParams, String body ->
return Response.ok(new JsonBuilder([List]).toString()).build();
}

You should instead declare it as

import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.json.JsonBuilder
import groovy.transform.BaseScript
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter

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

@BaseScript CustomEndpointDelegate delegate
Milestones(httpMethod: "GET", groups: admins) { MultivaluedMap queryParams, String body ->

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def versionManager = ComponentAccessor.getVersionManager()
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchService = ComponentAccessor.getComponent(SearchService)
def issueManager = ComponentAccessor.getIssueManager()

def customfield_INIKey = customFieldManager.getCustomFieldObject(15920)
def customfield_Cluster = customFieldManager.getCustomFieldObjectsByName("Cluster - Squad")
def customfield_Readiness = customFieldManager.getCustomFieldObject(15312)

def ClSq
def INIKey_Val
def Readiness_Val

def LatestFixV
String result

def user = ComponentAccessor.userManager.getUserByName("addon_com.codebarrel.addons.automation")
def IssKey
def MapIssue = [:]
def Map = [:]
def Map_Final = [:]
def List = []
def cluster
def squad
def iniIssue
def releaseDate

def query = jqlQueryParser.parseQuery("type = Epic AND FixVersion is not EMPTY AND issueFunction in linkedIssuesOf('type = Initiative AND Roadmap = 2022') AND 'Cluster - Squad' is not EMPTY AND fixVersion in releaseDate('before 2022-12-31 after 2022-01-01')")
def search = searchService.search(user, query, PagerFilter.getUnlimitedFilter())

search.results.each{ currentIssue ->
LatestFixV = null
ClSq = currentIssue.getCustomFieldValue(customfield_Cluster)
IssKey = currentIssue.key
cluster = ClSq.getAt(null) as String
squad = ClSq.getAt("1") as String
if (squad == null) squad = cluster
log.warn("currentIssue")
currentIssue.fixVersions.each { currentFixV ->
if (LatestFixV == null) {
LatestFixV = currentFixV
}else{
if (currentFixV.getReleaseDate() > LatestFixV.getReleaseDate())
LatestFixV = currentFixV
}
}
if(LatestFixV.getReleaseDate() == null) {
releaseDate = null
}
else {
releaseDate = LatestFixV.getReleaseDate().format("dd.MM.yyyy")
}

INIKey_Val = currentIssue.getCustomFieldValue(customfield_INIKey)
Readiness_Val = currentIssue.getCustomFieldValue(customfield_Readiness) as String
log.warn("${IssKey} ${Readiness_Val}")
if(Readiness_Val != null){
if(Readiness_Val.contains("Production business rollout")) Readiness_Val = "business"
else if(Readiness_Val.contains("Small improvements or Continuous development")) Readiness_Val = "development"
else if(Readiness_Val.contains("Pilot")) Readiness_Val = "Pilot"
} else Readiness_Val = "blank"

iniIssue = issueManager.getIssueObject(INIKey_Val)
MapIssue.Group = cluster
MapIssue.AxisY = squad
MapIssue.Details = iniIssue.getSummary()
MapIssue.StartDate = releaseDate
MapIssue.EndDate = releaseDate
MapIssue.Description = currentIssue.getSummary()
MapIssue.Category = Readiness_Val
MapIssue.JiraKey = INIKey_Val
MapIssue.Type = "Milestone"
MapIssue.value = "0.0"
Map.Key = IssKey
Map_Final += Map << MapIssue
List.add(Map_Final)
}


return Response.ok(new JsonBuilder([List]).toString()).build();
}
If you use your approach, the REST Endpoint link will not be accessible.
Also, in your code, if you notice, there are a couple of variables for which you have not specified the object type, for example:-
INIKey_Val = currentIssue.getCustomFieldValue(customfield_INIKey)
You should specify this as a long type i.e.
INIKey_Val = currentIssue.getCustomFieldValue(customfield_INIKey) as Long
Also, you have not specified its object type for the variable def LatestFixV that you have created. You have only set a value in this if-else condition:-
search.results.each{ currentIssue -> 
LatestFixV = null
ClSq = currentIssue.getCustomFieldValue(customfield_Cluster)
IssKey = currentIssue.key
cluster = ClSq.getAt(null) as String
squad = ClSq.getAt("1") as String
if (squad == null)
squad = cluster
log.warn("currentIssue")
currentIssue.fixVersions.each { currentFixV ->
if (LatestFixV == null) {
LatestFixV = currentFixV
} else{
if (currentFixV.getReleaseDate() > LatestFixV.getReleaseDate())
LatestFixV = currentFixV
}
}
the if (currentFixV.getReleaseDate() > LatestFixV.getReleaseDate()) won't work because LatestFixV is still null as shown in the image below:-
invalid_object_type.png
Once you have fixed this, you could use ScriptRunner's Jobs to invoke the REST Endpoint to get the latest value.
You can follow the example provided in the Adaptavist Library to create the REST Endpoint and also this example to invoke the REST Endpoint using the Jobs.
I hope this helps to solve your question. :)
Thank you and Kind Regards,
Ram
0 votes
David Vyslouzil March 14, 2022

Hi Ram, 
thank you for your fast response. 
Since I moved 

Milestones(httpMethod: "GET", groups: admins) { MultivaluedMap queryParams, String body ->

 to top it seems working as we needed.

Thank you for helping with that and finding improvements in code. 

Best regards, 
David V.

Suggest an answer

Log in or Sign up to answer