Forums

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

Updating a filter JQL query via REST API in JIRA automation

Patrizia Heinzl
Contributor
September 12, 2023

Hello all,

I need some help:

I want to create an automation for updating one value in the JQL query of specific filters.

For this, I created a REST API call to first GET the JQL query of one specific filter. And then I use the PUT request to change the filter. Overall this is working but I'm struggling with replacing the specific string for my use case:

 

I want to use the replace function to convert the Customer Go Live (custom field) to the next release. 

example JQL query from filter: 

project = TRX AND issuetype = Epic AND status not in (Cancelled) AND "Customer Go Live" = "RM 23-17" 

> I want to replace only "RM 23-17" with "RM 23-18" - the rest of the query should stay the same

 

I tested this with a log action and it works after the GET call: 

{{webhookResponse.body.jql.replace("RM 23-17"," RM 23-18")}}

But it does not work like this (which is what I need):

{{webhookResponse.body.jql.replace("RM [0-9]{2}-[0-9]{2}"," RM 23-18")}} 

 

And it the PUT call itself I could not get either to work:

{
"jql":"{{webhookResponse.body.jql.replace(\"RM 23-17\",\"MDDM 23-18\")}}"
}

tempsnip.jpg

 

1 answer

0 votes
Bill Sheboy
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.
September 12, 2023

Hi @Patrizia Heinzl -- Welcome to the Atlassian Community!

My understanding is the replace() function uses plain text and the replaceAll() function is the one which can use a regular expression: https://confluence.atlassian.com/automation/jira-smart-values-text-fields-993924863.html

Please try using the replaceAll() function instead.

Kind regards,
Bill

Patrizia Heinzl
Contributor
September 12, 2023

Thank you! This solved my first issue with the log action! Now I only need to know how to format it for the PUT request!

Bill Sheboy
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.
September 12, 2023

If you use that expression in the request data is it working?

Patrizia Heinzl
Contributor
September 14, 2023

Its working if I post it as an audit log yes.

But with the REST API request not. I tried it with additional quotes or brackets around the expression, but it didn't work. THE PUT request is sent successful but it returns an empty string and changes the query of the filter jql to nothing.

In addition, I'm not sure anymore, if I'm using the correct expression for my use case with ReplaceAll().

Since I want to dynamically change the number in the string:

e.g RM 23-17 should change to RM 23-18

But in another filter if it says RM 23-20, in should change to 23-21...

So I guess in addition I would somehow need to convert it to a number first and then add 1 to it?

Sorry, I'm quite new to this...

Bill Sheboy
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.
September 14, 2023

No worries; we are all learning all the time!

Would you please show the complete expression you are trying in the PUT data?

I have done this technique before to dynamically update saved JQL filters, so I will look around to find my example to learn how I did it.

Patrizia Heinzl
Contributor
September 15, 2023

ok, thank you so much!

Since I'm still not sure how to dynamically change the number, I just started with using one filter and below expression to see if it works - knowing it will not be the final one.

My final vision was to use the Rest API call not only for one filter, but for around 15 filters I created. All of them have different queries and the only thing which should change is the RM-xx. 

But like mentioned, the current call returns an empty filter query already. I tried adding quotes or brackets but it didn't change.

Since I only need to change the jql query and not the name or description, I did not include those two in the request:

{
"jql":"{{webhookResponse.body.jql.replace("RM [0-9]{2}-[0-9]{2}"," RM 23-18")}} "
}

Screenshot 2023-09-15 081836.jpg

Bill Sheboy
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.
September 16, 2023

Thanks for that information!

Remember you need to use the replaceAll() function, and not replace() when you want to use a regular expression.

Next, your final JQL needs to have quotation marks around the string values, like this from your original question:

project = TRX AND issuetype = Epic AND status not in (Cancelled) AND "Customer Go Live" = "RM 23-17"

So we need to handle two things: the quotation marks and encoding them for sending in JSON.

The first thing we do by escaping the quotation marks when you do the replaceAll():

{{webhookResponse.body.jql.replaceAll("RM [0-9]{2}-[0-9]{2}", "\"RM 23-18\"")}}

Next we need to encode that so it works for JSON with jsonEncode.  So the final custom data would be this:

{
"jql":"{{webhookResponse.body.jql.replaceAll("RM [0-9]{2}-[0-9]{2}", "\"RM 23-18\"").jsonEncode}}"
}

 

Please look here for more information about the additional functions I suggested: https://confluence.atlassian.com/automation/jira-smart-values-text-fields-993924863.html

Patrizia Heinzl
Contributor
October 12, 2023

Thank you!! I

Like Bill Sheboy likes this

Suggest an answer

Log in or Sign up to answer