File size: 27,460 Bytes
aec3094 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 | /* eslint-disable @typescript-eslint/no-loop-func */
import type { User } from '@n8n/db';
import { WorkflowRepository } from '@n8n/db';
import { Container } from '@n8n/di';
import { Flags } from '@oclif/core';
import fs from 'fs';
import { diff } from 'json-diff';
import pick from 'lodash/pick';
import type { IRun, ITaskData, IWorkflowBase, IWorkflowExecutionDataProcess } from 'n8n-workflow';
import { jsonParse, UnexpectedError } from 'n8n-workflow';
import os from 'os';
import { sep } from 'path';
import { ActiveExecutions } from '@/active-executions';
import { OwnershipService } from '@/services/ownership.service';
import { findCliWorkflowStart } from '@/utils';
import { WorkflowRunner } from '@/workflow-runner';
import { BaseCommand } from './base-command';
import config from '../config';
import type {
IExecutionResult,
INodeSpecialCase,
INodeSpecialCases,
IResult,
IWorkflowExecutionProgress,
} from '../types/commands.types';
const re = /\d+/;
interface ISkipList {
workflowId: string;
status: string;
skipReason: string;
ticketReference: string;
}
export class ExecuteBatch extends BaseCommand {
static description = '\nExecutes multiple workflows once';
static cancelled = false;
static workflowExecutionsProgress: IWorkflowExecutionProgress[][];
static shallow = false;
static compare: string;
static snapshot: string;
static concurrency = 1;
static githubWorkflow = false;
static debug = false;
static executionTimeout = 3 * 60 * 1000;
static instanceOwner: User;
static examples = [
'$ n8n executeBatch',
'$ n8n executeBatch --concurrency=10 --skipList=/data/skipList.json',
'$ n8n executeBatch --debug --output=/data/output.json',
'$ n8n executeBatch --ids=10,13,15 --shortOutput',
'$ n8n executeBatch --snapshot=/data/snapshots --shallow',
'$ n8n executeBatch --compare=/data/previousExecutionData --retries=2',
];
static flags = {
help: Flags.help({ char: 'h' }),
debug: Flags.boolean({
description: 'Toggles on displaying all errors and debug messages.',
}),
ids: Flags.string({
description:
'Specifies workflow IDs to get executed, separated by a comma or a file containing the ids',
}),
concurrency: Flags.integer({
default: 1,
description:
'How many workflows can run in parallel. Defaults to 1 which means no concurrency.',
}),
output: Flags.string({
description:
'Enable execution saving, You must inform an existing folder to save execution via this param',
}),
snapshot: Flags.string({
description:
'Enables snapshot saving. You must inform an existing folder to save snapshots via this param.',
}),
compare: Flags.string({
description:
'Compares current execution with an existing snapshot. You must inform an existing folder where the snapshots are saved.',
}),
shallow: Flags.boolean({
description:
'Compares only if attributes output from node are the same, with no regards to nested JSON objects.',
}),
githubWorkflow: Flags.boolean({
description:
'Enables more lenient comparison for GitHub workflows. This is useful for reducing false positives when comparing Test workflows.',
}),
skipList: Flags.string({
description: 'File containing a comma separated list of workflow IDs to skip.',
}),
retries: Flags.integer({
description: 'Retries failed workflows up to N tries. Default is 1. Set 0 to disable.',
default: 1,
}),
shortOutput: Flags.boolean({
description: 'Omits the full execution information from output, displaying only summary.',
}),
};
static aliases = ['executeBatch'];
override needsCommunityPackages = true;
override needsTaskRunner = true;
/**
* Gracefully handles exit.
* @param {boolean} skipExit Whether to skip exit or number according to received signal
*/
async stopProcess(skipExit: boolean | number = false) {
if (ExecuteBatch.cancelled) {
process.exit(0);
}
ExecuteBatch.cancelled = true;
await Container.get(ActiveExecutions).shutdown(true);
// We may receive true but when called from `process.on`
// we get the signal (SIGINT, etc.)
if (skipExit !== true) {
process.exit(0);
}
}
private formatJsonOutput(data: object) {
return JSON.stringify(data, null, 2);
}
private shouldBeConsideredAsWarning(errorMessage: string) {
const warningStrings = [
'refresh token is invalid',
'unable to connect to',
'econnreset',
'429',
'econnrefused',
'missing a required parameter',
'insufficient credit balance',
'internal server error',
'503',
'502',
'504',
'insufficient balance',
'request timed out',
'status code 401',
];
errorMessage = errorMessage.toLowerCase();
for (let i = 0; i < warningStrings.length; i++) {
if (errorMessage.includes(warningStrings[i])) {
return true;
}
}
return false;
}
async init() {
await super.init();
await this.initBinaryDataService();
await this.initDataDeduplicationService();
await this.initExternalHooks();
}
// eslint-disable-next-line complexity
async run() {
const { flags } = await this.parse(ExecuteBatch);
ExecuteBatch.debug = flags.debug;
ExecuteBatch.concurrency = flags.concurrency || 1;
const ids: string[] = [];
const skipIds: string[] = [];
if (flags.snapshot !== undefined) {
if (fs.existsSync(flags.snapshot)) {
if (!fs.lstatSync(flags.snapshot).isDirectory()) {
this.logger.error('The parameter --snapshot must be an existing directory');
return;
}
} else {
this.logger.error('The parameter --snapshot must be an existing directory');
return;
}
ExecuteBatch.snapshot = flags.snapshot;
}
if (flags.compare !== undefined) {
if (fs.existsSync(flags.compare)) {
if (!fs.lstatSync(flags.compare).isDirectory()) {
this.logger.error('The parameter --compare must be an existing directory');
return;
}
} else {
this.logger.error('The parameter --compare must be an existing directory');
return;
}
ExecuteBatch.compare = flags.compare;
}
if (flags.output !== undefined) {
if (fs.existsSync(flags.output)) {
if (fs.lstatSync(flags.output).isDirectory()) {
this.logger.error('The parameter --output must be a writable file');
return;
}
}
}
if (flags.ids !== undefined) {
if (fs.existsSync(flags.ids)) {
const contents = fs.readFileSync(flags.ids, { encoding: 'utf-8' });
ids.push(
...contents
.trimEnd()
.split(',')
.filter((id) => re.exec(id)),
);
} else {
const paramIds = flags.ids.split(',');
const matchedIds = paramIds.filter((id) => re.exec(id));
if (matchedIds.length === 0) {
this.logger.error(
'The parameter --ids must be a list of numeric IDs separated by a comma or a file with this content.',
);
return;
}
ids.push(...matchedIds);
}
}
if (flags.skipList !== undefined) {
if (fs.existsSync(flags.skipList)) {
const contents = fs.readFileSync(flags.skipList, { encoding: 'utf-8' });
try {
const parsedSkipList = JSON.parse(contents) as ISkipList[];
parsedSkipList.forEach((item) => {
skipIds.push(item.workflowId);
});
} catch (error) {
this.logger.error('Skip list file is not a valid JSON. Exiting.');
return;
}
} else {
this.logger.error('Skip list file not found. Exiting.');
return;
}
}
if (flags.shallow) {
ExecuteBatch.shallow = true;
}
if (flags.githubWorkflow) {
ExecuteBatch.githubWorkflow = true;
}
ExecuteBatch.instanceOwner = await Container.get(OwnershipService).getInstanceOwner();
const query = Container.get(WorkflowRepository).createQueryBuilder('workflows');
if (ids.length > 0) {
query.andWhere('workflows.id in (:...ids)', { ids });
}
if (skipIds.length > 0) {
query.andWhere('workflows.id not in (:...skipIds)', { skipIds });
}
const allWorkflows = (await query.getMany()) as IWorkflowBase[];
if (ExecuteBatch.debug) {
process.stdout.write(`Found ${allWorkflows.length} workflows to execute.\n`);
}
// Send a shallow copy of allWorkflows so we still have all workflow data.
const results = await this.runTests([...allWorkflows]);
let { retries } = flags;
while (
retries > 0 &&
results.summary.warningExecutions + results.summary.failedExecutions > 0 &&
!ExecuteBatch.cancelled
) {
const failedWorkflowIds = results.summary.errors.map((execution) => execution.workflowId);
failedWorkflowIds.push(...results.summary.warnings.map((execution) => execution.workflowId));
const newWorkflowList = allWorkflows.filter((workflow) =>
failedWorkflowIds.includes(workflow.id),
);
const retryResults = await this.runTests(newWorkflowList);
this.mergeResults(results, retryResults);
// By now, `results` has been updated with the new successful executions.
retries--;
}
if (flags.output !== undefined) {
fs.writeFileSync(flags.output, this.formatJsonOutput(results));
this.logger.info('\nExecution finished.');
this.logger.info('Summary:');
this.logger.info(`\tSuccess: ${results.summary.successfulExecutions}`);
this.logger.info(`\tFailures: ${results.summary.failedExecutions}`);
this.logger.info(`\tWarnings: ${results.summary.warningExecutions}`);
this.logger.info('\nNodes successfully tested:');
Object.entries(results.coveredNodes).forEach(([nodeName, nodeCount]) => {
this.logger.info(`\t${nodeName}: ${nodeCount}`);
});
this.logger.info('\nCheck the JSON file for more details.');
} else if (flags.shortOutput) {
this.logger.info(
this.formatJsonOutput({
...results,
executions: results.executions.filter(
(execution) => execution.executionStatus !== 'success',
),
}),
);
} else {
this.logger.info(this.formatJsonOutput(results));
}
await this.stopProcess(true);
if (results.summary.failedExecutions > 0) {
this.exit(1);
}
}
mergeResults(results: IResult, retryResults: IResult) {
if (retryResults.summary.successfulExecutions === 0) {
// Nothing to replace.
return;
}
// Find successful executions and replace them on previous result.
retryResults.executions.forEach((newExecution) => {
if (newExecution.executionStatus === 'success') {
// Remove previous execution from list.
results.executions = results.executions.filter(
(previousExecutions) => previousExecutions.workflowId !== newExecution.workflowId,
);
const errorIndex = results.summary.errors.findIndex(
(summaryInformation) => summaryInformation.workflowId === newExecution.workflowId,
);
if (errorIndex !== -1) {
// This workflow errored previously. Decrement error count.
results.summary.failedExecutions--;
// Remove from the list of errors.
results.summary.errors.splice(errorIndex, 1);
}
const warningIndex = results.summary.warnings.findIndex(
(summaryInformation) => summaryInformation.workflowId === newExecution.workflowId,
);
if (warningIndex !== -1) {
// This workflow errored previously. Decrement error count.
results.summary.warningExecutions--;
// Remove from the list of errors.
results.summary.warnings.splice(warningIndex, 1);
}
// Increment successful executions count and push it to all executions array.
results.summary.successfulExecutions++;
results.executions.push(newExecution);
}
});
}
private async runTests(allWorkflows: IWorkflowBase[]): Promise<IResult> {
const result: IResult = {
totalWorkflows: allWorkflows.length,
slackMessage: '',
summary: {
failedExecutions: 0,
warningExecutions: 0,
successfulExecutions: 0,
errors: [],
warnings: [],
},
coveredNodes: {},
executions: [],
};
if (ExecuteBatch.debug) {
this.initializeLogs();
}
return await new Promise(async (res) => {
const promisesArray = [];
for (let i = 0; i < ExecuteBatch.concurrency; i++) {
const promise = new Promise(async (resolve) => {
let workflow: IWorkflowBase | undefined;
while (allWorkflows.length > 0) {
workflow = allWorkflows.shift();
if (ExecuteBatch.cancelled) {
process.stdout.write(`Thread ${i + 1} resolving and quitting.`);
resolve(true);
break;
}
// This if shouldn't be really needed
// but it's a concurrency precaution.
if (workflow === undefined) {
resolve(true);
return;
}
if (ExecuteBatch.debug) {
ExecuteBatch.workflowExecutionsProgress[i].push({
workflowId: workflow.id,
status: 'running',
});
this.updateStatus();
}
await this.startThread(workflow).then((executionResult) => {
if (ExecuteBatch.debug) {
ExecuteBatch.workflowExecutionsProgress[i].pop();
}
result.executions.push(executionResult);
if (executionResult.executionStatus === 'success') {
if (ExecuteBatch.debug) {
ExecuteBatch.workflowExecutionsProgress[i].push({
workflowId: workflow!.id,
status: 'success',
});
this.updateStatus();
}
result.summary.successfulExecutions++;
const nodeNames = Object.keys(executionResult.coveredNodes);
nodeNames.map((nodeName) => {
if (result.coveredNodes[nodeName] === undefined) {
result.coveredNodes[nodeName] = 0;
}
result.coveredNodes[nodeName] += executionResult.coveredNodes[nodeName];
});
} else if (executionResult.executionStatus === 'warning') {
result.summary.warningExecutions++;
result.summary.warnings.push({
workflowId: executionResult.workflowId,
error: executionResult.error!,
});
if (ExecuteBatch.debug) {
ExecuteBatch.workflowExecutionsProgress[i].push({
workflowId: workflow!.id,
status: 'warning',
});
this.updateStatus();
}
} else if (executionResult.executionStatus === 'error') {
result.summary.failedExecutions++;
result.summary.errors.push({
workflowId: executionResult.workflowId,
error: executionResult.error!,
});
if (ExecuteBatch.debug) {
ExecuteBatch.workflowExecutionsProgress[i].push({
workflowId: workflow!.id,
status: 'error',
});
this.updateStatus();
}
} else {
throw new UnexpectedError('Wrong execution status - cannot proceed');
}
});
}
resolve(true);
});
promisesArray.push(promise);
}
await Promise.allSettled(promisesArray);
if (ExecuteBatch.githubWorkflow) {
if (result.summary.errors.length < 6) {
const errorMessage = result.summary.errors.map((error) => {
return `*${error.workflowId}*: ${error.error}`;
});
result.slackMessage = `*${
result.summary.errors.length
} Executions errors*. Workflows failing: ${errorMessage.join(' ')} `;
} else {
result.slackMessage = `*${result.summary.errors.length} Executions errors*`;
}
this.setOutput('slackMessage', JSON.stringify(result.slackMessage));
}
res(result);
});
}
setOutput(key: string, value: any) {
// Temporary hack until we move to the new action.
const output = process.env.GITHUB_OUTPUT;
fs.appendFileSync(output as unknown as fs.PathOrFileDescriptor, `${key}=${value}${os.EOL}`);
}
updateStatus() {
if (ExecuteBatch.cancelled) {
return;
}
if (process.stdout.isTTY) {
process.stdout.moveCursor(0, -ExecuteBatch.concurrency);
process.stdout.cursorTo(0);
process.stdout.clearLine(0);
}
ExecuteBatch.workflowExecutionsProgress.map((concurrentThread, index) => {
let message = `${index + 1}: `;
concurrentThread.map((executionItem, workflowIndex) => {
let openColor = '\x1b[0m';
const closeColor = '\x1b[0m';
switch (executionItem.status) {
case 'success':
openColor = '\x1b[32m';
break;
case 'error':
openColor = '\x1b[31m';
break;
case 'warning':
openColor = '\x1b[33m';
break;
default:
break;
}
message += `${workflowIndex > 0 ? ', ' : ''}${openColor}${
executionItem.workflowId
}${closeColor}`;
});
if (process.stdout.isTTY) {
process.stdout.cursorTo(0);
process.stdout.clearLine(0);
}
process.stdout.write(`${message}\n`);
});
}
initializeLogs() {
process.stdout.write('**********************************************\n');
process.stdout.write(' n8n test workflows\n');
process.stdout.write('**********************************************\n');
process.stdout.write('\n');
process.stdout.write('Batch number:\n');
ExecuteBatch.workflowExecutionsProgress = [];
for (let i = 0; i < ExecuteBatch.concurrency; i++) {
ExecuteBatch.workflowExecutionsProgress.push([]);
process.stdout.write(`${i + 1}: \n`);
}
}
async startThread(workflowData: IWorkflowBase): Promise<IExecutionResult> {
// This will be the object returned by the promise.
// It will be updated according to execution progress below.
const executionResult: IExecutionResult = {
workflowId: workflowData.id,
workflowName: workflowData.name,
executionTime: 0,
finished: false,
executionStatus: 'running',
coveredNodes: {},
};
// We have a cool feature here.
// On each node, on the Settings tab in the node editor you can change
// the `Notes` field to add special cases for comparison and snapshots.
// You need to set one configuration per line with the following possible keys:
// CAP_RESULTS_LENGTH=x where x is a number. Cap the number of rows from this node to x.
// This means if you set CAP_RESULTS_LENGTH=1 we will have only 1 row in the output
// IGNORED_PROPERTIES=x,y,z where x, y and z are JSON property names. Removes these
// properties from the JSON object (useful for optional properties that can
// cause the comparison to detect changes when not true).
const nodeEdgeCases = {} as INodeSpecialCases;
workflowData.nodes.forEach((node) => {
executionResult.coveredNodes[node.type] = (executionResult.coveredNodes[node.type] || 0) + 1;
if (node.notes !== undefined && node.notes !== '') {
node.notes.split('\n').forEach((note) => {
const parts = note.split('=');
if (parts.length === 2) {
if (nodeEdgeCases[node.name] === undefined) {
nodeEdgeCases[node.name] = {} as INodeSpecialCase;
}
if (parts[0] === 'CAP_RESULTS_LENGTH') {
nodeEdgeCases[node.name].capResults = parseInt(parts[1], 10);
} else if (parts[0] === 'IGNORED_PROPERTIES') {
nodeEdgeCases[node.name].ignoredProperties = parts[1]
.split(',')
.map((property) => property.trim());
} else if (parts[0] === 'KEEP_ONLY_PROPERTIES') {
nodeEdgeCases[node.name].keepOnlyProperties = parts[1]
.split(',')
.map((property) => property.trim());
}
}
});
}
});
const workflowRunner = Container.get(WorkflowRunner);
if (config.getEnv('executions.mode') === 'queue') {
this.logger.warn('`executeBatch` does not support queue mode. Falling back to regular mode.');
workflowRunner.setExecutionMode('regular');
}
return await new Promise(async (resolve) => {
let gotCancel = false;
// Timeouts execution after 5 minutes.
const timeoutTimer = setTimeout(() => {
gotCancel = true;
executionResult.error = 'Workflow execution timed out.';
executionResult.executionStatus = 'warning';
resolve(executionResult);
}, ExecuteBatch.executionTimeout);
try {
const startingNode = findCliWorkflowStart(workflowData.nodes);
const runData: IWorkflowExecutionDataProcess = {
executionMode: 'cli',
startNodes: [{ name: startingNode.name, sourceData: null }],
workflowData,
userId: ExecuteBatch.instanceOwner.id,
};
const executionId = await workflowRunner.run(runData);
const activeExecutions = Container.get(ActiveExecutions);
const data = await activeExecutions.getPostExecutePromise(executionId);
if (gotCancel || ExecuteBatch.cancelled) {
clearTimeout(timeoutTimer);
// The promise was settled already so we simply ignore.
return;
}
if (data === undefined) {
executionResult.error = 'Workflow did not return any data.';
executionResult.executionStatus = 'error';
} else {
executionResult.executionTime =
(Date.parse(data.stoppedAt as unknown as string) -
Date.parse(data.startedAt as unknown as string)) /
1000;
executionResult.finished = data?.finished !== undefined;
const resultError = data.data.resultData.error;
if (resultError) {
executionResult.error = resultError.description || resultError.message;
if (data.data.resultData.lastNodeExecuted !== undefined) {
executionResult.error += ` on node ${data.data.resultData.lastNodeExecuted}`;
}
executionResult.executionStatus = 'error';
if (this.shouldBeConsideredAsWarning(executionResult.error || '')) {
executionResult.executionStatus = 'warning';
}
} else {
if (ExecuteBatch.shallow) {
// What this does is guarantee that top-level attributes
// from the JSON are kept and the are the same type.
// We convert nested JSON objects to a simple {object:true}
// and we convert nested arrays to ['json array']
// This reduces the chance of false positives but may
// result in not detecting deeper changes.
Object.keys(data.data.resultData.runData).map((nodeName: string) => {
data.data.resultData.runData[nodeName].map((taskData: ITaskData) => {
if (taskData.data === undefined) {
return;
}
Object.keys(taskData.data).map((connectionName) => {
const connection = taskData.data![connectionName];
connection.map((executionDataArray) => {
if (executionDataArray === null) {
return;
}
const { capResults, ignoredProperties, keepOnlyProperties } =
nodeEdgeCases[nodeName] || {};
if (capResults !== undefined) {
executionDataArray.splice(capResults);
}
executionDataArray.map((executionData) => {
if (executionData.json === undefined) {
return;
}
if (ignoredProperties !== undefined) {
ignoredProperties.forEach(
(ignoredProperty) => delete executionData.json[ignoredProperty],
);
}
let keepOnlyFields = [] as string[];
if (keepOnlyProperties !== undefined) {
keepOnlyFields = keepOnlyProperties;
}
executionData.json =
keepOnlyFields.length > 0
? pick(executionData.json, keepOnlyFields)
: executionData.json;
const jsonProperties = executionData.json;
const nodeOutputAttributes = Object.keys(jsonProperties);
nodeOutputAttributes.map((attributeName) => {
if (Array.isArray(jsonProperties[attributeName])) {
jsonProperties[attributeName] = ['json array'];
} else if (typeof jsonProperties[attributeName] === 'object') {
jsonProperties[attributeName] = { object: true };
}
});
});
});
});
});
});
} else {
// If not using shallow comparison then we only treat nodeEdgeCases.
const specialCases = Object.keys(nodeEdgeCases);
specialCases.forEach((nodeName) => {
data.data.resultData.runData[nodeName].map((taskData: ITaskData) => {
if (taskData.data === undefined) {
return;
}
Object.keys(taskData.data).map((connectionName) => {
const connection = taskData.data![connectionName];
connection.map((executionDataArray) => {
if (executionDataArray === null) {
return;
}
const capResults = nodeEdgeCases[nodeName].capResults;
if (capResults !== undefined) {
executionDataArray.splice(capResults);
}
if (nodeEdgeCases[nodeName].ignoredProperties !== undefined) {
executionDataArray.map((executionData) => {
if (executionData.json === undefined) {
return;
}
nodeEdgeCases[nodeName].ignoredProperties!.forEach(
(ignoredProperty) => delete executionData.json[ignoredProperty],
);
});
}
});
});
});
});
}
const serializedData = this.formatJsonOutput(data);
if (ExecuteBatch.compare === undefined) {
executionResult.executionStatus = 'success';
} else {
const fileName = `${
ExecuteBatch.compare.endsWith(sep)
? ExecuteBatch.compare
: ExecuteBatch.compare + sep
}${workflowData.id}-snapshot.json`;
if (fs.existsSync(fileName)) {
const contents = fs.readFileSync(fileName, { encoding: 'utf-8' });
const expected = jsonParse<IRun>(contents);
const received = jsonParse<IRun>(serializedData);
const changes = diff(expected, received, { keysOnly: true }) as object;
if (changes !== undefined) {
// If we had only additions with no removals
// Then we treat as a warning and not an error.
// To find this, we convert the object to JSON
// and search for the `__deleted` string
const changesJson = JSON.stringify(changes);
if (changesJson.includes('__deleted')) {
if (ExecuteBatch.githubWorkflow) {
const deletedChanges = changesJson.match(/__deleted/g) ?? [];
// we have structural changes. Report them.
executionResult.error = `Workflow contains ${deletedChanges.length} deleted data.`;
} else {
executionResult.error = 'Workflow may contain breaking changes';
}
executionResult.changes = changes;
executionResult.executionStatus = 'error';
} else {
executionResult.error =
'Workflow contains new data that previously did not exist.';
executionResult.changes = changes;
executionResult.executionStatus = 'warning';
}
} else {
executionResult.executionStatus = 'success';
}
} else {
executionResult.error = 'Snapshot for not found.';
executionResult.executionStatus = 'warning';
}
}
// Save snapshots only after comparing - this is to make sure we're updating
// After comparing to existing version.
if (ExecuteBatch.snapshot !== undefined) {
const fileName = `${
ExecuteBatch.snapshot.endsWith(sep)
? ExecuteBatch.snapshot
: ExecuteBatch.snapshot + sep
}${workflowData.id}-snapshot.json`;
fs.writeFileSync(fileName, serializedData);
}
}
}
} catch (e) {
this.errorReporter.error(e, {
extra: {
workflowId: workflowData.id,
},
});
executionResult.error = `Workflow failed to execute: ${(e as Error).message}`;
executionResult.executionStatus = 'error';
}
clearTimeout(timeoutTimer);
resolve(executionResult);
});
}
}
|