Create a scripted field to count the number of open issues by reporter

Николай September 25, 2019

I need assistance in creating a scripted field using Scriptrunner to count the number of open issues by reporter of current issue.

Thank you for any help.

2 answers

2 accepted

Suggest an answer

Log in or Sign up to answer
2 votes
Answer accepted
Николай Петроф September 29, 2019

Working scripted field for me (thanks for @Peter-Dave Sheehan):

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.issue.Issue

def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def jql = "project = $issue.projectObject.key and reporter = '$issue.reporter.name' and statusCategory != Done"
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchService = ComponentAccessor.getComponent(SearchService)
def query = jqlQueryParser.parseQuery(jql)

searchService.searchCount(user,query)
0 votes
Answer accepted
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 26, 2019

This script should work for that:

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

def jql= "project = $issue.projectObject.key and reporter = $user.name and resolution is empty"

def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchService = ComponentAccessor.getComponent(SearchService)
def query = jqlQueryParser.parseQuery(jql)

searchService.searchCount(user,query)
Николай September 26, 2019

Thank you for example, but i have trouble with it. I need jql like this: 

def jql= "project = 123 and reporter = $issue.reporter and resolution is empty"

but when i use this jql i get result "null". I trying some variants: $issue.getReported(), $issue.reporter.getEmailAddress() - still not work. When i remove "reporter = ... " script work fine. When i put "issue.reporter" in the end of script - i get reporter, work fine. I get error when use "reporter = $issue.reporter" in jql.

What am i doing wrong?

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 26, 2019

Try 

def jql= "project = 123 and reporter = $issue.reporter.name and resolution is empty"

BTW, if you want to include a more complex method with parens like "getEmailAddress()" you have to enclose the expression in curly brace.

What happens is the that $simple.expression or ${complex.getExpression()} ins the Gstring will be extrapolated when it gets coerced to a String when callling the parseQuery

"${issue.getEmailAddress()}"  is equivalent to "$issue.emailAddress"

Like Николай likes this
Николай September 26, 2019

Thanks for information. Still not working, get error in log: 

2019-09-27 02:00:14,024 ERROR [runner.ScriptFieldPreviewRunner]: *************************************************************************************
2019-09-27 02:00:14,024 ERROR [runner.ScriptFieldPreviewRunner]: Script field preview failed for field that has not yet been created
com.atlassian.jira.jql.parser.JqlParseException: com.atlassian.jira.jql.parser.antlr.RuntimeRecognitionException: MismatchedSetException(64!=null)
at com.atlassian.jira.jql.parser.DefaultJqlQueryParser.parseClause(DefaultJqlQueryParser.java:110)
at com.atlassian.jira.jql.parser.DefaultJqlQueryParser.parseQuery(DefaultJqlQueryParser.java:33)
at Script168.run(Script168.groovy:12)
Caused by: com.atlassian.jira.jql.parser.antlr.RuntimeRecognitionException: MismatchedSetException(64!=null)
at com.atlassian.jira.jql.parser.antlr.LexerErrorHelper.handleError(LexerErrorHelper.java:48)
at com.atlassian.jira.jql.parser.antlr.JqlLexer.recover(JqlLexer.java:130)
at com.atlassian.jira.jql.parser.antlr.JqlLexer.recover(JqlLexer.java:136)
at com.atlassian.jira.jql.parser.antlr.JqlLexer.mERROR_RESERVED(JqlLexer.java:1921)
at com.atlassian.jira.jql.parser.antlr.JqlLexer.mTokens(JqlLexer.java:2684)
at org.antlr.runtime.Lexer.nextToken(Lexer.java:85)
at org.antlr.runtime.BufferedTokenStream.fetch(BufferedTokenStream.java:143)
at org.antlr.runtime.BufferedTokenStream.sync(BufferedTokenStream.java:137)
at org.antlr.runtime.CommonTokenStream.skipOffTokenChannels(CommonTokenStream.java:113)
at org.antlr.runtime.CommonTokenStream.LT(CommonTokenStream.java:102)
at org.antlr.runtime.BufferedTokenStream.LA(BufferedTokenStream.java:174)
at com.atlassian.jira.jql.parser.antlr.JqlParser.operand(JqlParser.java:1696)
at com.atlassian.jira.jql.parser.antlr.JqlParser.terminalClause(JqlParser.java:672)
at com.atlassian.jira.jql.parser.antlr.JqlParser.notClause(JqlParser.java:555)
at com.atlassian.jira.jql.parser.antlr.JqlParser.andClause(JqlParser.java:451)
at com.atlassian.jira.jql.parser.antlr.JqlParser.orClause(JqlParser.java:366)
at com.atlassian.jira.jql.parser.antlr.JqlParser.clause(JqlParser.java:328)
at com.atlassian.jira.jql.parser.antlr.JqlParser.query(JqlParser.java:237)
at com.atlassian.jira.jql.parser.DefaultJqlQueryParser.parseClause(DefaultJqlQueryParser.java:103)
... 2 more
Caused by: MismatchedSetException(64!=null)
at com.atlassian.jira.jql.parser.antlr.JqlLexer.recover(JqlLexer.java:135)
... 18 more
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 26, 2019

What version of Jira are you using?

Николай September 26, 2019

Jira 8.0.2

Adaptavist ScriptRunner 5.6.2.1-jira8 

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 26, 2019

Can you paste your full script?

