Skip to content

Commit

Permalink
fix(API): Do not add starting node on workflow creation (#6686)
Browse files Browse the repository at this point in the history
* fix(API): Do not add starting node on workflow creation

* chore: Remove comment
  • Loading branch information
ivov authored and netroy committed Jul 18, 2023
1 parent 47bc673 commit 36d549f
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 39 deletions.
4 changes: 2 additions & 2 deletions packages/cli/src/ActiveWorkflowRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ import { WorkflowRunner } from '@/WorkflowRunner';
import { ExternalHooks } from '@/ExternalHooks';
import { whereClause } from './UserManagement/UserManagementHelper';
import { WorkflowsService } from './workflows/workflows.services';
import { START_NODES } from './constants';
import { STARTING_NODES } from './constants';
import { webhookNotFoundErrorMessage } from './utils';
import { In } from 'typeorm';

Expand Down Expand Up @@ -793,7 +793,7 @@ export class ActiveWorkflowRunner {
settings: workflowData.settings,
});

const canBeActivated = workflowInstance.checkIfWorkflowCanBeActivated(START_NODES);
const canBeActivated = workflowInstance.checkIfWorkflowCanBeActivated(STARTING_NODES);
if (!canBeActivated) {
Logger.error(`Unable to activate workflow "${workflowData.name}"`);
throw new Error(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import {
setWorkflowAsActive,
setWorkflowAsInactive,
updateWorkflow,
hasStartNode,
getStartNode,
getSharedWorkflows,
createWorkflow,
getWorkflowIdsViaTags,
Expand All @@ -37,10 +35,6 @@ export = {

workflow.active = false;

if (!hasStartNode(workflow)) {
workflow.nodes.push(getStartNode());
}

await replaceInvalidCredentials(workflow);

addNodeIds(workflow);
Expand Down Expand Up @@ -164,10 +158,6 @@ export = {
return res.status(404).json({ message: 'Not Found' });
}

if (!hasStartNode(updateData)) {
updateData.nodes.push(getStartNode());
}

await replaceInvalidCredentials(updateData);
addNodeIds(updateData);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import type { FindManyOptions, UpdateResult } from 'typeorm';
import { In } from 'typeorm';
import intersection from 'lodash/intersection';
import type { INode } from 'n8n-workflow';
import { v4 as uuid } from 'uuid';

import * as Db from '@/Db';
import type { User } from '@db/entities/User';
Expand All @@ -11,7 +9,6 @@ import { SharedWorkflow } from '@db/entities/SharedWorkflow';
import { isInstanceOwner } from '../users/users.service.ee';
import type { Role } from '@db/entities/Role';
import config from '@/config';
import { START_NODES } from '@/constants';

function insertIf(condition: boolean, elements: string[]): string[] {
return condition ? elements : [];
Expand Down Expand Up @@ -123,25 +120,6 @@ export async function updateWorkflow(
return Db.collections.Workflow.update(workflowId, updateData);
}

export function hasStartNode(workflow: WorkflowEntity): boolean {
if (!workflow.nodes.length) return false;

const found = workflow.nodes.find((node) => START_NODES.includes(node.type));

return Boolean(found);
}

export function getStartNode(): INode {
return {
id: uuid(),
parameters: {},
name: 'Start',
type: 'n8n-nodes-base.start',
typeVersion: 1,
position: [240, 300],
};
}

export function parseTagNames(tags: string): string[] {
return tags.split(',').map((tag) => tag.trim());
}
2 changes: 1 addition & 1 deletion packages/cli/src/PublicApi/v1/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ info:
license:
name: Sustainable Use License
url: https://github.com/n8n-io/n8n/blob/master/packages/cli/LICENSE.md
version: 1.1.0
version: 1.1.1
externalDocs:
description: n8n API documentation
url: https://docs.n8n.io/api/
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function getN8nPackageJson() {
return jsonParse<n8n.PackageJson>(readFileSync(join(CLI_DIR, 'package.json'), 'utf8'));
}

export const START_NODES = ['n8n-nodes-base.start', 'n8n-nodes-base.manualTrigger'];
export const STARTING_NODES = ['n8n-nodes-base.start', 'n8n-nodes-base.manualTrigger'];

export const N8N_VERSION = getN8nPackageJson().version;
export const IS_V1_RELEASE = major(N8N_VERSION) > 0;
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
import { CliWorkflowOperationError, SubworkflowOperationError } from 'n8n-workflow';
import type { INode } from 'n8n-workflow';
import { START_NODES } from './constants';
import { STARTING_NODES } from './constants';

/**
* Returns if the given id is a valid workflow id
Expand All @@ -19,7 +19,7 @@ function findWorkflowStart(executionMode: 'integrated' | 'cli') {

if (executeWorkflowTriggerNode) return executeWorkflowTriggerNode;

const startNode = nodes.find((node) => START_NODES.includes(node.type));
const startNode = nodes.find((node) => STARTING_NODES.includes(node.type));

if (startNode) return startNode;

Expand Down
32 changes: 31 additions & 1 deletion packages/cli/test/integration/publicApi/workflows.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import type { ActiveWorkflowRunner } from '@/ActiveWorkflowRunner';
import { randomApiKey } from '../shared/random';
import * as utils from '../shared/utils';
import * as testDb from '../shared/testDb';
// import { generateNanoId } from '@/databases/utils/generators';
import type { INode } from 'n8n-workflow';
import { STARTING_NODES } from '@/constants';

let app: Application;
let workflowOwnerRole: Role;
Expand Down Expand Up @@ -702,6 +703,35 @@ describe('POST /workflows', () => {
expect(sharedWorkflow?.workflow.createdAt.toISOString()).toBe(createdAt);
expect(sharedWorkflow?.role).toEqual(workflowOwnerRole);
});

test('should not add a starting node if the payload has no starting nodes', async () => {
const response = await authMemberAgent.post('/workflows').send({
name: 'testing',
nodes: [
{
id: 'uuid-1234',
parameters: {},
name: 'Hacker News',
type: 'n8n-nodes-base.hackerNews',
typeVersion: 1,
position: [240, 300],
},
],
connections: {},
settings: {
saveExecutionProgress: true,
saveManualExecutions: true,
saveDataErrorExecution: 'all',
saveDataSuccessExecution: 'all',
executionTimeout: 3600,
timezone: 'America/New_York',
},
});

const found = response.body.nodes.find((node: INode) => STARTING_NODES.includes(node.type));

expect(found).toBeUndefined();
});
});

describe('PUT /workflows/:id', () => {
Expand Down

0 comments on commit 36d549f

Please sign in to comment.