Merge branch 'master' into BUDI-8866/builder-enable-typescript

This commit is contained in:
Adria Navarro 2024-11-26 15:18:30 +01:00 committed by GitHub
commit bd7ac2ed36
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 430 additions and 159 deletions

View File

@ -114,7 +114,7 @@
$: schemaFields = search.getFields(
$tables.list,
Object.values(schema || {}),
{ allowLinks: true }
{ allowLinks: false }
)
$: queryLimit = tableId?.includes("datasource") ? "∞" : "1000"
$: isTrigger = $memoBlock?.type === AutomationStepType.TRIGGER

View File

@ -1141,10 +1141,11 @@ export const buildFormSchema = (component, asset) => {
const fieldSetting = settings.find(
setting => setting.key === "field" && setting.type.startsWith("field/")
)
if (fieldSetting && component.field) {
if (fieldSetting) {
const type = fieldSetting.type.split("field/")[1]
if (type) {
schema[component.field] = { type }
const key = component.field || component._instanceName
if (type && key) {
schema[key] = { type }
}
}
component._children?.forEach(child => {

View File

@ -16,6 +16,7 @@ import {
AutomationTriggerStepId,
AutomationEventType,
AutomationStepType,
AutomationActionStepId,
} from "@budibase/types"
import { ActionStepID } from "constants/backend/automations"
import { FIELDS } from "constants/backend"
@ -466,9 +467,13 @@ const automationActions = store => ({
.getPathSteps(block.pathTo, automation)
.slice(0, -1)
// Current step will always be the last step of the path
const currentBlock = store.actions
.getPathSteps(block.pathTo, automation)
.at(-1)
// Extract all outputs from all previous steps as available bindingsx§x
let bindings = []
const addBinding = (name, value, icon, idx, isLoopBlock, bindingName) => {
if (!name) return
const runtimeBinding = determineRuntimeBinding(
@ -519,9 +524,24 @@ const automationActions = store => ({
runtimeName = `loop.${name}`
} else if (idx === 0) {
runtimeName = `trigger.${name}`
} else if (
currentBlock?.stepId === AutomationActionStepId.EXECUTE_SCRIPT
) {
const stepId = pathSteps[idx].id
if (!stepId) {
notifications.error("Error generating binding: Step ID not found.")
return null
}
runtimeName = `steps["${stepId}"].${name}`
} else {
runtimeName = `steps.${pathSteps[idx]?.id}.${name}`
const stepId = pathSteps[idx].id
if (!stepId) {
notifications.error("Error generating binding: Step ID not found.")
return null
}
runtimeName = `steps.${stepId}.${name}`
}
return runtimeName
}
@ -637,7 +657,6 @@ const automationActions = store => ({
console.error("Loop block missing.")
}
}
Object.entries(schema).forEach(([name, value]) => {
addBinding(name, value, icon, blockIdx, isLoopBlock, bindingName)
})

View File

@ -3096,7 +3096,6 @@
"name": "Text Field",
"icon": "Text",
"styles": ["size"],
"requiredAncestors": ["form"],
"editable": true,
"size": {
"width": 400,
@ -3106,8 +3105,7 @@
{
"type": "field/string",
"label": "Field",
"key": "field",
"required": true
"key": "field"
},
{
"type": "text",
@ -3226,13 +3224,22 @@
}
]
}
]
],
"context": {
"type": "static",
"values": [
{
"label": "Value",
"key": "value",
"type": "string"
}
]
}
},
"numberfield": {
"name": "Number Field",
"icon": "123",
"styles": ["size"],
"requiredAncestors": ["form"],
"editable": true,
"size": {
"width": 400,
@ -3242,8 +3249,7 @@
{
"type": "field/number",
"label": "Field",
"key": "field",
"required": true
"key": "field"
},
{
"type": "text",
@ -3328,13 +3334,22 @@
}
]
}
]
],
"context": {
"type": "static",
"values": [
{
"label": "Value",
"key": "value",
"type": "number"
}
]
}
},
"bigintfield": {
"name": "BigInt Field",
"icon": "TagBold",
"styles": ["size"],
"requiredAncestors": ["form"],
"editable": true,
"size": {
"width": 400,
@ -3344,8 +3359,7 @@
{
"type": "field/bigint",
"label": "Field",
"key": "field",
"required": true
"key": "field"
},
{
"type": "text",
@ -3414,13 +3428,22 @@
}
]
}
]
],
"context": {
"type": "static",
"values": [
{
"label": "Value",
"key": "value",
"type": "number"
}
]
}
},
"passwordfield": {
"name": "Password Field",
"icon": "LockClosed",
"styles": ["size"],
"requiredAncestors": ["form"],
"editable": true,
"size": {
"width": 400,
@ -3430,8 +3453,7 @@
{
"type": "field/string",
"label": "Field",
"key": "field",
"required": true
"key": "field"
},
{
"type": "text",
@ -3500,13 +3522,22 @@
}
]
}
]
],
"context": {
"type": "static",
"values": [
{
"label": "Value",
"key": "value",
"type": "string"
}
]
}
},
"optionsfield": {
"name": "Options Picker",
"icon": "Menu",
"styles": ["size"],
"requiredAncestors": ["form"],
"editable": true,
"size": {
"width": 400,
@ -3516,8 +3547,7 @@
{
"type": "field/options",
"label": "Field",
"key": "field",
"required": true
"key": "field"
},
{
"type": "text",
@ -3714,13 +3744,22 @@
}
]
}
]
],
"context": {
"type": "static",
"values": [
{
"label": "Value",
"key": "value",
"type": "string"
}
]
}
},
"multifieldselect": {
"name": "Multi-select Picker",
"icon": "ViewList",
"styles": ["size"],
"requiredAncestors": ["form"],
"editable": true,
"size": {
"width": 400,
@ -3730,8 +3769,7 @@
{
"type": "field/array",
"label": "Field",
"key": "field",
"required": true
"key": "field"
},
{
"type": "text",
@ -3922,13 +3960,22 @@
}
]
}
]
],
"context": {
"type": "static",
"values": [
{
"label": "Value",
"key": "value",
"type": "array"
}
]
}
},
"booleanfield": {
"name": "Checkbox",
"icon": "SelectBox",
"editable": true,
"requiredAncestors": ["form"],
"size": {
"width": 400,
"height": 60
@ -3937,8 +3984,7 @@
{
"type": "field/boolean",
"label": "Field",
"key": "field",
"required": true
"key": "field"
},
{
"type": "text",
@ -4047,13 +4093,22 @@
}
]
}
]
],
"context": {
"type": "static",
"values": [
{
"label": "Value",
"key": "value",
"type": "boolean"
}
]
}
},
"longformfield": {
"name": "Long Form Field",
"icon": "TextAlignLeft",
"styles": ["size"],
"requiredAncestors": ["form"],
"editable": true,
"size": {
"width": 400,
@ -4063,8 +4118,7 @@
{
"type": "field/longform",
"label": "Field",
"key": "field",
"required": true
"key": "field"
},
{
"type": "text",
@ -4171,13 +4225,22 @@
}
]
}
]
],
"context": {
"type": "static",
"values": [
{
"label": "Value",
"key": "value",
"type": "string"
}
]
}
},
"datetimefield": {
"name": "Date Picker",
"icon": "Date",
"styles": ["size"],
"requiredAncestors": ["form"],
"editable": true,
"size": {
"width": 400,
@ -4187,8 +4250,7 @@
{
"type": "field/datetime",
"label": "Field",
"key": "field",
"required": true
"key": "field"
},
{
"type": "text",
@ -4291,7 +4353,17 @@
}
]
}
]
],
"context": {
"type": "static",
"values": [
{
"label": "Value",
"key": "value",
"type": "datetime"
}
]
}
},
"codescanner": {
"name": "Barcode/QR Scanner",
@ -4305,8 +4377,7 @@
{
"type": "field/barcodeqr",
"label": "Field",
"key": "field",
"required": true
"key": "field"
},
{
"type": "text",
@ -4451,7 +4522,17 @@
}
]
}
]
],
"context": {
"type": "static",
"values": [
{
"label": "Value",
"key": "value",
"type": "string"
}
]
}
},
"signaturesinglefield": {
"name": "Signature",
@ -4924,7 +5005,6 @@
"icon": "Brackets",
"styles": ["size"],
"editable": true,
"requiredAncestors": ["form"],
"size": {
"width": 400,
"height": 100
@ -4933,8 +5013,7 @@
{
"type": "field/json",
"label": "Field",
"key": "field",
"required": true
"key": "field"
},
{
"type": "text",
@ -5014,7 +5093,17 @@
}
]
}
]
],
"context": {
"type": "static",
"values": [
{
"label": "Value",
"key": "value",
"type": "string"
}
]
}
},
"s3upload": {
"name": "S3 File Upload",
@ -5029,8 +5118,7 @@
{
"type": "field/s3",
"label": "Field",
"key": "field",
"required": true
"key": "field"
},
{
"type": "text",
@ -5075,7 +5163,17 @@
"label": "Validation",
"key": "validation"
}
]
],
"context": {
"type": "static",
"values": [
{
"label": "Value",
"key": "value",
"type": "array"
}
]
}
},
"dataprovider": {
"name": "Data Provider",
@ -7643,7 +7741,6 @@
"name": "User List Field",
"icon": "UserGroup",
"styles": ["size"],
"requiredAncestors": ["form"],
"editable": true,
"size": {
"width": 400,
@ -7653,8 +7750,7 @@
{
"type": "field/bb_reference",
"label": "Field",
"key": "field",
"required": true
"key": "field"
},
{
"type": "text",
@ -7744,14 +7840,23 @@
}
]
}
]
],
"context": {
"type": "static",
"values": [
{
"label": "Value",
"key": "value",
"type": "array"
}
]
}
},
"bbreferencesinglefield": {
"devComment": "As bb reference is only used for user subtype for now, we are using user for icon and labels",
"name": "User Field",
"icon": "User",
"styles": ["size"],
"requiredAncestors": ["form"],
"editable": true,
"size": {
"width": 400,
@ -7761,8 +7866,7 @@
{
"type": "field/bb_reference_single",
"label": "Field",
"key": "field",
"required": true
"key": "field"
},
{
"type": "text",
@ -7852,6 +7956,16 @@
}
]
}
]
],
"context": {
"type": "static",
"values": [
{
"label": "Value",
"key": "value",
"type": "string"
}
]
}
}
}

