Skip to content

Commit

Permalink
Merge pull request #15184 from Budibase/fix/automation-webhook-improv…
Browse files Browse the repository at this point in the history
…ements

Automation webhook binding fixes
  • Loading branch information
mike12345567 authored Dec 17, 2024
2 parents 5b835c5 + 57eaac8 commit c905d64
Show file tree
Hide file tree
Showing 11 changed files with 232 additions and 107 deletions.
14 changes: 11 additions & 3 deletions packages/server/src/api/controllers/webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
BuildWebhookSchemaResponse,
TriggerWebhookRequest,
TriggerWebhookResponse,
AutomationIOType,
} from "@budibase/types"
import sdk from "../../sdk"
import * as pro from "@budibase/pro"
Expand Down Expand Up @@ -60,14 +61,21 @@ export async function buildSchema(
if (webhook.action.type === WebhookActionType.AUTOMATION) {
let automation = await db.get<Automation>(webhook.action.target)
const autoOutputs = automation.definition.trigger.schema.outputs
let properties = webhook.bodySchema.properties
let properties = webhook.bodySchema?.properties
// reset webhook outputs
autoOutputs.properties = {
body: autoOutputs.properties.body,
}
for (let prop of Object.keys(properties)) {
for (let prop of Object.keys(properties || {})) {
if (properties?.[prop] == null) {
continue
}
const def = properties[prop]
if (typeof def === "boolean") {
continue
}
autoOutputs.properties[prop] = {
type: properties[prop].type,
type: def.type as AutomationIOType,
description: AUTOMATION_DESCRIPTION,
}
}
Expand Down
65 changes: 65 additions & 0 deletions packages/server/src/automations/tests/scenarios/webhook.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import * as automation from "../../index"
import * as setup from "../utilities"
import { Table, Webhook, WebhookActionType } from "@budibase/types"
import { createAutomationBuilder } from "../utilities/AutomationTestBuilder"
import { mocks } from "@budibase/backend-core/tests"

mocks.licenses.useSyncAutomations()

describe("Branching automations", () => {
let config = setup.getConfig(),
table: Table,
webhook: Webhook

async function createWebhookAutomation(testName: string) {
const builder = createAutomationBuilder({
name: testName,
})
const automation = await builder
.webhook({ fields: { parameter: "string" } })
.createRow({
row: { tableId: table._id!, name: "{{ trigger.parameter }}" },
})
.collect({ collection: `{{ trigger.parameter }}` })
.save()

webhook = await config.api.webhook.save({
name: "hook",
live: true,
action: {
type: WebhookActionType.AUTOMATION,
target: automation._id!,
},
bodySchema: {},
})
await config.api.webhook.buildSchema(config.getAppId(), webhook._id!, {
parameter: "string",
})
await config.publish()
return { webhook, automation }
}

beforeEach(async () => {
await automation.init()
await config.init()
table = await config.createTable()
})

afterAll(setup.afterAll)

it("should run the webhook automation - checking for parameters", async () => {
const { webhook } = await createWebhookAutomation(
"Check a basic webhook works as expected"
)
const res = await config.api.webhook.trigger(
config.getProdAppId(),
webhook._id!,
{
parameter: "testing",
}
)
expect(typeof res).toBe("object")
const collectedInfo = res as Record<string, any>
expect(collectedInfo.value).toEqual("testing")
})
})
Original file line number Diff line number Diff line change
@@ -1,42 +1,44 @@
import { v4 as uuidv4 } from "uuid"
import { testAutomation } from "../../../api/routes/tests/utilities/TestFunctions"
import {} from "../../steps/createRow"
import { BUILTIN_ACTION_DEFINITIONS } from "../../actions"
import { TRIGGER_DEFINITIONS } from "../../triggers"
import {
LoopStepInputs,
DeleteRowStepInputs,
UpdateRowStepInputs,
CreateRowStepInputs,
AppActionTriggerInputs,
AppActionTriggerOutputs,
Automation,
AutomationTrigger,
AutomationResults,
SmtpEmailStepInputs,
ExecuteQueryStepInputs,
QueryRowsStepInputs,
AutomationActionStepId,
AutomationTriggerStepId,
AutomationResults,
AutomationStep,
AutomationTriggerDefinition,
RowDeletedTriggerInputs,
RowDeletedTriggerOutputs,
RowUpdatedTriggerOutputs,
RowUpdatedTriggerInputs,
RowCreatedTriggerInputs,
RowCreatedTriggerOutputs,
AppActionTriggerOutputs,
CronTriggerOutputs,
AppActionTriggerInputs,
AutomationStepInputs,
AutomationTrigger,
AutomationTriggerDefinition,
AutomationTriggerInputs,
ServerLogStepInputs,
BranchStepInputs,
SearchFilters,
AutomationTriggerStepId,
BashStepInputs,
Branch,
FilterStepInputs,
BranchStepInputs,
CollectStepInputs,
CreateRowStepInputs,
CronTriggerOutputs,
DeleteRowStepInputs,
ExecuteQueryStepInputs,
ExecuteScriptStepInputs,
FilterStepInputs,
LoopStepInputs,
OpenAIStepInputs,
BashStepInputs,
QueryRowsStepInputs,
RowCreatedTriggerInputs,
RowCreatedTriggerOutputs,
RowDeletedTriggerInputs,
RowDeletedTriggerOutputs,
RowUpdatedTriggerInputs,
RowUpdatedTriggerOutputs,
SearchFilters,
ServerLogStepInputs,
SmtpEmailStepInputs,
UpdateRowStepInputs,
WebhookTriggerInputs,
WebhookTriggerOutputs,
} from "@budibase/types"
import TestConfiguration from "../../../tests/utilities/TestConfiguration"
import * as setup from "../utilities"
Expand All @@ -47,6 +49,7 @@ type TriggerOutputs =
| RowUpdatedTriggerOutputs
| RowDeletedTriggerOutputs
| AppActionTriggerOutputs
| WebhookTriggerOutputs
| CronTriggerOutputs
| undefined

Expand Down Expand Up @@ -180,6 +183,7 @@ class BaseStepBuilder {
opts
)
}

loop(
inputs: LoopStepInputs,
opts?: { stepName?: string; stepId?: string }
Expand Down Expand Up @@ -247,7 +251,20 @@ class BaseStepBuilder {
opts
)
}

collect(
input: CollectStepInputs,
opts?: { stepName?: string; stepId?: string }
): this {
return this.step(
AutomationActionStepId.COLLECT,
BUILTIN_ACTION_DEFINITIONS.COLLECT,
input,
opts
)
}
}

class StepBuilder extends BaseStepBuilder {
build(): AutomationStep[] {
return this.steps
Expand Down Expand Up @@ -329,6 +346,16 @@ class AutomationBuilder extends BaseStepBuilder {
)
}

webhook(outputs: WebhookTriggerOutputs, inputs?: WebhookTriggerInputs) {
this.triggerOutputs = outputs
return this.trigger(
TRIGGER_DEFINITIONS.WEBHOOK,
AutomationTriggerStepId.WEBHOOK,
inputs,
outputs
)
}

private trigger<TStep extends AutomationTriggerStepId>(
triggerSchema: AutomationTriggerDefinition,
stepId: TStep,
Expand Down Expand Up @@ -361,12 +388,16 @@ class AutomationBuilder extends BaseStepBuilder {
return this.automationConfig
}

async run() {
async save() {
if (!Object.keys(this.automationConfig.definition.trigger).length) {
throw new Error("Please add a trigger to this automation test")
}
this.automationConfig.definition.steps = this.steps
const automation = await this.config.createAutomation(this.build())
return await this.config.createAutomation(this.build())
}

async run() {
const automation = await this.save()
const results = await testAutomation(
this.config,
automation,
Expand Down
8 changes: 6 additions & 2 deletions packages/server/src/automations/triggers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,14 @@ export async function externalTrigger(
coercedFields[key] = coerce(params.fields[key], fields[key])
}
params.fields = coercedFields
} else if (sdk.automations.isRowAction(automation)) {
}
// row actions and webhooks flatten the fields down
else if (
sdk.automations.isRowAction(automation) ||
sdk.automations.isWebhookAction(automation)
) {
params = {
...params,
// Until we don't refactor all the types, we want to flatten the nested "fields" object
...params.fields,
fields: {},
}
Expand Down
3 changes: 3 additions & 0 deletions packages/server/src/tests/utilities/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { TemplateAPI } from "./template"
import { RowActionAPI } from "./rowAction"
import { AutomationAPI } from "./automation"
import { PluginAPI } from "./plugin"
import { WebhookAPI } from "./webhook"

export default class API {
table: TableAPI
Expand All @@ -35,6 +36,7 @@ export default class API {
rowAction: RowActionAPI
automation: AutomationAPI
plugin: PluginAPI
webhook: WebhookAPI

constructor(config: TestConfiguration) {
this.table = new TableAPI(config)
Expand All @@ -54,5 +56,6 @@ export default class API {
this.rowAction = new RowActionAPI(config)
this.automation = new AutomationAPI(config)
this.plugin = new PluginAPI(config)
this.webhook = new WebhookAPI(config)
}
}
58 changes: 58 additions & 0 deletions packages/server/src/tests/utilities/api/webhook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { Expectations, TestAPI } from "./base"
import {
BuildWebhookSchemaResponse,
SaveWebhookResponse,
TriggerWebhookResponse,
Webhook,
} from "@budibase/types"

export class WebhookAPI extends TestAPI {
save = async (webhook: Webhook, expectations?: Expectations) => {
const resp = await this._put<SaveWebhookResponse>("/api/webhooks", {
body: webhook,
expectations: {
status: 200,
...expectations,
},
})
return resp.webhook
}

buildSchema = async (
appId: string,
webhookId: string,
fields: Record<string, any>,
expectations?: Expectations
) => {
const resp = await this._post<BuildWebhookSchemaResponse>(
`/api/webhooks/schema/${appId}/${webhookId}`,
{
body: fields,
expectations: {
status: 200,
...expectations,
},
}
)
return resp.id
}

trigger = async (
appId: string,
webhookId: string,
fields: Record<string, any>,
expectations?: Expectations
) => {
const resp = await this._post<TriggerWebhookResponse>(
`/api/webhooks/trigger/${appId}/${webhookId}`,
{
body: fields,
expectations: {
status: 200,
...expectations,
},
}
)
return resp
}
}
14 changes: 9 additions & 5 deletions packages/shared-core/src/sdk/documents/automations.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { Automation, AutomationTriggerStepId } from "@budibase/types"

export function isRowAction(automation: Automation) {
const result =
return (
automation.definition.trigger?.stepId === AutomationTriggerStepId.ROW_ACTION
return result
)
}

export function isWebhookAction(automation: Automation) {
return (
automation.definition.trigger?.stepId === AutomationTriggerStepId.WEBHOOK
)
}

export function isAppAction(automation: Automation) {
const result =
automation.definition.trigger?.stepId === AutomationTriggerStepId.APP
return result
return automation.definition.trigger?.stepId === AutomationTriggerStepId.APP
}
3 changes: 2 additions & 1 deletion packages/types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
"jest": {},
"devDependencies": {
"@budibase/nano": "10.1.5",
"@types/json-schema": "^7.0.15",
"@types/koa": "2.13.4",
"@types/redlock": "4.0.7",
"koa-useragent": "^4.1.0",
"rimraf": "3.0.2",
"typescript": "5.7.2",
"koa-useragent": "^4.1.0",
"zod": "^3.23.8"
},
"dependencies": {
Expand Down
10 changes: 10 additions & 0 deletions packages/types/src/documents/app/automation/StepInputsOutputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ export type OpenAIStepInputs = {
prompt: string
model: Model
}

export enum Model {
GPT_35_TURBO = "gpt-3.5-turbo",
// will only work with api keys that have access to the GPT4 API
Expand Down Expand Up @@ -296,3 +297,12 @@ export type RowUpdatedTriggerOutputs = {
id: string
revision?: string
}

export type WebhookTriggerInputs = {
schemaUrl: string
triggerUrl: string
}

export type WebhookTriggerOutputs = {
fields: Record<string, any>
}
Loading

0 comments on commit c905d64

Please sign in to comment.