Forums

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

Jira Automation Rule: How to use a smart value in a regex match pattern

joshua.peeling
Contributor
April 23, 2021

Hey there,

 

I'm writing a Jira Automation Rule that is making a web request to gitlab. In the next step of the rule, I am trying to perform a regex match on the data using a pattern which needs to contain the value of another string variable I saved earlier. I want to save the matching string to a new variable I can use later.

Save the result of this regex match to a new variable:

{{apiData.match("(id=\d+[\w ,\n/\-.=]*?path_with_namespace={{myJiraSmartValue}})")}}

 

When I try this though, I get back the below error:

Error rendering smart-values when executing this rule:Failed to get value for apiData.match("(id=\d+[\w ,\n/\-.=]*?path_with_namespace={{myJiraSmartValue: {{apiData.match("(id=\d+[\w ,\n/\-.=]*?path_with_namespace={{myJiraSmartValue}})")}}

It looks like my syntax is incorrect when I do the above, the first pair of closing braces }} causes the evaluation of the whole expression to short circuit.

 

To get around the above, I tries saving the entire regex to a new smart value variable called myRegexPattern like so:

{{"(id=\d+[\w ,\n/\-.=]*?path_with_namespace=myJiraSmartValue}"}}

 

Finally, when I try to perform the regex match by passing in the myRegexPattern variable, I get back zero results:

{{apiData.match(myRegexPattern)}}

 

I KNOW that my regex pattern is correct because I've tested this scenario by replacing the variable with a hard-coded value. I can't just do this though because I won't always know what the value of the variable is... which is why I am using the variable in the first place! Does anyone know how I can pass in a variable to the regex string match function??

 

Thank You

2 answers

1 accepted

2 votes
Answer accepted
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.
April 23, 2021

Hi @joshua.peeling 

In addition nesting smart value issues, I believe another issue you are encountering is that created variables cannot consistently be used as parameters in function calls for other smart values. For example: {{issue.summary.match(myCreatedVariable)}}  I put in a support ticket for that a while ago and heard back: not intended to work that way...yet.

You do not show how you are determining {{myJiraSmartValue}} and I wonder if:

  • You incrementally search/reduce the string in steps, and
  • Always have the created variable as the first expression if that will help.  For example, {{myCreatedVar.somefunction(...

My other work-around was to parse the response calls into name/value pairs, split the lists, and the match on what I need with RegEx.

Best regards,

Bill

joshua.peeling
Contributor
April 26, 2021

Hey @Bill Sheboy

Yes that seems to be my exact issue, I cannot consistently use my created variable as a parameter in a function call for another smart value. The inconsistency is extremely frustrating because I had based my whole logic for my Jira Automation rule around the fact that I would be able to pass in {{myCreatedVariable}} to the .match() function. It doesn't make sense that I can pass in a variable to something like .plus() but not the regex function. And there's no documentation to explain this is the case!

To get off my soapbox though... The way I am storing the {{myJiraSmartValue}} is that my Jira Rule triggers whenever a Pull Request on a Jira ID has been merged. I am grabbing the pullRequest.url value, and doing a .match() on that to strip part of the URL path away and just leave me with the unique section of the URL that would take me to the gitlab project where the Pull Request was merged. Then I am storing that stripped value in the {{myJiraSmartValue}}.

Later, I then make a call to the below API:

gitlab.com/api/v4/projects?membership=true&last_activity_after={{48 hrs ago}}

Then I am storing the json list of projects that I get back in response to another variable (call it gitbabRecentlyUpdatedProjects). Then I am trying to perform gitlabRecentlyUpdatedProjects.match(partial regex pattern + myJiraSmartValue), to get back another strip of information nested in the json response that I need.

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.
April 26, 2021

Thanks for explaining, Joshua.  How about these alternatives:

Although you cannot consistently use the created variable in a function, they do seem to consistently work as the left-most expression: {{myCreatedVar.aFunction(asdfasds).anotherFunction(asfdasfasdfa)...}}  So if you can organize your rule that way it might work.

The other alternative is one from performance improvement: in-line the expression.  Once you believe you have the pieces working, try to perform that match in one single operation, without using any created variables.  I cannot tell if that is possible from your use case. 

Please note: for this second alternative, save your work as you incrementally build the rule.  If you have an error in complex RegEx, the rule builder can hang during publish.  I often work in a text editor to build up my rule's expression...just in case the rule editor hangs and loses my changes.

joshua.peeling
Contributor
April 28, 2021

@Bill Sheboy Thanks for the additional suggestions! Fortunately I was able to work around this issue entirely from the perspective of the API WebRequest I was sending.

I was able to narrow down the API response data to just the specific gitlab project that I know had changed. Because of this, I was able to update the regex I was using so that it didn't need to make use of a variable.

I definitely still think the use of variables as parameters within functions called by another smart value should be more consistent - I stick by my previous remark that it doesn't make sense I can pass in a variable to something like .plus() but not .match(). There should at least be documentation that makes this clear.

But thanks again for the help!

Like # people like 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.
April 28, 2021

I am glad to hear you got it to work by altering your response data.

Of note, here are a suggestion and a defect related to improving the issues you note for created variables.  Resolving either one of these would have made your use case much easier to implement.

https://jira.atlassian.com/browse/JRACLOUD-75019

https://codebarrel.atlassian.net/browse/AUT-2022

Like # people like this
0 votes
Radek Dostál
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.
April 23, 2021

Found this post, has a bit of discussion about this - https://community.atlassian.com/t5/Jira-Core-questions/Is-there-a-way-to-concatenate-or-nest-smart-values/qaq-p/1374187

And it ended up with a feature request: https://codebarrel.atlassian.net/browse/AUT-2086

 

So it looks that nesting smart values is just not possible - so this would need to be likely a few ugly .matches() AND .matches() AND .matches() or something to that extent.

joshua.peeling
Contributor
April 23, 2021

I see... unfortunately I don't think doing a .matches() AND .matches() AND .matches() will work, because without passing in my variable, the regex will potentially have multiple results. The only way to narrow it down from there is to make use of the variable I have, and if I can't pass it in as part of the .matches() function then I don't know how else I will be able to get just the one match I'm looking for.

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
PREMIUM
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events