View File

@ -1,7 +1,10 @@
<script>
import Placeholder from "../Placeholder.svelte"
import { getContext, onDestroy } from "svelte"
import { writable } from "svelte/store"
import { Icon } from "@budibase/bbui"
import { memo } from "@budibase/frontend-core"
import Placeholder from "../Placeholder.svelte"
import InnerForm from "./InnerForm.svelte"
export let label
export let field
@ -20,26 +23,39 @@
const formContext = getContext("form")
const formStepContext = getContext("form-step")
const fieldGroupContext = getContext("field-group")
const { styleable, builderStore } = getContext("sdk")
const { styleable, builderStore, Provider } = getContext("sdk")
const component = getContext("component")
// Register field with form
const formApi = formContext?.formApi
const labelPos = fieldGroupContext?.labelPosition || "above"
let formField
let touched = false
let labelNode
$: formStep = formStepContext ? $formStepContext || 1 : 1
$: formField = formApi?.registerField(
field,
// Memoize values required to register the field to avoid loops
const formStep = formStepContext || writable(1)
const fieldInfo = memo({
field: field || $component.name,
type,
defaultValue,
disabled,
readonly,
validation,
formStep
)
formStep: $formStep || 1,
})
$: fieldInfo.set({
field: field || $component.name,
type,
defaultValue,
disabled,
readonly,
validation,
formStep: $formStep || 1,
})
$: registerField($fieldInfo)
$: schemaType =
fieldSchema?.type !== "formula" && fieldSchema?.type !== "bigint"
? fieldSchema?.type
@ -58,6 +74,18 @@
// Determine label class from position
$: labelClass = labelPos === "above" ? "" : `spectrum-FieldLabel--${labelPos}`
const registerField = info => {
formField = formApi?.registerField(
info.field,
info.type,
info.defaultValue,
info.disabled,
info.readonly,
info.validation,
info.formStep
)
}
const updateLabel = e => {
if (touched) {
builderStore.actions.updateProp("label", e.target.textContent)
@ -71,52 +99,65 @@
})
</script>
<div
class="spectrum-Form-item"
class:span-2={span === 2}
class:span-3={span === 3}
class:span-6={span === 6 || !span}
use:styleable={$component.styles}
class:above={labelPos === "above"}
>
{#key $component.editing}
<label
bind:this={labelNode}
contenteditable={$component.editing}
on:blur={$component.editing ? updateLabel : null}
on:input={() => (touched = true)}
class:hidden={!label}
class:readonly
for={fieldState?.fieldId}
class={`spectrum-FieldLabel spectrum-FieldLabel--sizeM spectrum-Form-itemLabel ${labelClass}`}
<Provider data={{ value: fieldState?.value }}>
{#if !formContext}
<InnerForm
{disabled}
{readonly}
currentStep={writable(1)}
provideContext={false}
>
{label || " "}
</label>
{/key}
<div class="spectrum-Form-itemField">
{#if !formContext}
<Placeholder text="Form components need to be wrapped in a form" />
{:else if !fieldState}
<Placeholder />
{:else if schemaType && schemaType !== type && !["options", "longform"].includes(type)}
<Placeholder
text="This Field setting is the wrong data type for this component"
/>
{:else}
<slot />
{#if fieldState.error}
<div class="error">
<Icon name="Alert" />
<span>{fieldState.error}</span>
</div>
{:else if helpText}
<div class="helpText">
<Icon name="HelpOutline" /> <span>{helpText}</span>
</div>
{/if}
{/if}
</div>
</div>
<svelte:self {...$$props} bind:fieldState bind:fieldApi bind:fieldSchema>
<slot />
</svelte:self>
</InnerForm>
{:else}
<div
class="spectrum-Form-item"
class:span-2={span === 2}
class:span-3={span === 3}
class:span-6={span === 6 || !span}
use:styleable={$component.styles}
class:above={labelPos === "above"}
>
{#key $component.editing}
<label
bind:this={labelNode}
contenteditable={$component.editing}
on:blur={$component.editing ? updateLabel : null}
on:input={() => (touched = true)}
class:hidden={!label}
class:readonly
for={fieldState?.fieldId}
class={`spectrum-FieldLabel spectrum-FieldLabel--sizeM spectrum-Form-itemLabel ${labelClass}`}
>
{label || " "}
</label>
{/key}
<div class="spectrum-Form-itemField">
{#if !fieldState}
<Placeholder />
{:else if schemaType && schemaType !== type && !["options", "longform"].includes(type)}
<Placeholder
text="This Field setting is the wrong data type for this component"
/>
{:else}
<slot />
{#if fieldState.error}
<div class="error">
<Icon name="Alert" />
<span>{fieldState.error}</span>
</div>
{:else if helpText}
<div class="helpText">
<Icon name="HelpOutline" /> <span>{helpText}</span>
</div>
{/if}
{/if}
</div>
</div>
{/if}
</Provider>
<style>
:global(.form-block .spectrum-Form-item.span-2) {

View File

@ -5,7 +5,6 @@
import { writable } from "svelte/store"
export let dataSource
export let theme
export let size
export let disabled = false
export let readonly = false
@ -113,11 +112,9 @@
{#key resetKey}
<InnerForm
{dataSource}
{theme}
{size}
{disabled}
{readonly}
{actionType}
{schema}
{definition}
{initialValues}

View File

@ -14,6 +14,10 @@
export let disableSchemaValidation = false
export let editAutoColumns = false
// For internal use only, to disable context when being used with standalone
// fields
export let provideContext = true
// We export this store so that when we remount the inner form we can still
// persist what step we're on
export let currentStep
@ -442,8 +446,14 @@
]
</script>
<Provider {actions} data={dataContext}>
{#if provideContext}
<Provider {actions} data={dataContext}>
<div use:styleable={$component.styles} class={size}>
<slot />
</div>
</Provider>
{:else}
<div use:styleable={$component.styles} class={size}>
<slot />
</div>
</Provider>
{/if}

View File

@ -61,6 +61,9 @@ export async function run({
inputs: ServerLogStepInputs
appId: string
}): Promise<ServerLogStepOutputs> {
if (typeof inputs.text !== "string") {
inputs.text = JSON.stringify(inputs.text)
}
const message = `App ${appId} - ${inputs.text}`
console.log(message)
return {

View File

@ -1,50 +1,123 @@
import { getConfig, afterAll as _afterAll, runStep, actions } from "./utilities"
import { createAutomationBuilder } from "./utilities/AutomationTestBuilder"
import * as automation from "../index"
import * as setup from "./utilities"
import { Table } from "@budibase/types"
describe("test the execute script action", () => {
let config = getConfig()
describe("Execute Script Automations", () => {
let config = setup.getConfig(),
table: Table
beforeAll(async () => {
beforeEach(async () => {
await automation.init()
await config.init()
table = await config.createTable()
await config.createRow()
})
afterAll(_afterAll)
it("should be able to execute a script", async () => {
const res = await runStep(config, actions.EXECUTE_SCRIPT.stepId, {
code: "return 1 + 1",
afterAll(setup.afterAll)
it("should execute a basic script and return the result", async () => {
const builder = createAutomationBuilder({
name: "Basic Script Execution",
})
expect(res.value).toEqual(2)
expect(res.success).toEqual(true)
const results = await builder
.appAction({ fields: {} })
.executeScript({ code: "return 2 + 2" })
.run()
expect(results.steps[0].outputs.value).toEqual(4)
})
it("should handle a null value", async () => {
const res = await runStep(config, actions.EXECUTE_SCRIPT.stepId, {
code: null,
it("should access bindings from previous steps", async () => {
const builder = createAutomationBuilder({
name: "Access Bindings",
})
expect(res.response.message).toEqual("Invalid inputs")
expect(res.success).toEqual(false)
const results = await builder
.appAction({ fields: { data: [1, 2, 3] } })
.executeScript(
{
code: "return trigger.fields.data.map(x => x * 2)",
},
{ stepId: "binding-script-step" }
)
.run()
expect(results.steps[0].outputs.value).toEqual([2, 4, 6])
})
it("should be able to get a value from context", async () => {
const res = await runStep(
config,
actions.EXECUTE_SCRIPT.stepId,
{
code: "return steps.map(d => d.value)",
},
{
steps: [{ value: 0 }, { value: 1 }],
}
it("should handle script execution errors gracefully", async () => {
const builder = createAutomationBuilder({
name: "Handle Script Errors",
})
const results = await builder
.appAction({ fields: {} })
.executeScript({ code: "return nonexistentVariable.map(x => x)" })
.run()
expect(results.steps[0].outputs.response).toContain(
"ReferenceError: nonexistentVariable is not defined"
)
expect(res.value).toEqual([0, 1])
expect(res.response).toBeUndefined()
expect(res.success).toEqual(true)
expect(results.steps[0].outputs.success).toEqual(false)
})
it("should be able to handle an error gracefully", async () => {
const res = await runStep(config, actions.EXECUTE_SCRIPT.stepId, {
code: "return something.map(x => x.name)",
it("should handle conditional logic in scripts", async () => {
const builder = createAutomationBuilder({
name: "Conditional Script Logic",
})
expect(res.response).toEqual("ReferenceError: something is not defined")
expect(res.success).toEqual(false)
const results = await builder
.appAction({ fields: { value: 10 } })
.executeScript({
code: `
if (trigger.fields.value > 5) {
return "Value is greater than 5";
} else {
return "Value is 5 or less";
}
`,
})
.run()
expect(results.steps[0].outputs.value).toEqual("Value is greater than 5")
})
it("should use multiple steps and validate script execution", async () => {
const builder = createAutomationBuilder({
name: "Multi-Step Script Execution",
})
const results = await builder
.appAction({ fields: {} })
.serverLog(
{ text: "Starting multi-step automation" },
{ stepId: "start-log-step" }
)
.createRow(
{ row: { name: "Test Row", value: 42, tableId: table._id } },
{ stepId: "abc123" }
)
.executeScript(
{
code: `
const createdRow = steps['abc123'];
return createdRow.row.value * 2;
`,
},
{ stepId: "ScriptingStep1" }
)
.serverLog({
text: `Final result is {{ steps.ScriptingStep1.value }}`,
})
.run()
expect(results.steps[0].outputs.message).toContain(
"Starting multi-step automation"
)
expect(results.steps[1].outputs.row.value).toEqual(42)
expect(results.steps[2].outputs.value).toEqual(84)
expect(results.steps[3].outputs.message).toContain("Final result is 84")
})
})

View File

@ -34,6 +34,7 @@ import {
SearchFilters,
Branch,
FilterStepInputs,
ExecuteScriptStepInputs,
} from "@budibase/types"
import TestConfiguration from "../../../tests/utilities/TestConfiguration"
import * as setup from "../utilities"
@ -201,6 +202,18 @@ class BaseStepBuilder {
)
}
executeScript(
input: ExecuteScriptStepInputs,
opts?: { stepName?: string; stepId?: string }
): this {
return this.step(
AutomationActionStepId.EXECUTE_SCRIPT,
BUILTIN_ACTION_DEFINITIONS.EXECUTE_SCRIPT,
input,
opts
)
}
filter(input: FilterStepInputs): this {
return this.step(
AutomationActionStepId.FILTER,

View File

@ -385,7 +385,7 @@ class Orchestrator {
stepIdx: number,
pathIdx?: number
): Promise<number> {
await processObject(loopStep.inputs, this.processContext(this.context))
await processObject(loopStep.inputs, this.mergeContexts(this.context))
const iterations = getLoopIterations(loopStep)
let stepToLoopIndex = stepIdx + 1
let pathStepIdx = (pathIdx || stepIdx) + 1
@ -573,14 +573,14 @@ class Orchestrator {
for (const [field, value] of Object.entries(filters[filterKey])) {
const fromContext = processStringSync(
field,
this.processContext(this.context)
this.mergeContexts(this.context)
)
toFilter[field] = fromContext
if (typeof value === "string" && findHBSBlocks(value).length > 0) {
const processedVal = processStringSync(
value,
this.processContext(this.context)
this.mergeContexts(this.context)
)
filters[filterKey][field] = processedVal
@ -637,7 +637,7 @@ class Orchestrator {
const stepFn = await this.getStepFunctionality(step.stepId)
let inputs = await processObject(
originalStepInput,
this.processContext(this.context)
this.mergeContexts(this.context)
)
inputs = automationUtils.cleanInputValues(inputs, step.schema.inputs)
@ -645,7 +645,7 @@ class Orchestrator {
inputs: inputs,
appId: this.appId,
emitter: this.emitter,
context: this.context,
context: this.mergeContexts(this.context),
})
this.handleStepOutput(step, outputs, loopIteration)
}
@ -665,8 +665,8 @@ class Orchestrator {
return null
}
private processContext(context: AutomationContext) {
const processContext = {
private mergeContexts(context: AutomationContext) {
const mergeContexts = {
...context,
steps: {
...context.steps,
@ -674,7 +674,7 @@ class Orchestrator {
...context.stepsByName,
},
}
return processContext
return mergeContexts
}
private handleStepOutput(