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

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How to use script JQL function with regex in groovy script Edited

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

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\"");

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.

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

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.

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

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

Use that:

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

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.

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

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
TAGS
Community showcase
Published in Trello

🏊‍♀️ Atlympic Event: Trello

Hello Community!  Quick disclaimer: We are running a contest on Community (The Atlympics!) from July 23rd - August 8th of 2021. If you are interested in participating in this contest (prizes! ...

124 views 1 12
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you