I have this working on 7.13.2 and 8.3.2

But I had to make a quick change, make sure the "def user=" line is above the "def jql=" line.

Николай September 26, 2019
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.issue.Issue

def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def jql= "project = PROJ and reporter = $issue.reporter.name and resolution is empty"
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchService = ComponentAccessor.getComponent(SearchService)
def query = jqlQueryParser.parseQuery(jql)

searchService.searchCount(user,query)
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 26, 2019

Not sure where to go from here... 

I copied your script exactly and all I changed was the project key in the jql... and that works for me.

2019-09-26 17_00_58-Window.png

Like Николай likes this
Николай September 26, 2019

Thanks for help, i suggest trouble with my setups, will checkin on clear install.

Николай September 26, 2019

I maked fresh install latest jira service desk, dont changed any settings, installed scriprunner, truing make script field and get same trouble, script wont work for me. Sad.

Nic Brough -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.
September 28, 2019

Could you explain "script won't work for me"?  What are the error messages or results, and what are you doing differently to what Peter-Dave has done?

Николай Петроф September 28, 2019

Fresh install. Jira 8.4.1, Adaptavist ScriptRunner for JIRA v. 5.6.2.1-jira8

 

2019-09-28_18-45-03.png

Log:

2019-09-28 18:44:53,112 ERROR [runner.ScriptFieldPreviewRunner]: *************************************************************************************
2019-09-28 18:44:53,113 ERROR [runner.ScriptFieldPreviewRunner]: Script field preview failed for field that has not yet been created
com.atlassian.jira.jql.parser.JqlParseException: com.atlassian.jira.jql.parser.antlr.RuntimeRecognitionException: MismatchedSetException(64!=null)
at com.atlassian.jira.jql.parser.DefaultJqlQueryParser.parseClause(DefaultJqlQueryParser.java:110)
at com.atlassian.jira.jql.parser.DefaultJqlQueryParser.parseQuery(DefaultJqlQueryParser.java:33)
at Script7.run(Script7.groovy:10)
Caused by: com.atlassian.jira.jql.parser.antlr.RuntimeRecognitionException: MismatchedSetException(64!=null)
at com.atlassian.jira.jql.parser.antlr.LexerErrorHelper.handleError(LexerErrorHelper.java:48)
at com.atlassian.jira.jql.parser.antlr.JqlLexer.recover(JqlLexer.java:130)
at com.atlassian.jira.jql.parser.antlr.JqlLexer.recover(JqlLexer.java:136)
at com.atlassian.jira.jql.parser.antlr.JqlLexer.mERROR_RESERVED(JqlLexer.java:1921)
at com.atlassian.jira.jql.parser.antlr.JqlLexer.mTokens(JqlLexer.java:2684)
at org.antlr.runtime.Lexer.nextToken(Lexer.java:85)
at org.antlr.runtime.BufferedTokenStream.fetch(BufferedTokenStream.java:143)
at org.antlr.runtime.BufferedTokenStream.sync(BufferedTokenStream.java:137)
at org.antlr.runtime.CommonTokenStream.skipOffTokenChannels(CommonTokenStream.java:113)
at org.antlr.runtime.CommonTokenStream.LT(CommonTokenStream.java:102)
at org.antlr.runtime.BufferedTokenStream.LA(BufferedTokenStream.java:174)
at com.atlassian.jira.jql.parser.antlr.JqlParser.operand(JqlParser.java:1696)
at com.atlassian.jira.jql.parser.antlr.JqlParser.terminalClause(JqlParser.java:672)
at com.atlassian.jira.jql.parser.antlr.JqlParser.notClause(JqlParser.java:555)
at com.atlassian.jira.jql.parser.antlr.JqlParser.andClause(JqlParser.java:451)
at com.atlassian.jira.jql.parser.antlr.JqlParser.orClause(JqlParser.java:366)
at com.atlassian.jira.jql.parser.antlr.JqlParser.clause(JqlParser.java:328)
at com.atlassian.jira.jql.parser.antlr.JqlParser.query(JqlParser.java:237)
at com.atlassian.jira.jql.parser.DefaultJqlQueryParser.parseClause(DefaultJqlQueryParser.java:103)
... 2 more
Caused by: MismatchedSetException(64!=null)
at com.atlassian.jira.jql.parser.antlr.JqlLexer.recover(JqlLexer.java:135)
... 18 more
Николай Петроф September 28, 2019

JIRA Service Desk 4.4.1

Project type: customer service

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 28, 2019

Looks like the query parser doesn't like the JQL that we generate.

Let's try to add some debug message.

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.issue.Issue

def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
log.debug "Reporter name is: ${issue.reporter?.name}"
//I think name and username are supposed to be the same, but let's be sure
log.debug "Reporter Username is: ${issue.reporter?.username}"
def jql= "project = TEST and reporter = $issue.reporter.name and resolution is empty"
log.debug "JQL is: $jql"
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchService = ComponentAccessor.getComponent(SearchService)
def query = jqlQueryParser.parseQuery(jql)

searchService.searchCount(user,query)

Then post the logs (before the error) and also, paste the JQL exactly as it appears int he log into your Jira Issue Navigator and show us what it renders.

Николай Петроф September 29, 2019

Thanks for idea with logs! Working jql for me is:

def jql = "project = TEST and reporter = '$issue.reporter.name' and resolution is empty"
TAGS
AUG Leaders

Atlassian Community Events