From 01f349bc574360dba6daf2f32cd448a13204876d Mon Sep 17 00:00:00 2001 From: Charles Driesler Date: Wed, 29 May 2024 12:36:02 +0100 Subject: [PATCH 1/2] do not trigger functions if automation is a test automation --- ...523192300_add_is_test_automation_column.ts | 11 ++---- .../modules/automate/services/trigger.ts | 36 ++++++++++++++++--- 2 files changed, 34 insertions(+), 13 deletions(-) diff --git a/packages/server/modules/automate/migrations/20240523192300_add_is_test_automation_column.ts b/packages/server/modules/automate/migrations/20240523192300_add_is_test_automation_column.ts index 110177c5e..2d45a7110 100644 --- a/packages/server/modules/automate/migrations/20240523192300_add_is_test_automation_column.ts +++ b/packages/server/modules/automate/migrations/20240523192300_add_is_test_automation_column.ts @@ -1,7 +1,6 @@ import { Knex } from 'knex' const AUTOMATIONS_TABLE = 'automations' -const PLACEHOLDER_NULL_ID = 'null-execution-engine-automation-id' export async function up(knex: Knex): Promise { await knex.schema.alterTable(AUTOMATIONS_TABLE, (table) => { @@ -11,21 +10,15 @@ export async function up(knex: Knex): Promise { await knex.schema.alterTable(AUTOMATIONS_TABLE, (table) => { table.setNullable('executionEngineAutomationId') }) - - await knex(AUTOMATIONS_TABLE) - .where('executionEngineAutomationId', PLACEHOLDER_NULL_ID) - .update({ executionEngineAutomationId: null }) } export async function down(knex: Knex): Promise { + await knex(AUTOMATIONS_TABLE).where('isTestAutomation', true).delete() + await knex.schema.alterTable(AUTOMATIONS_TABLE, (table) => { table.dropColumn('isTestAutomation') }) - await knex(AUTOMATIONS_TABLE) - .whereNull('executionEngineAutomationId') - .update({ executionEngineAutomationId: PLACEHOLDER_NULL_ID }) - await knex.schema.alterTable(AUTOMATIONS_TABLE, (table) => { table.string('executionEngineAutomationId').notNullable().alter() }) diff --git a/packages/server/modules/automate/services/trigger.ts b/packages/server/modules/automate/services/trigger.ts index f9dc01271..8201b5a5c 100644 --- a/packages/server/modules/automate/services/trigger.ts +++ b/packages/server/modules/automate/services/trigger.ts @@ -5,7 +5,8 @@ import { getFullAutomationRevisionMetadata, getAutomationToken, getAutomationTriggerDefinitions, - upsertAutomationRun + upsertAutomationRun, + getAutomationRevision } from '@/modules/automate/repositories/automations' import { AutomationWithRevision, @@ -45,6 +46,8 @@ import { LibsodiumEncryptionError } from '@/modules/shared/errors/encryption' import { AutomateRunsEmitter } from '@/modules/automate/events/runs' export type OnModelVersionCreateDeps = { + getAutomation: typeof getAutomation + getAutomationRevision: typeof getAutomationRevision getTriggers: typeof getActiveTriggerDefinitions triggerFunction: ReturnType } @@ -56,7 +59,7 @@ export const onModelVersionCreate = (deps: OnModelVersionCreateDeps) => async (params: { modelId: string; versionId: string; projectId: string }) => { const { modelId, versionId, projectId } = params - const { getTriggers, triggerFunction } = deps + const { getAutomation, getAutomationRevision, getTriggers, triggerFunction } = deps // get triggers where modelId matches const triggerDefinitions = await getTriggers({ @@ -68,13 +71,38 @@ export const onModelVersionCreate = await Promise.all( triggerDefinitions.map(async (tr) => { try { + const { automationRevisionId, triggeringId, triggerType } = tr + + const automationRevisionRecord = await getAutomationRevision({ + automationRevisionId + }) + + if (!automationRevisionRecord) { + throw new AutomateInvalidTriggerError( + 'Specified automation revision does not exist' + ) + } + + const automationRecord = await getAutomation({ + automationId: automationRevisionRecord.automationId + }) + + if (!automationRecord) { + throw new AutomateInvalidTriggerError('Specified automation does not exist') + } + + if (automationRecord.isTestAutomation) { + // Do not trigger functions on test automations + return + } + await triggerFunction({ revisionId: tr.automationRevisionId, manifest: { versionId, projectId, - modelId: tr.triggeringId, - triggerType: tr.triggerType + modelId: triggeringId, + triggerType } }) } catch (error) { From 2df4c4a23357fa53032cf174692c3c64d84a61cf Mon Sep 17 00:00:00 2001 From: Charles Driesler Date: Wed, 29 May 2024 12:45:45 +0100 Subject: [PATCH 2/2] whoops --- packages/server/modules/automate/index.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/server/modules/automate/index.ts b/packages/server/modules/automate/index.ts index 4a9d36450..d75308227 100644 --- a/packages/server/modules/automate/index.ts +++ b/packages/server/modules/automate/index.ts @@ -8,6 +8,8 @@ import { import { Environment } from '@speckle/shared' import { getActiveTriggerDefinitions, + getAutomation, + getAutomationRevision, getAutomationRunFullTriggers } from '@/modules/automate/repositories/automations' import { ScopeRecord } from '@/modules/auth/helpers/types' @@ -70,6 +72,8 @@ const initializeEventListeners = () => { VersionEvents.Created, async ({ modelId, version, projectId }) => { await onModelVersionCreate({ + getAutomation, + getAutomationRevision, getTriggers: getActiveTriggerDefinitions, triggerFunction: triggerFn })({ modelId, versionId: version.id, projectId })