JIRA automation: Compare a string with components

William van Dalen
Contributor
August 1, 2024

Hi,

In the Description of an issue there is a text/string after the word 'subject:'. I want to compare that string with the existing components from the project. If the text/string does not exist in the components, then the field 'components' in the issue will be cleared.

For now i have something like this:

In a smart values condition:

{{issue.description.substringAfter("subject: ")}}

does not contain

{{project.components.name}}

then

{ "fields": { "components": [] } }

but this is not right. Can anyone help me please?

 

With kind regards, Will

1 answer

1 accepted

0 votes
Answer accepted
Rudy Holtkamp
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 1, 2024

Hi @William van Dalen ,

If you create a (string) variable of the list {{project.components.name}} and compare that to the smart value {{issue.description.substringAfter("subject: ")}}, you should be Ok.

Take a look at the example below. Note that I reversed the condition and added an extra condition.

image.pngimage.pngimage.png

Rudy Holtkamp
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 1, 2024

Of course you should use the trigger you want and instead of the 'debug' logs, you can simply replace that by an edit issue with the components field. And leave the components field empty, like this:image.png

 

Like William van Dalen likes this
William van Dalen
Contributor
August 6, 2024

Hi Rudy,

 

It's clear that i've got less experience with rule automation...at this moment :). Your recommendation, to use variables and add the results to the audit log, helped me a lot.

Also i reversed the condition and added an extra condition, as you told me.

Furthermore i experimented with the syntax of the rule......and now i'm very satisfied with the results of when triggering the automation rule. I send a message to the consultant, who asked me to develop this, so he can test it.

Many thanks for your help.

Regards, Will

 

Like Rudy Holtkamp likes this
William van Dalen
Contributor
August 7, 2024

Hi Rudy,

In the audit log i found out that, when the subject {{issue.description.substringAfter("subject: ")}} has more than one word, the whole string/text will be added to the variable, but only the first part/word until a whitespace appears must be put into the variable {{subject}}.

I've experimented with several functions and smart values, but everything failed. May i ask you how can i use achieve this? Do you know what i mean?

 

Regards, Will

Rudy Holtkamp
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 8, 2024

Hi @William van Dalen ,

So if you only want to have the first word after "subject:"

Change: {{issue.description.substringAfter("subject: ")}} to a find a match with a regular expression. In your case:

{{issue.description.match("(?<=subject:\s*)\w+")}}

 

Tip: If you are not familiar with regular expressions, then you can enter the rabbit hole here

Pro tip: ignore the previous tip and just use chatgpt. Provide an example text and just ask the regex to get certain content. ;-)

Like William van Dalen likes this
William van Dalen
Contributor
August 12, 2024

Hi Rudy,

It's very easy to use Chatgpt, but i prefer to learn/use Regex by myself :).

Thank you very much for your help. I'm going to figure this out.

Regards, Will

 

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.
August 12, 2024

Hi @William van Dalen 

I recommend always experimenting with regular expressions in rules to confirm what works for your needs, first trying the simplest thing that can possibly work, frequently writing results to the audit log, and incrementally improving it.  My reason is...

 

This is what the documentation states, with emphasis added by me:

The underlying implementation is based on Java's Pattern class and uses Matcher.find() to find matches.

https://support.atlassian.com/cloud-automation/docs/jira-smart-values-text-fields/#match--

 

The documentation does not state what is (and is not) supported for regular expressions with automation rules.  In my experience, there are discrepancies in supported behaviors versus the linked Java references.  And you may find community posts where customers become frustrated by trying a regular expression validated with an online regex testing tool, only to find later it returns nothing or errors in automation rules.

 

There also appears to be a parsing order problem with regular expressions in rules, particularly with the match() and replaceAll() functions.  That is, some tokens such as escaped characters may not always work because they are escaped after the expression validation is attempted.  The workaround for that is to create the regular expression in a created variable first (forcing early parsing) and then use that variable as a parameter for the regular expression.  That technique also helps when a dynamic regular expression is needed.

 

Kind regards,
Bill

William van Dalen
Contributor
August 16, 2024

Hi Bill/Rudy,

Because to combine regular expressions with smart values is very frustrating i tried first to create a variable 'subject':  {{issue.description.substringAfter("Subject: ")}}.

Then i create a variable 'subjecttrim': {{issue.description.trim(subject)}}.

Finally i set 'components'.

image.png

But that doesn't work either.

Do you have another suggestion?

 

Regards, Bill

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.
August 16, 2024

Without seeing an example of your Description field, please consider...

Let's assume your Description looks like this, and you want findMe:

some text
Subject: findMe text after subject
yet more text
the end of the description

Assuming the case for "Subject:" is consistent.

 

Parsing that with text functions, you could use this:

{{issue.description.substringAfter("Subject: ").split(" ").get(0)}}

That will grab the text after the prefix, split the remainder into a list of values, and get the first one.

 

Doing that with a match() needs an adjustment as there might be embedded newlines; using replace("\n", " ") before the match() will eliminate them.  Once that is added, please try the expression Rudy suggested.

Like William van Dalen likes this
William van Dalen
Contributor
September 6, 2024

Hi Bill,

Something unexplained happens (at least to  me)

For the time being (for test purposes) i created a variable 'compo' with a smart value to extract the desired component = {{project.components.name.get(1)}}. I wrote in the log. Of course the result is the expected value.

I created a variable 'subject' < {{issue.description.substringAfter("Subject: ").split("\n").first()}}.

In the rule i put an if-else-statement. In the if-statement i check the condition if {{compo}} equals ((subject}}.

In this rule every time the rue to the else-statement with the error 'The issue did not match the condition'. How is this possible, because in the audit log it wrote that {{compo}} exactly matches {{subject}}.?

Do you have explanation?

Regards, Will

Rudy Holtkamp
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 8, 2024

Is there perhaps a space in one of the strings? Is there a casing difference?

Like Bill Sheboy likes 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 9, 2024

Will, would you please post images of your current, complete rule, the create variable action, and the condition where it is tested?  Those will help provide context.

And to help test Rudy's suggestion, consider writing the variables to the audit log to check what they contain *exactly*, such as like this:

compo: ZZZ{{compo}}ZZZ

That will make it easier to see any spacing differences.

Thanks!

 

William van Dalen
Contributor
September 12, 2024

Hi Bill/Rudy,

Eventually the solution was easy.

Previously i already checked if there were spaces in the variable, but apparently it was not sufficient. The suggestion of Rudy triggered me. Then i checked again.......and what did i find in the audit log?

screenshot auditlog false.png

So i created a variable called subjecttmp with the smart value {{issue.description.substringAfter("Onderwerp:").split("\n").first()}}

then i created a variable called subject with the smart value {{subjecttmp.trim()}}

screenshot autditlog.png

So now the comparison between {{compo}} and {{subject}} was succesful.

I learned a lot. Thank you both of you.

Regards, Will

Like Bill Sheboy likes this

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