From ad41264764664bcc334773fb8449a138a9e92c57 Mon Sep 17 00:00:00 2001 From: Kristaps Fabians Geikins Date: Fri, 20 Sep 2024 15:29:23 +0300 Subject: [PATCH 1/2] chore(server): automate IoC 11 - manuallyTriggerAutomationFactory --- .../modules/automate/domain/operations.ts | 8 ++ .../automate/graph/resolvers/automate.ts | 7 +- .../automate/repositories/automations.ts | 78 ++++++++++--------- .../modules/automate/services/trigger.ts | 8 +- .../modules/automate/tests/trigger.spec.ts | 9 ++- 5 files changed, 63 insertions(+), 47 deletions(-) diff --git a/packages/server/modules/automate/domain/operations.ts b/packages/server/modules/automate/domain/operations.ts index 8e8aad365..83423db24 100644 --- a/packages/server/modules/automate/domain/operations.ts +++ b/packages/server/modules/automate/domain/operations.ts @@ -112,6 +112,14 @@ export type UpsertAutomationRun = ( automationRun: InsertableAutomationRun ) => Promise +export type GetAutomationTriggerDefinitions = < + T extends AutomationTriggerType = AutomationTriggerType +>(params: { + automationId: string + projectId?: string + triggerType?: T +}) => Promise & { automationId: string }>> + export type CreateStoredAuthCode = ( params: Omit ) => Promise diff --git a/packages/server/modules/automate/graph/resolvers/automate.ts b/packages/server/modules/automate/graph/resolvers/automate.ts index 81ee590f1..26a7689ad 100644 --- a/packages/server/modules/automate/graph/resolvers/automate.ts +++ b/packages/server/modules/automate/graph/resolvers/automate.ts @@ -15,7 +15,7 @@ import { getAutomationRunsItems, getAutomationRunsTotalCount, getAutomationTokenFactory, - getAutomationTriggerDefinitions, + getAutomationTriggerDefinitionsFactory, getFullAutomationRevisionMetadataFactory, getFunctionRunFactory, getLatestAutomationRevision, @@ -64,7 +64,7 @@ import { } from '@/modules/core/repositories/branches' import { createTestAutomationRun, - manuallyTriggerAutomation, + manuallyTriggerAutomationFactory, triggerAutomationRevisionRunFactory } from '@/modules/automate/services/trigger' import { @@ -125,6 +125,7 @@ const getFullAutomationRevisionMetadata = getFullAutomationRevisionMetadataFacto }) const getAutomationToken = getAutomationTokenFactory({ db }) const upsertAutomationRun = upsertAutomationRunFactory({ db }) +const getAutomationTriggerDefinitions = getAutomationTriggerDefinitionsFactory({ db }) export = (FF_AUTOMATE_MODULE_ENABLED ? { @@ -532,7 +533,7 @@ export = (FF_AUTOMATE_MODULE_ENABLED }) }, async trigger(parent, { automationId }, ctx) { - const trigger = manuallyTriggerAutomation({ + const trigger = manuallyTriggerAutomationFactory({ getAutomationTriggerDefinitions, getAutomation, getBranchLatestCommits, diff --git a/packages/server/modules/automate/repositories/automations.ts b/packages/server/modules/automate/repositories/automations.ts index f1e756eaa..8fde5c52e 100644 --- a/packages/server/modules/automate/repositories/automations.ts +++ b/packages/server/modules/automate/repositories/automations.ts @@ -6,6 +6,7 @@ import { GetAutomationRunFullTriggers, GetAutomations, GetAutomationToken, + GetAutomationTriggerDefinitions, GetFullAutomationRevisionMetadata, GetFullAutomationRunById, GetFunctionRun, @@ -493,47 +494,52 @@ export const updateAutomationFactory = return ret } -export async function getAutomationTriggerDefinitions< - T extends AutomationTriggerType = AutomationTriggerType ->(params: { automationId: string; projectId?: string; triggerType?: T }) { - const { automationId, projectId, triggerType } = params +export const getAutomationTriggerDefinitionsFactory = + (deps: { db: Knex }): GetAutomationTriggerDefinitions => + async (params: { + automationId: string + projectId?: string + triggerType?: T + }) => { + const { automationId, projectId, triggerType } = params - const revisionQuery = AutomationRevisions.knex() - .select([AutomationRevisions.col.id]) - .where(AutomationRevisions.col.automationId, automationId) - .andWhere(AutomationRevisions.col.active, true) - .innerJoin( - AutomationTriggers.name, - AutomationTriggers.col.automationRevisionId, - AutomationRevisions.col.id - ) - .limit(1) - - if (projectId) { - revisionQuery + const revisionQuery = tables + .automationRevisions(deps.db) + .select([AutomationRevisions.col.id]) + .where(AutomationRevisions.col.automationId, automationId) + .andWhere(AutomationRevisions.col.active, true) .innerJoin( - Automations.name, - Automations.col.id, - AutomationRevisions.col.automationId + AutomationTriggers.name, + AutomationTriggers.col.automationRevisionId, + AutomationRevisions.col.id ) - .andWhere(Automations.col.projectId, projectId) + .limit(1) + + if (projectId) { + revisionQuery + .innerJoin( + Automations.name, + Automations.col.id, + AutomationRevisions.col.automationId + ) + .andWhere(Automations.col.projectId, projectId) + } + + const mainQ = tables + .automationTriggers(deps.db) + .select[]>('*') + .where(AutomationTriggers.col.automationRevisionId, revisionQuery) + + if (triggerType) { + mainQ.andWhere(AutomationTriggers.col.triggerType, triggerType) + } + + return (await mainQ).map((r) => ({ + ...r, + automationId + })) } - const mainQ = AutomationTriggers.knex[]>().where( - AutomationTriggers.col.automationRevisionId, - revisionQuery - ) - - if (triggerType) { - mainQ.andWhere(AutomationTriggers.col.triggerType, triggerType) - } - - return (await mainQ).map((r) => ({ - ...r, - automationId - })) -} - export async function updateFunctionRun( run: SetRequired, 'id'> ) { diff --git a/packages/server/modules/automate/services/trigger.ts b/packages/server/modules/automate/services/trigger.ts index b22d43442..d04084760 100644 --- a/packages/server/modules/automate/services/trigger.ts +++ b/packages/server/modules/automate/services/trigger.ts @@ -1,6 +1,5 @@ import { InsertableAutomationRun, - getAutomationTriggerDefinitions, getLatestAutomationRevision, getFullAutomationRevisionMetadataFactory } from '@/modules/automate/repositories/automations' @@ -46,6 +45,7 @@ import { GetAutomation, GetAutomationRevision, GetAutomationToken, + GetAutomationTriggerDefinitions, GetEncryptionKeyPairFor, GetFullAutomationRevisionMetadata, TriggerAutomationRevisionRun, @@ -461,13 +461,13 @@ async function composeTriggerData(params: { } export type ManuallyTriggerAutomationDeps = { - getAutomationTriggerDefinitions: typeof getAutomationTriggerDefinitions + getAutomationTriggerDefinitions: GetAutomationTriggerDefinitions getAutomation: GetAutomation getBranchLatestCommits: typeof getBranchLatestCommits - triggerFunction: ReturnType + triggerFunction: TriggerAutomationRevisionRun } -export const manuallyTriggerAutomation = +export const manuallyTriggerAutomationFactory = (deps: ManuallyTriggerAutomationDeps) => async (params: { automationId: string diff --git a/packages/server/modules/automate/tests/trigger.spec.ts b/packages/server/modules/automate/tests/trigger.spec.ts index e8a38b715..d9da1d54c 100644 --- a/packages/server/modules/automate/tests/trigger.spec.ts +++ b/packages/server/modules/automate/tests/trigger.spec.ts @@ -5,7 +5,7 @@ import { import { ManuallyTriggerAutomationDeps, ensureRunConditionsFactory, - manuallyTriggerAutomation, + manuallyTriggerAutomationFactory, onModelVersionCreateFactory, triggerAutomationRevisionRunFactory } from '@/modules/automate/services/trigger' @@ -33,7 +33,6 @@ import { import { createTestCommit } from '@/test/speckle-helpers/commitHelper' import { InsertableAutomationRun, - getAutomationTriggerDefinitions, updateAutomationRevision, updateAutomationRun, storeAutomationFactory, @@ -45,7 +44,8 @@ import { upsertAutomationFunctionRunFactory, getFullAutomationRunByIdFactory, upsertAutomationRunFactory, - getAutomationTokenFactory + getAutomationTokenFactory, + getAutomationTriggerDefinitionsFactory } from '@/modules/automate/repositories/automations' import { beforeEachContext, truncateTables } from '@/test/hooks' import { Automate } from '@speckle/shared' @@ -88,6 +88,7 @@ const upsertAutomationFunctionRun = upsertAutomationFunctionRunFactory({ db }) const getFullAutomationRunById = getFullAutomationRunByIdFactory({ db }) const upsertAutomationRun = upsertAutomationRunFactory({ db }) const getAutomationToken = getAutomationTokenFactory({ db }) +const getAutomationTriggerDefinitions = getAutomationTriggerDefinitionsFactory({ db }) ;(FF_AUTOMATE_MODULE_ENABLED ? describe : describe.skip)( 'Automate triggers @automate', @@ -965,7 +966,7 @@ const getAutomationToken = getAutomationTokenFactory({ db }) const buildManuallyTriggerAutomation = ( overrides?: Partial ) => { - const trigger = manuallyTriggerAutomation({ + const trigger = manuallyTriggerAutomationFactory({ getAutomationTriggerDefinitions, getAutomation, getBranchLatestCommits, From 0368f9b79256844af1f1230c44f4fb2d41114118 Mon Sep 17 00:00:00 2001 From: Kristaps Fabians Geikins Date: Fri, 20 Sep 2024 15:31:34 +0300 Subject: [PATCH 2/2] fixx --- packages/server/modules/automate/graph/resolvers/automate.ts | 3 ++- packages/server/modules/automate/services/trigger.ts | 4 +++- packages/server/modules/automate/tests/trigger.spec.ts | 2 ++ 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/server/modules/automate/graph/resolvers/automate.ts b/packages/server/modules/automate/graph/resolvers/automate.ts index 26a7689ad..448eb7b8a 100644 --- a/packages/server/modules/automate/graph/resolvers/automate.ts +++ b/packages/server/modules/automate/graph/resolvers/automate.ts @@ -547,7 +547,8 @@ export = (FF_AUTOMATE_MODULE_ENABLED automateRunsEmitter: AutomateRunsEmitter.emit, getAutomationToken, upsertAutomationRun - }) + }), + validateStreamAccess }) const { automationRunId } = await trigger({ diff --git a/packages/server/modules/automate/services/trigger.ts b/packages/server/modules/automate/services/trigger.ts index d04084760..efaee20c8 100644 --- a/packages/server/modules/automate/services/trigger.ts +++ b/packages/server/modules/automate/services/trigger.ts @@ -465,6 +465,7 @@ export type ManuallyTriggerAutomationDeps = { getAutomation: GetAutomation getBranchLatestCommits: typeof getBranchLatestCommits triggerFunction: TriggerAutomationRevisionRun + validateStreamAccess: typeof validateStreamAccess } export const manuallyTriggerAutomationFactory = @@ -480,7 +481,8 @@ export const manuallyTriggerAutomationFactory = getAutomationTriggerDefinitions, getAutomation, getBranchLatestCommits, - triggerFunction + triggerFunction, + validateStreamAccess } = deps const [automation, triggerDefs] = await Promise.all([ diff --git a/packages/server/modules/automate/tests/trigger.spec.ts b/packages/server/modules/automate/tests/trigger.spec.ts index d9da1d54c..5d84f156e 100644 --- a/packages/server/modules/automate/tests/trigger.spec.ts +++ b/packages/server/modules/automate/tests/trigger.spec.ts @@ -75,6 +75,7 @@ import { mapGqlStatusToDbStatus } from '@/modules/automate/utils/automateFunctio import { db } from '@/db/knex' import { AutomateRunsEmitter } from '@/modules/automate/events/runs' import { createAppToken } from '@/modules/core/services/tokens' +import { validateStreamAccess } from '@/modules/core/services/streams/streamAccessService' const { FF_AUTOMATE_MODULE_ENABLED } = getFeatureFlags() @@ -983,6 +984,7 @@ const getAutomationTriggerDefinitions = getAutomationTriggerDefinitionsFactory({ getAutomationToken, upsertAutomationRun }), + validateStreamAccess, ...(overrides || {}) }) return trigger