From 1cfe84fc3dd25e377d671c7ad6924f9e6b9da27f Mon Sep 17 00:00:00 2001 From: Faruk AYDIN Date: Mon, 31 Oct 2022 18:26:58 +0100 Subject: [PATCH 1/2] refactor: Use only arguments for action definitions --- .../actions/send-message-to-channel/index.ts | 63 ++++---- .../apps/github/actions/create-issue/index.ts | 69 ++++----- .../apps/slack/actions/find-message/index.ts | 90 +++++------- .../send-a-message-to-channel/index.ts | 58 +++----- .../src/apps/smtp/actions/send-email/index.ts | 136 ++++++++---------- .../src/apps/twilio/actions/send-sms/index.ts | 56 +++----- .../twitter/actions/create-tweet/index.ts | 28 +--- packages/backend/src/helpers/define-action.ts | 4 +- packages/backend/src/helpers/get-app.ts | 52 ++++--- packages/types/index.d.ts | 11 +- 10 files changed, 246 insertions(+), 321 deletions(-) diff --git a/packages/backend/src/apps/discord/actions/send-message-to-channel/index.ts b/packages/backend/src/apps/discord/actions/send-message-to-channel/index.ts index 34af6ce6..2aec322a 100644 --- a/packages/backend/src/apps/discord/actions/send-message-to-channel/index.ts +++ b/packages/backend/src/apps/discord/actions/send-message-to-channel/index.ts @@ -4,46 +4,32 @@ export default defineAction({ name: 'Send a message to channel', key: 'sendMessageToChannel', description: 'Send a message to a specific channel you specify.', - substeps: [ + arguments: [ { - key: 'chooseConnection', - name: 'Choose connection', - }, - { - key: 'chooseAction', - name: 'Set up action', - arguments: [ - { - label: 'Channel', - key: 'channel', - type: 'dropdown' as const, - required: true, - description: 'Pick a channel to send the message to.', - variables: false, - source: { - type: 'query', - name: 'getData', - arguments: [ - { - name: 'key', - value: 'listChannels', - }, - ], + label: 'Channel', + key: 'channel', + type: 'dropdown' as const, + required: true, + description: 'Pick a channel to send the message to.', + variables: false, + source: { + type: 'query', + name: 'getData', + arguments: [ + { + name: 'key', + value: 'listChannels', }, - }, - { - label: 'Message text', - key: 'message', - type: 'string' as const, - required: true, - description: 'The content of your new message.', - variables: true, - } - ], + ], + }, }, { - key: 'testStep', - name: 'Test action', + label: 'Message text', + key: 'message', + type: 'string' as const, + required: true, + description: 'The content of your new message.', + variables: true, }, ], @@ -51,7 +37,10 @@ export default defineAction({ const data = { content: $.step.parameters.message as string, }; - const response = await $.http?.post(`/channels/${$.step.parameters.channel}/messages`, data); + const response = await $.http?.post( + `/channels/${$.step.parameters.channel}/messages`, + data + ); $.setActionItem({ raw: response.data }); }, diff --git a/packages/backend/src/apps/github/actions/create-issue/index.ts b/packages/backend/src/apps/github/actions/create-issue/index.ts index 71bddb71..0545499b 100644 --- a/packages/backend/src/apps/github/actions/create-issue/index.ts +++ b/packages/backend/src/apps/github/actions/create-issue/index.ts @@ -1,4 +1,3 @@ -import { IActionOutput } from '@automatisch/types'; import defineAction from '../../../../helpers/define-action'; import getRepoOwnerAndRepo from '../../common/get-repo-owner-and-repo'; @@ -6,51 +5,37 @@ export default defineAction({ name: 'Create issue', key: 'createIssue', description: 'Create a new issue.', - substeps: [ + arguments: [ { - key: 'chooseConnection', - name: 'Choose connection', - }, - { - key: 'chooseAction', - name: 'Set up action', - arguments: [ - { - label: 'Repo', - key: 'repo', - type: 'dropdown' as const, - required: false, - variables: false, - source: { - type: 'query', - name: 'getData', - arguments: [ - { - name: 'key', - value: 'listRepos', - }, - ], + label: 'Repo', + key: 'repo', + type: 'dropdown' as const, + required: false, + variables: false, + source: { + type: 'query', + name: 'getData', + arguments: [ + { + name: 'key', + value: 'listRepos', }, - }, - { - label: 'Title', - key: 'title', - type: 'string' as const, - required: true, - variables: true, - }, - { - label: 'Body', - key: 'body', - type: 'string' as const, - required: true, - variables: true, - }, - ], + ], + }, }, { - key: 'testStep', - name: 'Test action', + label: 'Title', + key: 'title', + type: 'string' as const, + required: true, + variables: true, + }, + { + label: 'Body', + key: 'body', + type: 'string' as const, + required: true, + variables: true, }, ], diff --git a/packages/backend/src/apps/slack/actions/find-message/index.ts b/packages/backend/src/apps/slack/actions/find-message/index.ts index 01086405..03963efb 100644 --- a/packages/backend/src/apps/slack/actions/find-message/index.ts +++ b/packages/backend/src/apps/slack/actions/find-message/index.ts @@ -5,69 +5,55 @@ export default defineAction({ name: 'Find message', key: 'findMessage', description: 'Find a Slack message using the Slack Search feature.', - substeps: [ + arguments: [ { - key: 'chooseConnection', - name: 'Choose connection', + label: 'Search Query', + key: 'query', + type: 'string' as const, + required: true, + description: + 'Search query to use for finding matching messages. See the Slack Search Documentation for more information on constructing a query.', + variables: true, }, { - key: 'setupAction', - name: 'Set up action', - arguments: [ + label: 'Sort by', + key: 'sortBy', + type: 'dropdown' as const, + description: + 'Sort messages by their match strength or by their date. Default is score.', + required: true, + value: 'score', + variables: false, + options: [ { - label: 'Search Query', - key: 'query', - type: 'string' as const, - required: true, - description: - 'Search query to use for finding matching messages. See the Slack Search Documentation for more information on constructing a query.', - variables: true, - }, - { - label: 'Sort by', - key: 'sortBy', - type: 'dropdown' as const, - description: - 'Sort messages by their match strength or by their date. Default is score.', - required: true, + label: 'Match strength', value: 'score', - variables: false, - options: [ - { - label: 'Match strength', - value: 'score', - }, - { - label: 'Message date time', - value: 'timestamp', - }, - ], }, { - label: 'Sort direction', - key: 'sortDirection', - type: 'dropdown' as const, - description: - 'Sort matching messages in ascending or descending order. Default is descending.', - required: true, - value: 'desc', - variables: false, - options: [ - { - label: 'Descending (newest or best match first)', - value: 'desc', - }, - { - label: 'Ascending (oldest or worst match first)', - value: 'asc', - }, - ], + label: 'Message date time', + value: 'timestamp', }, ], }, { - key: 'testStep', - name: 'Test action', + label: 'Sort direction', + key: 'sortDirection', + type: 'dropdown' as const, + description: + 'Sort matching messages in ascending or descending order. Default is descending.', + required: true, + value: 'desc', + variables: false, + options: [ + { + label: 'Descending (newest or best match first)', + value: 'desc', + }, + { + label: 'Ascending (oldest or worst match first)', + value: 'asc', + }, + ], }, ], diff --git a/packages/backend/src/apps/slack/actions/send-a-message-to-channel/index.ts b/packages/backend/src/apps/slack/actions/send-a-message-to-channel/index.ts index 91fe6928..33df770e 100644 --- a/packages/backend/src/apps/slack/actions/send-a-message-to-channel/index.ts +++ b/packages/backend/src/apps/slack/actions/send-a-message-to-channel/index.ts @@ -5,46 +5,32 @@ export default defineAction({ name: 'Send a message to channel', key: 'sendMessageToChannel', description: 'Send a message to a specific channel you specify.', - substeps: [ + arguments: [ { - key: 'chooseConnection', - name: 'Choose connection', - }, - { - key: 'setupAction', - name: 'Set up action', - arguments: [ - { - label: 'Channel', - key: 'channel', - type: 'dropdown' as const, - required: true, - description: 'Pick a channel to send the message to.', - variables: false, - source: { - type: 'query', - name: 'getData', - arguments: [ - { - name: 'key', - value: 'listChannels', - }, - ], + label: 'Channel', + key: 'channel', + type: 'dropdown' as const, + required: true, + description: 'Pick a channel to send the message to.', + variables: false, + source: { + type: 'query', + name: 'getData', + arguments: [ + { + name: 'key', + value: 'listChannels', }, - }, - { - label: 'Message text', - key: 'message', - type: 'string' as const, - required: true, - description: 'The content of your new message.', - variables: true, - }, - ], + ], + }, }, { - key: 'testStep', - name: 'Test action', + label: 'Message text', + key: 'message', + type: 'string' as const, + required: true, + description: 'The content of your new message.', + variables: true, }, ], diff --git a/packages/backend/src/apps/smtp/actions/send-email/index.ts b/packages/backend/src/apps/smtp/actions/send-email/index.ts index 3bc4db27..c332f99e 100644 --- a/packages/backend/src/apps/smtp/actions/send-email/index.ts +++ b/packages/backend/src/apps/smtp/actions/send-email/index.ts @@ -6,86 +6,72 @@ export default defineAction({ name: 'Send Email', key: 'sendEmail', description: 'Send an email', - substeps: [ + arguments: [ { - key: 'chooseConnection', - name: 'Choose connection', + label: 'From name', + key: 'fromName', + type: 'string' as const, + required: false, + description: 'Display name of the sender.', + variables: true, }, { - key: 'chooseAction', - name: 'Set up action', - arguments: [ - { - label: 'From name', - key: 'fromName', - type: 'string' as const, - required: false, - description: 'Display name of the sender.', - variables: true, - }, - { - label: 'From email', - key: 'fromEmail', - type: 'string' as const, - required: true, - description: 'Email address of the sender.', - variables: true, - }, - { - label: 'Reply to', - key: 'replyTo', - type: 'string' as const, - required: false, - description: - 'Email address to reply to. Defaults to the from email address.', - variables: true, - }, - { - label: 'To', - key: 'to', - type: 'string' as const, - required: true, - description: - 'Comma seperated list of email addresses to send the email to.', - variables: true, - }, - { - label: 'Cc', - key: 'cc', - type: 'string' as const, - required: false, - description: 'Comma seperated list of email addresses.', - variables: true, - }, - { - label: 'Bcc', - key: 'bcc', - type: 'string' as const, - required: false, - description: 'Comma seperated list of email addresses.', - variables: true, - }, - { - label: 'Subject', - key: 'subject', - type: 'string' as const, - required: true, - description: 'Subject of the email.', - variables: true, - }, - { - label: 'Body', - key: 'body', - type: 'string' as const, - required: true, - description: 'Body of the email.', - variables: true, - }, - ], + label: 'From email', + key: 'fromEmail', + type: 'string' as const, + required: true, + description: 'Email address of the sender.', + variables: true, }, { - key: 'testStep', - name: 'Test action', + label: 'Reply to', + key: 'replyTo', + type: 'string' as const, + required: false, + description: + 'Email address to reply to. Defaults to the from email address.', + variables: true, + }, + { + label: 'To', + key: 'to', + type: 'string' as const, + required: true, + description: + 'Comma seperated list of email addresses to send the email to.', + variables: true, + }, + { + label: 'Cc', + key: 'cc', + type: 'string' as const, + required: false, + description: 'Comma seperated list of email addresses.', + variables: true, + }, + { + label: 'Bcc', + key: 'bcc', + type: 'string' as const, + required: false, + description: 'Comma seperated list of email addresses.', + variables: true, + }, + { + label: 'Subject', + key: 'subject', + type: 'string' as const, + required: true, + description: 'Subject of the email.', + variables: true, + }, + { + label: 'Body', + key: 'body', + type: 'string' as const, + required: true, + description: 'Body of the email.', + variables: true, }, ], diff --git a/packages/backend/src/apps/twilio/actions/send-sms/index.ts b/packages/backend/src/apps/twilio/actions/send-sms/index.ts index 319d6606..037f14e4 100644 --- a/packages/backend/src/apps/twilio/actions/send-sms/index.ts +++ b/packages/backend/src/apps/twilio/actions/send-sms/index.ts @@ -4,46 +4,32 @@ export default defineAction({ name: 'Send SMS', key: 'sendSms', description: 'Send an SMS', - substeps: [ + arguments: [ { - key: 'chooseConnection', - name: 'Choose connection', + label: 'From Number', + key: 'fromNumber', + type: 'string' as const, + required: true, + description: + 'The number to send the SMS from. Include country code. Example: 15551234567', + variables: true, }, { - key: 'chooseAction', - name: 'Set up action', - arguments: [ - { - label: 'From Number', - key: 'fromNumber', - type: 'string' as const, - required: true, - description: - 'The number to send the SMS from. Include country code. Example: 15551234567', - variables: true, - }, - { - label: 'To Number', - key: 'toNumber', - type: 'string' as const, - required: true, - description: - 'The number to send the SMS to. Include country code. Example: 15551234567', - variables: true, - }, - { - label: 'Message', - key: 'message', - type: 'string' as const, - required: true, - description: 'The message to send.', - variables: true, - }, - ], + label: 'To Number', + key: 'toNumber', + type: 'string' as const, + required: true, + description: + 'The number to send the SMS to. Include country code. Example: 15551234567', + variables: true, }, { - key: 'testStep', - name: 'Test action', + label: 'Message', + key: 'message', + type: 'string' as const, + required: true, + description: 'The message to send.', + variables: true, }, ], diff --git a/packages/backend/src/apps/twitter/actions/create-tweet/index.ts b/packages/backend/src/apps/twitter/actions/create-tweet/index.ts index aecef502..1d3b7f41 100644 --- a/packages/backend/src/apps/twitter/actions/create-tweet/index.ts +++ b/packages/backend/src/apps/twitter/actions/create-tweet/index.ts @@ -4,28 +4,14 @@ export default defineAction({ name: 'Create Tweet', key: 'createTweet', description: 'Create a tweet.', - substeps: [ + arguments: [ { - key: 'chooseConnection', - name: 'Choose connection', - }, - { - key: 'chooseAction', - name: 'Set up action', - arguments: [ - { - label: 'Tweet body', - key: 'tweet', - type: 'string' as const, - required: true, - description: 'The content of your new tweet.', - variables: true, - }, - ], - }, - { - key: 'testStep', - name: 'Test action', + label: 'Tweet body', + key: 'tweet', + type: 'string' as const, + required: true, + description: 'The content of your new tweet.', + variables: true, }, ], diff --git a/packages/backend/src/helpers/define-action.ts b/packages/backend/src/helpers/define-action.ts index bc21ceb9..6d8bf92c 100644 --- a/packages/backend/src/helpers/define-action.ts +++ b/packages/backend/src/helpers/define-action.ts @@ -1,5 +1,5 @@ -import { IAction } from '@automatisch/types'; +import { IRawAction } from '@automatisch/types'; -export default function defineAction(actionDefinition: IAction): IAction { +export default function defineAction(actionDefinition: IRawAction): IRawAction { return actionDefinition; } diff --git a/packages/backend/src/helpers/get-app.ts b/packages/backend/src/helpers/get-app.ts index 8632d71f..44d102c2 100644 --- a/packages/backend/src/helpers/get-app.ts +++ b/packages/backend/src/helpers/get-app.ts @@ -1,4 +1,10 @@ -import { IApp, IRawTrigger, ITrigger } from '@automatisch/types'; +import { + IAction, + IApp, + IRawAction, + IRawTrigger, + ITrigger, +} from '@automatisch/types'; import { omit, cloneDeep } from 'lodash'; async function getDefaultExport(path: string) { @@ -23,25 +29,11 @@ const getApp = async (appKey: string, stripFuncs = true) => { const appData: IApp = cloneDeep(await getDefaultExport(`../apps/${appKey}`)); appData.triggers = appData?.triggers?.map((trigger: IRawTrigger) => { - const computedTrigger: ITrigger = omit(trigger, ['arguments']); + return addStaticSubsteps(appData, trigger); + }); - computedTrigger.substeps = []; - - if (appData.supportsConnections) { - computedTrigger.substeps.push(chooseConnectionStep); - } - - if (trigger.arguments) { - computedTrigger.substeps.push({ - key: 'chooseTrigger', - name: 'Set up a trigger', - arguments: trigger.arguments, - }); - } - - computedTrigger.substeps.push(testStep); - - return computedTrigger; + appData.actions = appData?.actions?.map((action: IRawAction) => { + return addStaticSubsteps(appData, action); }); if (stripFuncs) { @@ -51,4 +43,26 @@ const getApp = async (appKey: string, stripFuncs = true) => { return appData; }; +const addStaticSubsteps = (appData: IApp, step: IRawTrigger | IRawAction) => { + const computedStep: ITrigger | IAction = omit(step, ['arguments']); + + computedStep.substeps = []; + + if (appData.supportsConnections) { + computedStep.substeps.push(chooseConnectionStep); + } + + if (step.arguments) { + computedStep.substeps.push({ + key: 'chooseTrigger', + name: 'Set up a trigger', + arguments: step.arguments, + }); + } + + computedStep.substeps.push(testStep); + + return computedStep; +}; + export default getApp; diff --git a/packages/types/index.d.ts b/packages/types/index.d.ts index 84a267da..08446cf9 100644 --- a/packages/types/index.d.ts +++ b/packages/types/index.d.ts @@ -232,14 +232,21 @@ export interface IActionItem { raw: IJSONObject; } -export interface IAction { +export interface IBaseAction { name: string; key: string; description: string; - substeps: ISubstep[]; run($: IGlobalVariable): Promise; } +export interface IRawAction extends IBaseAction { + arguments?: IField[]; +} + +export interface IAction extends IBaseAction { + substeps?: ISubstep[]; +} + export interface IAuthentication { client: unknown; verifyCredentials(): Promise; From d6c0690324039e3e7caa701f33135242cee6a3c7 Mon Sep 17 00:00:00 2001 From: Faruk AYDIN Date: Mon, 31 Oct 2022 23:31:28 +0100 Subject: [PATCH 2/2] refactor: Differentiate trigger and action for get app helper --- packages/backend/src/helpers/get-app.ts | 36 ++++++++++++++----------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/packages/backend/src/helpers/get-app.ts b/packages/backend/src/helpers/get-app.ts index 44d102c2..c3217d72 100644 --- a/packages/backend/src/helpers/get-app.ts +++ b/packages/backend/src/helpers/get-app.ts @@ -15,25 +15,15 @@ function stripFunctions(data: C): C { return JSON.parse(JSON.stringify(data)); } -const chooseConnectionStep = { - key: 'chooseConnection', - name: 'Choose connection', -}; - -const testStep = { - key: 'testStep', - name: 'Test trigger', -}; - const getApp = async (appKey: string, stripFuncs = true) => { const appData: IApp = cloneDeep(await getDefaultExport(`../apps/${appKey}`)); appData.triggers = appData?.triggers?.map((trigger: IRawTrigger) => { - return addStaticSubsteps(appData, trigger); + return addStaticSubsteps('trigger', appData, trigger); }); appData.actions = appData?.actions?.map((action: IRawAction) => { - return addStaticSubsteps(appData, action); + return addStaticSubsteps('action', appData, action); }); if (stripFuncs) { @@ -43,7 +33,23 @@ const getApp = async (appKey: string, stripFuncs = true) => { return appData; }; -const addStaticSubsteps = (appData: IApp, step: IRawTrigger | IRawAction) => { +const chooseConnectionStep = { + key: 'chooseConnection', + name: 'Choose connection', +}; + +const testStep = (stepType: 'trigger' | 'action') => { + return { + key: 'testStep', + name: stepType === 'trigger' ? 'Test trigger' : 'Test action', + }; +}; + +const addStaticSubsteps = ( + stepType: 'trigger' | 'action', + appData: IApp, + step: IRawTrigger | IRawAction +) => { const computedStep: ITrigger | IAction = omit(step, ['arguments']); computedStep.substeps = []; @@ -55,12 +61,12 @@ const addStaticSubsteps = (appData: IApp, step: IRawTrigger | IRawAction) => { if (step.arguments) { computedStep.substeps.push({ key: 'chooseTrigger', - name: 'Set up a trigger', + name: stepType === 'trigger' ? 'Set up a trigger' : 'Set up action', arguments: step.arguments, }); } - computedStep.substeps.push(testStep); + computedStep.substeps.push(testStep(stepType)); return computedStep; };