Merge branch 'chuck/testAutomationsNewEndpoints' into chuck/createTestAutomation

This commit is contained in:
Charles Driesler
2024-05-30 11:08:39 +01:00
3 changed files with 38 additions and 13 deletions
@@ -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 })
@@ -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<void> {
await knex.schema.alterTable(AUTOMATIONS_TABLE, (table) => {
@@ -11,21 +10,15 @@ export async function up(knex: Knex): Promise<void> {
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<void> {
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()
})
@@ -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<typeof triggerAutomationRevisionRun>
}
@@ -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<VersionCreatedTriggerManifest>({
revisionId: tr.automationRevisionId,
manifest: {
versionId,
projectId,
modelId: tr.triggeringId,
triggerType: tr.triggerType
modelId: triggeringId,
triggerType
}
})
} catch (error) {