refactor(core): Abstract away duplication when finding first pinned trigger (no-changelog) (#5927)

This commit is contained in:
Michael Kret 2023-08-10 15:06:16 +03:00 committed by GitHub
parent 258e0ebb0f
commit 7f0db60f15
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 66 additions and 16 deletions

View file

@ -26,11 +26,13 @@ import type {
IWorkflowExecutionDataProcess, IWorkflowExecutionDataProcess,
} from '@/Interfaces'; } from '@/Interfaces';
import { NodeTypes } from '@/NodeTypes'; import { NodeTypes } from '@/NodeTypes';
// eslint-disable-next-line import/no-cycle
import { WorkflowRunner } from '@/WorkflowRunner'; import { WorkflowRunner } from '@/WorkflowRunner';
import config from '@/config'; import config from '@/config';
import type { WorkflowEntity } from '@db/entities/WorkflowEntity'; import type { WorkflowEntity } from '@db/entities/WorkflowEntity';
import type { User } from '@db/entities/User'; import type { User } from '@db/entities/User';
import omit from 'lodash/omit'; import omit from 'lodash/omit';
// eslint-disable-next-line import/no-cycle
import { PermissionChecker } from './UserManagement/PermissionChecker'; import { PermissionChecker } from './UserManagement/PermissionChecker';
import { isWorkflowIdValid } from './utils'; import { isWorkflowIdValid } from './utils';
import { UserService } from './user/user.service'; import { UserService } from './user/user.service';
@ -575,6 +577,18 @@ export function validateWorkflowCredentialUsage(
return newWorkflowVersion; return newWorkflowVersion;
} }
export function getExecutionStartNode(data: IWorkflowExecutionDataProcess, workflow: Workflow) {
let startNode;
if (
data.startNodes?.length === 1 &&
Object.keys(data.pinData ?? {}).includes(data.startNodes[0])
) {
startNode = workflow.getNode(data.startNodes[0]) ?? undefined;
}
return startNode;
}
export async function getVariables(): Promise<IDataObject> { export async function getVariables(): Promise<IDataObject> {
const variables = await Container.get(VariablesService).getAllCached(); const variables = await Container.get(VariablesService).getAllCached();
return Object.freeze( return Object.freeze(

View file

@ -37,9 +37,12 @@ import type {
} from '@/Interfaces'; } from '@/Interfaces';
import { NodeTypes } from '@/NodeTypes'; import { NodeTypes } from '@/NodeTypes';
import type { Job, JobData, JobQueue, JobResponse } from '@/Queue'; import type { Job, JobData, JobQueue, JobResponse } from '@/Queue';
// eslint-disable-next-line import/no-cycle
import { Queue } from '@/Queue'; import { Queue } from '@/Queue';
import * as WebhookHelpers from '@/WebhookHelpers'; import * as WebhookHelpers from '@/WebhookHelpers';
// eslint-disable-next-line import/no-cycle
import * as WorkflowHelpers from '@/WorkflowHelpers'; import * as WorkflowHelpers from '@/WorkflowHelpers';
// eslint-disable-next-line import/no-cycle
import * as WorkflowExecuteAdditionalData from '@/WorkflowExecuteAdditionalData'; import * as WorkflowExecuteAdditionalData from '@/WorkflowExecuteAdditionalData';
import { generateFailedExecutionFromError } from '@/WorkflowHelpers'; import { generateFailedExecutionFromError } from '@/WorkflowHelpers';
import { initErrorHandling } from '@/ErrorReporting'; import { initErrorHandling } from '@/ErrorReporting';
@ -345,13 +348,7 @@ export class WorkflowRunner {
Logger.debug(`Execution ID ${executionId} will run executing all nodes.`, { executionId }); Logger.debug(`Execution ID ${executionId} will run executing all nodes.`, { executionId });
// Execute all nodes // Execute all nodes
let startNode; const startNode = WorkflowHelpers.getExecutionStartNode(data, workflow);
if (
data.startNodes?.length === 1 &&
Object.keys(data.pinData ?? {}).includes(data.startNodes[0])
) {
startNode = workflow.getNode(data.startNodes[0]) ?? undefined;
}
// Can execute without webhook so go on // Can execute without webhook so go on
const workflowExecute = new WorkflowExecute(additionalData, data.executionMode); const workflowExecute = new WorkflowExecute(additionalData, data.executionMode);

View file

@ -292,13 +292,7 @@ class WorkflowRunnerProcess {
) { ) {
// Execute all nodes // Execute all nodes
let startNode; const startNode = WorkflowHelpers.getExecutionStartNode(this.data, this.workflow);
if (
this.data.startNodes?.length === 1 &&
Object.keys(this.data.pinData ?? {}).includes(this.data.startNodes[0])
) {
startNode = this.workflow.getNode(this.data.startNodes[0]) ?? undefined;
}
// Can execute without webhook so go on // Can execute without webhook so go on
this.workflowExecute = new WorkflowExecute(additionalData, this.data.executionMode); this.workflowExecute = new WorkflowExecute(additionalData, this.data.executionMode);

View file

@ -1,9 +1,14 @@
import type { INode } from 'n8n-workflow'; import type { INode } from 'n8n-workflow';
import { LoggerProxy } from 'n8n-workflow'; import { LoggerProxy, type Workflow } from 'n8n-workflow';
import { WorkflowEntity } from '@db/entities/WorkflowEntity'; import { WorkflowEntity } from '@db/entities/WorkflowEntity';
import { CredentialsEntity } from '@db/entities/CredentialsEntity'; import { CredentialsEntity } from '@db/entities/CredentialsEntity';
import { getNodesWithInaccessibleCreds, validateWorkflowCredentialUsage } from '@/WorkflowHelpers'; import {
getExecutionStartNode,
getNodesWithInaccessibleCreds,
validateWorkflowCredentialUsage,
} from '@/WorkflowHelpers';
import { getLogger } from '@/Logger'; import { getLogger } from '@/Logger';
import type { IWorkflowExecutionDataProcess } from '../../src/Interfaces';
const FIRST_CREDENTIAL_ID = '1'; const FIRST_CREDENTIAL_ID = '1';
const SECOND_CREDENTIAL_ID = '2'; const SECOND_CREDENTIAL_ID = '2';
@ -145,6 +150,46 @@ describe('WorkflowHelpers', () => {
}).toThrow(); }).toThrow();
}); });
}); });
describe('getExecutionStartNode', () => {
it('Should return undefined', () => {
const data = {
pinData: {},
startNodes: [],
} as unknown as IWorkflowExecutionDataProcess;
const workflow = {
getNode(nodeName: string) {
return {
name: nodeName,
};
},
} as unknown as Workflow;
const executionStartNode = getExecutionStartNode(data, workflow);
expect(executionStartNode).toBeUndefined();
});
it('Should return startNode', () => {
const data = {
pinData: {
node1: {},
node2: {},
},
startNodes: ['node2'],
} as unknown as IWorkflowExecutionDataProcess;
const workflow = {
getNode(nodeName: string) {
if (nodeName === 'node2') {
return {
name: 'node2',
};
}
return undefined;
},
} as unknown as Workflow;
const executionStartNode = getExecutionStartNode(data, workflow);
expect(executionStartNode).toEqual({
name: 'node2',
});
});
});
}); });
function generateCredentialEntity(credentialId: string) { function generateCredentialEntity(credentialId: string) {