From 3fc170c16bd1a99848cc260c94440f381497e4e2 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Tue, 15 Oct 2024 17:33:32 +0100 Subject: [PATCH 1/2] Add settings to automation context. --- .../src/api/routes/tests/automation.spec.ts | 66 +++++++++++++++++-- .../tests/utilities/AutomationTestBuilder.ts | 7 +- .../server/src/definitions/automations.ts | 5 ++ packages/server/src/threads/automation.ts | 9 ++- 4 files changed, 80 insertions(+), 7 deletions(-) diff --git a/packages/server/src/api/routes/tests/automation.spec.ts b/packages/server/src/api/routes/tests/automation.spec.ts index b6233b24ad..a517346c82 100644 --- a/packages/server/src/api/routes/tests/automation.spec.ts +++ b/packages/server/src/api/routes/tests/automation.spec.ts @@ -9,9 +9,15 @@ import { TRIGGER_DEFINITIONS, BUILTIN_ACTION_DEFINITIONS, } from "../../../automations" -import { events } from "@budibase/backend-core" +import { configs, context, events } from "@budibase/backend-core" import sdk from "../../../sdk" -import { Automation, FieldType, Table } from "@budibase/types" +import { + Automation, + ConfigType, + FieldType, + SettingsConfig, + Table, +} from "@budibase/types" import { mocks } from "@budibase/backend-core/tests" import { FilterConditions } from "../../../automations/steps/filter" import { removeDeprecated } from "../../../automations/utils" @@ -39,8 +45,7 @@ describe("/automations", () => { }) beforeEach(() => { - // @ts-ignore - events.automation.deleted.mockClear() + jest.clearAllMocks() }) describe("get definitions", () => { @@ -244,6 +249,59 @@ describe("/automations", () => { }) }) + describe("run", () => { + let oldConfig: SettingsConfig + beforeAll(async () => { + await context.doInTenant(config.getTenantId(), async () => { + oldConfig = await configs.getSettingsConfigDoc() + + const settings: SettingsConfig = { + _id: oldConfig._id, + _rev: oldConfig._rev, + type: ConfigType.SETTINGS, + config: { + platformUrl: "https://example.com", + logoUrl: "https://example.com/logo.png", + company: "Test Company", + }, + } + const saved = await configs.save(settings) + oldConfig._rev = saved.rev + }) + }) + + afterAll(async () => { + await context.doInTenant(config.getTenantId(), async () => { + await configs.save(oldConfig) + }) + }) + + it("should be able to access platformUrl, logoUrl and company in the automation", async () => { + const result = await createAutomationBuilder({ + name: "Test Automation", + appId: config.getAppId(), + config, + }) + .appAction({ fields: {} }) + .serverLog({ + text: "{{ settings.url }}", + }) + .serverLog({ + text: "{{ settings.logo }}", + }) + .serverLog({ + text: "{{ settings.company }}", + }) + .run() + + expect(result.steps[0].outputs.message).toEndWith("https://example.com") + expect(result.steps[1].outputs.message).toEndWith( + "https://example.com/logo.png" + ) + expect(result.steps[2].outputs.message).toEndWith("Test Company") + }) + }) + describe("test", () => { it("tests the automation successfully", async () => { let table = await config.createTable() diff --git a/packages/server/src/automations/tests/utilities/AutomationTestBuilder.ts b/packages/server/src/automations/tests/utilities/AutomationTestBuilder.ts index 6aaf22cd6a..fa8061edce 100644 --- a/packages/server/src/automations/tests/utilities/AutomationTestBuilder.ts +++ b/packages/server/src/automations/tests/utilities/AutomationTestBuilder.ts @@ -225,7 +225,9 @@ class AutomationBuilder extends BaseStepBuilder { private triggerOutputs: any private triggerSet: boolean = false - constructor(options: { name?: string; appId?: string } = {}) { + constructor( + options: { name?: string; appId?: string; config?: TestConfiguration } = {} + ) { super() this.automationConfig = { name: options.name || `Test Automation ${uuidv4()}`, @@ -237,7 +239,7 @@ class AutomationBuilder extends BaseStepBuilder { type: "automation", appId: options.appId ?? setup.getConfig().getAppId(), } - this.config = setup.getConfig() + this.config = options.config || setup.getConfig() } // TRIGGERS @@ -347,6 +349,7 @@ class AutomationBuilder extends BaseStepBuilder { export function createAutomationBuilder(options?: { name?: string appId?: string + config?: TestConfiguration }) { return new AutomationBuilder(options) } diff --git a/packages/server/src/definitions/automations.ts b/packages/server/src/definitions/automations.ts index 9433075da7..afee81ee0a 100644 --- a/packages/server/src/definitions/automations.ts +++ b/packages/server/src/definitions/automations.ts @@ -19,4 +19,9 @@ export interface AutomationContext extends AutomationResults { stepsByName: Record env?: Record trigger: any + settings?: { + url?: string + logo?: string + company?: string + } } diff --git a/packages/server/src/threads/automation.ts b/packages/server/src/threads/automation.ts index 3b47634663..f7db52ec1a 100644 --- a/packages/server/src/threads/automation.ts +++ b/packages/server/src/threads/automation.ts @@ -29,7 +29,7 @@ import { } from "@budibase/types" import { AutomationContext, TriggerOutput } from "../definitions/automations" import { WorkerCallback } from "./definitions" -import { context, logging } from "@budibase/backend-core" +import { context, logging, configs } from "@budibase/backend-core" import { processObject, processStringSync } from "@budibase/string-templates" import { cloneDeep } from "lodash/fp" import { performance } from "perf_hooks" @@ -259,6 +259,13 @@ class Orchestrator { }) this.context.env = await sdkUtils.getEnvironmentVariables() + const { config } = await configs.getSettingsConfigDoc() + this.context.settings = { + url: config.platformUrl, + logo: config.logoUrl, + company: config.company, + } + let metadata // check if this is a recurring automation, From 407484bbe53e7ed05e3a2381b1fdd98547a21def Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Fri, 25 Oct 2024 16:21:10 +0100 Subject: [PATCH 2/2] Respond to PR comments. --- packages/server/src/threads/automation.ts | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/packages/server/src/threads/automation.ts b/packages/server/src/threads/automation.ts index 7083562006..1af8646cf4 100644 --- a/packages/server/src/threads/automation.ts +++ b/packages/server/src/threads/automation.ts @@ -263,11 +263,16 @@ class Orchestrator { this.context.env = await sdkUtils.getEnvironmentVariables() this.context.user = this.currentUser - const { config } = await configs.getSettingsConfigDoc() - this.context.settings = { - url: config.platformUrl, - logo: config.logoUrl, - company: config.company, + try { + const { config } = await configs.getSettingsConfigDoc() + this.context.settings = { + url: config.platformUrl, + logo: config.logoUrl, + company: config.company, + } + } catch (e) { + // if settings doc doesn't exist, make the settings blank + this.context.settings = {} } let metadata