Merge branch 'master' of github.com:budibase/budibase into remove-mocks-2

This commit is contained in:
Sam Rose 2025-02-19 15:24:05 +00:00
commit 57fd9e8d3a
No known key found for this signature in database
35 changed files with 813 additions and 1045 deletions

View File

@ -266,9 +266,9 @@ export const getProdAppId = () => {
return conversions.getProdAppID(appId)
}
export function doInEnvironmentContext(
export function doInEnvironmentContext<T>(
values: Record<string, string>,
task: any
task: () => T
) {
if (!values) {
throw new Error("Must supply environment variables.")

View File

@ -15,23 +15,27 @@ const conversion: Record<DurationType, number> = {
}
export class Duration {
constructor(public ms: number) {}
to(type: DurationType) {
return this.ms / conversion[type]
}
toMs() {
return this.ms
}
toSeconds() {
return this.to(DurationType.SECONDS)
}
static convert(from: DurationType, to: DurationType, duration: number) {
const milliseconds = duration * conversion[from]
return milliseconds / conversion[to]
}
static from(from: DurationType, duration: number) {
return {
to: (to: DurationType) => {
return Duration.convert(from, to, duration)
},
toMs: () => {
return Duration.convert(from, DurationType.MILLISECONDS, duration)
},
toSeconds: () => {
return Duration.convert(from, DurationType.SECONDS, duration)
},
}
return new Duration(duration * conversion[from])
}
static fromSeconds(duration: number) {

View File

@ -2,3 +2,4 @@ export * from "./hashing"
export * from "./utils"
export * from "./stringUtils"
export * from "./Duration"
export * from "./time"

View File

@ -0,0 +1,7 @@
import { Duration } from "./Duration"
export async function time<T>(f: () => Promise<T>): Promise<[T, Duration]> {
const start = performance.now()
const result = await f()
return [result, Duration.fromMilliseconds(performance.now() - start)]
}

View File

@ -15,7 +15,6 @@ import {
import {
AutomationTriggerStepId,
AutomationEventType,
AutomationStepType,
AutomationActionStepId,
Automation,
AutomationStep,
@ -26,10 +25,14 @@ import {
UILogicalOperator,
EmptyFilterOption,
AutomationIOType,
AutomationStepSchema,
AutomationTriggerSchema,
BranchPath,
BlockDefinitions,
isBranchStep,
isTrigger,
isRowUpdateTrigger,
isRowSaveTrigger,
isAppTrigger,
BranchStep,
} from "@budibase/types"
import { ActionStepID } from "@/constants/backend/automations"
import { FIELDS } from "@/constants/backend"
@ -291,16 +294,16 @@ const automationActions = (store: AutomationStore) => ({
let result: (AutomationStep | AutomationTrigger)[] = []
pathWay.forEach(path => {
const { stepIdx, branchIdx } = path
let last = result.length ? result[result.length - 1] : []
if (!result.length) {
// Preceeding steps.
result = steps.slice(0, stepIdx + 1)
return
}
if (last && "inputs" in last) {
let last = result[result.length - 1]
if (isBranchStep(last)) {
if (Number.isInteger(branchIdx)) {
const branchId = last.inputs.branches[branchIdx].id
const children = last.inputs.children[branchId]
const children = last.inputs.children?.[branchId] || []
const stepChildren = children.slice(0, stepIdx + 1)
// Preceeding steps.
result = result.concat(stepChildren)
@ -473,23 +476,28 @@ const automationActions = (store: AutomationStore) => ({
id: block.id,
},
]
const branches: Branch[] = block.inputs?.branches || []
if (isBranchStep(block)) {
const branches = block.inputs?.branches || []
const children = block.inputs?.children || {}
branches.forEach((branch, bIdx) => {
block.inputs?.children[branch.id].forEach(
children[branch.id].forEach(
(bBlock: AutomationStep, sIdx: number, array: AutomationStep[]) => {
const ended =
array.length - 1 === sIdx && !bBlock.inputs?.branches?.length
const ended = array.length - 1 === sIdx && !branches.length
treeTraverse(bBlock, pathToCurrentNode, sIdx, bIdx, ended)
}
)
})
terminating = terminating && !branches.length
}
store.actions.registerBlock(
blockRefs,
block,
pathToCurrentNode,
terminating && !branches.length
terminating
)
}
@ -575,7 +583,6 @@ const automationActions = (store: AutomationStore) => ({
pathBlock.stepId === ActionStepID.LOOP &&
pathBlock.blockToLoop in blocks
}
const isTrigger = pathBlock.type === AutomationStepType.TRIGGER
if (isLoopBlock && loopBlockCount == 0) {
schema = {
@ -586,17 +593,14 @@ const automationActions = (store: AutomationStore) => ({
}
}
const icon = isTrigger
const icon = isTrigger(pathBlock)
? pathBlock.icon
: isLoopBlock
? "Reuse"
: pathBlock.icon
if (blockIdx === 0 && isTrigger) {
if (
pathBlock.event === AutomationEventType.ROW_UPDATE ||
pathBlock.event === AutomationEventType.ROW_SAVE
) {
if (blockIdx === 0 && isTrigger(pathBlock)) {
if (isRowUpdateTrigger(pathBlock) || isRowSaveTrigger(pathBlock)) {
let table: any = get(tables).list.find(
(table: Table) => table._id === pathBlock.inputs.tableId
)
@ -608,7 +612,7 @@ const automationActions = (store: AutomationStore) => ({
}
}
delete schema.row
} else if (pathBlock.event === AutomationEventType.APP_TRIGGER) {
} else if (isAppTrigger(pathBlock)) {
schema = Object.fromEntries(
Object.keys(pathBlock.inputs.fields || []).map(key => [
key,
@ -915,8 +919,10 @@ const automationActions = (store: AutomationStore) => ({
]
let cache:
| AutomationStepSchema<AutomationActionStepId>
| AutomationTriggerSchema<AutomationTriggerStepId>
| AutomationStep
| AutomationTrigger
| AutomationStep[]
| undefined = undefined
pathWay.forEach((path, pathIdx, array) => {
const { stepIdx, branchIdx } = path
@ -938,9 +944,13 @@ const automationActions = (store: AutomationStore) => ({
}
return
}
if (Number.isInteger(branchIdx)) {
if (
Number.isInteger(branchIdx) &&
!Array.isArray(cache) &&
isBranchStep(cache)
) {
const branchId = cache.inputs.branches[branchIdx].id
const children = cache.inputs.children[branchId]
const children = cache.inputs.children?.[branchId] || []
if (final) {
insertBlock(children, stepIdx)
@ -1090,7 +1100,7 @@ const automationActions = (store: AutomationStore) => ({
branchLeft: async (
pathTo: Array<any>,
automation: Automation,
block: AutomationStep
block: BranchStep
) => {
const update = store.actions.shiftBranch(pathTo, block)
if (update) {
@ -1113,7 +1123,7 @@ const automationActions = (store: AutomationStore) => ({
branchRight: async (
pathTo: Array<BranchPath>,
automation: Automation,
block: AutomationStep
block: BranchStep
) => {
const update = store.actions.shiftBranch(pathTo, block, 1)
if (update) {
@ -1133,7 +1143,7 @@ const automationActions = (store: AutomationStore) => ({
* @param {Number} direction - the direction of the swap. Defaults to -1 for left, add 1 for right
* @returns
*/
shiftBranch: (pathTo: Array<any>, block: AutomationStep, direction = -1) => {
shiftBranch: (pathTo: Array<any>, block: BranchStep, direction = -1) => {
let newBlock = cloneDeep(block)
const branchPath = pathTo.at(-1)
const targetIdx = branchPath.branchIdx

View File

@ -8,6 +8,7 @@ import {
UIComponentError,
ComponentDefinition,
DependsOnComponentSetting,
Screen,
} from "@budibase/types"
import { queries } from "./queries"
import { views } from "./views"
@ -66,6 +67,7 @@ export const screenComponentErrorList = derived(
if (!$selectedScreen) {
return []
}
const screen = $selectedScreen
const datasources = {
...reduceBy("_id", $tables.list),
@ -79,7 +81,9 @@ export const screenComponentErrorList = derived(
const errors: UIComponentError[] = []
function checkComponentErrors(component: Component, ancestors: string[]) {
errors.push(...getInvalidDatasources(component, datasources, definitions))
errors.push(
...getInvalidDatasources(screen, component, datasources, definitions)
)
errors.push(...getMissingRequiredSettings(component, definitions))
errors.push(...getMissingAncestors(component, definitions, ancestors))
@ -95,6 +99,7 @@ export const screenComponentErrorList = derived(
)
function getInvalidDatasources(
screen: Screen,
component: Component,
datasources: Record<string, any>,
definitions: Record<string, ComponentDefinition>

@ -1 +1 @@
Subproject commit eb96d8b2f2029033b0f758078ed30c888e8fb249
Subproject commit 45f5673d5e5ab3c22deb6663cea2e31a628aa133

View File

@ -13,6 +13,7 @@ import sdk from "../../../sdk"
import {
ConfigType,
FieldType,
FilterCondition,
isDidNotTriggerResponse,
SettingsConfig,
Table,
@ -20,12 +21,9 @@ import {
import { mocks } from "@budibase/backend-core/tests"
import { removeDeprecated } from "../../../automations/utils"
import { createAutomationBuilder } from "../../../automations/tests/utilities/AutomationTestBuilder"
import { automations } from "@budibase/shared-core"
import { basicTable } from "../../../tests/utilities/structures"
import TestConfiguration from "../../../tests/utilities/TestConfiguration"
const FilterConditions = automations.steps.filter.FilterConditions
const MAX_RETRIES = 4
const {
basicAutomation,
@ -487,15 +485,40 @@ describe("/automations", () => {
expect(events.automation.created).not.toHaveBeenCalled()
expect(events.automation.triggerUpdated).not.toHaveBeenCalled()
})
it("can update an input field", async () => {
const { automation } = await createAutomationBuilder(config)
.onRowDeleted({ tableId: "tableId" })
.serverLog({ text: "test" })
.save()
automation.definition.trigger.inputs.tableId = "newTableId"
const { automation: updatedAutomation } =
await config.api.automation.update(automation)
expect(updatedAutomation.definition.trigger.inputs.tableId).toEqual(
"newTableId"
)
})
it("cannot update a readonly field", async () => {
const { automation } = await createAutomationBuilder(config)
.onRowAction({ tableId: "tableId" })
.serverLog({ text: "test" })
.save()
automation.definition.trigger.inputs.tableId = "newTableId"
await config.api.automation.update(automation, {
status: 400,
body: {
message: "Field tableId is readonly and it cannot be modified",
},
})
})
})
describe("fetch", () => {
it("return all the automations for an instance", async () => {
const fetchResponse = await config.api.automation.fetch()
for (const auto of fetchResponse.automations) {
await config.api.automation.delete(auto)
}
const { automation: automation1 } = await config.api.automation.post(
newAutomation()
)
@ -594,7 +617,7 @@ describe("/automations", () => {
steps: [
{
inputs: {
condition: FilterConditions.EQUAL,
condition: FilterCondition.EQUAL,
field: "{{ trigger.row.City }}",
value: "{{ trigger.oldRow.City }}",
},

View File

@ -27,6 +27,8 @@ import {
Hosting,
ActionImplementation,
AutomationStepDefinition,
AutomationStepInputs,
AutomationStepOutputs,
} from "@budibase/types"
import sdk from "../sdk"
import { getAutomationPlugin } from "../utilities/fileSystem"
@ -120,11 +122,15 @@ export async function getActionDefinitions(): Promise<
}
/* istanbul ignore next */
export async function getAction(
stepId: AutomationActionStepId
): Promise<ActionImplementation<any, any> | undefined> {
export async function getAction<
TStep extends AutomationActionStepId,
TInputs = AutomationStepInputs<TStep>,
TOutputs = AutomationStepOutputs<TStep>
>(stepId: TStep): Promise<ActionImplementation<TInputs, TOutputs> | undefined> {
if (ACTION_IMPLS[stepId as keyof ActionImplType] != null) {
return ACTION_IMPLS[stepId as keyof ActionImplType]
return ACTION_IMPLS[
stepId as keyof ActionImplType
] as unknown as ActionImplementation<TInputs, TOutputs>
}
// must be a plugin

View File

@ -6,10 +6,10 @@ import {
import sdk from "../sdk"
import {
AutomationAttachment,
BaseIOStructure,
FieldSchema,
FieldType,
Row,
LoopStepType,
LoopStepInputs,
} from "@budibase/types"
import { objectStore, context } from "@budibase/backend-core"
import * as uuid from "uuid"
@ -32,33 +32,34 @@ import path from "path"
* primitive types.
*/
export function cleanInputValues<T extends Record<string, any>>(
inputs: any,
schema?: any
inputs: T,
schema?: Partial<Record<keyof T, FieldSchema | BaseIOStructure>>
): T {
if (schema == null) {
return inputs
}
for (let inputKey of Object.keys(inputs)) {
const keys = Object.keys(inputs) as (keyof T)[]
for (let inputKey of keys) {
let input = inputs[inputKey]
if (typeof input !== "string") {
continue
}
let propSchema = schema.properties[inputKey]
let propSchema = schema?.[inputKey]
if (!propSchema) {
continue
}
if (propSchema.type === "boolean") {
let lcInput = input.toLowerCase()
if (lcInput === "true") {
// @ts-expect-error - indexing a generic on purpose
inputs[inputKey] = true
}
if (lcInput === "false") {
// @ts-expect-error - indexing a generic on purpose
inputs[inputKey] = false
}
}
if (propSchema.type === "number") {
let floatInput = parseFloat(input)
if (!isNaN(floatInput)) {
// @ts-expect-error - indexing a generic on purpose
inputs[inputKey] = floatInput
}
}
@ -93,7 +94,7 @@ export function cleanInputValues<T extends Record<string, any>>(
*/
export async function cleanUpRow(tableId: string, row: Row) {
let table = await sdk.tables.getTable(tableId)
return cleanInputValues(row, { properties: table.schema })
return cleanInputValues(row, table.schema)
}
export function getError(err: any) {
@ -271,36 +272,3 @@ export function stringSplit(value: string | string[]) {
}
return value.split(",")
}
export function typecastForLooping(input: LoopStepInputs) {
if (!input || !input.binding) {
return null
}
try {
switch (input.option) {
case LoopStepType.ARRAY:
if (typeof input.binding === "string") {
return JSON.parse(input.binding)
}
break
case LoopStepType.STRING:
if (Array.isArray(input.binding)) {
return input.binding.join(",")
}
break
}
} catch (err) {
throw new Error("Unable to cast to correct type")
}
return input.binding
}
export function ensureMaxIterationsAsNumber(
value: number | string | undefined
): number | undefined {
if (typeof value === "number") return value
if (typeof value === "string") {
return parseInt(value)
}
return undefined
}

View File

@ -1,46 +0,0 @@
import * as automationUtils from "./automationUtils"
import { isPlainObject } from "lodash"
type ObjValue = {
[key: string]: string | ObjValue
}
export function replaceFakeBindings<T extends Record<string, any>>(
originalStepInput: T,
loopStepNumber: number
): T {
const result: Record<string, any> = {}
for (const [key, value] of Object.entries(originalStepInput)) {
result[key] = replaceBindingsRecursive(value, loopStepNumber)
}
return result as T
}
function replaceBindingsRecursive(
value: string | ObjValue,
loopStepNumber: number
) {
if (value === null || value === undefined) {
return value
}
if (typeof value === "object") {
for (const [innerKey, innerValue] of Object.entries(value)) {
if (typeof innerValue === "string") {
value[innerKey] = automationUtils.substituteLoopStep(
innerValue,
`steps.${loopStepNumber}`
)
} else if (
innerValue &&
isPlainObject(innerValue) &&
Object.keys(innerValue).length > 0
) {
value[innerKey] = replaceBindingsRecursive(innerValue, loopStepNumber)
}
}
} else if (typeof value === "string") {
value = automationUtils.substituteLoopStep(value, `steps.${loopStepNumber}`)
}
return value
}

View File

@ -1,7 +1,8 @@
import { FilterStepInputs, FilterStepOutputs } from "@budibase/types"
import { automations } from "@budibase/shared-core"
const FilterConditions = automations.steps.filter.FilterConditions
import {
FilterCondition,
FilterStepInputs,
FilterStepOutputs,
} from "@budibase/types"
export async function run({
inputs,
@ -26,16 +27,16 @@ export async function run({
let result = false
if (typeof field !== "object" && typeof value !== "object") {
switch (condition) {
case FilterConditions.EQUAL:
case FilterCondition.EQUAL:
result = field === value
break
case FilterConditions.NOT_EQUAL:
case FilterCondition.NOT_EQUAL:
result = field !== value
break
case FilterConditions.GREATER_THAN:
case FilterCondition.GREATER_THAN:
result = field > value
break
case FilterConditions.LESS_THAN:
case FilterCondition.LESS_THAN:
result = field < value
break
}

View File

@ -1,9 +1,5 @@
import {
typecastForLooping,
cleanInputValues,
substituteLoopStep,
} from "../automationUtils"
import { LoopStepType } from "@budibase/types"
import { AutomationIOType } from "@budibase/types"
import { cleanInputValues, substituteLoopStep } from "../automationUtils"
describe("automationUtils", () => {
describe("substituteLoopStep", () => {
@ -30,29 +26,6 @@ describe("automationUtils", () => {
})
})
describe("typeCastForLooping", () => {
it("should parse to correct type", () => {
expect(
typecastForLooping({ option: LoopStepType.ARRAY, binding: [1, 2, 3] })
).toEqual([1, 2, 3])
expect(
typecastForLooping({ option: LoopStepType.ARRAY, binding: "[1,2,3]" })
).toEqual([1, 2, 3])
expect(
typecastForLooping({ option: LoopStepType.STRING, binding: [1, 2, 3] })
).toEqual("1,2,3")
})
it("should handle null values", () => {
// expect it to handle where the binding is null
expect(
typecastForLooping({ option: LoopStepType.ARRAY, binding: null })
).toEqual(null)
expect(() =>
typecastForLooping({ option: LoopStepType.ARRAY, binding: "test" })
).toThrow()
})
})
describe("cleanInputValues", () => {
it("should handle array relationship fields from read binding", () => {
const schema = {
@ -70,15 +43,12 @@ describe("automationUtils", () => {
},
}
expect(
cleanInputValues(
{
cleanInputValues({
row: {
relationship: `[{"_id": "ro_ta_users_us_3"}]`,
},
schema,
},
schema
)
})
).toEqual({
row: {
relationship: [{ _id: "ro_ta_users_us_3" }],
@ -103,15 +73,12 @@ describe("automationUtils", () => {
},
}
expect(
cleanInputValues(
{
cleanInputValues({
row: {
relationship: `ro_ta_users_us_3`,
},
schema,
},
schema
)
})
).toEqual({
row: {
relationship: "ro_ta_users_us_3",
@ -122,28 +89,27 @@ describe("automationUtils", () => {
it("should be able to clean inputs with the utilities", () => {
// can't clean without a schema
let output = cleanInputValues({ a: "1" })
expect(output.a).toBe("1")
output = cleanInputValues(
const one = cleanInputValues({ a: "1" })
expect(one.a).toBe("1")
const two = cleanInputValues(
{ a: "1", b: "true", c: "false", d: 1, e: "help" },
{
properties: {
a: {
type: "number",
type: AutomationIOType.NUMBER,
},
b: {
type: "boolean",
type: AutomationIOType.BOOLEAN,
},
c: {
type: "boolean",
},
type: AutomationIOType.BOOLEAN,
},
}
)
expect(output.a).toBe(1)
expect(output.b).toBe(true)
expect(output.c).toBe(false)
expect(output.d).toBe(1)
expect(two.a).toBe(1)
expect(two.b).toBe(true)
expect(two.c).toBe(false)
expect(two.d).toBe(1)
})
})
})

View File

@ -1,5 +1,11 @@
import * as automation from "../index"
import { LoopStepType, FieldType, Table, Datasource } from "@budibase/types"
import {
LoopStepType,
FieldType,
Table,
Datasource,
FilterCondition,
} from "@budibase/types"
import { createAutomationBuilder } from "./utilities/AutomationTestBuilder"
import {
DatabaseName,
@ -7,12 +13,9 @@ import {
} from "../../integrations/tests/utils"
import { Knex } from "knex"
import { generator } from "@budibase/backend-core/tests"
import { automations } from "@budibase/shared-core"
import TestConfiguration from "../../tests/utilities/TestConfiguration"
import { basicTable } from "../../tests/utilities/structures"
const FilterConditions = automations.steps.filter.FilterConditions
describe("Automation Scenarios", () => {
const config = new TestConfiguration()
@ -256,7 +259,7 @@ describe("Automation Scenarios", () => {
})
.filter({
field: "{{ steps.2.rows.0.value }}",
condition: FilterConditions.EQUAL,
condition: FilterCondition.EQUAL,
value: 20,
})
.serverLog({ text: "Equal condition met" })
@ -282,7 +285,7 @@ describe("Automation Scenarios", () => {
})
.filter({
field: "{{ steps.2.rows.0.value }}",
condition: FilterConditions.NOT_EQUAL,
condition: FilterCondition.NOT_EQUAL,
value: 20,
})
.serverLog({ text: "Not Equal condition met" })
@ -295,37 +298,37 @@ describe("Automation Scenarios", () => {
const testCases = [
{
condition: FilterConditions.EQUAL,
condition: FilterCondition.EQUAL,
value: 10,
rowValue: 10,
expectPass: true,
},
{
condition: FilterConditions.NOT_EQUAL,
condition: FilterCondition.NOT_EQUAL,
value: 10,
rowValue: 20,
expectPass: true,
},
{
condition: FilterConditions.GREATER_THAN,
condition: FilterCondition.GREATER_THAN,
value: 10,
rowValue: 15,
expectPass: true,
},
{
condition: FilterConditions.LESS_THAN,
condition: FilterCondition.LESS_THAN,
value: 10,
rowValue: 5,
expectPass: true,
},
{
condition: FilterConditions.GREATER_THAN,
condition: FilterCondition.GREATER_THAN,
value: 10,
rowValue: 5,
expectPass: false,
},
{
condition: FilterConditions.LESS_THAN,
condition: FilterCondition.LESS_THAN,
value: 10,
rowValue: 15,
expectPass: false,

View File

@ -4,7 +4,7 @@ import {
} from "../../../tests/utilities/structures"
import { objectStore } from "@budibase/backend-core"
import { createAutomationBuilder } from "../utilities/AutomationTestBuilder"
import { Row, Table } from "@budibase/types"
import { FilterCondition, Row, Table } from "@budibase/types"
import TestConfiguration from "../../../tests/utilities/TestConfiguration"
async function uploadTestFile(filename: string) {
@ -90,7 +90,7 @@ describe("test the create row action", () => {
.createRow({ row: {} }, { stepName: "CreateRow" })
.filter({
field: "{{ stepsByName.CreateRow.success }}",
condition: "equal",
condition: FilterCondition.EQUAL,
value: true,
})
.serverLog(
@ -131,7 +131,7 @@ describe("test the create row action", () => {
.createRow({ row: attachmentRow }, { stepName: "CreateRow" })
.filter({
field: "{{ stepsByName.CreateRow.success }}",
condition: "equal",
condition: FilterCondition.EQUAL,
value: true,
})
.serverLog(

View File

@ -1,19 +1,21 @@
import { automations } from "@budibase/shared-core"
import { createAutomationBuilder } from "../utilities/AutomationTestBuilder"
import TestConfiguration from "../../../tests/utilities/TestConfiguration"
import { FilterCondition } from "@budibase/types"
const FilterConditions = automations.steps.filter.FilterConditions
function stringToFilterCondition(condition: "==" | "!=" | ">" | "<"): string {
function stringToFilterCondition(
condition: "==" | "!=" | ">" | "<"
): FilterCondition {
switch (condition) {
case "==":
return FilterConditions.EQUAL
return FilterCondition.EQUAL
case "!=":
return FilterConditions.NOT_EQUAL
return FilterCondition.NOT_EQUAL
case ">":
return FilterConditions.GREATER_THAN
return FilterCondition.GREATER_THAN
case "<":
return FilterConditions.LESS_THAN
return FilterCondition.LESS_THAN
default:
throw new Error(`Unsupported condition: ${condition}`)
}
}

View File

@ -6,8 +6,8 @@ import {
ServerLogStepOutputs,
CreateRowStepOutputs,
FieldType,
FilterCondition,
} from "@budibase/types"
import * as loopUtils from "../../loopUtils"
import { createAutomationBuilder } from "../utilities/AutomationTestBuilder"
import TestConfiguration from "../../../tests/utilities/TestConfiguration"
@ -30,8 +30,8 @@ describe("Attempt to run a basic loop automation", () => {
await config.api.row.save(table._id!, {})
})
afterAll(() => {
automation.shutdown()
afterAll(async () => {
await automation.shutdown()
config.end()
})
@ -535,97 +535,30 @@ describe("Attempt to run a basic loop automation", () => {
expect(results.steps[2].outputs.rows).toHaveLength(0)
})
describe("replaceFakeBindings", () => {
it("should replace loop bindings in nested objects", () => {
const originalStepInput = {
schema: {
name: {
type: "string",
constraints: {
type: "string",
length: { maximum: null },
presence: false,
},
name: "name",
display: { type: "Text" },
},
},
row: {
tableId: "ta_aaad4296e9f74b12b1b90ef7a84afcad",
name: "{{ loop.currentItem.pokemon }}",
},
}
const loopStepNumber = 3
const result = loopUtils.replaceFakeBindings(
originalStepInput,
loopStepNumber
)
expect(result).toEqual({
schema: {
name: {
type: "string",
constraints: {
type: "string",
length: { maximum: null },
presence: false,
},
name: "name",
display: { type: "Text" },
},
},
row: {
tableId: "ta_aaad4296e9f74b12b1b90ef7a84afcad",
name: "{{ steps.3.currentItem.pokemon }}",
},
describe("loop output", () => {
it("should not output anything if a filter stops the automation", async () => {
const results = await createAutomationBuilder(config)
.onAppAction()
.filter({
condition: FilterCondition.EQUAL,
field: "1",
value: "2",
})
.loop({
option: LoopStepType.ARRAY,
binding: [1, 2, 3],
})
.serverLog({ text: "Message {{loop.currentItem}}" })
.test({ fields: {} })
it("should handle null values in nested objects", () => {
const originalStepInput = {
nullValue: null,
nestedNull: {
someKey: null,
},
validValue: "{{ loop.someValue }}",
}
const loopStepNumber = 2
const result = loopUtils.replaceFakeBindings(
originalStepInput,
loopStepNumber
)
expect(result).toEqual({
nullValue: null,
nestedNull: {
someKey: null,
},
validValue: "{{ steps.2.someValue }}",
expect(results.steps.length).toBe(1)
expect(results.steps[0].outputs).toEqual({
comparisonValue: 2,
refValue: 1,
result: false,
success: true,
status: "stopped",
})
})
it("should handle empty objects and arrays", () => {
const originalStepInput = {
emptyObject: {},
emptyArray: [],
nestedEmpty: {
emptyObj: {},
emptyArr: [],
},
}
const loopStepNumber = 1
const result = loopUtils.replaceFakeBindings(
originalStepInput,
loopStepNumber
)
expect(result).toEqual(originalStepInput)
})
})
})

View File

@ -66,7 +66,7 @@ describe("cron trigger", () => {
})
})
it("should stop if the job fails more than 3 times", async () => {
it("should stop if the job fails more than N times", async () => {
const { automation } = await createAutomationBuilder(config)
.onCron({ cron: "* * * * *" })
.queryRows({

View File

@ -5,7 +5,7 @@ import TestConfiguration from "../../../tests/utilities/TestConfiguration"
mocks.licenses.useSyncAutomations()
describe("Branching automations", () => {
describe("Webhook trigger test", () => {
const config = new TestConfiguration()
let table: Table
let webhook: Webhook

View File

@ -63,6 +63,7 @@ class TriggerBuilder {
onRowDeleted = this.trigger(AutomationTriggerStepId.ROW_DELETED)
onWebhook = this.trigger(AutomationTriggerStepId.WEBHOOK)
onCron = this.trigger(AutomationTriggerStepId.CRON)
onRowAction = this.trigger(AutomationTriggerStepId.ROW_ACTION)
}
class BranchStepBuilder<TStep extends AutomationTriggerStepId> {

View File

@ -182,11 +182,12 @@ export async function externalTrigger(
// values are likely to be submitted as strings, so we shall convert to correct type
const coercedFields: any = {}
const fields = automation.definition.trigger.inputs.fields
for (let key of Object.keys(fields || {})) {
for (const key of Object.keys(fields || {})) {
coercedFields[key] = coerce(params.fields[key], fields[key])
}
params.fields = coercedFields
}
// row actions and webhooks flatten the fields down
else if (
sdk.automations.isRowAction(automation) ||
@ -198,6 +199,7 @@ export async function externalTrigger(
fields: {},
}
}
const data: AutomationData = { automation, event: params }
const shouldTrigger = await checkTriggerFilters(automation, {

View File

@ -18,6 +18,7 @@ import {
import { automationsEnabled } from "../features"
import { helpers, REBOOT_CRON } from "@budibase/shared-core"
import tracer from "dd-trace"
import { JobId } from "bull"
const CRON_STEP_ID = automations.triggers.definitions.CRON.stepId
let Runner: Thread
@ -155,11 +156,11 @@ export async function disableAllCrons(appId: any) {
return { count: results.length / 2 }
}
export async function disableCronById(jobId: number | string) {
const repeatJobs = await automationQueue.getRepeatableJobs()
for (let repeatJob of repeatJobs) {
if (repeatJob.id === jobId) {
await automationQueue.removeRepeatableByKey(repeatJob.key)
export async function disableCronById(jobId: JobId) {
const jobs = await automationQueue.getRepeatableJobs()
for (const job of jobs) {
if (job.id === jobId) {
await automationQueue.removeRepeatableByKey(job.key)
}
}
console.log(`jobId=${jobId} disabled`)
@ -248,33 +249,3 @@ export async function enableCronTrigger(appId: any, automation: Automation) {
export async function cleanupAutomations(appId: any) {
await disableAllCrons(appId)
}
/**
* Checks if the supplied automation is of a recurring type.
* @param automation The automation to check.
* @return if it is recurring (cron).
*/
export function isRecurring(automation: Automation) {
return (
automation.definition.trigger.stepId ===
automations.triggers.definitions.CRON.stepId
)
}
export function isErrorInOutput(output: {
steps: { outputs?: { success: boolean } }[]
}) {
let first = true,
error = false
for (let step of output.steps) {
// skip the trigger, its always successful if automation ran
if (first) {
first = false
continue
}
if (!step.outputs?.success) {
error = true
}
}
return error
}

View File

@ -130,11 +130,6 @@ export enum InvalidColumns {
TABLE_ID = "tableId",
}
export enum AutomationErrors {
INCORRECT_TYPE = "INCORRECT_TYPE",
FAILURE_CONDITION = "FAILURE_CONDITION_MET",
}
// pass through the list from the auth/core lib
export const ObjectStoreBuckets = objectStore.ObjectStoreBuckets
export const MAX_AUTOMATION_RECURRING_ERRORS = 5

View File

@ -1,4 +1,9 @@
import { AutomationResults, LoopStepType, UserBindings } from "@budibase/types"
import {
AutomationStepResultOutputs,
AutomationTriggerResultOutputs,
LoopStepType,
UserBindings,
} from "@budibase/types"
export interface LoopInput {
option: LoopStepType
@ -13,19 +18,17 @@ export interface TriggerOutput {
timestamp?: number
}
export interface AutomationContext extends AutomationResults {
steps: any[]
stepsById: Record<string, any>
stepsByName: Record<string, any>
export interface AutomationContext {
trigger: AutomationTriggerResultOutputs
steps: [AutomationTriggerResultOutputs, ...AutomationStepResultOutputs[]]
stepsById: Record<string, AutomationStepResultOutputs>
stepsByName: Record<string, AutomationStepResultOutputs>
env?: Record<string, string>
user?: UserBindings
trigger: any
settings?: {
url?: string
logo?: string
company?: string
}
loop?: { currentItem: any }
}
export interface AutomationResponse
extends Omit<AutomationContext, "stepsByName" | "stepsById"> {}

View File

@ -40,7 +40,8 @@ function cleanAutomationInputs(automation: Automation) {
if (step == null) {
continue
}
for (let inputName of Object.keys(step.inputs)) {
for (const key of Object.keys(step.inputs)) {
const inputName = key as keyof typeof step.inputs
if (!step.inputs[inputName] || step.inputs[inputName] === "") {
delete step.inputs[inputName]
}
@ -281,7 +282,8 @@ function guardInvalidUpdatesAndThrow(
const readonlyFields = Object.keys(
step.schema.inputs.properties || {}
).filter(k => step.schema.inputs.properties[k].readonly)
readonlyFields.forEach(readonlyField => {
readonlyFields.forEach(key => {
const readonlyField = key as keyof typeof step.inputs
const oldStep = oldStepDefinitions.find(i => i.id === step.id)
if (step.inputs[readonlyField] !== oldStep?.inputs[readonlyField]) {
throw new HTTPError(

View File

@ -1,69 +0,0 @@
import { sample } from "lodash/fp"
import { Automation } from "@budibase/types"
import { generator } from "@budibase/backend-core/tests"
import TestConfiguration from "../../../../tests/utilities/TestConfiguration"
import automationSdk from "../"
import { structures } from "../../../../api/routes/tests/utilities"
describe("automation sdk", () => {
const config = new TestConfiguration()
beforeAll(async () => {
await config.init()
})
describe("update", () => {
it("can rename existing automations", async () => {
await config.doInContext(config.getAppId(), async () => {
const automation = structures.newAutomation()
const response = await automationSdk.create(automation)
const newName = generator.guid()
const update = { ...response, name: newName }
const result = await automationSdk.update(update)
expect(result.name).toEqual(newName)
})
})
it.each([
["trigger", (a: Automation) => a.definition.trigger],
["step", (a: Automation) => a.definition.steps[0]],
])("can update input fields (for a %s)", async (_, getStep) => {
await config.doInContext(config.getAppId(), async () => {
const automation = structures.newAutomation()
const keyToUse = sample(Object.keys(getStep(automation).inputs))!
getStep(automation).inputs[keyToUse] = "anyValue"
const response = await automationSdk.create(automation)
const update = { ...response }
getStep(update).inputs[keyToUse] = "anyUpdatedValue"
const result = await automationSdk.update(update)
expect(getStep(result).inputs[keyToUse]).toEqual("anyUpdatedValue")
})
})
it.each([
["trigger", (a: Automation) => a.definition.trigger],
["step", (a: Automation) => a.definition.steps[0]],
])("cannot update readonly fields (for a %s)", async (_, getStep) => {
await config.doInContext(config.getAppId(), async () => {
const automation = structures.newAutomation()
getStep(automation).schema.inputs.properties["readonlyProperty"] = {
readonly: true,
}
getStep(automation).inputs["readonlyProperty"] = "anyValue"
const response = await automationSdk.create(automation)
const update = { ...response }
getStep(update).inputs["readonlyProperty"] = "anyUpdatedValue"
await expect(automationSdk.update(update)).rejects.toThrow(
"Field readonlyProperty is readonly and it cannot be modified"
)
})
})
})
})

View File

@ -35,6 +35,8 @@ import {
WebhookActionType,
BuiltinPermissionID,
DeepPartial,
FilterCondition,
AutomationTriggerResult,
} from "@budibase/types"
import { LoopInput } from "../../definitions/automations"
import { merge } from "lodash"
@ -372,7 +374,11 @@ export function filterAutomation(opts?: DeepPartial<Automation>): Automation {
type: AutomationStepType.ACTION,
internal: true,
stepId: AutomationActionStepId.FILTER,
inputs: { field: "name", value: "test", condition: "EQ" },
inputs: {
field: "name",
value: "test",
condition: FilterCondition.EQUAL,
},
schema: BUILTIN_ACTION_DEFINITIONS.EXECUTE_SCRIPT.schema,
},
],
@ -437,15 +443,24 @@ export function updateRowAutomationWithFilters(
export function basicAutomationResults(
automationId: string
): AutomationResults {
const trigger: AutomationTriggerResult = {
id: "trigger",
stepId: AutomationTriggerStepId.APP,
outputs: {},
}
return {
automationId,
status: AutomationStatus.SUCCESS,
trigger: "trigger" as any,
trigger,
steps: [
trigger,
{
id: "step1",
stepId: AutomationActionStepId.SERVER_LOG,
inputs: {},
outputs: {},
outputs: {
success: true,
},
},
],
}

File diff suppressed because it is too large Load Diff

View File

@ -3,20 +3,14 @@ import {
AutomationStepDefinition,
AutomationStepType,
AutomationIOType,
FilterCondition,
} from "@budibase/types"
export const FilterConditions = {
EQUAL: "EQUAL",
NOT_EQUAL: "NOT_EQUAL",
GREATER_THAN: "GREATER_THAN",
LESS_THAN: "LESS_THAN",
}
export const PrettyFilterConditions = {
[FilterConditions.EQUAL]: "Equals",
[FilterConditions.NOT_EQUAL]: "Not equals",
[FilterConditions.GREATER_THAN]: "Greater than",
[FilterConditions.LESS_THAN]: "Less than",
[FilterCondition.EQUAL]: "Equals",
[FilterCondition.NOT_EQUAL]: "Not equals",
[FilterCondition.GREATER_THAN]: "Greater than",
[FilterCondition.LESS_THAN]: "Less than",
}
export const definition: AutomationStepDefinition = {
@ -30,7 +24,7 @@ export const definition: AutomationStepDefinition = {
features: {},
stepId: AutomationActionStepId.FILTER,
inputs: {
condition: FilterConditions.EQUAL,
condition: FilterCondition.EQUAL,
},
schema: {
inputs: {
@ -42,7 +36,7 @@ export const definition: AutomationStepDefinition = {
condition: {
type: AutomationIOType.STRING,
title: "Condition",
enum: Object.values(FilterConditions),
enum: Object.values(FilterCondition),
pretty: Object.values(PrettyFilterConditions),
},
value: {

View File

@ -105,10 +105,10 @@ export function cancelableTimeout(
export async function withTimeout<T>(
timeout: number,
promise: Promise<T>
promise: () => Promise<T>
): Promise<T> {
const [timeoutPromise, cancel] = cancelableTimeout(timeout)
const result = (await Promise.race([promise, timeoutPromise])) as T
const result = (await Promise.race([promise(), timeoutPromise])) as T
cancel()
return result
}

View File

@ -7,8 +7,20 @@ import {
} from "../../../sdk"
import { HttpMethod } from "../query"
import { Row } from "../row"
import { LoopStepType, EmailAttachment, AutomationResults } from "./automation"
import { AutomationStep, AutomationStepOutputs } from "./schema"
import {
LoopStepType,
EmailAttachment,
AutomationResults,
AutomationStepResult,
} from "./automation"
import { AutomationStep } from "./schema"
export enum FilterCondition {
EQUAL = "EQUAL",
NOT_EQUAL = "NOT_EQUAL",
GREATER_THAN = "GREATER_THAN",
LESS_THAN = "LESS_THAN",
}
export type BaseAutomationOutputs = {
success?: boolean
@ -92,7 +104,7 @@ export type ExecuteScriptStepOutputs = BaseAutomationOutputs & {
export type FilterStepInputs = {
field: any
condition: string
condition: FilterCondition
value: any
}
@ -110,7 +122,7 @@ export type LoopStepInputs = {
}
export type LoopStepOutputs = {
items: AutomationStepOutputs[]
items: AutomationStepResult[]
success: boolean
iterations: number
}

View File

@ -146,7 +146,7 @@ export interface Automation extends Document {
}
}
interface BaseIOStructure {
export interface BaseIOStructure {
type?: AutomationIOType
subtype?: AutomationIOType
customType?: AutomationCustomIOType
@ -176,6 +176,8 @@ export enum AutomationFeature {
export enum AutomationStepStatus {
NO_ITERATIONS = "no_iterations",
MAX_ITERATIONS = "max_iterations_reached",
FAILURE_CONDITION = "FAILURE_CONDITION_MET",
INCORRECT_TYPE = "INCORRECT_TYPE",
}
export enum AutomationStatus {
@ -190,19 +192,37 @@ export enum AutomationStoppedReason {
TRIGGER_FILTER_NOT_MET = "Automation did not run. Filter conditions in trigger were not met.",
}
export interface AutomationStepResultOutputs {
success: boolean
[key: string]: any
}
export interface AutomationStepResultInputs {
[key: string]: any
}
export interface AutomationStepResult {
id: string
stepId: AutomationActionStepId
inputs: AutomationStepResultInputs
outputs: AutomationStepResultOutputs
}
export type AutomationTriggerResultInputs = Record<string, any>
export type AutomationTriggerResultOutputs = Record<string, any>
export interface AutomationTriggerResult {
id: string
stepId: AutomationTriggerStepId
inputs?: AutomationTriggerResultInputs | null
outputs: AutomationTriggerResultOutputs
}
export interface AutomationResults {
automationId?: string
status?: AutomationStatus
trigger?: AutomationTrigger
steps: {
stepId: AutomationTriggerStepId | AutomationActionStepId
inputs: {
[key: string]: any
}
outputs: {
[key: string]: any
}
}[]
trigger: AutomationTriggerResult
steps: [AutomationTriggerResult, ...AutomationStepResult[]]
}
export interface DidNotTriggerResponse {
@ -236,6 +256,7 @@ export type ActionImplementation<TInputs, TOutputs> = (
inputs: TInputs
} & AutomationStepInputBase
) => Promise<TOutputs>
export interface AutomationMetadata extends Document {
errorCount?: number
automationChainCount?: number

View File

@ -164,24 +164,6 @@ export interface AutomationStepSchemaBase {
features?: Partial<Record<AutomationFeature, boolean>>
}
export type AutomationStepOutputs =
| CollectStepOutputs
| CreateRowStepOutputs
| DelayStepOutputs
| DeleteRowStepOutputs
| ExecuteQueryStepOutputs
| ExecuteScriptStepOutputs
| FilterStepOutputs
| QueryRowsStepOutputs
| BaseAutomationOutputs
| BashStepOutputs
| ExternalAppStepOutputs
| OpenAIStepOutputs
| ServerLogStepOutputs
| TriggerAutomationStepOutputs
| UpdateRowStepOutputs
| ZapierStepOutputs
export type AutomationStepInputs<T extends AutomationActionStepId> =
T extends AutomationActionStepId.COLLECT
? CollectStepInputs
@ -229,11 +211,56 @@ export type AutomationStepInputs<T extends AutomationActionStepId> =
? BranchStepInputs
: never
export type AutomationStepOutputs<T extends AutomationActionStepId> =
T extends AutomationActionStepId.COLLECT
? CollectStepOutputs
: T extends AutomationActionStepId.CREATE_ROW
? CreateRowStepOutputs
: T extends AutomationActionStepId.DELAY
? DelayStepOutputs
: T extends AutomationActionStepId.DELETE_ROW
? DeleteRowStepOutputs
: T extends AutomationActionStepId.EXECUTE_QUERY
? ExecuteQueryStepOutputs
: T extends AutomationActionStepId.EXECUTE_SCRIPT
? ExecuteScriptStepOutputs
: T extends AutomationActionStepId.FILTER
? FilterStepOutputs
: T extends AutomationActionStepId.QUERY_ROWS
? QueryRowsStepOutputs
: T extends AutomationActionStepId.SEND_EMAIL_SMTP
? BaseAutomationOutputs
: T extends AutomationActionStepId.SERVER_LOG
? ServerLogStepOutputs
: T extends AutomationActionStepId.TRIGGER_AUTOMATION_RUN
? TriggerAutomationStepOutputs
: T extends AutomationActionStepId.UPDATE_ROW
? UpdateRowStepOutputs
: T extends AutomationActionStepId.OUTGOING_WEBHOOK
? ExternalAppStepOutputs
: T extends AutomationActionStepId.discord
? ExternalAppStepOutputs
: T extends AutomationActionStepId.slack
? ExternalAppStepOutputs
: T extends AutomationActionStepId.zapier
? ZapierStepOutputs
: T extends AutomationActionStepId.integromat
? ExternalAppStepOutputs
: T extends AutomationActionStepId.n8n
? ExternalAppStepOutputs
: T extends AutomationActionStepId.EXECUTE_BASH
? BashStepOutputs
: T extends AutomationActionStepId.OPENAI
? OpenAIStepOutputs
: T extends AutomationActionStepId.LOOP
? BaseAutomationOutputs
: never
export interface AutomationStepSchema<TStep extends AutomationActionStepId>
extends AutomationStepSchemaBase {
id: string
stepId: TStep
inputs: AutomationStepInputs<TStep> & Record<string, any> // The record union to be removed once the types are fixed
inputs: AutomationStepInputs<TStep>
}
export type CollectStep = AutomationStepSchema<AutomationActionStepId.COLLECT>
@ -315,6 +342,36 @@ export type AutomationStep =
| OpenAIStep
| BranchStep
export function isBranchStep(
step: AutomationStep | AutomationTrigger
): step is BranchStep {
return step.stepId === AutomationActionStepId.BRANCH
}
export function isTrigger(
step: AutomationStep | AutomationTrigger
): step is AutomationTrigger {
return step.type === AutomationStepType.TRIGGER
}
export function isRowUpdateTrigger(
step: AutomationStep | AutomationTrigger
): step is RowUpdatedTrigger {
return step.stepId === AutomationTriggerStepId.ROW_UPDATED
}
export function isRowSaveTrigger(
step: AutomationStep | AutomationTrigger
): step is RowSavedTrigger {
return step.stepId === AutomationTriggerStepId.ROW_SAVED
}
export function isAppTrigger(
step: AutomationStep | AutomationTrigger
): step is AppActionTrigger {
return step.stepId === AutomationTriggerStepId.APP
}
type EmptyInputs = {}
export type AutomationStepDefinition = Omit<AutomationStep, "id" | "inputs"> & {
inputs: EmptyInputs

View File

@ -82,6 +82,10 @@ type RangeFilter = Record<
type LogicalFilter = { conditions: SearchFilters[] }
export function isLogicalFilter(filter: any): filter is LogicalFilter {
return "conditions" in filter
}
export type AnySearchFilter = BasicFilter | ArrayFilter | RangeFilter
export interface SearchFilters {

View File

@ -31,8 +31,8 @@ describe("/api/global/email", () => {
) {
let response, text
try {
await helpers.withTimeout(20000, config.saveEtherealSmtpConfig())
await helpers.withTimeout(20000, config.saveSettingsConfig())
await helpers.withTimeout(20000, () => config.saveEtherealSmtpConfig())
await helpers.withTimeout(20000, () => config.saveSettingsConfig())
let res
if (attachments) {
res = await config.api.emails