Merge pull request #14787 from Budibase/branching/branch-evaluation-bindings

Changes to support processing bindings on both sides of branch condition evaluation
This commit is contained in:
Peter Clement 2024-10-14 10:09:47 +01:00 committed by GitHub
commit 20acd90838
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 113 additions and 11 deletions

View File

@ -48,7 +48,9 @@ describe("Branching automations", () => {
{ stepId: branch1LogId } { stepId: branch1LogId }
), ),
condition: { condition: {
equal: { [`{{ steps.${firstLogId}.success }}`]: true }, equal: {
[`{{ literal steps.${firstLogId}.success }}`]: true,
},
}, },
}, },
branch2: { branch2: {
@ -58,19 +60,21 @@ describe("Branching automations", () => {
{ stepId: branch2LogId } { stepId: branch2LogId }
), ),
condition: { condition: {
equal: { [`{{ steps.${firstLogId}.success }}`]: false }, equal: {
[`{{ literal steps.${firstLogId}.success }}`]: false,
},
}, },
}, },
}), }),
condition: { condition: {
equal: { [`{{ steps.${firstLogId}.success }}`]: true }, equal: { [`{{ literal steps.${firstLogId}.success }}`]: true },
}, },
}, },
topLevelBranch2: { topLevelBranch2: {
steps: stepBuilder => steps: stepBuilder =>
stepBuilder.serverLog({ text: "Branch 2" }, { stepId: branch2Id }), stepBuilder.serverLog({ text: "Branch 2" }, { stepId: branch2Id }),
condition: { condition: {
equal: { [`{{ steps.${firstLogId}.success }}`]: false }, equal: { [`{{ literal steps.${firstLogId}.success }}`]: false },
}, },
}, },
}) })
@ -217,4 +221,90 @@ describe("Branching automations", () => {
) )
expect(results.steps[2]).toBeUndefined() expect(results.steps[2]).toBeUndefined()
}) })
it("evaluate multiple conditions", async () => {
const builder = createAutomationBuilder({
name: "evaluate multiple conditions",
})
const results = await builder
.appAction({ fields: { test_trigger: true } })
.serverLog({ text: "Starting automation" }, { stepId: "aN6znRYHG" })
.branch({
specialBranch: {
steps: stepBuilder => stepBuilder.serverLog({ text: "Special user" }),
condition: {
$or: {
conditions: [
{
equal: {
'{{ js "cmV0dXJuICQoInRyaWdnZXIuZmllbGRzLnRlc3RfdHJpZ2dlciIp" }}':
"{{ literal trigger.fields.test_trigger}}",
},
},
],
},
},
},
regularBranch: {
steps: stepBuilder => stepBuilder.serverLog({ text: "Regular user" }),
condition: {
$and: {
conditions: [
{
equal: { "{{ literal trigger.fields.test_trigger}}": "blah" },
},
{
equal: { "{{ literal trigger.fields.test_trigger}}": "123" },
},
],
},
},
},
})
.run()
expect(results.steps[2].outputs.message).toContain("Special user")
})
it("evaluate multiple conditions with interpolated text", async () => {
const builder = createAutomationBuilder({
name: "evaluate multiple conditions",
})
const results = await builder
.appAction({ fields: { test_trigger: true } })
.serverLog({ text: "Starting automation" }, { stepId: "aN6znRYHG" })
.branch({
specialBranch: {
steps: stepBuilder => stepBuilder.serverLog({ text: "Special user" }),
condition: {
$or: {
conditions: [
{
equal: {
"{{ trigger.fields.test_trigger }} 5":
"{{ trigger.fields.test_trigger }} 5",
},
},
],
},
},
},
regularBranch: {
steps: stepBuilder => stepBuilder.serverLog({ text: "Regular user" }),
condition: {
$and: {
conditions: [
{ equal: { "{{ trigger.fields.test_trigger }}": "blah" } },
{ equal: { "{{ trigger.fields.test_trigger }}": "123" } },
],
},
},
},
})
.run()
expect(results.steps[2].outputs.message).toContain("Special user")
})
}) })

View File

@ -88,13 +88,13 @@ class BaseStepBuilder {
Object.entries(branchConfig).forEach(([key, branch]) => { Object.entries(branchConfig).forEach(([key, branch]) => {
const stepBuilder = new StepBuilder() const stepBuilder = new StepBuilder()
branch.steps(stepBuilder) branch.steps(stepBuilder)
let branchId = uuidv4()
branchStepInputs.branches.push({ branchStepInputs.branches.push({
name: key, name: key,
condition: branch.condition, condition: branch.condition,
id: uuidv4(), id: branchId,
}) })
branchStepInputs.children![key] = stepBuilder.build() branchStepInputs.children![branchId] = stepBuilder.build()
}) })
const branchStep: AutomationStep = { const branchStep: AutomationStep = {
...definition, ...definition,

View File

@ -30,7 +30,11 @@ import {
import { AutomationContext, TriggerOutput } from "../definitions/automations" import { AutomationContext, TriggerOutput } from "../definitions/automations"
import { WorkerCallback } from "./definitions" import { WorkerCallback } from "./definitions"
import { context, logging } from "@budibase/backend-core" import { context, logging } from "@budibase/backend-core"
import { processObject, processStringSync } from "@budibase/string-templates" import {
findHBSBlocks,
processObject,
processStringSync,
} from "@budibase/string-templates"
import { cloneDeep } from "lodash/fp" import { cloneDeep } from "lodash/fp"
import { performance } from "perf_hooks" import { performance } from "perf_hooks"
import * as sdkUtils from "../sdk/utils" import * as sdkUtils from "../sdk/utils"
@ -535,13 +539,21 @@ class Orchestrator {
conditions, conditions,
filter => { filter => {
Object.entries(filter).forEach(([_, value]) => { Object.entries(filter).forEach(([_, value]) => {
Object.entries(value).forEach(([field, _]) => { Object.entries(value).forEach(([field, val]) => {
const updatedField = field.replace("{{", "{{ literal ")
const fromContext = processStringSync( const fromContext = processStringSync(
updatedField, field,
this.processContext(this.context) this.processContext(this.context)
) )
toFilter[field] = fromContext toFilter[field] = fromContext
if (typeof val === "string" && findHBSBlocks(val).length > 0) {
const processedVal = processStringSync(
val,
this.processContext(this.context)
)
value[field] = processedVal
}
}) })
}) })
return filter return filter