Merge pull request #668 from automatisch/refactor/action-arguments

refactor: Use only arguments for action definitions
This commit is contained in:
Ömer Faruk Aydın
2022-11-01 00:50:45 +01:00
committed by GitHub
10 changed files with 262 additions and 331 deletions

View File

@@ -4,14 +4,6 @@ export default defineAction({
name: 'Send a message to channel',
key: 'sendMessageToChannel',
description: 'Send a message to a specific channel you specify.',
substeps: [
{
key: 'chooseConnection',
name: 'Choose connection',
},
{
key: 'chooseAction',
name: 'Set up action',
arguments: [
{
label: 'Channel',
@@ -38,12 +30,6 @@ export default defineAction({
required: true,
description: 'The content of your new message.',
variables: true,
}
],
},
{
key: 'testStep',
name: 'Test action',
},
],
@@ -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 });
},

View File

@@ -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,14 +5,6 @@ export default defineAction({
name: 'Create issue',
key: 'createIssue',
description: 'Create a new issue.',
substeps: [
{
key: 'chooseConnection',
name: 'Choose connection',
},
{
key: 'chooseAction',
name: 'Set up action',
arguments: [
{
label: 'Repo',
@@ -47,12 +38,6 @@ export default defineAction({
variables: true,
},
],
},
{
key: 'testStep',
name: 'Test action',
},
],
async run($) {
const repoParameter = $.step.parameters.repo as string;

View File

@@ -5,14 +5,6 @@ export default defineAction({
name: 'Find message',
key: 'findMessage',
description: 'Find a Slack message using the Slack Search feature.',
substeps: [
{
key: 'chooseConnection',
name: 'Choose connection',
},
{
key: 'setupAction',
name: 'Set up action',
arguments: [
{
label: 'Search Query',
@@ -64,12 +56,6 @@ export default defineAction({
],
},
],
},
{
key: 'testStep',
name: 'Test action',
},
],
async run($) {
const parameters = $.step.parameters;

View File

@@ -5,14 +5,6 @@ export default defineAction({
name: 'Send a message to channel',
key: 'sendMessageToChannel',
description: 'Send a message to a specific channel you specify.',
substeps: [
{
key: 'chooseConnection',
name: 'Choose connection',
},
{
key: 'setupAction',
name: 'Set up action',
arguments: [
{
label: 'Channel',
@@ -41,12 +33,6 @@ export default defineAction({
variables: true,
},
],
},
{
key: 'testStep',
name: 'Test action',
},
],
async run($) {
const channelId = $.step.parameters.channel as string;

View File

@@ -6,14 +6,6 @@ export default defineAction({
name: 'Send Email',
key: 'sendEmail',
description: 'Send an email',
substeps: [
{
key: 'chooseConnection',
name: 'Choose connection',
},
{
key: 'chooseAction',
name: 'Set up action',
arguments: [
{
label: 'From name',
@@ -82,12 +74,6 @@ export default defineAction({
variables: true,
},
],
},
{
key: 'testStep',
name: 'Test action',
},
],
async run($) {
const info = await transporter($).sendMail({

View File

@@ -4,14 +4,6 @@ export default defineAction({
name: 'Send SMS',
key: 'sendSms',
description: 'Send an SMS',
substeps: [
{
key: 'chooseConnection',
name: 'Choose connection',
},
{
key: 'chooseAction',
name: 'Set up action',
arguments: [
{
label: 'From Number',
@@ -40,12 +32,6 @@ export default defineAction({
variables: true,
},
],
},
{
key: 'testStep',
name: 'Test action',
},
],
async run($) {
const requestPath = `/2010-04-01/Accounts/${$.auth.data.accountSid}/Messages.json`;

View File

@@ -4,14 +4,6 @@ export default defineAction({
name: 'Create Tweet',
key: 'createTweet',
description: 'Create a tweet.',
substeps: [
{
key: 'chooseConnection',
name: 'Choose connection',
},
{
key: 'chooseAction',
name: 'Set up action',
arguments: [
{
label: 'Tweet body',
@@ -22,12 +14,6 @@ export default defineAction({
variables: true,
},
],
},
{
key: 'testStep',
name: 'Test action',
},
],
async run($) {
const text = $.step.parameters.tweet;

View File

@@ -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;
}

View File

@@ -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) {
@@ -9,39 +15,15 @@ function stripFunctions<C>(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) => {
const computedTrigger: ITrigger = omit(trigger, ['arguments']);
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,
return addStaticSubsteps('trigger', appData, trigger);
});
}
computedTrigger.substeps.push(testStep);
return computedTrigger;
appData.actions = appData?.actions?.map((action: IRawAction) => {
return addStaticSubsteps('action', appData, action);
});
if (stripFuncs) {
@@ -51,4 +33,42 @@ const getApp = async (appKey: string, stripFuncs = true) => {
return appData;
};
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 = [];
if (appData.supportsConnections) {
computedStep.substeps.push(chooseConnectionStep);
}
if (step.arguments) {
computedStep.substeps.push({
key: 'chooseTrigger',
name: stepType === 'trigger' ? 'Set up a trigger' : 'Set up action',
arguments: step.arguments,
});
}
computedStep.substeps.push(testStep(stepType));
return computedStep;
};
export default getApp;

View File

@@ -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<void>;
}
export interface IRawAction extends IBaseAction {
arguments?: IField[];
}
export interface IAction extends IBaseAction {
substeps?: ISubstep[];
}
export interface IAuthentication {
client: unknown;
verifyCredentials(): Promise<IJSONObject>;