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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,463,963
Community Members
 
Community Events
176
Community Groups

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

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

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

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?

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events