Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): Expose item index being processed #3590

Merged
merged 5 commits into from
Jun 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/core/src/NodeExecuteFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2317,6 +2317,9 @@ export function getExecuteSingleFunctions(

return allItems[itemIndex];
},
getItemIndex() {
return itemIndex;
},
getMode: (): WorkflowExecuteMode => {
return mode;
},
Expand Down
1 change: 1 addition & 0 deletions packages/workflow/src/Interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,7 @@ export interface IExecuteSingleFunctions {
getContext(type: string): IContextObject;
getCredentials(type: string): Promise<ICredentialDataDecryptedObject>;
getInputData(inputIndex?: number, inputName?: string): INodeExecutionData;
getItemIndex(): number;
getMode(): WorkflowExecuteMode;
getNode(): INode;
getNodeParameter(
Expand Down
3 changes: 3 additions & 0 deletions packages/workflow/test/Helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,9 @@ export function getExecuteSingleFunctions(

return allItems[itemIndex];
},
getItemIndex: () => {
return itemIndex;
},
getMode: (): WorkflowExecuteMode => {
return mode;
},
Expand Down
190 changes: 190 additions & 0 deletions packages/workflow/test/RoutingNode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1748,4 +1748,194 @@ describe('RoutingNode', () => {
});
}
});

describe('itemIndex', () => {
const tests: Array<{
description: string;
input: {
nodeType: {
properties?: INodeProperties[];
credentials?: INodeCredentialDescription[];
requestDefaults?: IHttpRequestOptions;
requestOperations?: IN8nRequestOperations;
};
node: {
parameters: INodeParameters;
};
};
output: INodeExecutionData[][] | undefined;
}> = [
{
description: 'single parameter, only send defined, fixed value, using requestDefaults',
input: {
nodeType: {
requestDefaults: {
baseURL: 'http://127.0.0.1:5678',
url: '/test-url',
},
properties: [
{
displayName: 'Email',
name: 'email',
type: 'string',
routing: {
send: {
property: 'toEmail',
type: 'body',
value: 'fixedValue',
},
},
default: '',
},
],
},
node: {
parameters: {},
},
},
output: [
[
{
json: {
headers: {},
statusCode: 200,
requestOptions: {
url: '/test-url',
qs: {},
body: {
toEmail: 'fixedValue',
},
baseURL: 'http://127.0.0.1:5678',
returnFullResponse: true,
},
},
},
],
],
},
];

const nodeTypes = Helpers.NodeTypes();
const baseNode: INode = {
parameters: {},
name: 'test',
type: 'test.set',
typeVersion: 1,
position: [0, 0],
};

const mode = 'internal';
const runIndex = 0;
const itemIndex = 0;
const connectionInputData: INodeExecutionData[] = [];
const runExecutionData: IRunExecutionData = { resultData: { runData: {} } };
const additionalData = Helpers.WorkflowExecuteAdditionalData();
const nodeType = nodeTypes.getByNameAndVersion(baseNode.type);

const inputData: ITaskDataConnections = {
main: [
[
{
json: {},
},
{
json: {},
},
{
json: {},
},
],
],
};

for (const testData of tests) {
test(testData.description, async () => {
const node: INode = { ...baseNode, ...testData.input.node };

const workflowData = {
nodes: [node],
connections: {},
};

// @ts-ignore
nodeType.description = { ...testData.input.nodeType };

const workflow = new Workflow({
nodes: workflowData.nodes,
connections: workflowData.connections,
active: false,
nodeTypes,
});

const routingNode = new RoutingNode(
workflow,
node,
connectionInputData,
runExecutionData ?? null,
additionalData,
mode,
);

const executeData = {
data: {},
node,
source: null,
} as IExecuteData;

let currentItemIndex = 0;
for (let iteration = 0; iteration < inputData.main[0]!.length; iteration++) {
// @ts-ignore
const nodeExecuteFunctions: INodeExecuteFunctions = {
getExecuteFunctions: () => {
return Helpers.getExecuteFunctions(
workflow,
runExecutionData,
runIndex,
connectionInputData,
{},
node,
itemIndex + iteration,
additionalData,
executeData,
mode,
);
},
getExecuteSingleFunctions: () => {
return Helpers.getExecuteSingleFunctions(
workflow,
runExecutionData,
runIndex,
connectionInputData,
{},
node,
itemIndex + iteration,
additionalData,
executeData,
mode,
);
},
};

const routingNodeExecutionContext = nodeExecuteFunctions.getExecuteSingleFunctions(
routingNode.workflow,
routingNode.runExecutionData,
runIndex,
routingNode.connectionInputData,
inputData,
routingNode.node,
iteration,
routingNode.additionalData,
executeData,
routingNode.mode,
);

currentItemIndex = routingNodeExecutionContext.getItemIndex();
}

const expectedItemIndex = inputData.main[0]!.length - 1;

expect(currentItemIndex).toEqual(expectedItemIndex);
});
}
});
});