Custom JQL Function returns 0 results in issue navigator

Hello, 

I am trying to create a custom JQL function using scriptrunner. I have created the below code and it runs with no errors, but no issues are displaying in the issue navigator.  I am very new to groovy and scriptrunner, so I was hoping someone could help me figure out why no results are displaying in the issue navigator when I try to run the query: "issuefunction in sprint(2015, "Jan 1")

 

class Sprint extends AbstractScriptedJqlFunction implements JqlQueryFunction {

    private def log = Category.getInstance(DateCompare.class)
    //log.setLevel(org.apache.log4j.Level.DEBUG)

    def start = [
            2015: ['Jan 1': "2014-12-29",
                   'Jan 2': "2015-01-12",
                   'Feb 1': "2015-01-26",
                   'Feb 2': "2015-02-09",
                   'Mar 1': "2015-02-23",
                   'Mar 2': "2015-03-09",
                   'Mar 3': "2015-03-23",
                   'Apr 1': "2015-04-06",
                   'Apr 2': "2015-04-20",
                   'May 1': "2015-04-30",
                   'May 2': "2015-05-14",
                   'Jun 1': "2015-05-28",
                   'Jun 2': "2015-06-11",
                   'Jul 1': "2015-06-25",
                   'Jul 2': "2015-07-09",
                   'Aug 1': "2015-07-23",
                   'Aug 2': "2015-08-06",
                   'Sep 1': "2015-08-20",
                   'Sep 2': "2015-09-03",
                   'Sep 3': "2015-09-17",
                   'Oct 1': "2015-10-01",
                   'Oct 2': "2015-10-15",
                   'Nov 1': "2015-10-29",
                   'Nov 2': "2015-11-12",
                   'Dec 1': "2015-11-26",
                   'Dec 2': "2015-12-10"]
    ]

    def end = [
            2015: ['Jan 1': "2015-01-11",
                   'Jan 2': "2015-01-25",
                   'Feb 1': "2015-02-08",
                   'Feb 2': "2015-02-22",
                   'Mar 1': "2015-03-08",
                   'Mar 2': "2015-03-22",
                   'Mar 3': "2015-04-05",
                   'Apr 1': "2015-04-19",
                   'Apr 2': "2015-04-29",
                   'May 1': "2015-05-13",
                   'May 2': "2015-05-27",
                   'Jun 1': "2015-06-10",
                   'Jun 2': "2015-06-24",
                   'Jul 1': "2015-07-08",
                   'Jul 2': "2015-07-22",
                   'Aug 1': "2015-08-05",
                   'Aug 2': "2015-08-19",
                   'Sep 1': "2015-09-02",
                   'Sep 2': "2015-09-16",
                   'Sep 3': "2015-09-30",
                   'Oct 1': "2015-10-14",
                   'Oct 2': "2015-10-28",
                   'Nov 1': "2015-11-11",
                   'Nov 2': "2015-11-25",
                   'Dec 1': "2015-12-09",
                   'Dec 2': "2015-12-23"]
    ]

    def int year

    def String sprint


    @Override
    MessageSet validate(User user, FunctionOperand operand, TerminalClause terminalClause) {

        def messageSet = new MessageSetImpl()
        messageSet = new NumberOfArgumentsValidator(2, 2, getI18n()).validate(operand)

        if (messageSet.hasAnyErrors()){
            return messageSet
        }

        year = operand.args[0].toInteger()
        sprint = operand.args[1]

         if (year > 2015) {
                messageSet.addErrorMessage("The minimum year that can be used is 2015")
         } else {
             if (start.get(year) == null) {
                 messageSet.addErrorMessage("That year is not supported yet")
             }

             if (start.get(year).get(sprint) == null) {
                 messageSet.addErrorMessage("That sprint does not exist for the year")
             }
         }



        messageSet
        }


        @Override
        String getDescription() {
            "Get issues within sprint"
        }

        @Override
        List<Map> getArguments() {
            [
                    ["description": "Year (2015, 2016, ...)", "required": true],
                    ["description": "Sprint (ex - 'Jan 1', 'Aug 1')", "required": true]
            ]
        }

        @Override
        String getFunctionName() {
            "sprint"
        }

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

            validate(queryCreationContext.user, operand, terminalClause)

            Set issueIds = new HashSet()

            def String startdate = start.get(year).get(sprint)
            def String enddate = end.get(year).get(sprint)
            //log.error "createddate >= '$startdate' and createddate <= '$enddate'"

            //def booleanQuery = new BooleanQuery()

            issues = getIssues("createddate >= '$startdate' and createddate <= '$enddate'", queryCreationContext.applicationUser)

            //log.error issues

            issues.each {
                issueIds << (issues.id as String)
            }

            //booleanQuery.add(new TermQuery(new Term("issue_id", issues.id as String)), BooleanClause.Occur.SHOULD)

            //log.error issueIds
           //log.error booleanQuery

            //return booleanQuery
            new ConstantScoreQuery(new IssueIdFilter(issueIds))
        }
}

