Display JQL results inside web panel from Script runner (fragments)

Swathi February 10, 2024

We are trying to insert a web panel on Jira view issue. We want that panel to display the JQL results from groovy script. However, the results are not rendering even though we displayed in writer.write() .Here is the code we used :

 

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 org.apache.log4j.Category

import org.apache.log4j.Logger

def log = Logger.getLogger("com.acme.jqlresults")
log.warn("JQL is running...")
 
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchService = ComponentAccessor.getComponent(SearchService)
def issueManager = ComponentAccessor.getIssueManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

// edit this query to suit
def query = jqlQueryParser.parseQuery("project = DT and assignee = currentUser()")
 
def search = searchService.search(user, query, PagerFilter.getUnlimitedFilter())
 
log.debug("Total issues: ${search.total}")

search.results.each { documentIssue ->
    log.debug(documentIssue.key)

     writer.write(documentIssue.key,null)

 

1 answer

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.
February 11, 2024

Hi @Swathi

Welcome to the Atlassian Community.

Could you please provide a bit more information, i.e. what type of Panel are you trying to add and where do you intend for it to display?

Could you also please share a screenshot of your web-panel configuration? I am asking for this to test it in my environment and try to provide a better solution.

Thank you and Kind regards,

Ram

Swathi February 11, 2024

Hi Ram Kumar,

 

We are using custom web panel from Script runner : https://docs.adaptavist.com/sr4js/latest/features/script-fragments/custom-fragments/web-panel

Currently , we were able to insert panel and display some TEXT. See screenshot

Instead of text, we would like to display issues that are result from a groovy script. 

Here is our requirement : For any given issue (created issue), we would want to display list of similar issues (with matching summary) within the same project under this web panel. Below is the groovy script which isn't working :

 


import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.bc.issue.search.SearchService.ParseResult
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.search.SearchException
import com.atlassian.jira.issue.search.SearchResults
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.web.bean.PagerFilter

SearchService searchService = ComponentAccessor.getComponentOfType(SearchService.class)
ApplicationUser adminUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()


ParseResult parseResult = searchService.parseQuery(adminUser,
"project = '" + issue.projectObject.key + "' " +
"and summary ~ '" + issue.summary +"'")

SearchResults queryResult

if (parseResult.isValid()) {
try
{
queryResult = searchService.search(adminUser,parseResult.getQuery(), PagerFilter.getUnlimitedFilter());
if (queryResult == null) {
log.debug("search - results is null - which should not happen");
return false
}
}
catch (SearchException e)
{
log.error("Error running search", e)
}
} else {
log.debug ("Some problem trying to search for duplicates ")
return false
}

return ! (queryResult.issues.find { it.summary == issue.summary })


 

'Even though this works in script console, it isn't working in a web panel.

 

Regardsimage.png

Configuration of web panel

image.png

 

Swathi

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.
February 12, 2024

Hi @Swathi

For your requirement, you will only need to include your script in the Provider class/script section.

Below is a sample working code for your reference:-

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.RendererManager

def rendererManager = ComponentAccessor.getComponent(RendererManager)
def fieldLayoutManager = ComponentAccessor.fieldLayoutManager

Issues.search('assignee = currentUser()').each { issue ->
def fieldLayoutItem = fieldLayoutManager.getFieldLayout(issue).getFieldLayoutItem("key")
def renderer = rendererManager.getRendererForField(fieldLayoutItem)
writer.write("${renderer.render(issue.key, null)}<br/>")
}

Please note that the sample code above is not 100% exact to your environment. Hence, you will need to make the required modifications.

If you want to restrict the display of the Web Panel for certain projects for example, then you can add a condition like the below to the Condition field:-

issue.projectObject.key = 'MOCK'

I have used the example provided in the ScriptRunner's Web Panel fragment and modified it according to my requirements.

If you notice in my code, I have included the Issues.search() method.

This is a very simple approach to executing the JQL query to filter which result I am currently assigned using ScriptRunner HAPI feature.

In my use case above, I have only set the JQL Query to return all the issues assigned to me. I have not specified if it has been completed or is still active. You will need to play around with this to suit your requirements.

Below is a screenshot of the ScriptRunner Web Panel configuration:-

web_panel_config.png

Below is how the web panel is displayed on the issue:-

output.png

I hope this helps to solve your question. :-)

Thank you and Kind regards,
Ram

Swathi February 12, 2024

Hi Ram Kumar,

 

Thanks, the code seems to be working :) 

As per our requirement mentioned above , we are trying to find similar issues with matching summary. I tried inserting the value of current issue summary using this ${issue.summary} , but that does not seem to work. Any idea how we can replace or pass the value of current issue fields?

 

CODE :

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.RendererManager

def rendererManager = ComponentAccessor.getComponent(RendererManager)
def fieldLayoutManager = ComponentAccessor.fieldLayoutManager

Issues.search('assignee = currentUser() and project = DT and summary ~ ${issue.summary}').each { issue ->
def fieldLayoutItem = fieldLayoutManager.getFieldLayout(issue).getFieldLayoutItem("key")
def renderer = rendererManager.getRendererForField(fieldLayoutItem)
writer.write("${renderer.render(issue.key, null)}<br/>")
writer.write("${renderer.render(issue.summary, null)}<br/>")
}

 

Thanks!

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.
February 13, 2024

Hi @Swathi

Your approach will not work.

If you intend to do a filter, i.e. from which projects you want to cross-check, you need to modify the code to:-

import com.adaptavist.hapi.jira.issues.Issues
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.RendererManager

def rendererManager = ComponentAccessor.getComponent(RendererManager)
def fieldLayoutManager = ComponentAccessor.fieldLayoutManager

def currentIssue = context.issue as Issue

Issues.search("""project = '${currentIssue.projectObject.key}'and assignee = currentUser() and summary ~ '${currentIssue.summary}' and key != '${currentIssue.key}' """).each { issue ->
def fieldLayoutItem = fieldLayoutManager.getFieldLayout(issue).getFieldLayoutItem("key")
def renderer = rendererManager.getRendererForField(fieldLayoutItem)
writer.write("${renderer.render(issue.key, null)}<br/>")
}

Please note that the sample working code above is not 100% exact to your environment. Hence, you will need to make the required modifications.

Below are a few test screens for your reference:-

test1.png

 

test2.png

 

I hope this helps to solve your question. :-)

Thank you and Kind regards,

Ram

 

 

Swathi February 13, 2024

Hello Ram,

 

Thank you very much. The code is working as expected now!

 

Regards

swathi

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.
February 13, 2024

Hi @Swathi

Great to hear the solution worked for you.

Please accept the answer.

Thank you and Kind regards,

Ram

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events