diff --git a/packages/cli/src/commands/import/workflow.ts b/packages/cli/src/commands/import/workflow.ts index df93f7f79..c6774b60a 100644 --- a/packages/cli/src/commands/import/workflow.ts +++ b/packages/cli/src/commands/import/workflow.ts @@ -131,6 +131,13 @@ export class ImportWorkflowsCommand extends BaseCommand { await setTagsForImport(transactionManager, workflow, tags); } + if (workflow.active) { + this.logger.info( + `Deactivating workflow "${workflow.name}" during import, remember to activate it later.`, + ); + workflow.active = false; + } + await this.storeWorkflow(workflow, user); } }); @@ -174,6 +181,12 @@ export class ImportWorkflowsCommand extends BaseCommand { if (Object.prototype.hasOwnProperty.call(workflow, 'tags')) { await setTagsForImport(transactionManager, workflow, tags); } + if (workflow.active) { + this.logger.info( + `Deactivating workflow "${workflow.name}" during import, remember to activate it later.`, + ); + workflow.active = false; + } await this.storeWorkflow(workflow, user); } diff --git a/packages/cli/test/integration/commands/import.cmd.test.ts b/packages/cli/test/integration/commands/import.cmd.test.ts new file mode 100644 index 000000000..bc00a5e53 --- /dev/null +++ b/packages/cli/test/integration/commands/import.cmd.test.ts @@ -0,0 +1,72 @@ +import * as testDb from '../shared/testDb'; +import { mockInstance } from '../shared/utils'; +import { InternalHooks } from '@/InternalHooks'; +import { ImportWorkflowsCommand } from '../../../src/commands/import/workflow'; +import * as Config from '@oclif/config'; + +beforeAll(async () => { + mockInstance(InternalHooks); + await testDb.init(); +}); + +beforeEach(async () => { + await testDb.truncate(['Workflow']); +}); + +afterAll(async () => { + await testDb.terminate(); +}); + +test('import:workflow should import active workflow and deactivate it', async () => { + const config: Config.IConfig = new Config.Config({ root: __dirname }); + const before = await testDb.getAllWorkflows(); + expect(before.length).toBe(0); + const importer = new ImportWorkflowsCommand( + ['--separate', '--input=./test/integration/commands/importWorkflows/separate'], + config, + ); + const mockExit = jest.spyOn(process, 'exit').mockImplementation((number) => { + throw new Error('process.exit'); + }); + + await importer.init(); + try { + await importer.run(); + } catch (error) { + expect(error.message).toBe('process.exit'); + } + const after = await testDb.getAllWorkflows(); + expect(after.length).toBe(2); + expect(after[0].name).toBe('active-workflow'); + expect(after[0].active).toBe(false); + expect(after[1].name).toBe('inactive-workflow'); + expect(after[1].active).toBe(false); + mockExit.mockRestore(); +}); + +test('import:workflow should import active workflow from combined file and deactivate it', async () => { + const config: Config.IConfig = new Config.Config({ root: __dirname }); + const before = await testDb.getAllWorkflows(); + expect(before.length).toBe(0); + const importer = new ImportWorkflowsCommand( + ['--input=./test/integration/commands/importWorkflows/combined/combined.json'], + config, + ); + const mockExit = jest.spyOn(process, 'exit').mockImplementation((number) => { + throw new Error('process.exit'); + }); + + await importer.init(); + try { + await importer.run(); + } catch (error) { + expect(error.message).toBe('process.exit'); + } + const after = await testDb.getAllWorkflows(); + expect(after.length).toBe(2); + expect(after[0].name).toBe('active-workflow'); + expect(after[0].active).toBe(false); + expect(after[1].name).toBe('inactive-workflow'); + expect(after[1].active).toBe(false); + mockExit.mockRestore(); +}); diff --git a/packages/cli/test/integration/commands/importWorkflows/combined/combined.json b/packages/cli/test/integration/commands/importWorkflows/combined/combined.json new file mode 100644 index 000000000..4bace0f04 --- /dev/null +++ b/packages/cli/test/integration/commands/importWorkflows/combined/combined.json @@ -0,0 +1,160 @@ +[ + { + "name": "active-workflow", + "nodes": [ + { + "parameters": { + "path": "e20b4873-fcf7-4bce-88fc-a1a56d66b138", + "responseMode": "responseNode", + "options": {} + }, + "id": "c26d8782-bd57-43d0-86dc-0c618a7e4024", + "name": "Webhook", + "type": "n8n-nodes-base.webhook", + "typeVersion": 1, + "position": [800, 580], + "webhookId": "e20b4873-fcf7-4bce-88fc-a1a56d66b138" + }, + { + "parameters": { + "values": { + "boolean": [ + { + "name": "hooked", + "value": true + } + ] + }, + "options": {} + }, + "id": "9701b1ef-9ab0-432a-b086-cf76981b097d", + "name": "Set", + "type": "n8n-nodes-base.set", + "typeVersion": 1, + "position": [1020, 580] + }, + { + "parameters": { + "options": {} + }, + "id": "d0f086b8-c2b2-4404-b347-95d3f91e555a", + "name": "Respond to Webhook", + "type": "n8n-nodes-base.respondToWebhook", + "typeVersion": 1, + "position": [1240, 580] + } + ], + "pinData": {}, + "connections": { + "Webhook": { + "main": [ + [ + { + "node": "Set", + "type": "main", + "index": 0 + } + ] + ] + }, + "Set": { + "main": [ + [ + { + "node": "Respond to Webhook", + "type": "main", + "index": 0 + } + ] + ] + } + }, + "active": true, + "settings": {}, + "versionId": "40a70df1-740f-47e7-8e16-50a0bcd5b70f", + "id": "998", + "meta": { + "instanceId": "95977dc4769098fc608439605527ee75d23f10d551aed6b87a3eea1a252c0ba9" + }, + "tags": [] + }, + { + "name": "inactive-workflow", + "nodes": [ + { + "parameters": { + "path": "e20b4873-fcf7-4bce-88fc-a1a56d66b137", + "responseMode": "responseNode", + "options": {} + }, + "id": "c26d8782-bd57-43d0-86dc-0c618a7e4024", + "name": "Webhook", + "type": "n8n-nodes-base.webhook", + "typeVersion": 1, + "position": [800, 580], + "webhookId": "e20b4873-fcf7-4bce-88fc-a1a56d66b137" + }, + { + "parameters": { + "values": { + "boolean": [ + { + "name": "hooked", + "value": true + } + ] + }, + "options": {} + }, + "id": "9701b1ef-9ab0-432a-b086-cf76981b097c", + "name": "Set", + "type": "n8n-nodes-base.set", + "typeVersion": 1, + "position": [1020, 580] + }, + { + "parameters": { + "options": {} + }, + "id": "d0f086b8-c2b2-4404-b347-95d3f91e555a", + "name": "Respond to Webhook", + "type": "n8n-nodes-base.respondToWebhook", + "typeVersion": 1, + "position": [1240, 580] + } + ], + "pinData": {}, + "connections": { + "Webhook": { + "main": [ + [ + { + "node": "Set", + "type": "main", + "index": 0 + } + ] + ] + }, + "Set": { + "main": [ + [ + { + "node": "Respond to Webhook", + "type": "main", + "index": 0 + } + ] + ] + } + }, + "active": false, + "settings": {}, + "versionId": "40a70df1-740f-47e7-8e16-50a0bcd5b70f", + "id": "999", + "meta": { + "instanceId": "95977dc4769098fc608439605527ee75d23f10d551aed6b87a3eea1a252c0ba9" + }, + "tags": [] + } +] diff --git a/packages/cli/test/integration/commands/importWorkflows/separate/001-activeWorkflow.json b/packages/cli/test/integration/commands/importWorkflows/separate/001-activeWorkflow.json new file mode 100644 index 000000000..71641916b --- /dev/null +++ b/packages/cli/test/integration/commands/importWorkflows/separate/001-activeWorkflow.json @@ -0,0 +1,79 @@ +{ + "name": "active-workflow", + "nodes": [ + { + "parameters": { + "path": "e20b4873-fcf7-4bce-88fc-a1a56d66b138", + "responseMode": "responseNode", + "options": {} + }, + "id": "c26d8782-bd57-43d0-86dc-0c618a7e4024", + "name": "Webhook", + "type": "n8n-nodes-base.webhook", + "typeVersion": 1, + "position": [800, 580], + "webhookId": "e20b4873-fcf7-4bce-88fc-a1a56d66b138" + }, + { + "parameters": { + "values": { + "boolean": [ + { + "name": "hooked", + "value": true + } + ] + }, + "options": {} + }, + "id": "9701b1ef-9ab0-432a-b086-cf76981b097d", + "name": "Set", + "type": "n8n-nodes-base.set", + "typeVersion": 1, + "position": [1020, 580] + }, + { + "parameters": { + "options": {} + }, + "id": "d0f086b8-c2b2-4404-b347-95d3f91e555a", + "name": "Respond to Webhook", + "type": "n8n-nodes-base.respondToWebhook", + "typeVersion": 1, + "position": [1240, 580] + } + ], + "pinData": {}, + "connections": { + "Webhook": { + "main": [ + [ + { + "node": "Set", + "type": "main", + "index": 0 + } + ] + ] + }, + "Set": { + "main": [ + [ + { + "node": "Respond to Webhook", + "type": "main", + "index": 0 + } + ] + ] + } + }, + "active": true, + "settings": {}, + "versionId": "40a70df1-740f-47e7-8e16-50a0bcd5b70f", + "id": "998", + "meta": { + "instanceId": "95977dc4769098fc608439605527ee75d23f10d551aed6b87a3eea1a252c0ba9" + }, + "tags": [] +} diff --git a/packages/cli/test/integration/commands/importWorkflows/separate/002-inactiveWorkflow.json b/packages/cli/test/integration/commands/importWorkflows/separate/002-inactiveWorkflow.json new file mode 100644 index 000000000..89e2c39d0 --- /dev/null +++ b/packages/cli/test/integration/commands/importWorkflows/separate/002-inactiveWorkflow.json @@ -0,0 +1,79 @@ +{ + "name": "inactive-workflow", + "nodes": [ + { + "parameters": { + "path": "e20b4873-fcf7-4bce-88fc-a1a56d66b137", + "responseMode": "responseNode", + "options": {} + }, + "id": "c26d8782-bd57-43d0-86dc-0c618a7e4024", + "name": "Webhook", + "type": "n8n-nodes-base.webhook", + "typeVersion": 1, + "position": [800, 580], + "webhookId": "e20b4873-fcf7-4bce-88fc-a1a56d66b137" + }, + { + "parameters": { + "values": { + "boolean": [ + { + "name": "hooked", + "value": true + } + ] + }, + "options": {} + }, + "id": "9701b1ef-9ab0-432a-b086-cf76981b097c", + "name": "Set", + "type": "n8n-nodes-base.set", + "typeVersion": 1, + "position": [1020, 580] + }, + { + "parameters": { + "options": {} + }, + "id": "d0f086b8-c2b2-4404-b347-95d3f91e555a", + "name": "Respond to Webhook", + "type": "n8n-nodes-base.respondToWebhook", + "typeVersion": 1, + "position": [1240, 580] + } + ], + "pinData": {}, + "connections": { + "Webhook": { + "main": [ + [ + { + "node": "Set", + "type": "main", + "index": 0 + } + ] + ] + }, + "Set": { + "main": [ + [ + { + "node": "Respond to Webhook", + "type": "main", + "index": 0 + } + ] + ] + } + }, + "active": false, + "settings": {}, + "versionId": "40a70df1-740f-47e7-8e16-50a0bcd5b70f", + "id": "999", + "meta": { + "instanceId": "95977dc4769098fc608439605527ee75d23f10d551aed6b87a3eea1a252c0ba9" + }, + "tags": [] +} diff --git a/packages/cli/test/integration/shared/testDb.ts b/packages/cli/test/integration/shared/testDb.ts index ec18d2f33..f6072dc75 100644 --- a/packages/cli/test/integration/shared/testDb.ts +++ b/packages/cli/test/integration/shared/testDb.ts @@ -489,6 +489,10 @@ export async function createWorkflowWithTrigger( return workflow; } +export async function getAllWorkflows() { + return Db.collections.Workflow.find(); +} + // ---------------------------------- // workflow sharing // ----------------------------------