1 answer

1 accepted

0 vote

What issues are returned by: 

issues = getIssues("createddate >= '$startdate' and createddate <= '$enddate'", queryCreationContext.applicationUser)

?

Can you put some logging in so you can see what that string evaluates to, then run that query to make sure results are returned. 

 

I get this for issues: [DocumentIssueImpl[issueKey=TP-221], DocumentIssueImpl[issueKey=TP-220], DocumentIssueImpl[issueKey=TP-219], DocumentIssueImpl[issueKey=TP-218], DocumentIssueImpl[issueKey=TP-217], DocumentIssueImpl[issueKey=TP-216], DocumentIssueImpl[issueKey=TP-215], DocumentIssueImpl[issueKey=TP-214], DocumentIssueImpl[issueKey=TP-213], DocumentIssueImpl[issueKey=TP-191], DocumentIssueImpl[issueKey=TP-190], DocumentIssueImpl[issueKey=TP-189], DocumentIssueImpl[issueKey=TP-188], DocumentIssueImpl[issueKey=TP-187], DocumentIssueImpl[issueKey=TP-186], DocumentIssueImpl[issueKey=TP-185], DocumentIssueImpl[issueKey=TP-184], DocumentIssueImpl[issueKey=TP-183], DocumentIssueImpl[issueKey=TP-182], DocumentIssueImpl[issueKey=TP-161], DocumentIssueImpl[issueKey=TP-160], DocumentIssueImpl[issueKey=TP-159], DocumentIssueImpl[issueKey=TP-158], DocumentIssueImpl[issueKey=TP-157], DocumentIssueImpl[issueKey=TP-156], DocumentIssueImpl[issueKey=TP-155], DocumentIssueImpl[issueKey=TP-154], DocumentIssueImpl[issueKey=TP-153], DocumentIssueImpl[issueKey=TP-152], DocumentIssueImpl[issueKey=TP-130], DocumentIssueImpl[issueKey=TP-129], DocumentIssueImpl[issueKey=TP-128], DocumentIssueImpl[issueKey=TP-127], DocumentIssueImpl[issueKey=TP-126], DocumentIssueImpl[issueKey=TP-125], DocumentIssueImpl[issueKey=TP-124], DocumentIssueImpl[issueKey=TP-123], DocumentIssueImpl[issueKey=TP-122], DocumentIssueImpl[issueKey=TP-121], DocumentIssueImpl[issueKey=TP-100], DocumentIssueImpl[issueKey=TP-99], DocumentIssueImpl[issueKey=TP-98], DocumentIssueImpl[issueKey=TP-97], DocumentIssueImpl[issueKey=TP-96], DocumentIssueImpl[issueKey=TP-95], DocumentIssueImpl[issueKey=TP-94], DocumentIssueImpl[issueKey=TP-93], DocumentIssueImpl[issueKey=TP-92], DocumentIssueImpl[issueKey=TP-91], DocumentIssueImpl[issueKey=TP-69], DocumentIssueImpl[issueKey=TP-68], DocumentIssueImpl[issueKey=TP-67], DocumentIssueImpl[issueKey=TP-66], DocumentIssueImpl[issueKey=TP-65], DocumentIssueImpl[issueKey=TP-64], DocumentIssueImpl[issueKey=TP-63], DocumentIssueImpl[issueKey=TP-62], DocumentIssueImpl[issueKey=TP-61], DocumentIssueImpl[issueKey=TP-60], DocumentIssueImpl[issueKey=TP-41], DocumentIssueImpl[issueKey=TP-40], DocumentIssueImpl[issueKey=TP-39], DocumentIssueImpl[issueKey=TP-38], DocumentIssueImpl[issueKey=TP-37], DocumentIssueImpl[issueKey=TP-36], DocumentIssueImpl[issueKey=TP-35], DocumentIssueImpl[issueKey=TP-34], DocumentIssueImpl[issueKey=TP-33], DocumentIssueImpl[issueKey=TP-32], DocumentIssueImpl[issueKey=TP-10], DocumentIssueImpl[issueKey=TP-9], DocumentIssueImpl[issueKey=TP-8], DocumentIssueImpl[issueKey=TP-7], DocumentIssueImpl[issueKey=TP-6], DocumentIssueImpl[issueKey=TP-5], DocumentIssueImpl[issueKey=TP-4], DocumentIssueImpl[issueKey=TP-3], DocumentIssueImpl[issueKey=TP-2], DocumentIssueImpl[issueKey=TP-1]] When I manually run createddate >= '$startdate' and createddate <= '$enddate', those are the correct issue numbers

This looks wrong: issues.each {                 issueIds << (issues.id as String)             } You probably want: issues.each {                 issueIds << (it.id as String)             }

That Worked!! thank you so much!! Can you please explain what it.id is so that I know for next time?

"it" is the default name for the item that you are iterating over. So it's equivalent to writing: issues.each { anIssue -> anIssue.id ... }

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published 11 hours ago in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

35 views 0 3
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you