From 41c3cc89ca58529d0ec6f8639b435b88cee5a9d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E0=A4=95=E0=A4=BE=E0=A4=B0=E0=A4=A4=E0=A5=8B=E0=A4=AB?= =?UTF-8?q?=E0=A5=8D=E0=A4=AB=E0=A5=87=E0=A4=B2=E0=A4=B8=E0=A5=8D=E0=A4=95?= =?UTF-8?q?=E0=A5=8D=E0=A4=B0=E0=A4=BF=E0=A4=AA=E0=A5=8D=E0=A4=9F=E2=84=A2?= Date: Thu, 17 Aug 2023 17:18:14 +0200 Subject: [PATCH] refactor(core): Delete some duplicate code between ActiveWebhooks and ActiveWorkflowRunner (no-changelog) (#6951) --- packages/cli/src/ActiveWebhooks.ts | 17 +------- packages/cli/src/ActiveWorkflowRunner.ts | 23 +---------- packages/workflow/src/Workflow.ts | 52 +++++++++++++++++++++--- 3 files changed, 50 insertions(+), 42 deletions(-) diff --git a/packages/cli/src/ActiveWebhooks.ts b/packages/cli/src/ActiveWebhooks.ts index 14e4f56a13..42e2425634 100644 --- a/packages/cli/src/ActiveWebhooks.ts +++ b/packages/cli/src/ActiveWebhooks.ts @@ -63,25 +63,13 @@ export class ActiveWebhooks { this.webhookUrls[webhookKey].push(webhookData); try { - const webhookExists = await workflow.runWebhookMethod( - 'checkExists', + await workflow.createWebhookIfNotExists( webhookData, NodeExecuteFunctions, mode, activation, this.testWebhooks, ); - if (webhookExists !== true) { - // If webhook does not exist yet create it - await workflow.runWebhookMethod( - 'create', - webhookData, - NodeExecuteFunctions, - mode, - activation, - this.testWebhooks, - ); - } } catch (error) { // If there was a problem unregister the webhook again if (this.webhookUrls[webhookKey].length <= 1) { @@ -183,8 +171,7 @@ export class ActiveWebhooks { // Go through all the registered webhooks of the workflow and remove them for (const webhookData of webhooks) { - await workflow.runWebhookMethod( - 'delete', + await workflow.deleteWebhook( webhookData, NodeExecuteFunctions, mode, diff --git a/packages/cli/src/ActiveWorkflowRunner.ts b/packages/cli/src/ActiveWorkflowRunner.ts index 688a819c10..f830becb53 100644 --- a/packages/cli/src/ActiveWorkflowRunner.ts +++ b/packages/cli/src/ActiveWorkflowRunner.ts @@ -392,25 +392,13 @@ export class ActiveWorkflowRunner implements IWebhookManager { try { // TODO: this should happen in a transaction, that way we don't need to manually remove this in `catch` await this.webhookService.storeWebhook(webhook); - const webhookExists = await workflow.runWebhookMethod( - 'checkExists', + await workflow.createWebhookIfNotExists( webhookData, NodeExecuteFunctions, mode, activation, false, ); - if (webhookExists !== true) { - // If webhook does not exist yet create it - await workflow.runWebhookMethod( - 'create', - webhookData, - NodeExecuteFunctions, - mode, - activation, - false, - ); - } } catch (error) { if ( activation === 'init' && @@ -489,14 +477,7 @@ export class ActiveWorkflowRunner implements IWebhookManager { const webhooks = WebhookHelpers.getWorkflowWebhooks(workflow, additionalData, undefined, true); for (const webhookData of webhooks) { - await workflow.runWebhookMethod( - 'delete', - webhookData, - NodeExecuteFunctions, - mode, - 'update', - false, - ); + await workflow.deleteWebhook(webhookData, NodeExecuteFunctions, mode, 'update', false); } await WorkflowHelpers.saveStaticData(workflow); diff --git a/packages/workflow/src/Workflow.ts b/packages/workflow/src/Workflow.ts index 02c104d2db..267780536c 100644 --- a/packages/workflow/src/Workflow.ts +++ b/packages/workflow/src/Workflow.ts @@ -972,12 +972,52 @@ export class Workflow { return this.__getStartNode(Object.keys(this.nodes)); } - /** - * Executes the Webhooks method of the node - * - * @param {WebhookSetupMethodNames} method The name of the method to execute - */ - async runWebhookMethod( + async createWebhookIfNotExists( + webhookData: IWebhookData, + nodeExecuteFunctions: INodeExecuteFunctions, + mode: WorkflowExecuteMode, + activation: WorkflowActivateMode, + isTest?: boolean, + ): Promise { + const webhookExists = await this.runWebhookMethod( + 'checkExists', + webhookData, + nodeExecuteFunctions, + mode, + activation, + isTest, + ); + if (!webhookExists) { + // If webhook does not exist yet create it + await this.runWebhookMethod( + 'create', + webhookData, + nodeExecuteFunctions, + mode, + activation, + isTest, + ); + } + } + + async deleteWebhook( + webhookData: IWebhookData, + nodeExecuteFunctions: INodeExecuteFunctions, + mode: WorkflowExecuteMode, + activation: WorkflowActivateMode, + isTest?: boolean, + ) { + await this.runWebhookMethod( + 'delete', + webhookData, + nodeExecuteFunctions, + mode, + activation, + isTest, + ); + } + + private async runWebhookMethod( method: WebhookSetupMethodNames, webhookData: IWebhookData, nodeExecuteFunctions: INodeExecuteFunctions,