Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Cannot execute JQL query in ScriptRunner to print number of issues having a specific value

Mouna Hammoudi August 5, 2022

I am trying to execute some code in script runner to know how many issues have a certain value as a summary. The code should be super easy and straightforward but I am running into a JQLParseException. I am looking for all issues with value "aa" for the summary field. I am getting this exception in the log file:

2022-08-05 15:18:52,115+0200 https-openssl-nio-443-exec-22 WARN mouh 918x48846x1 19eejnp 10.248.75.214 /secure/CommentAssignIssue.jspa [atlassian-jira.log] myyy exception: com.atlassian.jira.jql.parser.JqlParseException: com.atlassian.jira.jql.parser.antlr.RuntimeRecognitionException: NoViableAltException(59@[])

I also executed the query in Jira and it returns 274 results so it should work enter image description here Here is my code

 

  import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import org.apache.log4j.Logger
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.opensymphony.workflow.InvalidInputException;
import com.softwareag.jira.insight.helper.Helper;
import java.sql.ResultSet;
import java.util.Map;
import com.atlassian.jira.project.version.Version;
import com.atlassian.jira.workflow.function.issue.AbstractJiraFunctionProvider;
import com.opensymphony.module.propertyset.PropertySet;
import com.opensymphony.workflow.WorkflowException;
import groovy.sql.GroovyRowResult
import groovy.sql.*
import groovy.sql.Sql
import java.sql.*; 
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.log4j.Logger;
import org.ofbiz.core.entity.ConnectionFactory;
import org.ofbiz.core.entity.GenericEntityException;
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter


log.warn("HELLO 111")       

def log = Logger.getLogger("atlassian-jira.log")










def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
log.warn("MOUNA 3 ")
def searchProvider = ComponentAccessor.getComponent(SearchProvider)
log.warn("MOUNA 4 ")

def issueManager = ComponentAccessor.getIssueManager()
log.warn("MOUNA 5")




// edit this query to suit
// I am looking for all issues with value "aa" for the summary field
def myquery = "summary ~ aa" 
log.warn("total issues  "+ myquery)

try{
def query = jqlQueryParser.parseQuery(myquery)

}catch (Exception e){
    log.warn("myyy exception: "+e)
}

 

1 answer

Suggest an answer

Log in or Sign up to answer
0 votes
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.
August 5, 2022

Hi @Mouna Hammoudi

I have run a basic test in my environment using the code below, and I don't seem to be encountering any issues. It can return all the issues which contain the value Test.

import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter

def issueManager = ComponentAccessor.issueManager
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchService = ComponentAccessor.getComponent(SearchService)

def query = jqlQueryParser.parseQuery("text ~ 'Test'")

def search = searchService.search(loggedInUser, query, PagerFilter.getUnlimitedFilter())

log.warn "Total issues: ${search.total}"

search.results.each { documentIssue ->
log.warn "=======>>>> ${documentIssue.key}"

def issue = issueManager.getIssueObject(documentIssue.id)

log.warn "<<<<====== ${issue.key} ${issue.summary}"
}

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

Below is a screenshot of the test that I ran in my environment:-

image1-1.png

 The expected result is returned from the JQL Query if you observe the screenshot above.

I hope this helps to answer your question. :)

Thank you and Kind regards,

Ram

Radek Dostál
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.
August 6, 2022

You will see

NoViableAltException()

when the JQL has invalid syntax - although this indeed seems a bit odd why you'd be getting this.

I'd try to escape the search string such as

def myQuery = "summary ~ \"aa\""
or
def myQuery = "summary ~ 'aa'"

which is also in Ram's test case. Maybe just restricting the search string in quotes will do it - perhaps that Issue Navigator silently does that and sanitizes user input where as the API doesn't?

TAGS
AUG Leaders

Atlassian Community Events