Skip to content

Commit

Permalink
Merge pull request Budibase#14801 from Budibase/budi-8742-add-a-baseu…
Browse files Browse the repository at this point in the history
…rl-binding-inside-automations

Add settings to automation context.
  • Loading branch information
samwho authored Oct 28, 2024
2 parents 708b1d6 + 952610e commit c1d4c2c
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 7 deletions.
66 changes: 62 additions & 4 deletions packages/server/src/api/routes/tests/automation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -39,8 +45,7 @@ describe("/automations", () => {
})

beforeEach(() => {
// @ts-ignore
events.automation.deleted.mockClear()
jest.clearAllMocks()
})

describe("get definitions", () => {
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()}`,
Expand All @@ -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
Expand Down Expand Up @@ -347,6 +349,7 @@ class AutomationBuilder extends BaseStepBuilder {
export function createAutomationBuilder(options?: {
name?: string
appId?: string
config?: TestConfiguration
}) {
return new AutomationBuilder(options)
}
5 changes: 5 additions & 0 deletions packages/server/src/definitions/automations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ export interface AutomationContext extends AutomationResults {
env?: Record<string, string>
user?: UserBindings
trigger: any
settings?: {
url?: string
logo?: string
company?: string
}
}
14 changes: 13 additions & 1 deletion packages/server/src/threads/automation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,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"
Expand Down Expand Up @@ -263,6 +263,18 @@ class Orchestrator {
this.context.env = await sdkUtils.getEnvironmentVariables()
this.context.user = this.currentUser

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

// check if this is a recurring automation,
Expand Down

0 comments on commit c1d4c2c

Please sign in to comment.