Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Error while running custom JQL function in ScriptRunner

Dhananjay Kale February 6, 2023

Hi,

 

We are getting below error while running custom JQL function in ScriptRunner:

 

------------------------------------------------------

[Static type checking] - Cannot find matching method java.lang.Object#createLuceneQuery(com.atlassian.jira.jql.query.QueryCreationContext, com.atlassian.query.clause.Clause). Please check if the declared type is correct and if the method exists.

------------------------------------------------------

 

Here is the script for reference:

 

 

package com.onresolve.jira.groovy.jql

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.jql.query.LuceneQueryBuilder
import com.atlassian.jira.jql.query.QueryCreationContext
import com.atlassian.jira.jql.validator.NumberOfArgumentsValidator
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.util.MessageSet
import com.atlassian.query.clause.TerminalClause
import com.atlassian.query.operand.FunctionOperand
import org.apache.lucene.search.Query
import java.text.MessageFormat
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.web.bean.PagerFilter

class IssuesMovedToProject extends AbstractScriptedJqlFunction implements JqlQueryFunction {

    /**
     * Modify this query as appropriate.
     * See {@link java.text.MessageFormat} for details
     */

    public static final String TEMPLATE_QUERY =
        "project = {0}"

    @Override
    String getDescription() {
        "Issues moved to specified project"
    }

    @Override
    List<? extends Map> getArguments() {
        [
            [
                description: "Target Project",
                optional   : false,
            ]
        ]
    }

    @Override
    String getFunctionName() {
        "issuesMovedToProject"
    }

    def luceneQueryBuilder = ComponentAccessor.getComponent(LuceneQueryBuilder)
    def queryParser = ComponentAccessor.getComponent(JqlQueryParser)

    @Override
    Query getQuery(QueryCreationContext queryCreationContext, FunctionOperand operand, TerminalClause terminalClause) {

        def query = mergeQuery(operand)
        def searchService = ComponentAccessor.getComponent(SearchService.class)
        def user = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()
        List<Issue> issues = null

        def parseResult =  searchService.parseQuery(user, query.getQueryString())
        if (parseResult.isValid()) {

            def searchResult = searchService.search(user, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())
            issues = searchResult.results
        } else {
            log.error("Invalid JQL: " + TEMPLATE_QUERY)
        }

        def issueManager = ComponentAccessor.issueManager
        def issueList = new ArrayList<String>()

        issues.each{ Issue issue ->
            def allOtherKeys = issueManager.getAllIssueKeys(issue.id)?.findAll{it != issue.key}

            if (allOtherKeys?.size() > 0) {
                // hit, the issue had another key than the current one
                issueList.add(issue.key)
            }
        }

        def queryString = "issueKey in (" + issueList.join(",") + ")"
        def actualQuery = jqlQueryParser.parseQuery(queryString)

        return luceneQueryBuilder.createLuceneQuery(queryCreationContext, actualQuery.whereClause)      //getting error here
    }    

    private com.atlassian.query.Query mergeQuery(FunctionOperand operand) {
        def queryStr = MessageFormat.format(TEMPLATE_QUERY, operand.args.first())
        jqlQueryParser.parseQuery(queryStr)
    }    
}

Can you please look into it and share your thoughts?

Note: This script was working before JIRA upgrade (v8.13.24 from v7.*.*)

 

-DJ

2 answers

1 accepted

0 votes
Answer accepted
Jeroen Poismans
Community Champion
February 9, 2023

Ok,

We'll start debugging :-).

First, change these lines:

def luceneQueryBuilder = ComponentAccessor.getComponent(LuceneQueryBuilder)
def queryParser = ComponentAccessor.getComponent(JqlQueryParser)

To:

def luceneQueryBuilder = ComponentAccessor.getComponent(LuceneQueryBuilder)
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)

Because further in the code there are references to an unitialized variable.

 

Regards,

Jeroen 

Dhananjay Kale February 9, 2023

After changing these line, we get below errors now.

 

Cannot find matching method java.lang.Object#parseQuery(java.lang.String). Please check if the declared type is correct and if the method exists.

Script_Error.JPG

Jeroen Poismans
Community Champion
February 9, 2023

Never mind the red lines :-), does the code run?

Dhananjay Kale February 9, 2023

Many thanks Jeroen.

 

the script started working now. (y)

 

-DJ

0 votes
Jeroen Poismans
Community Champion
February 6, 2023

Hi,

Can you:

  • Put the Code in a code block? Formats -> Code black
    Better readable then :-)
  • Give the line where the error occurs?

Regards,

Jeroen

Dhananjay Kale February 6, 2023

Thank you Jeroen.

 

Have updated code block. Please have a look.

 

I am getting error on below line

 

        return luceneQueryBuilder.createLuceneQuery(queryCreationContext, actualQuery.whereClause) 

 

-DJ 

Dhananjay Kale February 9, 2023

Hello Jeroen,

 

Can I request you to look into?

 

-DJ

Suggest an answer

Log in or Sign up to answer