Skip to content

Commit

Permalink
✨ Update import workflows command for user management (#2701)
Browse files Browse the repository at this point in the history
* ⚡ Remove lint comments

* ⚡ Save shared workflow on import

* ✏️ Improve error message

* 📌 Pin chokidar version

* 🔥 Remove self-healing from getInstanceOwner

* ⚡ Simplify syntax

* ✏️ Improve readability

* ⚡ Refactor to use transactions

* ⚡ Implement assignee ID

* 🔥 Remove unused field

* ⚡ Fix build

* 👕 Remove unused imports

* 🔥 Remove error message section

* ⚡ Refactor to enforce all or nothing import

* 🚚 Rename id flag to userId
  • Loading branch information
ivov authored Jan 27, 2022
1 parent 6ce0ef0 commit fe07ead
Showing 1 changed file with 166 additions and 62 deletions.
228 changes: 166 additions & 62 deletions packages/cli/commands/import/workflow.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/* eslint-disable no-restricted-syntax */
/* eslint-disable @typescript-eslint/restrict-template-expressions */
/* eslint-disable @typescript-eslint/no-shadow */
/* eslint-disable @typescript-eslint/no-loop-func */
/* eslint-disable no-await-in-loop */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/no-unsafe-call */
/* eslint-disable @typescript-eslint/no-non-null-assertion */
/* eslint-disable no-console */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
import { Command, flags } from '@oclif/command';
Expand All @@ -7,15 +15,25 @@ import { INode, INodeCredentialsDetails, LoggerProxy } from 'n8n-workflow';
import * as fs from 'fs';
import * as glob from 'fast-glob';
import { UserSettings } from 'n8n-core';
import { EntityManager, getConnection } from 'typeorm';
import { getLogger } from '../../src/Logger';
import { Db, ICredentialsDb } from '../../src';
import { SharedWorkflow } from '../../src/databases/entities/SharedWorkflow';
import { WorkflowEntity } from '../../src/databases/entities/WorkflowEntity';
import { Role } from '../../src/databases/entities/Role';
import { User } from '../../src/databases/entities/User';

const FIX_INSTRUCTION =
'Please fix the database by running ./packages/cli/bin/n8n user-management:reset';

export class ImportWorkflowsCommand extends Command {
static description = 'Import workflows';

static examples = [
`$ n8n import:workflow --input=file.json`,
`$ n8n import:workflow --separate --input=backups/latest/`,
'$ n8n import:workflow --input=file.json',
'$ n8n import:workflow --separate --input=backups/latest/',
'$ n8n import:workflow --input=file.json --userId=1d64c3d2-85fe-4a83-a649-e446b07b3aae',
'$ n8n import:workflow --separate --input=backups/latest/ --userId=1d64c3d2-85fe-4a83-a649-e446b07b3aae',
];

static flags = {
Expand All @@ -27,51 +45,30 @@ export class ImportWorkflowsCommand extends Command {
separate: flags.boolean({
description: 'Imports *.json files from directory provided by --input',
}),
userId: flags.string({
description: 'The ID of the user to assign the imported workflows to',
}),
};

private transformCredentials(node: INode, credentialsEntities: ICredentialsDb[]) {
if (node.credentials) {
const allNodeCredentials = Object.entries(node.credentials);
// eslint-disable-next-line no-restricted-syntax
for (const [type, name] of allNodeCredentials) {
if (typeof name === 'string') {
const nodeCredentials: INodeCredentialsDetails = {
id: null,
name,
};
ownerWorkflowRole: Role;

const matchingCredentials = credentialsEntities.filter(
(credentials) => credentials.name === name && credentials.type === type,
);

if (matchingCredentials.length === 1) {
nodeCredentials.id = matchingCredentials[0].id.toString();
}

// eslint-disable-next-line no-param-reassign
node.credentials[type] = nodeCredentials;
}
}
}
}
transactionManager: EntityManager;

// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
async run() {
async run(): Promise<void> {
const logger = getLogger();
LoggerProxy.init(logger);

// eslint-disable-next-line @typescript-eslint/no-shadow
const { flags } = this.parse(ImportWorkflowsCommand);

if (!flags.input) {
console.info(`An input file or directory with --input must be provided`);
console.info('An input file or directory with --input must be provided');
return;
}

if (flags.separate) {
if (fs.existsSync(flags.input)) {
if (!fs.lstatSync(flags.input).isDirectory()) {
console.info(`The paramenter --input must be a directory`);
console.info('The argument to --input must be a directory');
return;
}
}
Expand All @@ -80,54 +77,161 @@ export class ImportWorkflowsCommand extends Command {
try {
await Db.init();

await this.initOwnerWorkflowRole();
const user = flags.userId ? await this.getAssignee(flags.userId) : await this.getOwner();

// Make sure the settings exist
await UserSettings.prepareUserSettings();
const credentialsEntities = (await Db.collections.Credentials?.find()) ?? [];
let i;
const credentials = (await Db.collections.Credentials?.find()) ?? [];

let totalImported = 0;

if (flags.separate) {
let inputPath = flags.input;
let { input: inputPath } = flags;

if (process.platform === 'win32') {
inputPath = inputPath.replace(/\\/g, '/');
}

inputPath = inputPath.replace(/\/$/g, '');

const files = await glob(`${inputPath}/*.json`);
for (i = 0; i < files.length; i++) {
const workflow = JSON.parse(fs.readFileSync(files[i], { encoding: 'utf8' }));
if (credentialsEntities.length > 0) {
// eslint-disable-next-line
workflow.nodes.forEach((node: INode) => {
this.transformCredentials(node, credentialsEntities);
});

totalImported = files.length;

await getConnection().transaction(async (transactionManager) => {
this.transactionManager = transactionManager;

for (const file of files) {
const workflow = JSON.parse(fs.readFileSync(file, { encoding: 'utf8' }));

if (credentials.length > 0) {
workflow.nodes.forEach((node: INode) => {
this.transformCredentials(node, credentials);
});
}

await this.storeWorkflow(workflow, user);
}
// eslint-disable-next-line no-await-in-loop, @typescript-eslint/no-non-null-assertion
await Db.collections.Workflow!.save(workflow);
}
} else {
const fileContents = JSON.parse(fs.readFileSync(flags.input, { encoding: 'utf8' }));
});

if (!Array.isArray(fileContents)) {
throw new Error(`File does not seem to contain workflows.`);
}
this.reportSuccess(totalImported);
process.exit();
}

const workflows = JSON.parse(fs.readFileSync(flags.input, { encoding: 'utf8' }));

totalImported = workflows.length;

if (!Array.isArray(workflows)) {
throw new Error(
'File does not seem to contain workflows. Make sure the workflows are contained in an array.',
);
}

await getConnection().transaction(async (transactionManager) => {
this.transactionManager = transactionManager;

for (i = 0; i < fileContents.length; i++) {
if (credentialsEntities.length > 0) {
// eslint-disable-next-line
fileContents[i].nodes.forEach((node: INode) => {
this.transformCredentials(node, credentialsEntities);
for (const workflow of workflows) {
if (credentials.length > 0) {
workflow.nodes.forEach((node: INode) => {
this.transformCredentials(node, credentials);
});
}
// eslint-disable-next-line no-await-in-loop, @typescript-eslint/no-non-null-assertion
await Db.collections.Workflow!.save(fileContents[i]);

await this.storeWorkflow(workflow, user);
}
}
});

console.info(`Successfully imported ${i} ${i === 1 ? 'workflow.' : 'workflows.'}`);
process.exit(0);
this.reportSuccess(totalImported);
process.exit();
} catch (error) {
console.error('An error occurred while exporting workflows. See log messages for details.');
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
logger.error(error.message);
console.error('An error occurred while importing workflows. See log messages for details.');
if (error instanceof Error) logger.error(error.message);
this.exit(1);
}
}

private reportSuccess(total: number) {
console.info(`Successfully imported ${total} ${total === 1 ? 'workflow.' : 'workflows.'}`);
}

private async initOwnerWorkflowRole() {
const ownerWorkflowRole = await Db.collections.Role!.findOne({
where: { name: 'owner', scope: 'workflow' },
});

if (!ownerWorkflowRole) {
throw new Error(`Failed to find owner workflow role. ${FIX_INSTRUCTION}`);
}

this.ownerWorkflowRole = ownerWorkflowRole;
}

private async storeWorkflow(workflow: object, user: User) {
const newWorkflow = new WorkflowEntity();

Object.assign(newWorkflow, workflow);

const savedWorkflow = await this.transactionManager.save<WorkflowEntity>(newWorkflow);

const newSharedWorkflow = new SharedWorkflow();

Object.assign(newSharedWorkflow, {
workflow: savedWorkflow,
user,
role: this.ownerWorkflowRole,
});

await this.transactionManager.save<SharedWorkflow>(newSharedWorkflow);
}

private async getOwner() {
const ownerGlobalRole = await Db.collections.Role!.findOne({
where: { name: 'owner', scope: 'global' },
});

const owner = await Db.collections.User!.findOne({ globalRole: ownerGlobalRole });

if (!owner) {
throw new Error(`Failed to find owner. ${FIX_INSTRUCTION}`);
}

return owner;
}

private async getAssignee(userId: string) {
const user = await Db.collections.User!.findOne(userId);

if (!user) {
throw new Error(`Failed to find user with ID ${userId}`);
}

return user;
}

private transformCredentials(node: INode, credentialsEntities: ICredentialsDb[]) {
if (node.credentials) {
const allNodeCredentials = Object.entries(node.credentials);
for (const [type, name] of allNodeCredentials) {
if (typeof name === 'string') {
const nodeCredentials: INodeCredentialsDetails = {
id: null,
name,
};

const matchingCredentials = credentialsEntities.filter(
(credentials) => credentials.name === name && credentials.type === type,
);

if (matchingCredentials.length === 1) {
nodeCredentials.id = matchingCredentials[0].id.toString();
}

// eslint-disable-next-line no-param-reassign
node.credentials[type] = nodeCredentials;
}
}
}
}
}

0 comments on commit fe07ead

Please sign in to comment.