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)
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
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.
Regards
Configuration of web panel
Swathi
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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:-
Below is how the web panel is displayed on the issue:-
I hope this helps to solve your question. :-)
Thank you and Kind regards,
Ram
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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:-
I hope this helps to solve your question. :-)
Thank you and Kind regards,
Ram
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Swathi
Great to hear the solution worked for you.
Please accept the answer.
Thank you and Kind regards,
Ram
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.