Forums

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

Automation iterate over smart variable coming from a web request

Pavel __ Helia May 23, 2024

I am struggling to parse data from the Bitbucket REST API into smart values. What I am trying to do is to:

  1. get pullRequests from the Bitbucket (done),
  2. filter out irrelevant PRs by "name", looking for Jira key,
  3. print out the status of 1 (or more) relevant PR.

I have webRequest like this

webhookResponse.body.detail.pullRequests: [

{ author: a, name: "ABCD", ..., status: "OPEN" }

{ author: b, name: "DEFG", ..., status: "OPEN" }

]

I want the status of "ABCD" PR only.

 

Now the issues.

1. 

If I do "advanced branching", smart value = {{webhookResponse.body.detail.pullRequests.status}}

as variable "PR", log action "PR: {{PR}}", it will produce

PR: OPEN, OPEN

That means it did NOT iterate over the list, but rather joined it to a string?

 

2. if I do 

log action: "distinct: {{webhookResponse.body.detail.pullRequests.status.distinct}}"

it will produce

distinct: [OPEN, OPEN]

does it mean it's a string, not a list?

 

3. if I do

log action: "substring: {{webhookResponse.body.detail.pullRequests.status.substring(3)}}"

to test for a string, it will produce

substring: []

So it is NOT a string.

 

4. if I do

log action: "match: {{webhookResponse.body.detail.pullRequests.status.match("(.*)")}}"

it will produce

match: []

So no luck.

 

5. If I do

log action: "splitted: {{webhookResponse.body.detail.pullRequests.status.split(",")}}"

or

log action: "splitted: {{#webhookResponse.body.detail.pullRequests.name.split(",")}} {{.}} {{/}}"

It will produce

splitted: []

Which contradicts the earlier "distinct: [OPEN, OPEN]"

 

6. If I do

log action: "name-status: {{#webhookResponse.body.detail.pullRequests}} {{name}}-{{status}}, {{/}}"

it will produce

name-status: ABCD, DEFG-OPEN, OPEN,

However I would expect it to produce

ABCD-OPEN, DEFG-OPEN

But I think I missing something here.

 

Ultimately I want to do either:

1. Smart branching and iterate over {{webhookResponse.body.detail.pullRequests}} = PR

Then check if {{PR.name}} contains "ABCD" and return {{PR.status}}.

I would need the whole pull request as a variable, not just one property like "name".

or

2. Variable 

{{#webhookResponse.body.detail.pullRequests}}

{{#if(name.match("(ABCD)")}} {{status}} {{/}} {{/}}

Which would simply give me the status.

 

Any help appreciated!

1 answer

1 accepted

1 vote
Answer accepted
Pavel __ Helia May 23, 2024

OK, I cracked it after 2 days.

webhookResponse.body.detail.pullRequests is actually an array of an array, so you need to iterate over

{{webhookResponse.body.detail.pullRequests.get(0)}} as PR

and then you can do

if {{PR.name}} contains "ABCD", do whatever.

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.
May 23, 2024

Hi @Pavel __ Helia -- Welcome to the Atlassian Community!

I am glad to learn you found an expression that works for you.

As you discovered in automation rules, sometimes nested lists get implicitly converted to arrays...which then totally breaks use in many functions and iterators.  Another example of this is the Sprint field within a list of issues, such as Lookup Issues.

Often the only solution is to serialize the objects into a created variable in a known format with delimiters, use a dynamic match() expression to extract the target, and then convert types (as needed) to get the data.

Kind regards,
Bill

Suggest an answer

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

Atlassian Community Events