Merge remote-tracking branch 'origin/execute-script-v2' into execute-script-v2-frontend
This commit is contained in:
commit
2ec8484fd8
|
@ -0,0 +1 @@
|
||||||
|
scripts/resources/minio filter=lfs diff=lfs merge=lfs -text
|
|
@ -62,7 +62,6 @@ http {
|
||||||
proxy_connect_timeout 120s;
|
proxy_connect_timeout 120s;
|
||||||
proxy_send_timeout 120s;
|
proxy_send_timeout 120s;
|
||||||
proxy_http_version 1.1;
|
proxy_http_version 1.1;
|
||||||
|
|
||||||
proxy_set_header Host $host;
|
proxy_set_header Host $host;
|
||||||
proxy_set_header Connection "";
|
proxy_set_header Connection "";
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
ARG BASEIMG=budibase/couchdb:v3.3.3-sqs-v2.1.1
|
ARG BASEIMG=budibase/couchdb:v3.3.3-sqs-v2.1.1
|
||||||
FROM node:20-slim as build
|
FROM node:20-slim AS build
|
||||||
|
|
||||||
# install node-gyp dependencies
|
# install node-gyp dependencies
|
||||||
RUN apt-get update && apt-get install -y --no-install-recommends g++ make python3 jq
|
RUN apt-get update && apt-get install -y --no-install-recommends g++ make python3 jq
|
||||||
|
@ -34,13 +34,13 @@ COPY packages/worker/dist packages/worker/dist
|
||||||
COPY packages/worker/pm2.config.js packages/worker/pm2.config.js
|
COPY packages/worker/pm2.config.js packages/worker/pm2.config.js
|
||||||
|
|
||||||
|
|
||||||
FROM $BASEIMG as runner
|
FROM $BASEIMG AS runner
|
||||||
ARG TARGETARCH
|
ARG TARGETARCH
|
||||||
ENV TARGETARCH $TARGETARCH
|
ENV TARGETARCH=$TARGETARCH
|
||||||
#TARGETBUILD can be set to single (for single docker image) or aas (for azure app service)
|
#TARGETBUILD can be set to single (for single docker image) or aas (for azure app service)
|
||||||
# e.g. docker build --build-arg TARGETBUILD=aas ....
|
# e.g. docker build --build-arg TARGETBUILD=aas ....
|
||||||
ARG TARGETBUILD=single
|
ARG TARGETBUILD=single
|
||||||
ENV TARGETBUILD $TARGETBUILD
|
ENV TARGETBUILD=$TARGETBUILD
|
||||||
|
|
||||||
# install base dependencies
|
# install base dependencies
|
||||||
RUN apt-get update && \
|
RUN apt-get update && \
|
||||||
|
@ -67,6 +67,12 @@ RUN mkdir -p /var/log/nginx && \
|
||||||
|
|
||||||
# setup minio
|
# setup minio
|
||||||
WORKDIR /minio
|
WORKDIR /minio
|
||||||
|
|
||||||
|
# a 2022 version of minio that supports gateway mode
|
||||||
|
COPY scripts/resources/minio /minio
|
||||||
|
RUN chmod +x minio
|
||||||
|
|
||||||
|
# handles the installation of minio in non-aas environments
|
||||||
COPY scripts/install-minio.sh ./install.sh
|
COPY scripts/install-minio.sh ./install.sh
|
||||||
RUN chmod +x install.sh && ./install.sh
|
RUN chmod +x install.sh && ./install.sh
|
||||||
|
|
||||||
|
|
|
@ -1,53 +1,61 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
declare -a ENV_VARS=("COUCHDB_USER" "COUCHDB_PASSWORD" "DATA_DIR" "MINIO_ACCESS_KEY" "MINIO_SECRET_KEY" "INTERNAL_API_KEY" "JWT_SECRET" "REDIS_PASSWORD")
|
|
||||||
declare -a DOCKER_VARS=("APP_PORT" "APPS_URL" "ARCHITECTURE" "BUDIBASE_ENVIRONMENT" "CLUSTER_PORT" "DEPLOYMENT_ENVIRONMENT" "MINIO_URL" "NODE_ENV" "POSTHOG_TOKEN" "REDIS_URL" "SELF_HOSTED" "WORKER_PORT" "WORKER_URL" "TENANT_FEATURE_FLAGS" "ACCOUNT_PORTAL_URL")
|
|
||||||
# Check the env vars set in Dockerfile have come through, AAS seems to drop them
|
|
||||||
[[ -z "${APP_PORT}" ]] && export APP_PORT=4001
|
|
||||||
[[ -z "${ARCHITECTURE}" ]] && export ARCHITECTURE=amd
|
|
||||||
[[ -z "${BUDIBASE_ENVIRONMENT}" ]] && export BUDIBASE_ENVIRONMENT=PRODUCTION
|
|
||||||
[[ -z "${CLUSTER_PORT}" ]] && export CLUSTER_PORT=80
|
|
||||||
[[ -z "${DEPLOYMENT_ENVIRONMENT}" ]] && export DEPLOYMENT_ENVIRONMENT=docker
|
|
||||||
[[ -z "${MINIO_URL}" ]] && [[ -z "${USE_S3}" ]] && export MINIO_URL=http://127.0.0.1:9000
|
|
||||||
[[ -z "${NODE_ENV}" ]] && export NODE_ENV=production
|
|
||||||
[[ -z "${POSTHOG_TOKEN}" ]] && export POSTHOG_TOKEN=phc_bIjZL7oh2GEUd2vqvTBH8WvrX0fWTFQMs6H5KQxiUxU
|
|
||||||
[[ -z "${ACCOUNT_PORTAL_URL}" ]] && export ACCOUNT_PORTAL_URL=https://account.budibase.app
|
|
||||||
[[ -z "${REDIS_URL}" ]] && export REDIS_URL=127.0.0.1:6379
|
|
||||||
[[ -z "${SELF_HOSTED}" ]] && export SELF_HOSTED=1
|
|
||||||
[[ -z "${WORKER_PORT}" ]] && export WORKER_PORT=4002
|
|
||||||
[[ -z "${WORKER_URL}" ]] && export WORKER_URL=http://127.0.0.1:4002
|
|
||||||
[[ -z "${APPS_URL}" ]] && export APPS_URL=http://127.0.0.1:4001
|
|
||||||
[[ -z "${SERVER_TOP_LEVEL_PATH}" ]] && export SERVER_TOP_LEVEL_PATH=/app
|
|
||||||
# export CUSTOM_DOMAIN=budi001.custom.com
|
|
||||||
|
|
||||||
# Azure App Service customisations
|
echo "Starting runner.sh..."
|
||||||
if [[ "${TARGETBUILD}" = "aas" ]]; then
|
|
||||||
export DATA_DIR="${DATA_DIR:-/home}"
|
# set defaults for Docker-related variables
|
||||||
WEBSITES_ENABLE_APP_SERVICE_STORAGE=true
|
export APP_PORT="${APP_PORT:-4001}"
|
||||||
/etc/init.d/ssh start
|
export ARCHITECTURE="${ARCHITECTURE:-amd}"
|
||||||
else
|
export BUDIBASE_ENVIRONMENT="${BUDIBASE_ENVIRONMENT:-PRODUCTION}"
|
||||||
export DATA_DIR=${DATA_DIR:-/data}
|
export CLUSTER_PORT="${CLUSTER_PORT:-80}"
|
||||||
|
export DEPLOYMENT_ENVIRONMENT="${DEPLOYMENT_ENVIRONMENT:-docker}"
|
||||||
|
|
||||||
|
# only set MINIO_URL if neither MINIO_URL nor USE_S3 is set
|
||||||
|
if [[ -z "${MINIO_URL}" && -z "${USE_S3}" ]]; then
|
||||||
|
export MINIO_URL="http://127.0.0.1:9000"
|
||||||
fi
|
fi
|
||||||
mkdir -p ${DATA_DIR}
|
|
||||||
# Mount NFS or GCP Filestore if env vars exist for it
|
export NODE_ENV="${NODE_ENV:-production}"
|
||||||
if [[ ! -z ${FILESHARE_IP} && ! -z ${FILESHARE_NAME} ]]; then
|
export POSTHOG_TOKEN="${POSTHOG_TOKEN:-phc_bIjZL7oh2GEUd2vqvTBH8WvrX0fWTFQMs6H5KQxiUxU}"
|
||||||
|
export ACCOUNT_PORTAL_URL="${ACCOUNT_PORTAL_URL:-https://account.budibase.app}"
|
||||||
|
export REDIS_URL="${REDIS_URL:-127.0.0.1:6379}"
|
||||||
|
export SELF_HOSTED="${SELF_HOSTED:-1}"
|
||||||
|
export WORKER_PORT="${WORKER_PORT:-4002}"
|
||||||
|
export WORKER_URL="${WORKER_URL:-http://127.0.0.1:4002}"
|
||||||
|
export APPS_URL="${APPS_URL:-http://127.0.0.1:4001}"
|
||||||
|
export SERVER_TOP_LEVEL_PATH="${SERVER_TOP_LEVEL_PATH:-/app}"
|
||||||
|
|
||||||
|
# set DATA_DIR and ensure the directory exists
|
||||||
|
if [[ ${TARGETBUILD} == "aas" ]]; then
|
||||||
|
export DATA_DIR="/home"
|
||||||
|
else
|
||||||
|
export DATA_DIR="${DATA_DIR:-/data}"
|
||||||
|
fi
|
||||||
|
mkdir -p "${DATA_DIR}"
|
||||||
|
|
||||||
|
# mount NFS or GCP Filestore if FILESHARE_IP and FILESHARE_NAME are set
|
||||||
|
if [[ -n "${FILESHARE_IP}" && -n "${FILESHARE_NAME}" ]]; then
|
||||||
echo "Mounting NFS share"
|
echo "Mounting NFS share"
|
||||||
apt update && apt install -y nfs-common nfs-kernel-server
|
apt update && apt install -y nfs-common nfs-kernel-server
|
||||||
echo "Mount file share ${FILESHARE_IP}:/${FILESHARE_NAME} to ${DATA_DIR}"
|
echo "Mount file share ${FILESHARE_IP}:/${FILESHARE_NAME} to ${DATA_DIR}"
|
||||||
mount -o nolock ${FILESHARE_IP}:/${FILESHARE_NAME} ${DATA_DIR}
|
mount -o nolock "${FILESHARE_IP}:/${FILESHARE_NAME}" "${DATA_DIR}"
|
||||||
echo "Mounting result: $?"
|
echo "Mounting result: $?"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ -f "${DATA_DIR}/.env" ]; then
|
# source environment variables from a .env file if it exists in DATA_DIR
|
||||||
# Read in the .env file and export the variables
|
if [[ -f "${DATA_DIR}/.env" ]]; then
|
||||||
for LINE in $(cat ${DATA_DIR}/.env); do export $LINE; done
|
set -a # Automatically export all variables loaded from .env
|
||||||
|
source "${DATA_DIR}/.env"
|
||||||
|
set +a
|
||||||
fi
|
fi
|
||||||
# randomise any unset environment variables
|
|
||||||
for ENV_VAR in "${ENV_VARS[@]}"
|
# randomize any unset sensitive environment variables using uuidgen
|
||||||
do
|
env_vars=(COUCHDB_USER COUCHDB_PASSWORD MINIO_ACCESS_KEY MINIO_SECRET_KEY INTERNAL_API_KEY JWT_SECRET REDIS_PASSWORD)
|
||||||
if [[ -z "${!ENV_VAR}" ]]; then
|
for var in "${env_vars[@]}"; do
|
||||||
eval "export $ENV_VAR=$(uuidgen | sed -e 's/-//g')"
|
if [[ -z "${!var}" ]]; then
|
||||||
|
export "$var"="$(uuidgen | tr -d '-')"
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
if [[ -z "${COUCH_DB_URL}" ]]; then
|
if [[ -z "${COUCH_DB_URL}" ]]; then
|
||||||
export COUCH_DB_URL=http://$COUCHDB_USER:$COUCHDB_PASSWORD@127.0.0.1:5984
|
export COUCH_DB_URL=http://$COUCHDB_USER:$COUCHDB_PASSWORD@127.0.0.1:5984
|
||||||
fi
|
fi
|
||||||
|
@ -58,17 +66,15 @@ fi
|
||||||
|
|
||||||
if [ ! -f "${DATA_DIR}/.env" ]; then
|
if [ ! -f "${DATA_DIR}/.env" ]; then
|
||||||
touch ${DATA_DIR}/.env
|
touch ${DATA_DIR}/.env
|
||||||
for ENV_VAR in "${ENV_VARS[@]}"
|
for ENV_VAR in "${ENV_VARS[@]}"; do
|
||||||
do
|
|
||||||
temp=$(eval "echo \$$ENV_VAR")
|
temp=$(eval "echo \$$ENV_VAR")
|
||||||
echo "$ENV_VAR=$temp" >> ${DATA_DIR}/.env
|
echo "$ENV_VAR=$temp" >>${DATA_DIR}/.env
|
||||||
done
|
done
|
||||||
for ENV_VAR in "${DOCKER_VARS[@]}"
|
for ENV_VAR in "${DOCKER_VARS[@]}"; do
|
||||||
do
|
|
||||||
temp=$(eval "echo \$$ENV_VAR")
|
temp=$(eval "echo \$$ENV_VAR")
|
||||||
echo "$ENV_VAR=$temp" >> ${DATA_DIR}/.env
|
echo "$ENV_VAR=$temp" >>${DATA_DIR}/.env
|
||||||
done
|
done
|
||||||
echo "COUCH_DB_URL=${COUCH_DB_URL}" >> ${DATA_DIR}/.env
|
echo "COUCH_DB_URL=${COUCH_DB_URL}" >>${DATA_DIR}/.env
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Read in the .env file and export the variables
|
# Read in the .env file and export the variables
|
||||||
|
@ -79,31 +85,44 @@ ln -s ${DATA_DIR}/.env /worker/.env
|
||||||
# make these directories in runner, incase of mount
|
# make these directories in runner, incase of mount
|
||||||
mkdir -p ${DATA_DIR}/minio
|
mkdir -p ${DATA_DIR}/minio
|
||||||
mkdir -p ${DATA_DIR}/redis
|
mkdir -p ${DATA_DIR}/redis
|
||||||
|
mkdir -p ${DATA_DIR}/couch
|
||||||
chown -R couchdb:couchdb ${DATA_DIR}/couch
|
chown -R couchdb:couchdb ${DATA_DIR}/couch
|
||||||
|
|
||||||
REDIS_CONFIG="/etc/redis/redis.conf"
|
REDIS_CONFIG="/etc/redis/redis.conf"
|
||||||
sed -i "s#DATA_DIR#${DATA_DIR}#g" "${REDIS_CONFIG}"
|
sed -i "s#DATA_DIR#${DATA_DIR}#g" "${REDIS_CONFIG}"
|
||||||
|
|
||||||
if [[ -n "${USE_DEFAULT_REDIS_CONFIG}" ]]; then
|
if [[ -n "${USE_DEFAULT_REDIS_CONFIG}" ]]; then
|
||||||
REDIS_CONFIG=""
|
REDIS_CONFIG=""
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ -n "${REDIS_PASSWORD}" ]]; then
|
if [[ -n "${REDIS_PASSWORD}" ]]; then
|
||||||
redis-server "${REDIS_CONFIG}" --requirepass $REDIS_PASSWORD > /dev/stdout 2>&1 &
|
redis-server "${REDIS_CONFIG}" --requirepass $REDIS_PASSWORD >/dev/stdout 2>&1 &
|
||||||
else
|
else
|
||||||
redis-server "${REDIS_CONFIG}" > /dev/stdout 2>&1 &
|
redis-server "${REDIS_CONFIG}" >/dev/stdout 2>&1 &
|
||||||
fi
|
fi
|
||||||
/bbcouch-runner.sh &
|
|
||||||
|
echo "Starting callback CouchDB runner..."
|
||||||
|
./bbcouch-runner.sh &
|
||||||
|
|
||||||
# only start minio if use s3 isn't passed
|
# only start minio if use s3 isn't passed
|
||||||
if [[ -z "${USE_S3}" ]]; then
|
if [[ -z "${USE_S3}" ]]; then
|
||||||
/minio/minio server --console-address ":9001" ${DATA_DIR}/minio > /dev/stdout 2>&1 &
|
if [[ ${TARGETBUILD} == aas ]]; then
|
||||||
|
echo "Starting MinIO in Azure Gateway mode"
|
||||||
|
if [[ -z "${AZURE_STORAGE_ACCOUNT}" || -z "${AZURE_STORAGE_KEY}" || -z "${MINIO_ACCESS_KEY}" || -z "${MINIO_SECRET_KEY}" ]]; then
|
||||||
|
echo "The following environment variables must be set: AZURE_STORAGE_ACCOUNT, AZURE_STORAGE_KEY, MINIO_ACCESS_KEY, MINIO_SECRET_KEY"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
/minio/minio gateway azure --console-address ":9001" >/dev/stdout 2>&1 &
|
||||||
|
else
|
||||||
|
echo "Starting MinIO in standalone mode"
|
||||||
|
/minio/minio server --console-address ":9001" ${DATA_DIR}/minio >/dev/stdout 2>&1 &
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
/etc/init.d/nginx restart
|
/etc/init.d/nginx restart
|
||||||
if [[ ! -z "${CUSTOM_DOMAIN}" ]]; then
|
if [[ ! -z "${CUSTOM_DOMAIN}" ]]; then
|
||||||
# Add monthly cron job to renew certbot certificate
|
# Add monthly cron job to renew certbot certificate
|
||||||
echo -n "* * 2 * * root exec /app/letsencrypt/certificate-renew.sh ${CUSTOM_DOMAIN}" >> /etc/cron.d/certificate-renew
|
echo -n "* * 2 * * root exec /app/letsencrypt/certificate-renew.sh ${CUSTOM_DOMAIN}" >>/etc/cron.d/certificate-renew
|
||||||
chmod +x /etc/cron.d/certificate-renew
|
chmod +x /etc/cron.d/certificate-renew
|
||||||
# Request the certbot certificate
|
# Request the certbot certificate
|
||||||
/app/letsencrypt/certificate-request.sh ${CUSTOM_DOMAIN}
|
/app/letsencrypt/certificate-request.sh ${CUSTOM_DOMAIN}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"$schema": "node_modules/lerna/schemas/lerna-schema.json",
|
"$schema": "node_modules/lerna/schemas/lerna-schema.json",
|
||||||
"version": "3.4.12",
|
"version": "3.4.16",
|
||||||
"npmClient": "yarn",
|
"npmClient": "yarn",
|
||||||
"concurrency": 20,
|
"concurrency": 20,
|
||||||
"command": {
|
"command": {
|
||||||
|
|
|
@ -123,7 +123,7 @@ export async function doInAutomationContext<T>(params: {
|
||||||
task: () => T
|
task: () => T
|
||||||
}): Promise<T> {
|
}): Promise<T> {
|
||||||
await ensureSnippetContext()
|
await ensureSnippetContext()
|
||||||
return newContext(
|
return await newContext(
|
||||||
{
|
{
|
||||||
tenantId: getTenantIDFromAppID(params.appId),
|
tenantId: getTenantIDFromAppID(params.appId),
|
||||||
appId: params.appId,
|
appId: params.appId,
|
||||||
|
@ -266,9 +266,9 @@ export const getProdAppId = () => {
|
||||||
return conversions.getProdAppID(appId)
|
return conversions.getProdAppID(appId)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function doInEnvironmentContext(
|
export function doInEnvironmentContext<T>(
|
||||||
values: Record<string, string>,
|
values: Record<string, string>,
|
||||||
task: any
|
task: () => T
|
||||||
) {
|
) {
|
||||||
if (!values) {
|
if (!values) {
|
||||||
throw new Error("Must supply environment variables.")
|
throw new Error("Must supply environment variables.")
|
||||||
|
|
|
@ -5,10 +5,10 @@ import {
|
||||||
SqlQuery,
|
SqlQuery,
|
||||||
Table,
|
Table,
|
||||||
TableSourceType,
|
TableSourceType,
|
||||||
|
SEPARATOR,
|
||||||
} from "@budibase/types"
|
} from "@budibase/types"
|
||||||
import { DEFAULT_BB_DATASOURCE_ID } from "../constants"
|
import { DEFAULT_BB_DATASOURCE_ID } from "../constants"
|
||||||
import { Knex } from "knex"
|
import { Knex } from "knex"
|
||||||
import { SEPARATOR } from "../db"
|
|
||||||
import environment from "../environment"
|
import environment from "../environment"
|
||||||
|
|
||||||
const DOUBLE_SEPARATOR = `${SEPARATOR}${SEPARATOR}`
|
const DOUBLE_SEPARATOR = `${SEPARATOR}${SEPARATOR}`
|
||||||
|
|
|
@ -15,23 +15,27 @@ const conversion: Record<DurationType, number> = {
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Duration {
|
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) {
|
static convert(from: DurationType, to: DurationType, duration: number) {
|
||||||
const milliseconds = duration * conversion[from]
|
const milliseconds = duration * conversion[from]
|
||||||
return milliseconds / conversion[to]
|
return milliseconds / conversion[to]
|
||||||
}
|
}
|
||||||
|
|
||||||
static from(from: DurationType, duration: number) {
|
static from(from: DurationType, duration: number) {
|
||||||
return {
|
return new Duration(duration * conversion[from])
|
||||||
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)
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static fromSeconds(duration: number) {
|
static fromSeconds(duration: number) {
|
||||||
|
|
|
@ -2,3 +2,4 @@ export * from "./hashing"
|
||||||
export * from "./utils"
|
export * from "./utils"
|
||||||
export * from "./stringUtils"
|
export * from "./stringUtils"
|
||||||
export * from "./Duration"
|
export * from "./Duration"
|
||||||
|
export * from "./time"
|
||||||
|
|
|
@ -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)]
|
||||||
|
}
|
|
@ -57,6 +57,7 @@
|
||||||
import { tooltips } from "@codemirror/view"
|
import { tooltips } from "@codemirror/view"
|
||||||
import type { BindingCompletion, CodeValidator } from "@/types"
|
import type { BindingCompletion, CodeValidator } from "@/types"
|
||||||
import { validateHbsTemplate } from "./validator/hbs"
|
import { validateHbsTemplate } from "./validator/hbs"
|
||||||
|
import { validateJsTemplate } from "./validator/js"
|
||||||
|
|
||||||
export let label: string | undefined = undefined
|
export let label: string | undefined = undefined
|
||||||
export let completions: BindingCompletion[] = []
|
export let completions: BindingCompletion[] = []
|
||||||
|
@ -365,6 +366,9 @@
|
||||||
if (mode === EditorModes.Handlebars) {
|
if (mode === EditorModes.Handlebars) {
|
||||||
const diagnostics = validateHbsTemplate(value, validations)
|
const diagnostics = validateHbsTemplate(value, validations)
|
||||||
editor.dispatch(setDiagnostics(editor.state, diagnostics))
|
editor.dispatch(setDiagnostics(editor.state, diagnostics))
|
||||||
|
} else if (mode === EditorModes.JS) {
|
||||||
|
const diagnostics = validateJsTemplate(value, validations)
|
||||||
|
editor.dispatch(setDiagnostics(editor.state, diagnostics))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,101 @@
|
||||||
|
import { Parser } from "acorn"
|
||||||
|
import * as walk from "acorn-walk"
|
||||||
|
|
||||||
|
import type { Diagnostic } from "@codemirror/lint"
|
||||||
|
import { CodeValidator } from "@/types"
|
||||||
|
|
||||||
|
export function validateJsTemplate(
|
||||||
|
code: string,
|
||||||
|
validations: CodeValidator
|
||||||
|
): Diagnostic[] {
|
||||||
|
const diagnostics: Diagnostic[] = []
|
||||||
|
|
||||||
|
try {
|
||||||
|
const ast = Parser.parse(code, {
|
||||||
|
ecmaVersion: "latest",
|
||||||
|
locations: true,
|
||||||
|
allowReturnOutsideFunction: true,
|
||||||
|
})
|
||||||
|
|
||||||
|
const lineOffsets: number[] = []
|
||||||
|
let offset = 0
|
||||||
|
for (const line of code.split("\n")) {
|
||||||
|
lineOffsets.push(offset)
|
||||||
|
offset += line.length + 1 // +1 for newline character
|
||||||
|
}
|
||||||
|
|
||||||
|
let hasReturnStatement = false
|
||||||
|
walk.ancestor(ast, {
|
||||||
|
ReturnStatement(node, _state, ancestors) {
|
||||||
|
if (
|
||||||
|
// it returns a value
|
||||||
|
node.argument &&
|
||||||
|
// and it is top level
|
||||||
|
ancestors.length === 2 &&
|
||||||
|
ancestors[0].type === "Program" &&
|
||||||
|
ancestors[1].type === "ReturnStatement"
|
||||||
|
) {
|
||||||
|
hasReturnStatement = true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
CallExpression(node) {
|
||||||
|
const callee: any = node.callee
|
||||||
|
if (
|
||||||
|
node.type === "CallExpression" &&
|
||||||
|
callee.object?.name === "helpers" &&
|
||||||
|
node.loc
|
||||||
|
) {
|
||||||
|
const functionName = callee.property.name
|
||||||
|
const from =
|
||||||
|
lineOffsets[node.loc.start.line - 1] + node.loc.start.column
|
||||||
|
const to = lineOffsets[node.loc.end.line - 1] + node.loc.end.column
|
||||||
|
|
||||||
|
if (!(functionName in validations)) {
|
||||||
|
diagnostics.push({
|
||||||
|
from,
|
||||||
|
to,
|
||||||
|
severity: "warning",
|
||||||
|
message: `"${functionName}" function does not exist.`,
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const { arguments: expectedArguments } = validations[functionName]
|
||||||
|
if (
|
||||||
|
expectedArguments &&
|
||||||
|
node.arguments.length !== expectedArguments.length
|
||||||
|
) {
|
||||||
|
diagnostics.push({
|
||||||
|
from,
|
||||||
|
to,
|
||||||
|
severity: "error",
|
||||||
|
message: `Function "${functionName}" expects ${
|
||||||
|
expectedArguments.length
|
||||||
|
} parameters (${expectedArguments.join(", ")}), but got ${
|
||||||
|
node.arguments.length
|
||||||
|
}.`,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
if (!hasReturnStatement) {
|
||||||
|
diagnostics.push({
|
||||||
|
from: 0,
|
||||||
|
to: code.length,
|
||||||
|
severity: "error",
|
||||||
|
message: "Your code must return a value.",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
} catch (e: any) {
|
||||||
|
diagnostics.push({
|
||||||
|
from: 0,
|
||||||
|
to: code.length,
|
||||||
|
severity: "error",
|
||||||
|
message: `Syntax error: ${e.message}`,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return diagnostics
|
||||||
|
}
|
|
@ -0,0 +1,156 @@
|
||||||
|
import { validateJsTemplate } from "../js"
|
||||||
|
import { CodeValidator } from "@/types"
|
||||||
|
|
||||||
|
describe("js validator", () => {
|
||||||
|
it("validates valid code", () => {
|
||||||
|
const text = "return 7"
|
||||||
|
const validators = {}
|
||||||
|
|
||||||
|
const result = validateJsTemplate(text, validators)
|
||||||
|
expect(result).toEqual([])
|
||||||
|
})
|
||||||
|
|
||||||
|
it("does not validate runtime errors", () => {
|
||||||
|
const text = "return a"
|
||||||
|
const validators = {}
|
||||||
|
|
||||||
|
const result = validateJsTemplate(text, validators)
|
||||||
|
expect(result).toEqual([])
|
||||||
|
})
|
||||||
|
|
||||||
|
it("validates multiline code", () => {
|
||||||
|
const text = "const foo='bar'\nreturn 123"
|
||||||
|
const validators = {}
|
||||||
|
|
||||||
|
const result = validateJsTemplate(text, validators)
|
||||||
|
expect(result).toEqual([])
|
||||||
|
})
|
||||||
|
|
||||||
|
it("allows return not being on the last line", () => {
|
||||||
|
const text = "const foo='bar'\nreturn 123\nconsole.log(foo)"
|
||||||
|
const validators = {}
|
||||||
|
|
||||||
|
const result = validateJsTemplate(text, validators)
|
||||||
|
expect(result).toEqual([])
|
||||||
|
})
|
||||||
|
|
||||||
|
it("throws on missing return", () => {
|
||||||
|
const text = "const foo='bar'\nbar='foo'"
|
||||||
|
const validators = {}
|
||||||
|
|
||||||
|
const result = validateJsTemplate(text, validators)
|
||||||
|
expect(result).toEqual([
|
||||||
|
{
|
||||||
|
from: 0,
|
||||||
|
message: "Your code must return a value.",
|
||||||
|
severity: "error",
|
||||||
|
to: 25,
|
||||||
|
},
|
||||||
|
])
|
||||||
|
})
|
||||||
|
|
||||||
|
it("checks that returns are at top level", () => {
|
||||||
|
const text = `
|
||||||
|
function call(){
|
||||||
|
return 1
|
||||||
|
}`
|
||||||
|
const validators = {}
|
||||||
|
|
||||||
|
const result = validateJsTemplate(text, validators)
|
||||||
|
expect(result).toEqual([
|
||||||
|
{
|
||||||
|
from: 0,
|
||||||
|
message: "Your code must return a value.",
|
||||||
|
severity: "error",
|
||||||
|
to: text.length,
|
||||||
|
},
|
||||||
|
])
|
||||||
|
})
|
||||||
|
|
||||||
|
describe("helpers", () => {
|
||||||
|
const validators: CodeValidator = {
|
||||||
|
helperFunction: {
|
||||||
|
arguments: ["a", "b", "c"],
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
it("validates helpers with valid params", () => {
|
||||||
|
const text = "return helpers.helperFunction(1, 99, 'a')"
|
||||||
|
|
||||||
|
const result = validateJsTemplate(text, validators)
|
||||||
|
expect(result).toEqual([])
|
||||||
|
})
|
||||||
|
|
||||||
|
it("throws on too few params", () => {
|
||||||
|
const text = "return helpers.helperFunction(100)"
|
||||||
|
|
||||||
|
const result = validateJsTemplate(text, validators)
|
||||||
|
expect(result).toEqual([
|
||||||
|
{
|
||||||
|
from: 7,
|
||||||
|
message: `Function "helperFunction" expects 3 parameters (a, b, c), but got 1.`,
|
||||||
|
severity: "error",
|
||||||
|
to: 34,
|
||||||
|
},
|
||||||
|
])
|
||||||
|
})
|
||||||
|
|
||||||
|
it("throws on too many params", () => {
|
||||||
|
const text = "return helpers.helperFunction( 1, 99, 'a', 100)"
|
||||||
|
|
||||||
|
const result = validateJsTemplate(text, validators)
|
||||||
|
expect(result).toEqual([
|
||||||
|
{
|
||||||
|
from: 7,
|
||||||
|
message: `Function "helperFunction" expects 3 parameters (a, b, c), but got 4.`,
|
||||||
|
severity: "error",
|
||||||
|
to: 47,
|
||||||
|
},
|
||||||
|
])
|
||||||
|
})
|
||||||
|
|
||||||
|
it("validates helpers on inner functions", () => {
|
||||||
|
const text = `function call(){
|
||||||
|
return helpers.helperFunction(1, 99)
|
||||||
|
}
|
||||||
|
return call()`
|
||||||
|
|
||||||
|
const result = validateJsTemplate(text, validators)
|
||||||
|
expect(result).toEqual([
|
||||||
|
{
|
||||||
|
from: 46,
|
||||||
|
message: `Function "helperFunction" expects 3 parameters (a, b, c), but got 2.`,
|
||||||
|
severity: "error",
|
||||||
|
to: 75,
|
||||||
|
},
|
||||||
|
])
|
||||||
|
})
|
||||||
|
|
||||||
|
it("validates multiple helpers", () => {
|
||||||
|
const text =
|
||||||
|
"return helpers.helperFunction(1, 99, 'a') + helpers.helperFunction(1) + helpers.another(1) + helpers.another()"
|
||||||
|
const validators: CodeValidator = {
|
||||||
|
helperFunction: {
|
||||||
|
arguments: ["a", "b", "c"],
|
||||||
|
},
|
||||||
|
another: { arguments: [] },
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = validateJsTemplate(text, validators)
|
||||||
|
expect(result).toEqual([
|
||||||
|
{
|
||||||
|
from: 44,
|
||||||
|
message: `Function "helperFunction" expects 3 parameters (a, b, c), but got 1.`,
|
||||||
|
severity: "error",
|
||||||
|
to: 69,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
from: 72,
|
||||||
|
message: `Function "another" expects 0 parameters (), but got 1.`,
|
||||||
|
severity: "error",
|
||||||
|
to: 90,
|
||||||
|
},
|
||||||
|
])
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
|
@ -377,6 +377,7 @@
|
||||||
value={jsValue ? decodeJSBinding(jsValue) : ""}
|
value={jsValue ? decodeJSBinding(jsValue) : ""}
|
||||||
on:change={onChangeJSValue}
|
on:change={onChangeJSValue}
|
||||||
{completions}
|
{completions}
|
||||||
|
{validations}
|
||||||
mode={EditorModes.JS}
|
mode={EditorModes.JS}
|
||||||
bind:getCaretPosition
|
bind:getCaretPosition
|
||||||
bind:insertAtPos
|
bind:insertAtPos
|
||||||
|
|
|
@ -15,7 +15,6 @@ import {
|
||||||
import {
|
import {
|
||||||
AutomationTriggerStepId,
|
AutomationTriggerStepId,
|
||||||
AutomationEventType,
|
AutomationEventType,
|
||||||
AutomationStepType,
|
|
||||||
AutomationActionStepId,
|
AutomationActionStepId,
|
||||||
Automation,
|
Automation,
|
||||||
AutomationStep,
|
AutomationStep,
|
||||||
|
@ -26,20 +25,25 @@ import {
|
||||||
UILogicalOperator,
|
UILogicalOperator,
|
||||||
EmptyFilterOption,
|
EmptyFilterOption,
|
||||||
AutomationIOType,
|
AutomationIOType,
|
||||||
AutomationStepSchema,
|
|
||||||
AutomationTriggerSchema,
|
|
||||||
BranchPath,
|
BranchPath,
|
||||||
BlockDefinitions,
|
BlockDefinitions,
|
||||||
|
isBranchStep,
|
||||||
|
isTrigger,
|
||||||
|
isRowUpdateTrigger,
|
||||||
|
isRowSaveTrigger,
|
||||||
|
isAppTrigger,
|
||||||
|
BranchStep,
|
||||||
GetAutomationTriggerDefinitionsResponse,
|
GetAutomationTriggerDefinitionsResponse,
|
||||||
GetAutomationActionDefinitionsResponse,
|
GetAutomationActionDefinitionsResponse,
|
||||||
AppSelfResponse,
|
AppSelfResponse,
|
||||||
TestAutomationResponse,
|
TestAutomationResponse,
|
||||||
isAutomationResults,
|
isAutomationResults,
|
||||||
AutomationTestTrigger,
|
|
||||||
TriggerTestOutputs,
|
|
||||||
RowActionTriggerOutputs,
|
RowActionTriggerOutputs,
|
||||||
WebhookTriggerOutputs,
|
WebhookTriggerOutputs,
|
||||||
AutomationCustomIOType,
|
AutomationCustomIOType,
|
||||||
|
AutomationTriggerResultOutputs,
|
||||||
|
AutomationTriggerResult,
|
||||||
|
AutomationStepType,
|
||||||
} from "@budibase/types"
|
} from "@budibase/types"
|
||||||
import { ActionStepID, TriggerStepID } from "@/constants/backend/automations"
|
import { ActionStepID, TriggerStepID } from "@/constants/backend/automations"
|
||||||
import { FIELDS as COLUMNS } from "@/constants/backend"
|
import { FIELDS as COLUMNS } from "@/constants/backend"
|
||||||
|
@ -111,7 +115,7 @@ const automationActions = (store: AutomationStore) => ({
|
||||||
const results: TestAutomationResponse | undefined =
|
const results: TestAutomationResponse | undefined =
|
||||||
$selectedAutomation?.testResults
|
$selectedAutomation?.testResults
|
||||||
|
|
||||||
const testData: TriggerTestOutputs | undefined =
|
const testData: AutomationTriggerResultOutputs | undefined =
|
||||||
$selectedAutomation.data?.testData
|
$selectedAutomation.data?.testData
|
||||||
const triggerDef = $selectedAutomation.data?.definition?.trigger
|
const triggerDef = $selectedAutomation.data?.definition?.trigger
|
||||||
|
|
||||||
|
@ -122,13 +126,13 @@ const automationActions = (store: AutomationStore) => ({
|
||||||
? $tables.list.find(table => table._id === rowActionTableId)
|
? $tables.list.find(table => table._id === rowActionTableId)
|
||||||
: undefined
|
: undefined
|
||||||
|
|
||||||
let triggerData: TriggerTestOutputs | undefined
|
let triggerData: AutomationTriggerResultOutputs | undefined
|
||||||
|
|
||||||
if (results && isAutomationResults(results)) {
|
if (results && isAutomationResults(results)) {
|
||||||
const automationTrigger: AutomationTestTrigger | undefined =
|
const automationTrigger: AutomationTriggerResult | undefined =
|
||||||
results?.trigger
|
results?.trigger
|
||||||
|
|
||||||
const outputs: TriggerTestOutputs | undefined =
|
const outputs: AutomationTriggerResultOutputs | undefined =
|
||||||
automationTrigger?.outputs
|
automationTrigger?.outputs
|
||||||
triggerData = outputs ? outputs : undefined
|
triggerData = outputs ? outputs : undefined
|
||||||
|
|
||||||
|
@ -417,16 +421,16 @@ const automationActions = (store: AutomationStore) => ({
|
||||||
let result: (AutomationStep | AutomationTrigger)[] = []
|
let result: (AutomationStep | AutomationTrigger)[] = []
|
||||||
pathWay.forEach(path => {
|
pathWay.forEach(path => {
|
||||||
const { stepIdx, branchIdx } = path
|
const { stepIdx, branchIdx } = path
|
||||||
let last = result.length ? result[result.length - 1] : []
|
|
||||||
if (!result.length) {
|
if (!result.length) {
|
||||||
// Preceeding steps.
|
// Preceeding steps.
|
||||||
result = steps.slice(0, stepIdx + 1)
|
result = steps.slice(0, stepIdx + 1)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if (last && "inputs" in last) {
|
let last = result[result.length - 1]
|
||||||
|
if (isBranchStep(last)) {
|
||||||
if (Number.isInteger(branchIdx)) {
|
if (Number.isInteger(branchIdx)) {
|
||||||
const branchId = last.inputs.branches[branchIdx].id
|
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)
|
const stepChildren = children.slice(0, stepIdx + 1)
|
||||||
// Preceeding steps.
|
// Preceeding steps.
|
||||||
result = result.concat(stepChildren)
|
result = result.concat(stepChildren)
|
||||||
|
@ -599,23 +603,28 @@ const automationActions = (store: AutomationStore) => ({
|
||||||
id: block.id,
|
id: block.id,
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
const branches: Branch[] = block.inputs?.branches || []
|
|
||||||
|
|
||||||
branches.forEach((branch, bIdx) => {
|
if (isBranchStep(block)) {
|
||||||
block.inputs?.children[branch.id].forEach(
|
const branches = block.inputs?.branches || []
|
||||||
(bBlock: AutomationStep, sIdx: number, array: AutomationStep[]) => {
|
const children = block.inputs?.children || {}
|
||||||
const ended =
|
|
||||||
array.length - 1 === sIdx && !bBlock.inputs?.branches?.length
|
branches.forEach((branch, bIdx) => {
|
||||||
treeTraverse(bBlock, pathToCurrentNode, sIdx, bIdx, ended)
|
children[branch.id].forEach(
|
||||||
}
|
(bBlock: AutomationStep, sIdx: number, array: AutomationStep[]) => {
|
||||||
)
|
const ended = array.length - 1 === sIdx && !branches.length
|
||||||
})
|
treeTraverse(bBlock, pathToCurrentNode, sIdx, bIdx, ended)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
terminating = terminating && !branches.length
|
||||||
|
}
|
||||||
|
|
||||||
store.actions.registerBlock(
|
store.actions.registerBlock(
|
||||||
blockRefs,
|
blockRefs,
|
||||||
block,
|
block,
|
||||||
pathToCurrentNode,
|
pathToCurrentNode,
|
||||||
terminating && !branches.length
|
terminating
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -732,7 +741,6 @@ const automationActions = (store: AutomationStore) => ({
|
||||||
pathBlock.stepId === ActionStepID.LOOP &&
|
pathBlock.stepId === ActionStepID.LOOP &&
|
||||||
pathBlock.blockToLoop in blocks
|
pathBlock.blockToLoop in blocks
|
||||||
}
|
}
|
||||||
const isTrigger = pathBlock.type === AutomationStepType.TRIGGER
|
|
||||||
|
|
||||||
if (isLoopBlock && loopBlockCount == 0) {
|
if (isLoopBlock && loopBlockCount == 0) {
|
||||||
schema = {
|
schema = {
|
||||||
|
@ -743,17 +751,14 @@ const automationActions = (store: AutomationStore) => ({
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const icon = isTrigger
|
const icon = isTrigger(pathBlock)
|
||||||
? pathBlock.icon
|
? pathBlock.icon
|
||||||
: isLoopBlock
|
: isLoopBlock
|
||||||
? "Reuse"
|
? "Reuse"
|
||||||
: pathBlock.icon
|
: pathBlock.icon
|
||||||
|
|
||||||
if (blockIdx === 0 && isTrigger) {
|
if (blockIdx === 0 && isTrigger(pathBlock)) {
|
||||||
if (
|
if (isRowUpdateTrigger(pathBlock) || isRowSaveTrigger(pathBlock)) {
|
||||||
pathBlock.event === AutomationEventType.ROW_UPDATE ||
|
|
||||||
pathBlock.event === AutomationEventType.ROW_SAVE
|
|
||||||
) {
|
|
||||||
let table: any = get(tables).list.find(
|
let table: any = get(tables).list.find(
|
||||||
(table: Table) => table._id === pathBlock.inputs.tableId
|
(table: Table) => table._id === pathBlock.inputs.tableId
|
||||||
)
|
)
|
||||||
|
@ -765,7 +770,7 @@ const automationActions = (store: AutomationStore) => ({
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
delete schema.row
|
delete schema.row
|
||||||
} else if (pathBlock.event === AutomationEventType.APP_TRIGGER) {
|
} else if (isAppTrigger(pathBlock)) {
|
||||||
schema = Object.fromEntries(
|
schema = Object.fromEntries(
|
||||||
Object.keys(pathBlock.inputs.fields || []).map(key => [
|
Object.keys(pathBlock.inputs.fields || []).map(key => [
|
||||||
key,
|
key,
|
||||||
|
@ -1081,8 +1086,10 @@ const automationActions = (store: AutomationStore) => ({
|
||||||
]
|
]
|
||||||
|
|
||||||
let cache:
|
let cache:
|
||||||
| AutomationStepSchema<AutomationActionStepId>
|
| AutomationStep
|
||||||
| AutomationTriggerSchema<AutomationTriggerStepId>
|
| AutomationTrigger
|
||||||
|
| AutomationStep[]
|
||||||
|
| undefined = undefined
|
||||||
|
|
||||||
pathWay.forEach((path, pathIdx, array) => {
|
pathWay.forEach((path, pathIdx, array) => {
|
||||||
const { stepIdx, branchIdx } = path
|
const { stepIdx, branchIdx } = path
|
||||||
|
@ -1104,9 +1111,13 @@ const automationActions = (store: AutomationStore) => ({
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if (Number.isInteger(branchIdx)) {
|
if (
|
||||||
|
Number.isInteger(branchIdx) &&
|
||||||
|
!Array.isArray(cache) &&
|
||||||
|
isBranchStep(cache)
|
||||||
|
) {
|
||||||
const branchId = cache.inputs.branches[branchIdx].id
|
const branchId = cache.inputs.branches[branchIdx].id
|
||||||
const children = cache.inputs.children[branchId]
|
const children = cache.inputs.children?.[branchId] || []
|
||||||
|
|
||||||
if (final) {
|
if (final) {
|
||||||
insertBlock(children, stepIdx)
|
insertBlock(children, stepIdx)
|
||||||
|
@ -1256,7 +1267,7 @@ const automationActions = (store: AutomationStore) => ({
|
||||||
branchLeft: async (
|
branchLeft: async (
|
||||||
pathTo: Array<any>,
|
pathTo: Array<any>,
|
||||||
automation: Automation,
|
automation: Automation,
|
||||||
block: AutomationStep
|
block: BranchStep
|
||||||
) => {
|
) => {
|
||||||
const update = store.actions.shiftBranch(pathTo, block)
|
const update = store.actions.shiftBranch(pathTo, block)
|
||||||
if (update) {
|
if (update) {
|
||||||
|
@ -1279,7 +1290,7 @@ const automationActions = (store: AutomationStore) => ({
|
||||||
branchRight: async (
|
branchRight: async (
|
||||||
pathTo: Array<BranchPath>,
|
pathTo: Array<BranchPath>,
|
||||||
automation: Automation,
|
automation: Automation,
|
||||||
block: AutomationStep
|
block: BranchStep
|
||||||
) => {
|
) => {
|
||||||
const update = store.actions.shiftBranch(pathTo, block, 1)
|
const update = store.actions.shiftBranch(pathTo, block, 1)
|
||||||
if (update) {
|
if (update) {
|
||||||
|
@ -1299,7 +1310,7 @@ const automationActions = (store: AutomationStore) => ({
|
||||||
* @param {Number} direction - the direction of the swap. Defaults to -1 for left, add 1 for right
|
* @param {Number} direction - the direction of the swap. Defaults to -1 for left, add 1 for right
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
shiftBranch: (pathTo: Array<any>, block: AutomationStep, direction = -1) => {
|
shiftBranch: (pathTo: Array<any>, block: BranchStep, direction = -1) => {
|
||||||
let newBlock = cloneDeep(block)
|
let newBlock = cloneDeep(block)
|
||||||
const branchPath = pathTo.at(-1)
|
const branchPath = pathTo.at(-1)
|
||||||
const targetIdx = branchPath.branchIdx
|
const targetIdx = branchPath.branchIdx
|
||||||
|
@ -1595,7 +1606,7 @@ const automationActions = (store: AutomationStore) => ({
|
||||||
|
|
||||||
export interface AutomationContext {
|
export interface AutomationContext {
|
||||||
user: AppSelfResponse | null
|
user: AppSelfResponse | null
|
||||||
trigger?: TriggerTestOutputs
|
trigger?: AutomationTriggerResultOutputs
|
||||||
steps: Record<string, AutomationStep>
|
steps: Record<string, AutomationStep>
|
||||||
env: Record<string, any>
|
env: Record<string, any>
|
||||||
settings: Record<string, any>
|
settings: Record<string, any>
|
||||||
|
|
|
@ -8,6 +8,7 @@ import {
|
||||||
UIComponentError,
|
UIComponentError,
|
||||||
ComponentDefinition,
|
ComponentDefinition,
|
||||||
DependsOnComponentSetting,
|
DependsOnComponentSetting,
|
||||||
|
Screen,
|
||||||
} from "@budibase/types"
|
} from "@budibase/types"
|
||||||
import { queries } from "./queries"
|
import { queries } from "./queries"
|
||||||
import { views } from "./views"
|
import { views } from "./views"
|
||||||
|
@ -66,6 +67,7 @@ export const screenComponentErrorList = derived(
|
||||||
if (!$selectedScreen) {
|
if (!$selectedScreen) {
|
||||||
return []
|
return []
|
||||||
}
|
}
|
||||||
|
const screen = $selectedScreen
|
||||||
|
|
||||||
const datasources = {
|
const datasources = {
|
||||||
...reduceBy("_id", $tables.list),
|
...reduceBy("_id", $tables.list),
|
||||||
|
@ -79,7 +81,9 @@ export const screenComponentErrorList = derived(
|
||||||
const errors: UIComponentError[] = []
|
const errors: UIComponentError[] = []
|
||||||
|
|
||||||
function checkComponentErrors(component: Component, ancestors: string[]) {
|
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(...getMissingRequiredSettings(component, definitions))
|
||||||
errors.push(...getMissingAncestors(component, definitions, ancestors))
|
errors.push(...getMissingAncestors(component, definitions, ancestors))
|
||||||
|
|
||||||
|
@ -95,6 +99,7 @@ export const screenComponentErrorList = derived(
|
||||||
)
|
)
|
||||||
|
|
||||||
function getInvalidDatasources(
|
function getInvalidDatasources(
|
||||||
|
screen: Screen,
|
||||||
component: Component,
|
component: Component,
|
||||||
datasources: Record<string, any>,
|
datasources: Record<string, any>,
|
||||||
definitions: Record<string, ComponentDefinition>
|
definitions: Record<string, ComponentDefinition>
|
||||||
|
|
|
@ -14,18 +14,16 @@ import {
|
||||||
AutomationResults,
|
AutomationResults,
|
||||||
ConfigType,
|
ConfigType,
|
||||||
FieldType,
|
FieldType,
|
||||||
|
FilterCondition,
|
||||||
isDidNotTriggerResponse,
|
isDidNotTriggerResponse,
|
||||||
SettingsConfig,
|
SettingsConfig,
|
||||||
Table,
|
Table,
|
||||||
} from "@budibase/types"
|
} from "@budibase/types"
|
||||||
import { mocks } from "@budibase/backend-core/tests"
|
import { mocks } from "@budibase/backend-core/tests"
|
||||||
import { createAutomationBuilder } from "../../../automations/tests/utilities/AutomationTestBuilder"
|
import { createAutomationBuilder } from "../../../automations/tests/utilities/AutomationTestBuilder"
|
||||||
import { automations } from "@budibase/shared-core"
|
|
||||||
import { basicTable } from "../../../tests/utilities/structures"
|
import { basicTable } from "../../../tests/utilities/structures"
|
||||||
import TestConfiguration from "../../../tests/utilities/TestConfiguration"
|
import TestConfiguration from "../../../tests/utilities/TestConfiguration"
|
||||||
|
|
||||||
const FilterConditions = automations.steps.filter.FilterConditions
|
|
||||||
|
|
||||||
const MAX_RETRIES = 4
|
const MAX_RETRIES = 4
|
||||||
const {
|
const {
|
||||||
basicAutomation,
|
basicAutomation,
|
||||||
|
@ -483,15 +481,40 @@ describe("/automations", () => {
|
||||||
expect(events.automation.created).not.toHaveBeenCalled()
|
expect(events.automation.created).not.toHaveBeenCalled()
|
||||||
expect(events.automation.triggerUpdated).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", () => {
|
describe("fetch", () => {
|
||||||
it("return all the automations for an instance", async () => {
|
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(
|
const { automation: automation1 } = await config.api.automation.post(
|
||||||
newAutomation()
|
newAutomation()
|
||||||
)
|
)
|
||||||
|
@ -590,7 +613,7 @@ describe("/automations", () => {
|
||||||
steps: [
|
steps: [
|
||||||
{
|
{
|
||||||
inputs: {
|
inputs: {
|
||||||
condition: FilterConditions.EQUAL,
|
condition: FilterCondition.EQUAL,
|
||||||
field: "{{ trigger.row.City }}",
|
field: "{{ trigger.row.City }}",
|
||||||
value: "{{ trigger.oldRow.City }}",
|
value: "{{ trigger.oldRow.City }}",
|
||||||
},
|
},
|
||||||
|
|
|
@ -28,6 +28,8 @@ import {
|
||||||
Hosting,
|
Hosting,
|
||||||
ActionImplementation,
|
ActionImplementation,
|
||||||
AutomationStepDefinition,
|
AutomationStepDefinition,
|
||||||
|
AutomationStepInputs,
|
||||||
|
AutomationStepOutputs,
|
||||||
} from "@budibase/types"
|
} from "@budibase/types"
|
||||||
import sdk from "../sdk"
|
import sdk from "../sdk"
|
||||||
import { getAutomationPlugin } from "../utilities/fileSystem"
|
import { getAutomationPlugin } from "../utilities/fileSystem"
|
||||||
|
@ -123,11 +125,15 @@ export async function getActionDefinitions(): Promise<
|
||||||
}
|
}
|
||||||
|
|
||||||
/* istanbul ignore next */
|
/* istanbul ignore next */
|
||||||
export async function getAction(
|
export async function getAction<
|
||||||
stepId: AutomationActionStepId
|
TStep extends AutomationActionStepId,
|
||||||
): Promise<ActionImplementation<any, any> | undefined> {
|
TInputs = AutomationStepInputs<TStep>,
|
||||||
|
TOutputs = AutomationStepOutputs<TStep>
|
||||||
|
>(stepId: TStep): Promise<ActionImplementation<TInputs, TOutputs> | undefined> {
|
||||||
if (ACTION_IMPLS[stepId as keyof ActionImplType] != null) {
|
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
|
// must be a plugin
|
||||||
|
|
|
@ -6,10 +6,10 @@ import {
|
||||||
import sdk from "../sdk"
|
import sdk from "../sdk"
|
||||||
import {
|
import {
|
||||||
AutomationAttachment,
|
AutomationAttachment,
|
||||||
|
BaseIOStructure,
|
||||||
|
FieldSchema,
|
||||||
FieldType,
|
FieldType,
|
||||||
Row,
|
Row,
|
||||||
LoopStepType,
|
|
||||||
LoopStepInputs,
|
|
||||||
} from "@budibase/types"
|
} from "@budibase/types"
|
||||||
import { objectStore, context } from "@budibase/backend-core"
|
import { objectStore, context } from "@budibase/backend-core"
|
||||||
import * as uuid from "uuid"
|
import * as uuid from "uuid"
|
||||||
|
@ -32,33 +32,34 @@ import path from "path"
|
||||||
* primitive types.
|
* primitive types.
|
||||||
*/
|
*/
|
||||||
export function cleanInputValues<T extends Record<string, any>>(
|
export function cleanInputValues<T extends Record<string, any>>(
|
||||||
inputs: any,
|
inputs: T,
|
||||||
schema?: any
|
schema?: Partial<Record<keyof T, FieldSchema | BaseIOStructure>>
|
||||||
): T {
|
): T {
|
||||||
if (schema == null) {
|
const keys = Object.keys(inputs) as (keyof T)[]
|
||||||
return inputs
|
for (let inputKey of keys) {
|
||||||
}
|
|
||||||
for (let inputKey of Object.keys(inputs)) {
|
|
||||||
let input = inputs[inputKey]
|
let input = inputs[inputKey]
|
||||||
if (typeof input !== "string") {
|
if (typeof input !== "string") {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
let propSchema = schema.properties[inputKey]
|
let propSchema = schema?.[inputKey]
|
||||||
if (!propSchema) {
|
if (!propSchema) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if (propSchema.type === "boolean") {
|
if (propSchema.type === "boolean") {
|
||||||
let lcInput = input.toLowerCase()
|
let lcInput = input.toLowerCase()
|
||||||
if (lcInput === "true") {
|
if (lcInput === "true") {
|
||||||
|
// @ts-expect-error - indexing a generic on purpose
|
||||||
inputs[inputKey] = true
|
inputs[inputKey] = true
|
||||||
}
|
}
|
||||||
if (lcInput === "false") {
|
if (lcInput === "false") {
|
||||||
|
// @ts-expect-error - indexing a generic on purpose
|
||||||
inputs[inputKey] = false
|
inputs[inputKey] = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (propSchema.type === "number") {
|
if (propSchema.type === "number") {
|
||||||
let floatInput = parseFloat(input)
|
let floatInput = parseFloat(input)
|
||||||
if (!isNaN(floatInput)) {
|
if (!isNaN(floatInput)) {
|
||||||
|
// @ts-expect-error - indexing a generic on purpose
|
||||||
inputs[inputKey] = floatInput
|
inputs[inputKey] = floatInput
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -93,7 +94,7 @@ export function cleanInputValues<T extends Record<string, any>>(
|
||||||
*/
|
*/
|
||||||
export async function cleanUpRow(tableId: string, row: Row) {
|
export async function cleanUpRow(tableId: string, row: Row) {
|
||||||
let table = await sdk.tables.getTable(tableId)
|
let table = await sdk.tables.getTable(tableId)
|
||||||
return cleanInputValues(row, { properties: table.schema })
|
return cleanInputValues(row, table.schema)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getError(err: any) {
|
export function getError(err: any) {
|
||||||
|
@ -271,36 +272,3 @@ export function stringSplit(value: string | string[]) {
|
||||||
}
|
}
|
||||||
return value.split(",")
|
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
|
|
||||||
}
|
|
||||||
|
|
|
@ -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
|
|
||||||
}
|
|
|
@ -1,7 +1,8 @@
|
||||||
import { FilterStepInputs, FilterStepOutputs } from "@budibase/types"
|
import {
|
||||||
import { automations } from "@budibase/shared-core"
|
FilterCondition,
|
||||||
|
FilterStepInputs,
|
||||||
const FilterConditions = automations.steps.filter.FilterConditions
|
FilterStepOutputs,
|
||||||
|
} from "@budibase/types"
|
||||||
|
|
||||||
export async function run({
|
export async function run({
|
||||||
inputs,
|
inputs,
|
||||||
|
@ -26,16 +27,16 @@ export async function run({
|
||||||
let result = false
|
let result = false
|
||||||
if (typeof field !== "object" && typeof value !== "object") {
|
if (typeof field !== "object" && typeof value !== "object") {
|
||||||
switch (condition) {
|
switch (condition) {
|
||||||
case FilterConditions.EQUAL:
|
case FilterCondition.EQUAL:
|
||||||
result = field === value
|
result = field === value
|
||||||
break
|
break
|
||||||
case FilterConditions.NOT_EQUAL:
|
case FilterCondition.NOT_EQUAL:
|
||||||
result = field !== value
|
result = field !== value
|
||||||
break
|
break
|
||||||
case FilterConditions.GREATER_THAN:
|
case FilterCondition.GREATER_THAN:
|
||||||
result = field > value
|
result = field > value
|
||||||
break
|
break
|
||||||
case FilterConditions.LESS_THAN:
|
case FilterCondition.LESS_THAN:
|
||||||
result = field < value
|
result = field < value
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,5 @@
|
||||||
import {
|
import { AutomationIOType } from "@budibase/types"
|
||||||
typecastForLooping,
|
import { cleanInputValues, substituteLoopStep } from "../automationUtils"
|
||||||
cleanInputValues,
|
|
||||||
substituteLoopStep,
|
|
||||||
} from "../automationUtils"
|
|
||||||
import { LoopStepType } from "@budibase/types"
|
|
||||||
|
|
||||||
describe("automationUtils", () => {
|
describe("automationUtils", () => {
|
||||||
describe("substituteLoopStep", () => {
|
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", () => {
|
describe("cleanInputValues", () => {
|
||||||
it("should handle array relationship fields from read binding", () => {
|
it("should handle array relationship fields from read binding", () => {
|
||||||
const schema = {
|
const schema = {
|
||||||
|
@ -70,15 +43,12 @@ describe("automationUtils", () => {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
expect(
|
expect(
|
||||||
cleanInputValues(
|
cleanInputValues({
|
||||||
{
|
row: {
|
||||||
row: {
|
relationship: `[{"_id": "ro_ta_users_us_3"}]`,
|
||||||
relationship: `[{"_id": "ro_ta_users_us_3"}]`,
|
|
||||||
},
|
|
||||||
schema,
|
|
||||||
},
|
},
|
||||||
schema
|
schema,
|
||||||
)
|
})
|
||||||
).toEqual({
|
).toEqual({
|
||||||
row: {
|
row: {
|
||||||
relationship: [{ _id: "ro_ta_users_us_3" }],
|
relationship: [{ _id: "ro_ta_users_us_3" }],
|
||||||
|
@ -103,15 +73,12 @@ describe("automationUtils", () => {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
expect(
|
expect(
|
||||||
cleanInputValues(
|
cleanInputValues({
|
||||||
{
|
row: {
|
||||||
row: {
|
relationship: `ro_ta_users_us_3`,
|
||||||
relationship: `ro_ta_users_us_3`,
|
|
||||||
},
|
|
||||||
schema,
|
|
||||||
},
|
},
|
||||||
schema
|
schema,
|
||||||
)
|
})
|
||||||
).toEqual({
|
).toEqual({
|
||||||
row: {
|
row: {
|
||||||
relationship: "ro_ta_users_us_3",
|
relationship: "ro_ta_users_us_3",
|
||||||
|
@ -122,28 +89,27 @@ describe("automationUtils", () => {
|
||||||
|
|
||||||
it("should be able to clean inputs with the utilities", () => {
|
it("should be able to clean inputs with the utilities", () => {
|
||||||
// can't clean without a schema
|
// can't clean without a schema
|
||||||
let output = cleanInputValues({ a: "1" })
|
const one = cleanInputValues({ a: "1" })
|
||||||
expect(output.a).toBe("1")
|
expect(one.a).toBe("1")
|
||||||
output = cleanInputValues(
|
|
||||||
|
const two = cleanInputValues(
|
||||||
{ a: "1", b: "true", c: "false", d: 1, e: "help" },
|
{ a: "1", b: "true", c: "false", d: 1, e: "help" },
|
||||||
{
|
{
|
||||||
properties: {
|
a: {
|
||||||
a: {
|
type: AutomationIOType.NUMBER,
|
||||||
type: "number",
|
},
|
||||||
},
|
b: {
|
||||||
b: {
|
type: AutomationIOType.BOOLEAN,
|
||||||
type: "boolean",
|
},
|
||||||
},
|
c: {
|
||||||
c: {
|
type: AutomationIOType.BOOLEAN,
|
||||||
type: "boolean",
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
expect(output.a).toBe(1)
|
expect(two.a).toBe(1)
|
||||||
expect(output.b).toBe(true)
|
expect(two.b).toBe(true)
|
||||||
expect(output.c).toBe(false)
|
expect(two.c).toBe(false)
|
||||||
expect(output.d).toBe(1)
|
expect(two.d).toBe(1)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -1,5 +1,11 @@
|
||||||
import * as automation from "../index"
|
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 { createAutomationBuilder } from "./utilities/AutomationTestBuilder"
|
||||||
import {
|
import {
|
||||||
DatabaseName,
|
DatabaseName,
|
||||||
|
@ -7,12 +13,9 @@ import {
|
||||||
} from "../../integrations/tests/utils"
|
} from "../../integrations/tests/utils"
|
||||||
import { Knex } from "knex"
|
import { Knex } from "knex"
|
||||||
import { generator } from "@budibase/backend-core/tests"
|
import { generator } from "@budibase/backend-core/tests"
|
||||||
import { automations } from "@budibase/shared-core"
|
|
||||||
import TestConfiguration from "../../tests/utilities/TestConfiguration"
|
import TestConfiguration from "../../tests/utilities/TestConfiguration"
|
||||||
import { basicTable } from "../../tests/utilities/structures"
|
import { basicTable } from "../../tests/utilities/structures"
|
||||||
|
|
||||||
const FilterConditions = automations.steps.filter.FilterConditions
|
|
||||||
|
|
||||||
describe("Automation Scenarios", () => {
|
describe("Automation Scenarios", () => {
|
||||||
const config = new TestConfiguration()
|
const config = new TestConfiguration()
|
||||||
|
|
||||||
|
@ -256,7 +259,7 @@ describe("Automation Scenarios", () => {
|
||||||
})
|
})
|
||||||
.filter({
|
.filter({
|
||||||
field: "{{ steps.2.rows.0.value }}",
|
field: "{{ steps.2.rows.0.value }}",
|
||||||
condition: FilterConditions.EQUAL,
|
condition: FilterCondition.EQUAL,
|
||||||
value: 20,
|
value: 20,
|
||||||
})
|
})
|
||||||
.serverLog({ text: "Equal condition met" })
|
.serverLog({ text: "Equal condition met" })
|
||||||
|
@ -282,7 +285,7 @@ describe("Automation Scenarios", () => {
|
||||||
})
|
})
|
||||||
.filter({
|
.filter({
|
||||||
field: "{{ steps.2.rows.0.value }}",
|
field: "{{ steps.2.rows.0.value }}",
|
||||||
condition: FilterConditions.NOT_EQUAL,
|
condition: FilterCondition.NOT_EQUAL,
|
||||||
value: 20,
|
value: 20,
|
||||||
})
|
})
|
||||||
.serverLog({ text: "Not Equal condition met" })
|
.serverLog({ text: "Not Equal condition met" })
|
||||||
|
@ -295,37 +298,37 @@ describe("Automation Scenarios", () => {
|
||||||
|
|
||||||
const testCases = [
|
const testCases = [
|
||||||
{
|
{
|
||||||
condition: FilterConditions.EQUAL,
|
condition: FilterCondition.EQUAL,
|
||||||
value: 10,
|
value: 10,
|
||||||
rowValue: 10,
|
rowValue: 10,
|
||||||
expectPass: true,
|
expectPass: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
condition: FilterConditions.NOT_EQUAL,
|
condition: FilterCondition.NOT_EQUAL,
|
||||||
value: 10,
|
value: 10,
|
||||||
rowValue: 20,
|
rowValue: 20,
|
||||||
expectPass: true,
|
expectPass: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
condition: FilterConditions.GREATER_THAN,
|
condition: FilterCondition.GREATER_THAN,
|
||||||
value: 10,
|
value: 10,
|
||||||
rowValue: 15,
|
rowValue: 15,
|
||||||
expectPass: true,
|
expectPass: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
condition: FilterConditions.LESS_THAN,
|
condition: FilterCondition.LESS_THAN,
|
||||||
value: 10,
|
value: 10,
|
||||||
rowValue: 5,
|
rowValue: 5,
|
||||||
expectPass: true,
|
expectPass: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
condition: FilterConditions.GREATER_THAN,
|
condition: FilterCondition.GREATER_THAN,
|
||||||
value: 10,
|
value: 10,
|
||||||
rowValue: 5,
|
rowValue: 5,
|
||||||
expectPass: false,
|
expectPass: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
condition: FilterConditions.LESS_THAN,
|
condition: FilterCondition.LESS_THAN,
|
||||||
value: 10,
|
value: 10,
|
||||||
rowValue: 15,
|
rowValue: 15,
|
||||||
expectPass: false,
|
expectPass: false,
|
||||||
|
|
|
@ -4,7 +4,7 @@ import {
|
||||||
} from "../../../tests/utilities/structures"
|
} from "../../../tests/utilities/structures"
|
||||||
import { objectStore } from "@budibase/backend-core"
|
import { objectStore } from "@budibase/backend-core"
|
||||||
import { createAutomationBuilder } from "../utilities/AutomationTestBuilder"
|
import { createAutomationBuilder } from "../utilities/AutomationTestBuilder"
|
||||||
import { Row, Table } from "@budibase/types"
|
import { FilterCondition, Row, Table } from "@budibase/types"
|
||||||
import TestConfiguration from "../../../tests/utilities/TestConfiguration"
|
import TestConfiguration from "../../../tests/utilities/TestConfiguration"
|
||||||
|
|
||||||
async function uploadTestFile(filename: string) {
|
async function uploadTestFile(filename: string) {
|
||||||
|
@ -90,7 +90,7 @@ describe("test the create row action", () => {
|
||||||
.createRow({ row: {} }, { stepName: "CreateRow" })
|
.createRow({ row: {} }, { stepName: "CreateRow" })
|
||||||
.filter({
|
.filter({
|
||||||
field: "{{ stepsByName.CreateRow.success }}",
|
field: "{{ stepsByName.CreateRow.success }}",
|
||||||
condition: "equal",
|
condition: FilterCondition.EQUAL,
|
||||||
value: true,
|
value: true,
|
||||||
})
|
})
|
||||||
.serverLog(
|
.serverLog(
|
||||||
|
@ -131,7 +131,7 @@ describe("test the create row action", () => {
|
||||||
.createRow({ row: attachmentRow }, { stepName: "CreateRow" })
|
.createRow({ row: attachmentRow }, { stepName: "CreateRow" })
|
||||||
.filter({
|
.filter({
|
||||||
field: "{{ stepsByName.CreateRow.success }}",
|
field: "{{ stepsByName.CreateRow.success }}",
|
||||||
condition: "equal",
|
condition: FilterCondition.EQUAL,
|
||||||
value: true,
|
value: true,
|
||||||
})
|
})
|
||||||
.serverLog(
|
.serverLog(
|
||||||
|
|
|
@ -1,19 +1,21 @@
|
||||||
import { automations } from "@budibase/shared-core"
|
|
||||||
import { createAutomationBuilder } from "../utilities/AutomationTestBuilder"
|
import { createAutomationBuilder } from "../utilities/AutomationTestBuilder"
|
||||||
import TestConfiguration from "../../../tests/utilities/TestConfiguration"
|
import TestConfiguration from "../../../tests/utilities/TestConfiguration"
|
||||||
|
import { FilterCondition } from "@budibase/types"
|
||||||
|
|
||||||
const FilterConditions = automations.steps.filter.FilterConditions
|
function stringToFilterCondition(
|
||||||
|
condition: "==" | "!=" | ">" | "<"
|
||||||
function stringToFilterCondition(condition: "==" | "!=" | ">" | "<"): string {
|
): FilterCondition {
|
||||||
switch (condition) {
|
switch (condition) {
|
||||||
case "==":
|
case "==":
|
||||||
return FilterConditions.EQUAL
|
return FilterCondition.EQUAL
|
||||||
case "!=":
|
case "!=":
|
||||||
return FilterConditions.NOT_EQUAL
|
return FilterCondition.NOT_EQUAL
|
||||||
case ">":
|
case ">":
|
||||||
return FilterConditions.GREATER_THAN
|
return FilterCondition.GREATER_THAN
|
||||||
case "<":
|
case "<":
|
||||||
return FilterConditions.LESS_THAN
|
return FilterCondition.LESS_THAN
|
||||||
|
default:
|
||||||
|
throw new Error(`Unsupported condition: ${condition}`)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,8 +6,9 @@ import {
|
||||||
ServerLogStepOutputs,
|
ServerLogStepOutputs,
|
||||||
CreateRowStepOutputs,
|
CreateRowStepOutputs,
|
||||||
FieldType,
|
FieldType,
|
||||||
|
FilterCondition,
|
||||||
|
AutomationStepStatus,
|
||||||
} from "@budibase/types"
|
} from "@budibase/types"
|
||||||
import * as loopUtils from "../../loopUtils"
|
|
||||||
import { createAutomationBuilder } from "../utilities/AutomationTestBuilder"
|
import { createAutomationBuilder } from "../utilities/AutomationTestBuilder"
|
||||||
import TestConfiguration from "../../../tests/utilities/TestConfiguration"
|
import TestConfiguration from "../../../tests/utilities/TestConfiguration"
|
||||||
|
|
||||||
|
@ -30,8 +31,8 @@ describe("Attempt to run a basic loop automation", () => {
|
||||||
await config.api.row.save(table._id!, {})
|
await config.api.row.save(table._id!, {})
|
||||||
})
|
})
|
||||||
|
|
||||||
afterAll(() => {
|
afterAll(async () => {
|
||||||
automation.shutdown()
|
await automation.shutdown()
|
||||||
config.end()
|
config.end()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -535,97 +536,50 @@ describe("Attempt to run a basic loop automation", () => {
|
||||||
expect(results.steps[2].outputs.rows).toHaveLength(0)
|
expect(results.steps[2].outputs.rows).toHaveLength(0)
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("replaceFakeBindings", () => {
|
describe("loop output", () => {
|
||||||
it("should replace loop bindings in nested objects", () => {
|
it("should not output anything if a filter stops the automation", async () => {
|
||||||
const originalStepInput = {
|
const results = await createAutomationBuilder(config)
|
||||||
schema: {
|
.onAppAction()
|
||||||
name: {
|
.filter({
|
||||||
type: "string",
|
condition: FilterCondition.EQUAL,
|
||||||
constraints: {
|
field: "1",
|
||||||
type: "string",
|
value: "2",
|
||||||
length: { maximum: null },
|
})
|
||||||
presence: false,
|
.loop({
|
||||||
},
|
option: LoopStepType.ARRAY,
|
||||||
name: "name",
|
binding: [1, 2, 3],
|
||||||
display: { type: "Text" },
|
})
|
||||||
},
|
.serverLog({ text: "Message {{loop.currentItem}}" })
|
||||||
},
|
.test({ fields: {} })
|
||||||
row: {
|
|
||||||
tableId: "ta_aaad4296e9f74b12b1b90ef7a84afcad",
|
|
||||||
name: "{{ loop.currentItem.pokemon }}",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
const loopStepNumber = 3
|
expect(results.steps.length).toBe(1)
|
||||||
|
expect(results.steps[0].outputs).toEqual({
|
||||||
const result = loopUtils.replaceFakeBindings(
|
comparisonValue: 2,
|
||||||
originalStepInput,
|
refValue: 1,
|
||||||
loopStepNumber
|
result: false,
|
||||||
)
|
success: true,
|
||||||
|
status: "stopped",
|
||||||
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 }}",
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should handle null values in nested objects", () => {
|
it("should not fail if queryRows returns nothing", async () => {
|
||||||
const originalStepInput = {
|
const table = await config.api.table.save(basicTable())
|
||||||
nullValue: null,
|
const results = await createAutomationBuilder(config)
|
||||||
nestedNull: {
|
.onAppAction()
|
||||||
someKey: null,
|
.queryRows({
|
||||||
},
|
tableId: table._id!,
|
||||||
validValue: "{{ loop.someValue }}",
|
})
|
||||||
}
|
.loop({
|
||||||
|
option: LoopStepType.ARRAY,
|
||||||
|
binding: "{{ steps.1.rows }}",
|
||||||
|
})
|
||||||
|
.serverLog({ text: "Message {{loop.currentItem}}" })
|
||||||
|
.test({ fields: {} })
|
||||||
|
|
||||||
const loopStepNumber = 2
|
expect(results.steps[1].outputs.success).toBe(true)
|
||||||
|
expect(results.steps[1].outputs.status).toBe(
|
||||||
const result = loopUtils.replaceFakeBindings(
|
AutomationStepStatus.NO_ITERATIONS
|
||||||
originalStepInput,
|
|
||||||
loopStepNumber
|
|
||||||
)
|
)
|
||||||
|
|
||||||
expect(result).toEqual({
|
|
||||||
nullValue: null,
|
|
||||||
nestedNull: {
|
|
||||||
someKey: null,
|
|
||||||
},
|
|
||||||
validValue: "{{ steps.2.someValue }}",
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
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)
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -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)
|
const { automation } = await createAutomationBuilder(config)
|
||||||
.onCron({ cron: "* * * * *" })
|
.onCron({ cron: "* * * * *" })
|
||||||
.queryRows({
|
.queryRows({
|
||||||
|
|
|
@ -5,7 +5,7 @@ import TestConfiguration from "../../../tests/utilities/TestConfiguration"
|
||||||
|
|
||||||
mocks.licenses.useSyncAutomations()
|
mocks.licenses.useSyncAutomations()
|
||||||
|
|
||||||
describe("Branching automations", () => {
|
describe("Webhook trigger test", () => {
|
||||||
const config = new TestConfiguration()
|
const config = new TestConfiguration()
|
||||||
let table: Table
|
let table: Table
|
||||||
let webhook: Webhook
|
let webhook: Webhook
|
||||||
|
|
|
@ -64,6 +64,7 @@ class TriggerBuilder {
|
||||||
onRowDeleted = this.trigger(AutomationTriggerStepId.ROW_DELETED)
|
onRowDeleted = this.trigger(AutomationTriggerStepId.ROW_DELETED)
|
||||||
onWebhook = this.trigger(AutomationTriggerStepId.WEBHOOK)
|
onWebhook = this.trigger(AutomationTriggerStepId.WEBHOOK)
|
||||||
onCron = this.trigger(AutomationTriggerStepId.CRON)
|
onCron = this.trigger(AutomationTriggerStepId.CRON)
|
||||||
|
onRowAction = this.trigger(AutomationTriggerStepId.ROW_ACTION)
|
||||||
}
|
}
|
||||||
|
|
||||||
class BranchStepBuilder<TStep extends AutomationTriggerStepId> {
|
class BranchStepBuilder<TStep extends AutomationTriggerStepId> {
|
||||||
|
|
|
@ -184,11 +184,12 @@ export async function externalTrigger(
|
||||||
// values are likely to be submitted as strings, so we shall convert to correct type
|
// values are likely to be submitted as strings, so we shall convert to correct type
|
||||||
const coercedFields: any = {}
|
const coercedFields: any = {}
|
||||||
const fields = automation.definition.trigger.inputs.fields
|
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])
|
coercedFields[key] = coerce(params.fields[key], fields[key])
|
||||||
}
|
}
|
||||||
params.fields = coercedFields
|
params.fields = coercedFields
|
||||||
}
|
}
|
||||||
|
|
||||||
// row actions and webhooks flatten the fields down
|
// row actions and webhooks flatten the fields down
|
||||||
else if (
|
else if (
|
||||||
sdk.automations.isRowAction(automation) ||
|
sdk.automations.isRowAction(automation) ||
|
||||||
|
@ -200,6 +201,7 @@ export async function externalTrigger(
|
||||||
fields: {},
|
fields: {},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const data: AutomationData = { automation, event: params }
|
const data: AutomationData = { automation, event: params }
|
||||||
|
|
||||||
const shouldTrigger = await checkTriggerFilters(automation, {
|
const shouldTrigger = await checkTriggerFilters(automation, {
|
||||||
|
|
|
@ -9,6 +9,7 @@ import { Automation, AutomationJob, MetadataType } from "@budibase/types"
|
||||||
import { automationsEnabled } from "../features"
|
import { automationsEnabled } from "../features"
|
||||||
import { helpers, REBOOT_CRON } from "@budibase/shared-core"
|
import { helpers, REBOOT_CRON } from "@budibase/shared-core"
|
||||||
import tracer from "dd-trace"
|
import tracer from "dd-trace"
|
||||||
|
import { JobId } from "bull"
|
||||||
|
|
||||||
const CRON_STEP_ID = automations.triggers.definitions.CRON.stepId
|
const CRON_STEP_ID = automations.triggers.definitions.CRON.stepId
|
||||||
let Runner: Thread
|
let Runner: Thread
|
||||||
|
@ -30,39 +31,35 @@ function loggingArgs(job: AutomationJob) {
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function processEvent(job: AutomationJob) {
|
export async function processEvent(job: AutomationJob) {
|
||||||
return tracer.trace(
|
return tracer.trace("processEvent", async span => {
|
||||||
"processEvent",
|
const appId = job.data.event.appId!
|
||||||
{ resource: "automation" },
|
const automationId = job.data.automation._id!
|
||||||
async span => {
|
|
||||||
const appId = job.data.event.appId!
|
|
||||||
const automationId = job.data.automation._id!
|
|
||||||
|
|
||||||
span?.addTags({
|
span.addTags({
|
||||||
appId,
|
appId,
|
||||||
automationId,
|
automationId,
|
||||||
job: {
|
job: {
|
||||||
id: job.id,
|
id: job.id,
|
||||||
name: job.name,
|
name: job.name,
|
||||||
attemptsMade: job.attemptsMade,
|
attemptsMade: job.attemptsMade,
|
||||||
opts: {
|
attempts: job.opts.attempts,
|
||||||
attempts: job.opts.attempts,
|
priority: job.opts.priority,
|
||||||
priority: job.opts.priority,
|
delay: job.opts.delay,
|
||||||
delay: job.opts.delay,
|
repeat: job.opts.repeat,
|
||||||
repeat: job.opts.repeat,
|
backoff: job.opts.backoff,
|
||||||
backoff: job.opts.backoff,
|
lifo: job.opts.lifo,
|
||||||
lifo: job.opts.lifo,
|
timeout: job.opts.timeout,
|
||||||
timeout: job.opts.timeout,
|
jobId: job.opts.jobId,
|
||||||
jobId: job.opts.jobId,
|
removeOnComplete: job.opts.removeOnComplete,
|
||||||
removeOnComplete: job.opts.removeOnComplete,
|
removeOnFail: job.opts.removeOnFail,
|
||||||
removeOnFail: job.opts.removeOnFail,
|
stackTraceLimit: job.opts.stackTraceLimit,
|
||||||
stackTraceLimit: job.opts.stackTraceLimit,
|
preventParsingData: job.opts.preventParsingData,
|
||||||
preventParsingData: job.opts.preventParsingData,
|
},
|
||||||
},
|
})
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
const task = async () => {
|
const task = async () => {
|
||||||
try {
|
try {
|
||||||
|
return await tracer.trace("task", async () => {
|
||||||
if (isCronTrigger(job.data.automation) && !job.data.event.timestamp) {
|
if (isCronTrigger(job.data.automation) && !job.data.event.timestamp) {
|
||||||
// Requires the timestamp at run time
|
// Requires the timestamp at run time
|
||||||
job.data.event.timestamp = Date.now()
|
job.data.event.timestamp = Date.now()
|
||||||
|
@ -71,25 +68,19 @@ export async function processEvent(job: AutomationJob) {
|
||||||
console.log("automation running", ...loggingArgs(job))
|
console.log("automation running", ...loggingArgs(job))
|
||||||
|
|
||||||
const runFn = () => Runner.run(job)
|
const runFn = () => Runner.run(job)
|
||||||
const result = await quotas.addAutomation(runFn, {
|
const result = await quotas.addAutomation(runFn, { automationId })
|
||||||
automationId,
|
|
||||||
})
|
|
||||||
console.log("automation completed", ...loggingArgs(job))
|
console.log("automation completed", ...loggingArgs(job))
|
||||||
return result
|
return result
|
||||||
} catch (err) {
|
})
|
||||||
span?.addTags({ error: true })
|
} catch (err) {
|
||||||
console.error(
|
span.addTags({ error: true })
|
||||||
`automation was unable to run`,
|
console.error(`automation was unable to run`, err, ...loggingArgs(job))
|
||||||
err,
|
return { err }
|
||||||
...loggingArgs(job)
|
|
||||||
)
|
|
||||||
return { err }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return await context.doInAutomationContext({ appId, automationId, task })
|
|
||||||
}
|
}
|
||||||
)
|
|
||||||
|
return await context.doInAutomationContext({ appId, automationId, task })
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function updateTestHistory(
|
export async function updateTestHistory(
|
||||||
|
@ -129,11 +120,11 @@ export async function disableAllCrons(appId: any) {
|
||||||
return { count: results.length / 2 }
|
return { count: results.length / 2 }
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function disableCronById(jobId: number | string) {
|
export async function disableCronById(jobId: JobId) {
|
||||||
const repeatJobs = await automationQueue.getRepeatableJobs()
|
const jobs = await automationQueue.getRepeatableJobs()
|
||||||
for (let repeatJob of repeatJobs) {
|
for (const job of jobs) {
|
||||||
if (repeatJob.id === jobId) {
|
if (job.id === jobId) {
|
||||||
await automationQueue.removeRepeatableByKey(repeatJob.key)
|
await automationQueue.removeRepeatableByKey(job.key)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
console.log(`jobId=${jobId} disabled`)
|
console.log(`jobId=${jobId} disabled`)
|
||||||
|
@ -222,33 +213,3 @@ export async function enableCronTrigger(appId: any, automation: Automation) {
|
||||||
export async function cleanupAutomations(appId: any) {
|
export async function cleanupAutomations(appId: any) {
|
||||||
await disableAllCrons(appId)
|
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
|
|
||||||
}
|
|
||||||
|
|
|
@ -130,11 +130,6 @@ export enum InvalidColumns {
|
||||||
TABLE_ID = "tableId",
|
TABLE_ID = "tableId",
|
||||||
}
|
}
|
||||||
|
|
||||||
export enum AutomationErrors {
|
|
||||||
INCORRECT_TYPE = "INCORRECT_TYPE",
|
|
||||||
FAILURE_CONDITION = "FAILURE_CONDITION_MET",
|
|
||||||
}
|
|
||||||
|
|
||||||
// pass through the list from the auth/core lib
|
// pass through the list from the auth/core lib
|
||||||
export const ObjectStoreBuckets = objectStore.ObjectStoreBuckets
|
export const ObjectStoreBuckets = objectStore.ObjectStoreBuckets
|
||||||
export const MAX_AUTOMATION_RECURRING_ERRORS = 5
|
export const MAX_AUTOMATION_RECURRING_ERRORS = 5
|
||||||
|
|
|
@ -1,4 +1,9 @@
|
||||||
import { AutomationResults, LoopStepType, UserBindings } from "@budibase/types"
|
import {
|
||||||
|
AutomationStepResultOutputs,
|
||||||
|
AutomationTriggerResultOutputs,
|
||||||
|
LoopStepType,
|
||||||
|
UserBindings,
|
||||||
|
} from "@budibase/types"
|
||||||
|
|
||||||
export interface LoopInput {
|
export interface LoopInput {
|
||||||
option: LoopStepType
|
option: LoopStepType
|
||||||
|
@ -13,19 +18,17 @@ export interface TriggerOutput {
|
||||||
timestamp?: number
|
timestamp?: number
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface AutomationContext extends AutomationResults {
|
export interface AutomationContext {
|
||||||
steps: any[]
|
trigger: AutomationTriggerResultOutputs
|
||||||
stepsById: Record<string, any>
|
steps: [AutomationTriggerResultOutputs, ...AutomationStepResultOutputs[]]
|
||||||
stepsByName: Record<string, any>
|
stepsById: Record<string, AutomationStepResultOutputs>
|
||||||
|
stepsByName: Record<string, AutomationStepResultOutputs>
|
||||||
env?: Record<string, string>
|
env?: Record<string, string>
|
||||||
user?: UserBindings
|
user?: UserBindings
|
||||||
trigger: any
|
|
||||||
settings?: {
|
settings?: {
|
||||||
url?: string
|
url?: string
|
||||||
logo?: string
|
logo?: string
|
||||||
company?: string
|
company?: string
|
||||||
}
|
}
|
||||||
|
loop?: { currentItem: any }
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface AutomationResponse
|
|
||||||
extends Omit<AutomationContext, "stepsByName" | "stepsById"> {}
|
|
||||||
|
|
|
@ -62,12 +62,16 @@ const SCHEMA: Integration = {
|
||||||
type: DatasourceFieldType.STRING,
|
type: DatasourceFieldType.STRING,
|
||||||
required: true,
|
required: true,
|
||||||
},
|
},
|
||||||
|
rev: {
|
||||||
|
type: DatasourceFieldType.STRING,
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
class CouchDBIntegration implements IntegrationBase {
|
export class CouchDBIntegration implements IntegrationBase {
|
||||||
private readonly client: Database
|
private readonly client: Database
|
||||||
|
|
||||||
constructor(config: CouchDBConfig) {
|
constructor(config: CouchDBConfig) {
|
||||||
|
@ -82,7 +86,8 @@ class CouchDBIntegration implements IntegrationBase {
|
||||||
connected: false,
|
connected: false,
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
response.connected = await this.client.exists()
|
await this.client.allDocs({ limit: 1 })
|
||||||
|
response.connected = true
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
response.error = e.message as string
|
response.error = e.message as string
|
||||||
}
|
}
|
||||||
|
@ -99,13 +104,9 @@ class CouchDBIntegration implements IntegrationBase {
|
||||||
}
|
}
|
||||||
|
|
||||||
async read(query: { json: string | object }) {
|
async read(query: { json: string | object }) {
|
||||||
const parsed = this.parse(query)
|
const params = { include_docs: true, ...this.parse(query) }
|
||||||
const params = {
|
|
||||||
include_docs: true,
|
|
||||||
...parsed,
|
|
||||||
}
|
|
||||||
const result = await this.client.allDocs(params)
|
const result = await this.client.allDocs(params)
|
||||||
return result.rows.map(row => row.doc)
|
return result.rows.map(row => row.doc!)
|
||||||
}
|
}
|
||||||
|
|
||||||
async update(query: { json: string | object }) {
|
async update(query: { json: string | object }) {
|
||||||
|
@ -121,8 +122,8 @@ class CouchDBIntegration implements IntegrationBase {
|
||||||
return await this.client.get(query.id)
|
return await this.client.get(query.id)
|
||||||
}
|
}
|
||||||
|
|
||||||
async delete(query: { id: string }) {
|
async delete(query: { id: string; rev: string }) {
|
||||||
return await this.client.remove(query.id)
|
return await this.client.remove(query.id, query.rev)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,84 +1,87 @@
|
||||||
jest.mock("@budibase/backend-core", () => {
|
import { env } from "@budibase/backend-core"
|
||||||
const core = jest.requireActual("@budibase/backend-core")
|
import { CouchDBIntegration } from "../couchdb"
|
||||||
return {
|
import { generator } from "@budibase/backend-core/tests"
|
||||||
...core,
|
|
||||||
db: {
|
|
||||||
...core.db,
|
|
||||||
DatabaseWithConnection: function () {
|
|
||||||
return {
|
|
||||||
allDocs: jest.fn().mockReturnValue({ rows: [] }),
|
|
||||||
put: jest.fn(),
|
|
||||||
get: jest.fn().mockReturnValue({ _rev: "a" }),
|
|
||||||
remove: jest.fn(),
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
import { default as CouchDBIntegration } from "../couchdb"
|
function couchSafeID(): string {
|
||||||
|
// CouchDB IDs must start with a letter, so we prepend an 'a'.
|
||||||
|
return `a${generator.guid()}`
|
||||||
|
}
|
||||||
|
|
||||||
class TestConfiguration {
|
function doc(data: Record<string, any>): string {
|
||||||
integration: any
|
return JSON.stringify({ _id: couchSafeID(), ...data })
|
||||||
|
}
|
||||||
|
|
||||||
constructor(
|
function query(data?: Record<string, any>): { json: string } {
|
||||||
config: any = { url: "http://somewhere", database: "something" }
|
return { json: doc(data || {}) }
|
||||||
) {
|
|
||||||
this.integration = new CouchDBIntegration.integration(config)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
describe("CouchDB Integration", () => {
|
describe("CouchDB Integration", () => {
|
||||||
let config: any
|
let couchdb: CouchDBIntegration
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
config = new TestConfiguration()
|
couchdb = new CouchDBIntegration({
|
||||||
})
|
url: env.COUCH_DB_URL,
|
||||||
|
database: couchSafeID(),
|
||||||
it("calls the create method with the correct params", async () => {
|
|
||||||
const doc = {
|
|
||||||
test: 1,
|
|
||||||
}
|
|
||||||
await config.integration.create({
|
|
||||||
json: JSON.stringify(doc),
|
|
||||||
})
|
|
||||||
expect(config.integration.client.put).toHaveBeenCalledWith(doc)
|
|
||||||
})
|
|
||||||
|
|
||||||
it("calls the read method with the correct params", async () => {
|
|
||||||
const doc = {
|
|
||||||
name: "search",
|
|
||||||
}
|
|
||||||
|
|
||||||
await config.integration.read({
|
|
||||||
json: JSON.stringify(doc),
|
|
||||||
})
|
|
||||||
|
|
||||||
expect(config.integration.client.allDocs).toHaveBeenCalledWith({
|
|
||||||
include_docs: true,
|
|
||||||
name: "search",
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it("calls the update method with the correct params", async () => {
|
it("successfully connects", async () => {
|
||||||
const doc = {
|
const { connected } = await couchdb.testConnection()
|
||||||
_id: "1234",
|
expect(connected).toBe(true)
|
||||||
name: "search",
|
|
||||||
}
|
|
||||||
|
|
||||||
await config.integration.update({
|
|
||||||
json: JSON.stringify(doc),
|
|
||||||
})
|
|
||||||
|
|
||||||
expect(config.integration.client.put).toHaveBeenCalledWith({
|
|
||||||
...doc,
|
|
||||||
_rev: "a",
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it("calls the delete method with the correct params", async () => {
|
it("can create documents", async () => {
|
||||||
const id = "1234"
|
const { id, ok, rev } = await couchdb.create(query({ test: 1 }))
|
||||||
await config.integration.delete({ id })
|
expect(id).toBeDefined()
|
||||||
expect(config.integration.client.remove).toHaveBeenCalledWith(id)
|
expect(ok).toBe(true)
|
||||||
|
expect(rev).toBeDefined()
|
||||||
|
})
|
||||||
|
|
||||||
|
it("can read created documents", async () => {
|
||||||
|
const { id, ok, rev } = await couchdb.create(query({ test: 1 }))
|
||||||
|
expect(id).toBeDefined()
|
||||||
|
expect(ok).toBe(true)
|
||||||
|
expect(rev).toBeDefined()
|
||||||
|
|
||||||
|
const docs = await couchdb.read(query())
|
||||||
|
expect(docs).toEqual([
|
||||||
|
{
|
||||||
|
_id: id,
|
||||||
|
_rev: rev,
|
||||||
|
test: 1,
|
||||||
|
createdAt: expect.any(String),
|
||||||
|
updatedAt: expect.any(String),
|
||||||
|
},
|
||||||
|
])
|
||||||
|
})
|
||||||
|
|
||||||
|
it("can update documents", async () => {
|
||||||
|
const { id, ok, rev } = await couchdb.create(query({ test: 1 }))
|
||||||
|
expect(ok).toBe(true)
|
||||||
|
|
||||||
|
const { id: newId, rev: newRev } = await couchdb.update(
|
||||||
|
query({ _id: id, _rev: rev, test: 2 })
|
||||||
|
)
|
||||||
|
const docs = await couchdb.read(query())
|
||||||
|
expect(docs).toEqual([
|
||||||
|
{
|
||||||
|
_id: newId,
|
||||||
|
_rev: newRev,
|
||||||
|
test: 2,
|
||||||
|
createdAt: expect.any(String),
|
||||||
|
updatedAt: expect.any(String),
|
||||||
|
},
|
||||||
|
])
|
||||||
|
})
|
||||||
|
|
||||||
|
it("can delete documents", async () => {
|
||||||
|
const { id, ok, rev } = await couchdb.create(query({ test: 1 }))
|
||||||
|
expect(ok).toBe(true)
|
||||||
|
|
||||||
|
const deleteResponse = await couchdb.delete({ id, rev })
|
||||||
|
expect(deleteResponse.ok).toBe(true)
|
||||||
|
|
||||||
|
const docs = await couchdb.read(query())
|
||||||
|
expect(docs).toBeEmpty()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -40,7 +40,8 @@ function cleanAutomationInputs(automation: Automation) {
|
||||||
if (step == null) {
|
if (step == null) {
|
||||||
continue
|
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] === "") {
|
if (!step.inputs[inputName] || step.inputs[inputName] === "") {
|
||||||
delete step.inputs[inputName]
|
delete step.inputs[inputName]
|
||||||
}
|
}
|
||||||
|
@ -281,7 +282,8 @@ function guardInvalidUpdatesAndThrow(
|
||||||
const readonlyFields = Object.keys(
|
const readonlyFields = Object.keys(
|
||||||
step.schema.inputs.properties || {}
|
step.schema.inputs.properties || {}
|
||||||
).filter(k => step.schema.inputs.properties[k].readonly)
|
).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)
|
const oldStep = oldStepDefinitions.find(i => i.id === step.id)
|
||||||
if (step.inputs[readonlyField] !== oldStep?.inputs[readonlyField]) {
|
if (step.inputs[readonlyField] !== oldStep?.inputs[readonlyField]) {
|
||||||
throw new HTTPError(
|
throw new HTTPError(
|
||||||
|
|
|
@ -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"
|
|
||||||
)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
|
@ -35,6 +35,8 @@ import {
|
||||||
WebhookActionType,
|
WebhookActionType,
|
||||||
BuiltinPermissionID,
|
BuiltinPermissionID,
|
||||||
DeepPartial,
|
DeepPartial,
|
||||||
|
FilterCondition,
|
||||||
|
AutomationTriggerResult,
|
||||||
} from "@budibase/types"
|
} from "@budibase/types"
|
||||||
import { LoopInput } from "../../definitions/automations"
|
import { LoopInput } from "../../definitions/automations"
|
||||||
import { merge } from "lodash"
|
import { merge } from "lodash"
|
||||||
|
@ -372,7 +374,11 @@ export function filterAutomation(opts?: DeepPartial<Automation>): Automation {
|
||||||
type: AutomationStepType.ACTION,
|
type: AutomationStepType.ACTION,
|
||||||
internal: true,
|
internal: true,
|
||||||
stepId: AutomationActionStepId.FILTER,
|
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,
|
schema: BUILTIN_ACTION_DEFINITIONS.EXECUTE_SCRIPT.schema,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
@ -437,15 +443,24 @@ export function updateRowAutomationWithFilters(
|
||||||
export function basicAutomationResults(
|
export function basicAutomationResults(
|
||||||
automationId: string
|
automationId: string
|
||||||
): AutomationResults {
|
): AutomationResults {
|
||||||
|
const trigger: AutomationTriggerResult = {
|
||||||
|
id: "trigger",
|
||||||
|
stepId: AutomationTriggerStepId.APP,
|
||||||
|
outputs: {},
|
||||||
|
}
|
||||||
return {
|
return {
|
||||||
automationId,
|
automationId,
|
||||||
status: AutomationStatus.SUCCESS,
|
status: AutomationStatus.SUCCESS,
|
||||||
trigger: "trigger" as any,
|
trigger,
|
||||||
steps: [
|
steps: [
|
||||||
|
trigger,
|
||||||
{
|
{
|
||||||
|
id: "step1",
|
||||||
stepId: AutomationActionStepId.SERVER_LOG,
|
stepId: AutomationActionStepId.SERVER_LOG,
|
||||||
inputs: {},
|
inputs: {},
|
||||||
outputs: {},
|
outputs: {
|
||||||
|
success: true,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -3,20 +3,14 @@ import {
|
||||||
AutomationStepDefinition,
|
AutomationStepDefinition,
|
||||||
AutomationStepType,
|
AutomationStepType,
|
||||||
AutomationIOType,
|
AutomationIOType,
|
||||||
|
FilterCondition,
|
||||||
} from "@budibase/types"
|
} from "@budibase/types"
|
||||||
|
|
||||||
export const FilterConditions = {
|
|
||||||
EQUAL: "EQUAL",
|
|
||||||
NOT_EQUAL: "NOT_EQUAL",
|
|
||||||
GREATER_THAN: "GREATER_THAN",
|
|
||||||
LESS_THAN: "LESS_THAN",
|
|
||||||
}
|
|
||||||
|
|
||||||
export const PrettyFilterConditions = {
|
export const PrettyFilterConditions = {
|
||||||
[FilterConditions.EQUAL]: "Equals",
|
[FilterCondition.EQUAL]: "Equals",
|
||||||
[FilterConditions.NOT_EQUAL]: "Not equals",
|
[FilterCondition.NOT_EQUAL]: "Not equals",
|
||||||
[FilterConditions.GREATER_THAN]: "Greater than",
|
[FilterCondition.GREATER_THAN]: "Greater than",
|
||||||
[FilterConditions.LESS_THAN]: "Less than",
|
[FilterCondition.LESS_THAN]: "Less than",
|
||||||
}
|
}
|
||||||
|
|
||||||
export const definition: AutomationStepDefinition = {
|
export const definition: AutomationStepDefinition = {
|
||||||
|
@ -30,7 +24,7 @@ export const definition: AutomationStepDefinition = {
|
||||||
features: {},
|
features: {},
|
||||||
stepId: AutomationActionStepId.FILTER,
|
stepId: AutomationActionStepId.FILTER,
|
||||||
inputs: {
|
inputs: {
|
||||||
condition: FilterConditions.EQUAL,
|
condition: FilterCondition.EQUAL,
|
||||||
},
|
},
|
||||||
schema: {
|
schema: {
|
||||||
inputs: {
|
inputs: {
|
||||||
|
@ -42,7 +36,7 @@ export const definition: AutomationStepDefinition = {
|
||||||
condition: {
|
condition: {
|
||||||
type: AutomationIOType.STRING,
|
type: AutomationIOType.STRING,
|
||||||
title: "Condition",
|
title: "Condition",
|
||||||
enum: Object.values(FilterConditions),
|
enum: Object.values(FilterCondition),
|
||||||
pretty: Object.values(PrettyFilterConditions),
|
pretty: Object.values(PrettyFilterConditions),
|
||||||
},
|
},
|
||||||
value: {
|
value: {
|
||||||
|
|
|
@ -105,10 +105,10 @@ export function cancelableTimeout(
|
||||||
|
|
||||||
export async function withTimeout<T>(
|
export async function withTimeout<T>(
|
||||||
timeout: number,
|
timeout: number,
|
||||||
promise: Promise<T>
|
promise: () => Promise<T>
|
||||||
): Promise<T> {
|
): Promise<T> {
|
||||||
const [timeoutPromise, cancel] = cancelableTimeout(timeout)
|
const [timeoutPromise, cancel] = cancelableTimeout(timeout)
|
||||||
const result = (await Promise.race([promise, timeoutPromise])) as T
|
const result = (await Promise.race([promise(), timeoutPromise])) as T
|
||||||
cancel()
|
cancel()
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,8 +8,20 @@ import {
|
||||||
} from "../../../sdk"
|
} from "../../../sdk"
|
||||||
import { HttpMethod } from "../query"
|
import { HttpMethod } from "../query"
|
||||||
import { Row } from "../row"
|
import { Row } from "../row"
|
||||||
import { LoopStepType, EmailAttachment, AutomationResults } from "./automation"
|
import {
|
||||||
import { AutomationStep, AutomationStepOutputs } from "./schema"
|
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 = {
|
export type BaseAutomationOutputs = {
|
||||||
success?: boolean
|
success?: boolean
|
||||||
|
@ -93,7 +105,7 @@ export type ExecuteScriptStepOutputs = BaseAutomationOutputs & {
|
||||||
|
|
||||||
export type FilterStepInputs = {
|
export type FilterStepInputs = {
|
||||||
field: any
|
field: any
|
||||||
condition: string
|
condition: FilterCondition
|
||||||
value: any
|
value: any
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -111,7 +123,7 @@ export type LoopStepInputs = {
|
||||||
}
|
}
|
||||||
|
|
||||||
export type LoopStepOutputs = {
|
export type LoopStepOutputs = {
|
||||||
items: AutomationStepOutputs[]
|
items: AutomationStepResult[]
|
||||||
success: boolean
|
success: boolean
|
||||||
iterations: number
|
iterations: number
|
||||||
}
|
}
|
||||||
|
|
|
@ -146,10 +146,10 @@ export interface Automation extends Document {
|
||||||
internal?: boolean
|
internal?: boolean
|
||||||
type?: string
|
type?: string
|
||||||
disabled?: boolean
|
disabled?: boolean
|
||||||
testData?: TriggerTestOutputs
|
testData?: AutomationTriggerResultOutputs
|
||||||
}
|
}
|
||||||
|
|
||||||
interface BaseIOStructure {
|
export interface BaseIOStructure {
|
||||||
type?: AutomationIOType
|
type?: AutomationIOType
|
||||||
subtype?: AutomationIOType
|
subtype?: AutomationIOType
|
||||||
customType?: AutomationCustomIOType
|
customType?: AutomationCustomIOType
|
||||||
|
@ -179,6 +179,8 @@ export enum AutomationFeature {
|
||||||
export enum AutomationStepStatus {
|
export enum AutomationStepStatus {
|
||||||
NO_ITERATIONS = "no_iterations",
|
NO_ITERATIONS = "no_iterations",
|
||||||
MAX_ITERATIONS = "max_iterations_reached",
|
MAX_ITERATIONS = "max_iterations_reached",
|
||||||
|
FAILURE_CONDITION = "FAILURE_CONDITION_MET",
|
||||||
|
INCORRECT_TYPE = "INCORRECT_TYPE",
|
||||||
}
|
}
|
||||||
|
|
||||||
export enum AutomationStatus {
|
export enum AutomationStatus {
|
||||||
|
@ -193,45 +195,37 @@ export enum AutomationStoppedReason {
|
||||||
TRIGGER_FILTER_NOT_MET = "Automation did not run. Filter conditions in trigger were not met.",
|
TRIGGER_FILTER_NOT_MET = "Automation did not run. Filter conditions in trigger were not met.",
|
||||||
}
|
}
|
||||||
|
|
||||||
// UI and context fields
|
export interface AutomationStepResultOutputs {
|
||||||
export type AutomationResultFields = {
|
success: boolean
|
||||||
meta?: {
|
[key: string]: any
|
||||||
[key: string]: unknown
|
|
||||||
}
|
|
||||||
user?: AppSelfResponse
|
|
||||||
automation?: Automation
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Base types for Trigger outputs combined with UI and context fields
|
export interface AutomationStepResultInputs {
|
||||||
export type TriggerTestOutputs =
|
[key: string]: any
|
||||||
| (WebhookTriggerOutputs & AutomationResultFields)
|
}
|
||||||
| (AppActionTriggerOutputs & AutomationResultFields)
|
|
||||||
| (CronTriggerOutputs & AutomationResultFields)
|
|
||||||
| (RowActionTriggerOutputs & AutomationResultFields)
|
|
||||||
| (RowDeletedTriggerInputs & AutomationResultFields)
|
|
||||||
| (RowCreatedTriggerInputs & AutomationResultFields)
|
|
||||||
| (RowUpdatedTriggerOutputs & AutomationResultFields)
|
|
||||||
|
|
||||||
export type AutomationTestTrigger = {
|
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
|
id: string
|
||||||
inputs: null
|
|
||||||
stepId: AutomationTriggerStepId
|
stepId: AutomationTriggerStepId
|
||||||
outputs: TriggerTestOutputs
|
inputs?: AutomationTriggerResultInputs | null
|
||||||
|
outputs: AutomationTriggerResultOutputs
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface AutomationResults {
|
export interface AutomationResults {
|
||||||
automationId?: string
|
automationId?: string
|
||||||
status?: AutomationStatus
|
status?: AutomationStatus
|
||||||
trigger?: AutomationTestTrigger
|
trigger: AutomationTriggerResult
|
||||||
steps: {
|
steps: [AutomationTriggerResult, ...AutomationStepResult[]]
|
||||||
stepId: AutomationTriggerStepId | AutomationActionStepId
|
|
||||||
inputs: {
|
|
||||||
[key: string]: any
|
|
||||||
}
|
|
||||||
outputs: {
|
|
||||||
[key: string]: any
|
|
||||||
}
|
|
||||||
}[]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface DidNotTriggerResponse {
|
export interface DidNotTriggerResponse {
|
||||||
|
@ -265,6 +259,7 @@ export type ActionImplementation<TInputs, TOutputs> = (
|
||||||
inputs: TInputs
|
inputs: TInputs
|
||||||
} & AutomationStepInputBase
|
} & AutomationStepInputBase
|
||||||
) => Promise<TOutputs>
|
) => Promise<TOutputs>
|
||||||
|
|
||||||
export interface AutomationMetadata extends Document {
|
export interface AutomationMetadata extends Document {
|
||||||
errorCount?: number
|
errorCount?: number
|
||||||
automationChainCount?: number
|
automationChainCount?: number
|
||||||
|
|
|
@ -169,24 +169,6 @@ export interface AutomationStepSchemaBase {
|
||||||
features?: Partial<Record<AutomationFeature, boolean>>
|
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> =
|
export type AutomationStepInputs<T extends AutomationActionStepId> =
|
||||||
T extends AutomationActionStepId.COLLECT
|
T extends AutomationActionStepId.COLLECT
|
||||||
? CollectStepInputs
|
? CollectStepInputs
|
||||||
|
@ -236,11 +218,56 @@ export type AutomationStepInputs<T extends AutomationActionStepId> =
|
||||||
? BranchStepInputs
|
? BranchStepInputs
|
||||||
: never
|
: 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>
|
export interface AutomationStepSchema<TStep extends AutomationActionStepId>
|
||||||
extends AutomationStepSchemaBase {
|
extends AutomationStepSchemaBase {
|
||||||
id: string
|
id: string
|
||||||
stepId: TStep
|
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>
|
export type CollectStep = AutomationStepSchema<AutomationActionStepId.COLLECT>
|
||||||
|
@ -326,6 +353,36 @@ export type AutomationStep =
|
||||||
| OpenAIStep
|
| OpenAIStep
|
||||||
| BranchStep
|
| 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 = {}
|
type EmptyInputs = {}
|
||||||
export type AutomationStepDefinition = Omit<AutomationStep, "id" | "inputs"> & {
|
export type AutomationStepDefinition = Omit<AutomationStep, "id" | "inputs"> & {
|
||||||
inputs: EmptyInputs
|
inputs: EmptyInputs
|
||||||
|
|
|
@ -82,6 +82,10 @@ type RangeFilter = Record<
|
||||||
|
|
||||||
type LogicalFilter = { conditions: SearchFilters[] }
|
type LogicalFilter = { conditions: SearchFilters[] }
|
||||||
|
|
||||||
|
export function isLogicalFilter(filter: any): filter is LogicalFilter {
|
||||||
|
return "conditions" in filter
|
||||||
|
}
|
||||||
|
|
||||||
export type AnySearchFilter = BasicFilter | ArrayFilter | RangeFilter
|
export type AnySearchFilter = BasicFilter | ArrayFilter | RangeFilter
|
||||||
|
|
||||||
export interface SearchFilters {
|
export interface SearchFilters {
|
||||||
|
|
|
@ -82,7 +82,6 @@
|
||||||
"@swc/jest": "0.2.27",
|
"@swc/jest": "0.2.27",
|
||||||
"@types/jest": "29.5.5",
|
"@types/jest": "29.5.5",
|
||||||
"@types/jsonwebtoken": "9.0.3",
|
"@types/jsonwebtoken": "9.0.3",
|
||||||
"@types/koa": "2.13.4",
|
|
||||||
"@types/koa__router": "12.0.4",
|
"@types/koa__router": "12.0.4",
|
||||||
"@types/lodash": "4.14.200",
|
"@types/lodash": "4.14.200",
|
||||||
"@types/node-fetch": "2.6.4",
|
"@types/node-fetch": "2.6.4",
|
||||||
|
|
|
@ -31,8 +31,8 @@ describe("/api/global/email", () => {
|
||||||
) {
|
) {
|
||||||
let response, text
|
let response, text
|
||||||
try {
|
try {
|
||||||
await helpers.withTimeout(20000, config.saveEtherealSmtpConfig())
|
await helpers.withTimeout(20000, () => config.saveEtherealSmtpConfig())
|
||||||
await helpers.withTimeout(20000, config.saveSettingsConfig())
|
await helpers.withTimeout(20000, () => config.saveSettingsConfig())
|
||||||
let res
|
let res
|
||||||
if (attachments) {
|
if (attachments) {
|
||||||
res = await config.api.emails
|
res = await config.api.emails
|
||||||
|
|
|
@ -1,10 +1,18 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
if [[ $TARGETARCH == arm* ]] ;
|
|
||||||
then
|
if [[ $TARGETBUILD == "aas" ]]; then
|
||||||
|
echo "A aas-compatible version of Minio is already installed."
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ $TARGETARCH == arm* ]]; then
|
||||||
echo "INSTALLING ARM64 MINIO"
|
echo "INSTALLING ARM64 MINIO"
|
||||||
|
rm -f minio
|
||||||
wget https://dl.min.io/server/minio/release/linux-arm64/minio
|
wget https://dl.min.io/server/minio/release/linux-arm64/minio
|
||||||
else
|
else
|
||||||
echo "INSTALLING AMD64 MINIO"
|
echo "INSTALLING AMD64 MINIO"
|
||||||
|
rm -f minio
|
||||||
wget https://dl.min.io/server/minio/release/linux-amd64/minio
|
wget https://dl.min.io/server/minio/release/linux-amd64/minio
|
||||||
fi
|
fi
|
||||||
chmod +x minio
|
|
||||||
|
chmod +x minio
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
version https://git-lfs.github.com/spec/v1
|
||||||
|
oid sha256:63db3aa3c2299ebaf13b46c64523a589bd5bf272f9e971d17f1eaa55f6f1fd79
|
||||||
|
size 118595584
|
|
@ -2778,9 +2778,9 @@
|
||||||
through2 "^2.0.0"
|
through2 "^2.0.0"
|
||||||
|
|
||||||
"@budibase/pro@npm:@budibase/pro@latest":
|
"@budibase/pro@npm:@budibase/pro@latest":
|
||||||
version "3.4.6"
|
version "3.4.12"
|
||||||
resolved "https://registry.yarnpkg.com/@budibase/pro/-/pro-3.4.6.tgz#62b6ee13a015b98d4768dc7821f468f8177da3e9"
|
resolved "https://registry.yarnpkg.com/@budibase/pro/-/pro-3.4.12.tgz#60e630944de4e2de970a04179d8f0f57d48ce75e"
|
||||||
integrity sha512-MC3P5SMokmqbjejZMlNM6z7NB9o5H6hZ++yVvbyThniBPYfuDc2ssa1HNwwcuNE3uRLhcxcKe8CY/0SbFgn51g==
|
integrity sha512-msUBmcWxRDg+ugjZvd27XudERQqtQRdiARsO8MaDVTcp5ejIXgshEIVVshHOCj3hcbRblw9pXvBIMI53iTMUsA==
|
||||||
dependencies:
|
dependencies:
|
||||||
"@anthropic-ai/sdk" "^0.27.3"
|
"@anthropic-ai/sdk" "^0.27.3"
|
||||||
"@budibase/backend-core" "*"
|
"@budibase/backend-core" "*"
|
||||||
|
|
Loading…
Reference in New Issue