Improvements

This commit is contained in:
ricardo 2020-05-30 19:03:58 -04:00
parent 91c40367e1
commit 4e9490a88d
11 changed files with 62 additions and 45 deletions

View file

@ -241,13 +241,26 @@ export class ActiveWorkflowRunner {
} }
} catch (error) { } catch (error) {
// The workflow was saved with two webhooks with the
// same path/method so delete all webhooks saved let errorMessage = '';
await Db.collections.Webhook?.delete({ workflowId: workflow.id }); await Db.collections.Webhook?.delete({ workflowId: workflow.id });
// then show error to the user // if it's a workflow from the the insert
throw new Error(error.message || error.detail); // TODO check if there is standard error code for deplicate key violation that works
// with all databases
if (error.name === 'QueryFailedError') {
errorMessage = `The webhook path [${webhook.webhookPath}] and method [${webhook.method}] already exist.`;
} else if (error.detail) {
// it's a error runnig the webhook methods (checkExists, create)
errorMessage = error.detail;
} else {
errorMessage = error;
}
throw new Error(errorMessage);
} }
} }
// Save static data! // Save static data!

View file

@ -44,7 +44,6 @@ export interface IDatabaseCollections {
} }
export interface IWebhookDb { export interface IWebhookDb {
id?: number | ObjectID;
workflowId: number | string | ObjectID; workflowId: number | string | ObjectID;
webhookPath: string; webhookPath: string;
method: string; method: string;

View file

@ -141,12 +141,14 @@ export class TestWebhooks {
let key: string; let key: string;
for (const webhookData of webhooks) { for (const webhookData of webhooks) {
key = this.activeWebhooks!.getWebhookKey(webhookData.httpMethod, webhookData.path); key = this.activeWebhooks!.getWebhookKey(webhookData.httpMethod, webhookData.path);
await this.activeWebhooks!.add(workflow, webhookData, mode);
this.testWebhookData[key] = { this.testWebhookData[key] = {
sessionId, sessionId,
timeout, timeout,
workflowData, workflowData,
}; };
await this.activeWebhooks!.add(workflow, webhookData, mode);
// Save static data! // Save static data!
this.testWebhookData[key].workflowData.staticData = workflow.staticData; this.testWebhookData[key].workflowData.staticData = workflow.staticData;

View file

@ -1,9 +1,7 @@
import { import {
Column, Column,
Entity, Entity,
Unique, PrimaryColumn,
ObjectIdColumn,
ObjectID,
} from 'typeorm'; } from 'typeorm';
import { import {
@ -11,19 +9,15 @@ import {
} from '../../Interfaces'; } from '../../Interfaces';
@Entity() @Entity()
@Unique(['webhookPath', 'method'])
export class WebhookEntity implements IWebhookDb { export class WebhookEntity implements IWebhookDb {
@ObjectIdColumn()
id: ObjectID;
@Column() @Column()
workflowId: number; workflowId: number;
@Column() @PrimaryColumn()
webhookPath: string; webhookPath: string;
@Column() @PrimaryColumn()
method: string; method: string;
@Column() @Column()

View file

@ -1,8 +1,7 @@
import { import {
Column, Column,
Entity, Entity,
Unique, PrimaryColumn,
PrimaryGeneratedColumn,
} from 'typeorm'; } from 'typeorm';
import { import {
@ -10,19 +9,15 @@ import {
} from '../../Interfaces'; } from '../../Interfaces';
@Entity() @Entity()
@Unique(['webhookPath', 'method'])
export class WebhookEntity implements IWebhookDb { export class WebhookEntity implements IWebhookDb {
@PrimaryGeneratedColumn()
id: number;
@Column() @Column()
workflowId: number; workflowId: number;
@Column() @PrimaryColumn()
webhookPath: string; webhookPath: string;
@Column() @PrimaryColumn()
method: string; method: string;
@Column() @Column()

View file

@ -1,8 +1,7 @@
import { import {
Column, Column,
Entity, Entity,
Unique, PrimaryColumn,
PrimaryGeneratedColumn,
} from 'typeorm'; } from 'typeorm';
import { import {
@ -10,19 +9,15 @@ import {
} from '../../'; } from '../../';
@Entity() @Entity()
@Unique(['webhookPath', 'method'])
export class WebhookEntity implements IWebhookDb { export class WebhookEntity implements IWebhookDb {
@PrimaryGeneratedColumn()
id: number;
@Column() @Column()
workflowId: number; workflowId: number;
@Column() @PrimaryColumn()
webhookPath: string; webhookPath: string;
@Column() @PrimaryColumn()
method: string; method: string;
@Column() @Column()

View file

@ -1,4 +1,5 @@
import { MigrationInterface, QueryRunner } from 'typeorm'; import {
MigrationInterface, QueryRunner } from 'typeorm';
import * as config from '../../../../config'; import * as config from '../../../../config';

View file

@ -1,7 +1,18 @@
import {MigrationInterface, QueryRunner} from 'typeorm'; import {
MigrationInterface,
QueryRunner,
} from 'typeorm';
import {
IWorkflowDb,
NodeTypes,
WebhookHelpers,
} from '../../..';
import {
Workflow,
} from 'n8n-workflow/dist/src/Workflow';
import { IWorkflowDb, NodeTypes, WebhookHelpers } from '../../..';
import { Workflow } from 'n8n-workflow/dist/src/Workflow';
import { import {
IWebhookDb, IWebhookDb,
} from '../../../Interfaces'; } from '../../../Interfaces';
@ -18,7 +29,7 @@ export class WebhookModel1589476000887 implements MigrationInterface {
tablePrefix = schema + '.' + tablePrefix; tablePrefix = schema + '.' + tablePrefix;
} }
await queryRunner.query(`CREATE TABLE ${tablePrefix}webhook_entity ("id" SERIAL NOT NULL, "workflowId" integer NOT NULL, "webhookPath" character varying NOT NULL, "method" character varying NOT NULL, "node" character varying NOT NULL, CONSTRAINT "UQ_b21ace2e13596ccd87dc9bf4ea6" UNIQUE ("webhookPath", "method"), CONSTRAINT "PK_202217c8b912cf70b93b1e87256" PRIMARY KEY ("id"))`, undefined); await queryRunner.query(`CREATE TABLE ${tablePrefix}webhook_entity ("workflowId" integer NOT NULL, "webhookPath" character varying NOT NULL, "method" character varying NOT NULL, "node" character varying NOT NULL, CONSTRAINT "PK_b21ace2e13596ccd87dc9bf4ea6" PRIMARY KEY ("webhookPath", "method"))`, undefined);
const workflows = await queryRunner.query(`SELECT * FROM ${tablePrefix}workflow_entity WHERE active=true`) as IWorkflowDb[]; const workflows = await queryRunner.query(`SELECT * FROM ${tablePrefix}workflow_entity WHERE active=true`) as IWorkflowDb[];
const data: IWebhookDb[] = []; const data: IWebhookDb[] = [];

View file

@ -1,8 +1,7 @@
import { import {
Column, Column,
Entity, Entity,
Unique, PrimaryColumn,
PrimaryGeneratedColumn,
} from 'typeorm'; } from 'typeorm';
import { import {
@ -10,19 +9,15 @@ import {
} from '../../Interfaces'; } from '../../Interfaces';
@Entity() @Entity()
@Unique(['webhookPath', 'method'])
export class WebhookEntity implements IWebhookDb { export class WebhookEntity implements IWebhookDb {
@PrimaryGeneratedColumn()
id: number;
@Column() @Column()
workflowId: number; workflowId: number;
@Column() @PrimaryColumn()
webhookPath: string; webhookPath: string;
@Column() @PrimaryColumn()
method: string; method: string;
@Column() @Column()

View file

@ -35,13 +35,20 @@ export class ActiveWebhooks {
throw new Error('Webhooks can only be added for saved workflows as an id is needed!'); throw new Error('Webhooks can only be added for saved workflows as an id is needed!');
} }
const webhookKey = this.getWebhookKey(webhookData.httpMethod, webhookData.path);
//check that there is not a webhook already registed with that path/method
if (this.webhookUrls[webhookKey] !== undefined) {
throw new Error('There is test wenhook registered on that path');
}
if (this.workflowWebhooks[webhookData.workflowId] === undefined) { if (this.workflowWebhooks[webhookData.workflowId] === undefined) {
this.workflowWebhooks[webhookData.workflowId] = []; this.workflowWebhooks[webhookData.workflowId] = [];
} }
// Make the webhook available directly because sometimes to create it successfully // Make the webhook available directly because sometimes to create it successfully
// it gets called // it gets called
this.webhookUrls[this.getWebhookKey(webhookData.httpMethod, webhookData.path)] = webhookData; this.webhookUrls[webhookKey] = webhookData;
const webhookExists = await workflow.runWebhookMethod('checkExists', webhookData, NodeExecuteFunctions, mode, this.testWebhooks); const webhookExists = await workflow.runWebhookMethod('checkExists', webhookData, NodeExecuteFunctions, mode, this.testWebhooks);
if (webhookExists === false) { if (webhookExists === false) {

View file

@ -1585,6 +1585,11 @@ export default mixins(
console.error(e); // eslint-disable-line no-console console.error(e); // eslint-disable-line no-console
} }
node.parameters = nodeParameters !== null ? nodeParameters : {}; node.parameters = nodeParameters !== null ? nodeParameters : {};
// if it's a webhook and the path is empty set the UUID as the default path
if (node.type === 'n8n-nodes-base.webhook' && node.parameters.path === '') {
node.parameters.path = node.webhookPath as string;
}
} }
foundNodeIssues = this.getNodeIssues(nodeType, node); foundNodeIssues = this.getNodeIssues(nodeType, node);