How to use script JQL function with regex in groovy script

SWAPNIL SRIVASTAV December 6, 2019

When I am using '$' sign in groovy script for behaviour in jira, I am getting compliation error.

Objective: Trying to validate summary field. Not allowing duplicate summaries. Searching the exact text in summary and making it case-insensitive search.

JQL query which is working fine in Issue Navigator:

issueFunction in issueFieldMatch("project = "ABC and key !=AB-1", "Summary", "^(?i)Test$")

Script I tried:

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.issue.searchSearchProvider

import com.atlassian.jira.jql.parser.JqlQueryParser

import com.atlassian.jira.web.bean.PagerFilter

import com.atlassian.jira.issue.Issue

 

def summaryObj = getFieldById(getFieldChanged())

def summary = summaryObj.getValue()

def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)

def searchProvider = ComponentAccessor.getComponent(SearchProvider)

def issue = underlyingIssue

if(issue == null)

{

     return

}

def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

def query = jqlQueryParser.parseQuery("issueFunction in issueFieldMatch('project = Cluster and key!= "+issue.key+"', 'Summary', '^(?i)"+summary+"$')")

if(results.getTotal()>0)

{

     summaryObj.setError("Issue Summary already exists.")

}

else

{

     summaryObj.clearError()

}

 

I am getting error in the line I have made bold as Illegal String Body character after $ sign. I think it is because groovy performs String Interpolation also using $ sign. How to get the JQL accepted in this script?

Kindly help.

Thanks and Regards,

Swapnil Srivastav

 

 

 

1 answer

1 accepted

0 votes
Answer accepted
Damian Wodzinski
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.
December 6, 2019

Use \ before any character that can be interpreted improperly. So it would be:

def query = jqlQueryParser.parseQuery("issueFunction in issueFieldMatch('project = Cluster and key!= \"+issue.key+\"', 'Summary', '^(?i)\"+summary+\"\$')")

I do not know if you need to do it before ' characters, you need to check that.

I made similar one:

def query = jqlQueryParser.parseQuery("issueFunction in commented(\"by ${person} after startOfDay(-1d) before endOfDay(-1d)\") AND project = \"PCS\"");

SWAPNIL SRIVASTAV December 6, 2019

Hello @Damian Wodzinski ,

Thanks for your response.

I tried using \ and \\ before $. Both do not give a compilation error but do not get the task done. 

I guess that is because the regular expression meaning is altered in that case.

Regards,

Swapnil Srivastav.

Damian Wodzinski
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.
December 6, 2019

Why dont you put Regex as variable, and then add reference to your JQL part in script?

SWAPNIL SRIVASTAV December 6, 2019

Hello @Damian Wodzinski ,

Could you please let me know how to do that.

I have not worked with regular expressions before.

Thanks and Regards,

Swapnil Srivastav.

Damian Wodzinski
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.
December 6, 2019

Can you paste the whole JQL commend directly from Jira? Is it working there?

SWAPNIL SRIVASTAV December 6, 2019

Hi @Damian Wodzinski ,

Yes it is working there. I cannot paste data from JIRA. Here is the exact JQL

issueFunction in issueFieldMatch("project = "ABC and key !=AB-1", "Summary", "^(?i)Test$")

Thanks and Regards,

Swapnil Srivastav

Damian Wodzinski
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.
December 9, 2019

Use that:

def query = jqlQueryParser.parseQuery("issueFunction in issueFieldMatch(\"project = 'ABC' AND key != AB-1\",\"Summary\",\"^(?i)Test\$\")")

SWAPNIL SRIVASTAV December 9, 2019

Hello @Damian Wodzinski ,

JQL:

issueFunction in issueFieldMatch("project = ABC and issuetype = "DE", "Summary", "^(?i)Test$")

and for groovy:

issueFunction in issueFieldMatch("project = ABC" and key != ${issue.key}", "Summary", "^(?i)${summary}\$")

works fine for me.

Thanks a lot for your response

Best Regards,

Swapnil Srivastav.

Joe Pursel July 2, 2021

Hi @SWAPNIL SRIVASTAV ,

Thank you so much for the example you provided showing how to use the subquery by including multiple parameters.

Using your example and fitting it to my use case made all the difference between the query timing out and running amazingly fast.

My use case was

issueFunction in issueFieldMatch("project = ACT AND labels = S4WW AND summary ~ lifecycle.intake.firstkick", "description", "intake_date => 2021-04")

Best Regards,

Joe 

Like SWAPNIL SRIVASTAV likes this
SWAPNIL SRIVASTAV July 11, 2021

Hi @Joe Pursel ,

Glad to help. Feel free to click on the Like button next to my comment :)

Suggest an answer

Log in or Sign up to answer