From 777e24344018e5002eaabeb89dae2ac86bde1c30 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Tue, 7 Sep 2021 17:31:54 +0100 Subject: [PATCH] Re-working all of the actions to have a success output, and make sure everything has useful outputs. --- packages/server/src/automations/actions.js | 3 - packages/server/src/automations/steps/bash.js | 12 ++- .../server/src/automations/steps/createRow.js | 3 +- .../server/src/automations/steps/delay.js | 12 +++ .../server/src/automations/steps/deleteRow.js | 3 +- .../server/src/automations/steps/discord.js | 11 ++- .../src/automations/steps/executeQuery.js | 1 - .../src/automations/steps/executeScript.js | 1 - .../src/automations/steps/integromat.js | 22 +++--- .../src/automations/steps/outgoingWebhook.js | 21 +++--- .../src/automations/steps/sendSmtpEmail.js | 1 - .../src/automations/steps/sendgridEmail.js | 74 ------------------- .../server/src/automations/steps/serverLog.js | 3 + .../server/src/automations/steps/slack.js | 11 ++- .../server/src/automations/steps/updateRow.js | 1 - .../server/src/automations/steps/utils.js | 18 +++++ .../server/src/automations/steps/zapier.js | 19 ++--- 17 files changed, 87 insertions(+), 129 deletions(-) delete mode 100644 packages/server/src/automations/steps/sendgridEmail.js create mode 100644 packages/server/src/automations/steps/utils.js diff --git a/packages/server/src/automations/actions.js b/packages/server/src/automations/actions.js index 8d5d1693f1..0f395db890 100644 --- a/packages/server/src/automations/actions.js +++ b/packages/server/src/automations/actions.js @@ -1,4 +1,3 @@ -const sendgridEmail = require("./steps/sendgridEmail") const sendSmtpEmail = require("./steps/sendSmtpEmail") const createRow = require("./steps/createRow") const updateRow = require("./steps/updateRow") @@ -14,7 +13,6 @@ const zapier = require("./steps/zapier") const integromat = require("./steps/integromat") const ACTION_IMPLS = { - SEND_EMAIL: sendgridEmail.run, SEND_EMAIL_SMTP: sendSmtpEmail.run, CREATE_ROW: createRow.run, UPDATE_ROW: updateRow.run, @@ -31,7 +29,6 @@ const ACTION_IMPLS = { integromat: integromat.run, } const ACTION_DEFINITIONS = { - SEND_EMAIL: sendgridEmail.definition, SEND_EMAIL_SMTP: sendSmtpEmail.definition, CREATE_ROW: createRow.definition, UPDATE_ROW: updateRow.definition, diff --git a/packages/server/src/automations/steps/bash.js b/packages/server/src/automations/steps/bash.js index f2d98ce944..d97f055e14 100644 --- a/packages/server/src/automations/steps/bash.js +++ b/packages/server/src/automations/steps/bash.js @@ -24,7 +24,11 @@ exports.definition = { properties: { stdout: { type: "string", - description: "Standard output of your bash command or script.", + description: "Standard output of your bash command or script", + }, + success: { + type: "boolean", + description: "Whether the command was successful", }, }, }, @@ -42,18 +46,20 @@ exports.run = async function ({ inputs, context }) { try { const command = processStringSync(inputs.code, context) - let stdout + let stdout, + success = true try { stdout = execSync(command, { timeout: 500 }) } catch (err) { stdout = err.message + success = false } return { stdout, + success, } } catch (err) { - console.error(err) return { success: false, response: err, diff --git a/packages/server/src/automations/steps/createRow.js b/packages/server/src/automations/steps/createRow.js index a08adfb000..f98cbfe2cf 100644 --- a/packages/server/src/automations/steps/createRow.js +++ b/packages/server/src/automations/steps/createRow.js @@ -42,7 +42,7 @@ exports.definition = { }, success: { type: "boolean", - description: "Whether the action was successful", + description: "Whether the row creation was successful", }, id: { type: "string", @@ -97,7 +97,6 @@ exports.run = async function ({ inputs, appId, apiKey, emitter }) { success: ctx.status === 200, } } catch (err) { - console.error(err) return { success: false, response: err, diff --git a/packages/server/src/automations/steps/delay.js b/packages/server/src/automations/steps/delay.js index b79d28e92d..ee7ef018e3 100644 --- a/packages/server/src/automations/steps/delay.js +++ b/packages/server/src/automations/steps/delay.js @@ -17,10 +17,22 @@ exports.definition = { }, required: ["time"], }, + outputs: { + properties: { + success: { + type: "boolean", + description: "Whether the delay was successful", + }, + }, + required: ["success"], + }, }, type: "LOGIC", } exports.run = async function delay({ inputs }) { await wait(inputs.time) + return { + success: true, + } } diff --git a/packages/server/src/automations/steps/deleteRow.js b/packages/server/src/automations/steps/deleteRow.js index 83e5969381..0c0315b1b0 100644 --- a/packages/server/src/automations/steps/deleteRow.js +++ b/packages/server/src/automations/steps/deleteRow.js @@ -42,7 +42,7 @@ exports.definition = { }, success: { type: "boolean", - description: "Whether the action was successful", + description: "Whether the deletion was successful", }, }, required: ["row", "success"], @@ -84,7 +84,6 @@ exports.run = async function ({ inputs, appId, apiKey, emitter }) { success: ctx.status === 200, } } catch (err) { - console.error(err) return { success: false, response: err, diff --git a/packages/server/src/automations/steps/discord.js b/packages/server/src/automations/steps/discord.js index c01619b0ae..066eb23a4e 100644 --- a/packages/server/src/automations/steps/discord.js +++ b/packages/server/src/automations/steps/discord.js @@ -1,4 +1,5 @@ const fetch = require("node-fetch") +const { getFetchResponse } = require("./utils") const DEFAULT_USERNAME = "Budibase Automate" const DEFAULT_AVATAR_URL = "https://i.imgur.com/a1cmTKM.png" @@ -39,6 +40,10 @@ exports.definition = { type: "number", description: "The HTTP status code of the request", }, + response: { + type: "string", + description: "The response from the Discord Webhook", + }, success: { type: "boolean", description: "Whether the message sent successfully", @@ -68,8 +73,10 @@ exports.run = async function ({ inputs }) { }, }) + const { status, message } = await getFetchResponse(response) return { - httpStatus: response.status, - success: response.status === 200, + httpStatus: status, + success: status === 200, + response: message, } } diff --git a/packages/server/src/automations/steps/executeQuery.js b/packages/server/src/automations/steps/executeQuery.js index 474b03766e..f34d994183 100644 --- a/packages/server/src/automations/steps/executeQuery.js +++ b/packages/server/src/automations/steps/executeQuery.js @@ -75,7 +75,6 @@ exports.run = async function ({ inputs, appId, emitter }) { success: ctx.status === 200, } } catch (err) { - console.error(err) return { success: false, response: err, diff --git a/packages/server/src/automations/steps/executeScript.js b/packages/server/src/automations/steps/executeScript.js index 6eca4f4244..b320e38700 100644 --- a/packages/server/src/automations/steps/executeScript.js +++ b/packages/server/src/automations/steps/executeScript.js @@ -64,7 +64,6 @@ exports.run = async function ({ inputs, appId, context, emitter }) { value: ctx.body, } } catch (err) { - console.error(err) return { success: false, response: err, diff --git a/packages/server/src/automations/steps/integromat.js b/packages/server/src/automations/steps/integromat.js index ef9d551286..f8427b0dab 100644 --- a/packages/server/src/automations/steps/integromat.js +++ b/packages/server/src/automations/steps/integromat.js @@ -1,4 +1,5 @@ const fetch = require("node-fetch") +const { getFetchResponse } = require("./utils") exports.definition = { name: "Integromat Integration", @@ -45,6 +46,10 @@ exports.definition = { type: "boolean", description: "Whether call was successful", }, + httpStatus: { + type: "number", + description: "The HTTP status code returned", + }, response: { type: "object", description: "The webhook response - this can have properties", @@ -72,19 +77,10 @@ exports.run = async function ({ inputs }) { }, }) - let data - if (response.status === 200) { - try { - data = await response.json() - } catch (err) { - data = {} - } - } else { - data = await response.text() - } - + const { status, message } = await getFetchResponse(response) return { - success: response.status === 200, - response: data, + httpStatus: status, + success: status === 200, + response: message, } } diff --git a/packages/server/src/automations/steps/outgoingWebhook.js b/packages/server/src/automations/steps/outgoingWebhook.js index 317f8967f5..fe93cb38d8 100644 --- a/packages/server/src/automations/steps/outgoingWebhook.js +++ b/packages/server/src/automations/steps/outgoingWebhook.js @@ -1,4 +1,5 @@ const fetch = require("node-fetch") +const { getFetchResponse } = require("./utils") const RequestType = { POST: "POST", @@ -60,6 +61,10 @@ exports.definition = { type: "object", description: "The response from the webhook", }, + httpStatus: { + type: "number", + description: "The HTTP status code returned", + }, success: { type: "boolean", description: "Whether the action was successful", @@ -107,19 +112,11 @@ exports.run = async function ({ inputs }) { JSON.parse(request.body) } const response = await fetch(url, request) - const contentType = response.headers.get("content-type") - const success = response.status === 200 - let resp - if (!success) { - resp = response.statusText - } else if (contentType && contentType.indexOf("application/json") !== -1) { - resp = await response.json() - } else { - resp = await response.text() - } + const { status, message } = await getFetchResponse(response) return { - response: resp, - success: success, + httpStatus: status, + response: message, + success: status === 200, } } catch (err) { /* istanbul ignore next */ diff --git a/packages/server/src/automations/steps/sendSmtpEmail.js b/packages/server/src/automations/steps/sendSmtpEmail.js index 8b8d9b6db9..fc2274a582 100644 --- a/packages/server/src/automations/steps/sendSmtpEmail.js +++ b/packages/server/src/automations/steps/sendSmtpEmail.js @@ -58,7 +58,6 @@ exports.run = async function ({ inputs }) { response, } } catch (err) { - console.error(err) return { success: false, response: err, diff --git a/packages/server/src/automations/steps/sendgridEmail.js b/packages/server/src/automations/steps/sendgridEmail.js deleted file mode 100644 index bde5f96e4f..0000000000 --- a/packages/server/src/automations/steps/sendgridEmail.js +++ /dev/null @@ -1,74 +0,0 @@ -exports.definition = { - description: "Send an email using SendGrid", - tagline: "Send email to {{inputs.to}}", - icon: "ri-mail-open-line", - name: "Send Email (SendGrid)", - type: "ACTION", - stepId: "SEND_EMAIL", - inputs: {}, - schema: { - inputs: { - properties: { - apiKey: { - type: "string", - title: "SendGrid API key", - }, - to: { - type: "string", - title: "Send To", - }, - from: { - type: "string", - title: "Send From", - }, - subject: { - type: "string", - title: "Email Subject", - }, - contents: { - type: "string", - title: "Email Contents", - }, - }, - required: ["to", "from", "subject", "contents"], - }, - outputs: { - properties: { - success: { - type: "boolean", - description: "Whether the email was sent", - }, - response: { - type: "object", - description: "A response from the email client, this may be an error", - }, - }, - required: ["success"], - }, - }, -} - -exports.run = async function ({ inputs }) { - const sgMail = require("@sendgrid/mail") - sgMail.setApiKey(inputs.apiKey) - const msg = { - to: inputs.to, - from: inputs.from, - subject: inputs.subject, - text: inputs.contents ? inputs.contents : "Empty", - } - - try { - let response = await sgMail.send(msg) - return { - success: true, - response, - } - } catch (err) { - console.error(err) - return { - success: false, - response: err, - } - } -} diff --git a/packages/server/src/automations/steps/serverLog.js b/packages/server/src/automations/steps/serverLog.js index 7183ff89fd..0cde7b22ab 100644 --- a/packages/server/src/automations/steps/serverLog.js +++ b/packages/server/src/automations/steps/serverLog.js @@ -38,4 +38,7 @@ exports.definition = { exports.run = async function ({ inputs, appId }) { console.log(`App ${appId} - ${inputs.text}`) + return { + success: true, + } } diff --git a/packages/server/src/automations/steps/slack.js b/packages/server/src/automations/steps/slack.js index 77d1e3cfd2..a87c614151 100644 --- a/packages/server/src/automations/steps/slack.js +++ b/packages/server/src/automations/steps/slack.js @@ -1,4 +1,5 @@ const fetch = require("node-fetch") +const { getFetchResponse } = require("./utils") exports.definition = { name: "Slack Message", @@ -32,6 +33,10 @@ exports.definition = { type: "boolean", description: "Whether the message sent successfully", }, + response: { + type: "string", + description: "The response from the Slack Webhook", + }, }, }, }, @@ -49,8 +54,10 @@ exports.run = async function ({ inputs }) { }, }) + const { status, message } = await getFetchResponse(response) return { - httpStatus: response.status, - success: response.status === 200, + httpStatus: status, + response: message, + success: status === 200, } } diff --git a/packages/server/src/automations/steps/updateRow.js b/packages/server/src/automations/steps/updateRow.js index 898869959e..e58f1fe446 100644 --- a/packages/server/src/automations/steps/updateRow.js +++ b/packages/server/src/automations/steps/updateRow.js @@ -100,7 +100,6 @@ exports.run = async function ({ inputs, appId, emitter }) { success: ctx.status === 200, } } catch (err) { - console.error(err) return { success: false, response: err, diff --git a/packages/server/src/automations/steps/utils.js b/packages/server/src/automations/steps/utils.js new file mode 100644 index 0000000000..0429c71ca7 --- /dev/null +++ b/packages/server/src/automations/steps/utils.js @@ -0,0 +1,18 @@ +exports.getFetchResponse = async fetched => { + let status = fetched.status, + message + const contentType = fetched.headers.get("content-type") + try { + if (contentType && contentType.indexOf("application/json") !== -1) { + message = await fetched.json() + } else { + message = await fetched.text() + } + } catch (err) { + message = "Failed to retrieve response" + } + if (typeof message !== "string") { + message = JSON.stringify(message) + } + return { status, message } +} diff --git a/packages/server/src/automations/steps/zapier.js b/packages/server/src/automations/steps/zapier.js index 0a7362cede..57703286e6 100644 --- a/packages/server/src/automations/steps/zapier.js +++ b/packages/server/src/automations/steps/zapier.js @@ -1,4 +1,5 @@ const fetch = require("node-fetch") +const { getFetchResponse } = require("./utils") exports.definition = { name: "Zapier Webhook", @@ -43,9 +44,9 @@ exports.definition = { type: "number", description: "The HTTP status code of the request", }, - zapierStatus: { + response: { type: "string", - description: "The result status from Zapier", + description: "The response from Zapier", }, }, }, @@ -72,17 +73,11 @@ exports.run = async function ({ inputs }) { }, }) - let data = null - if (response.status === 200) { - try { - data = await response.json() - } catch (err) { - data = null - } - } + const { status, message } = await getFetchResponse(response) return { - httpStatus: response.status, - zapierStatus: data && data.status ? data.status : data, + success: status === 200, + httpStatus: status, + response: message, } }