Skip to content

Commit

Permalink
Readded ThreeWayMergeOutcome enum and added missing base version scen…
Browse files Browse the repository at this point in the history
…arios to ThreeWayDiffOutcome enum
  • Loading branch information
jpdjere committed Jul 24, 2024
1 parent 82c7779 commit 7851d36
Show file tree
Hide file tree
Showing 12 changed files with 173 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ export * from './model/diff/rule_diff/rule_diff';
export * from './model/diff/three_way_diff/three_way_diff_outcome';
export * from './model/diff/three_way_diff/three_way_diff';
export * from './model/diff/three_way_diff/three_way_diff_conflict';
export * from './model/diff/three_way_diff/three_way_merge_outcome';

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import type { ThreeWayDiffOutcome } from './three_way_diff_outcome';
import type { ThreeWayDiffConflict } from './three_way_diff_conflict';
import type { ThreeWayMergeOutcome } from './three_way_merge_outcome';

/**
* A symbol that represents a missing value and used when a base version of a
Expand Down Expand Up @@ -110,6 +111,14 @@ export interface ThreeWayDiff<TValue> {
*/
diff_outcome: ThreeWayDiffOutcome;

/**
* The type of result of an automatic three-way merge of three values:
* - current version
* - target version
* - merged version
*/
merge_outcome: ThreeWayMergeOutcome;

/**
* Boolean which determines if a base version was found and returned for the three-way-diff of the field
* - true: the base version of the field was found and is either defined or undefined
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ export enum ThreeWayDiffOutcome {

/** Customized rule, the value has changed in the target version and is not equal to the current version. */
CustomizedValueCanUpdate = 'BASE=A, CURRENT=B, TARGET=C',

/** Missing base, the value hasn't changed in the target version. */
MissingBaseNoUpdate = 'BASE=-, CURRENT=A, TARGET=A',

/** Missing base, the value changed in the target version. */
MissingBaseCanUpdate = 'BASE=-, CURRENT=A, TARGET=B',
}

export const determineDiffOutcome = <TValue>(
Expand Down Expand Up @@ -85,12 +91,12 @@ const getThreeWayDiffOutcome = ({
if (!hasBaseVersion) {
/**
* We couldn't find the base version of the rule in the package so further
* version comparison is not possible. We assume that the rule is not
* version comparison is not possible. We assume that the rule is
* customized and the value can be updated if there's an update.
*/
return currentEqlTarget
? ThreeWayDiffOutcome.StockValueNoUpdate
: ThreeWayDiffOutcome.StockValueCanUpdate;
? ThreeWayDiffOutcome.MissingBaseNoUpdate
: ThreeWayDiffOutcome.MissingBaseCanUpdate;
}

if (baseEqlCurrent) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

/**
* The type of result of an automatic three-way merge of three values:
* - current version
* - target version
* - merged version
*/
export enum ThreeWayMergeOutcome {
/** Took current version and returned as the merged one. */
Current = 'CURRENT',

/** Took target version and returned as the merged one. */
Target = 'TARGET',

/** Merged three versions successfully into a new one. */
Merged = 'MERGED',
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
KqlQueryType,
ThreeWayDiffConflict,
ThreeWayDiffOutcome,
ThreeWayMergeOutcome,
} from '../../../../../common/api/detection_engine';
import type { PartialRuleDiff } from '../../../../../common/api/detection_engine';
import { TestProviders } from '../../../../common/mock';
Expand All @@ -21,6 +22,7 @@ const ruleFieldsDiffBaseFieldsMock = {
conflict: ThreeWayDiffConflict.NONE,
has_update: true,
has_base_version: true,
merge_outcome: ThreeWayMergeOutcome.Target,
};

const ruleFieldsDiffMock: PartialRuleDiff = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import type { ThreeVersionsOf } from '../../../../../../../../common/api/detection_engine';
import {
ThreeWayDiffOutcome,
ThreeWayMergeOutcome,
MissingVersion,
ThreeWayDiffConflict,
} from '../../../../../../../../common/api/detection_engine';
Expand All @@ -28,6 +29,7 @@ describe('multiLineStringDiffAlgorithm', () => {
merged_version: mockVersions.current_version,
diff_outcome: ThreeWayDiffOutcome.StockValueNoUpdate,
conflict: ThreeWayDiffConflict.NONE,
merge_outcome: ThreeWayMergeOutcome.Current,
})
);
});
Expand All @@ -46,6 +48,7 @@ describe('multiLineStringDiffAlgorithm', () => {
merged_version: mockVersions.current_version,
diff_outcome: ThreeWayDiffOutcome.CustomizedValueNoUpdate,
conflict: ThreeWayDiffConflict.NONE,
merge_outcome: ThreeWayMergeOutcome.Current,
})
);
});
Expand All @@ -64,6 +67,7 @@ describe('multiLineStringDiffAlgorithm', () => {
merged_version: mockVersions.target_version,
diff_outcome: ThreeWayDiffOutcome.StockValueCanUpdate,
conflict: ThreeWayDiffConflict.NONE,
merge_outcome: ThreeWayMergeOutcome.Target,
})
);
});
Expand All @@ -82,6 +86,7 @@ describe('multiLineStringDiffAlgorithm', () => {
merged_version: mockVersions.current_version,
diff_outcome: ThreeWayDiffOutcome.CustomizedValueSameUpdate,
conflict: ThreeWayDiffConflict.NONE,
merge_outcome: ThreeWayMergeOutcome.Current,
})
);
});
Expand All @@ -103,6 +108,7 @@ describe('multiLineStringDiffAlgorithm', () => {
merged_version: expectedMergedVersion,
diff_outcome: ThreeWayDiffOutcome.CustomizedValueCanUpdate,
conflict: ThreeWayDiffConflict.SOLVABLE,
merge_outcome: ThreeWayMergeOutcome.Merged,
})
);
});
Expand All @@ -121,6 +127,7 @@ describe('multiLineStringDiffAlgorithm', () => {
merged_version: mockVersions.current_version,
diff_outcome: ThreeWayDiffOutcome.CustomizedValueCanUpdate,
conflict: ThreeWayDiffConflict.NON_SOLVABLE,
merge_outcome: ThreeWayMergeOutcome.Current,
})
);
});
Expand All @@ -141,7 +148,8 @@ describe('multiLineStringDiffAlgorithm', () => {
has_base_version: false,
base_version: undefined,
merged_version: mockVersions.current_version,
diff_outcome: ThreeWayDiffOutcome.StockValueNoUpdate,
diff_outcome: ThreeWayDiffOutcome.MissingBaseNoUpdate,
merge_outcome: ThreeWayMergeOutcome.Current,
conflict: ThreeWayDiffConflict.NONE,
})
);
Expand All @@ -161,7 +169,8 @@ describe('multiLineStringDiffAlgorithm', () => {
has_base_version: false,
base_version: undefined,
merged_version: mockVersions.target_version,
diff_outcome: ThreeWayDiffOutcome.StockValueCanUpdate,
diff_outcome: ThreeWayDiffOutcome.MissingBaseCanUpdate,
merge_outcome: ThreeWayMergeOutcome.Target,
conflict: ThreeWayDiffConflict.SOLVABLE,
})
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ import type {
import {
determineDiffOutcome,
determineIfValueCanUpdate,
ThreeWayDiffOutcome,
MissingVersion,
ThreeWayDiffConflict,
ThreeWayDiffOutcome,
ThreeWayMergeOutcome,
} from '../../../../../../../../common/api/detection_engine/prebuilt_rules';

/**
Expand All @@ -36,7 +37,7 @@ export const multiLineStringDiffAlgorithm = (

const hasBaseVersion = baseVersion !== MissingVersion;

const { conflict, mergedVersion } = mergeVersions({
const { mergeOutcome, conflict, mergedVersion } = mergeVersions({
hasBaseVersion,
baseVersion,
currentVersion,
Expand All @@ -50,6 +51,7 @@ export const multiLineStringDiffAlgorithm = (
current_version: currentVersion,
target_version: targetVersion,
merged_version: mergedVersion,
merge_outcome: mergeOutcome,

diff_outcome: diffOutcome,
conflict,
Expand All @@ -58,6 +60,7 @@ export const multiLineStringDiffAlgorithm = (
};

interface MergeResult {
mergeOutcome: ThreeWayMergeOutcome;
mergedVersion: string;
conflict: ThreeWayDiffConflict;
}
Expand All @@ -78,33 +81,26 @@ const mergeVersions = ({
diffOutcome,
}: MergeArgs): MergeResult => {
switch (diffOutcome) {
case ThreeWayDiffOutcome.StockValueNoUpdate: // Scenarios AAA and -AA
case ThreeWayDiffOutcome.CustomizedValueNoUpdate: // Scenario ABA
case ThreeWayDiffOutcome.CustomizedValueSameUpdate: // Scenario ABB
// Scenario -AA is treated as scenario AAA:
// https://github.com/elastic/kibana/pull/184889#discussion_r1636421293
case ThreeWayDiffOutcome.MissingBaseNoUpdate:
case ThreeWayDiffOutcome.StockValueNoUpdate:
case ThreeWayDiffOutcome.CustomizedValueNoUpdate:
case ThreeWayDiffOutcome.CustomizedValueSameUpdate:
return {
conflict: ThreeWayDiffConflict.NONE,
mergedVersion: currentVersion,
mergeOutcome: ThreeWayMergeOutcome.Current,
};

case ThreeWayDiffOutcome.StockValueCanUpdate: {
if (!hasBaseVersion) {
// Scenario -AB. Treated as scenario ABC, returns target
// version and marked as "SOLVABLE" conflict.
// https://github.com/elastic/kibana/pull/184889#discussion_r1636421293
return {
mergedVersion: targetVersion,
conflict: ThreeWayDiffConflict.SOLVABLE,
};
}

// Scenario AAB
return {
conflict: ThreeWayDiffConflict.NONE,
mergedVersion: targetVersion,
mergeOutcome: ThreeWayMergeOutcome.Target,
};
}

// Scenario ABC
case ThreeWayDiffOutcome.CustomizedValueCanUpdate: {
// TS does not realize that in ABC scenario, baseVersion cannot be missing
// Missing baseVersion scenarios were handled as -AA and -AB.
Expand All @@ -116,12 +112,25 @@ const mergeVersions = ({
? {
conflict: ThreeWayDiffConflict.NON_SOLVABLE,
mergedVersion: currentVersion,
mergeOutcome: ThreeWayMergeOutcome.Current,
}
: {
conflict: ThreeWayDiffConflict.SOLVABLE,
mergedVersion: mergedVersion.result.join(''),
mergeOutcome: ThreeWayMergeOutcome.Merged,
};
}

// Scenario -AB is treated as scenario ABC, but marked as
// SOLVABLE, and returns the target version as the merged version
// https://github.com/elastic/kibana/pull/184889#discussion_r1636421293
case ThreeWayDiffOutcome.MissingBaseCanUpdate: {
return {
mergedVersion: targetVersion,
mergeOutcome: ThreeWayMergeOutcome.Target,
conflict: ThreeWayDiffConflict.SOLVABLE,
};
}
default:
return assertUnreachable(diffOutcome);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import type { ThreeVersionsOf } from '../../../../../../../../common/api/detection_engine';
import {
ThreeWayDiffOutcome,
ThreeWayMergeOutcome,
MissingVersion,
ThreeWayDiffConflict,
} from '../../../../../../../../common/api/detection_engine';
Expand All @@ -27,6 +28,7 @@ describe('numberDiffAlgorithm', () => {
expect.objectContaining({
merged_version: mockVersions.current_version,
diff_outcome: ThreeWayDiffOutcome.StockValueNoUpdate,
merge_outcome: ThreeWayMergeOutcome.Current,
conflict: ThreeWayDiffConflict.NONE,
})
);
Expand All @@ -45,6 +47,7 @@ describe('numberDiffAlgorithm', () => {
expect.objectContaining({
merged_version: mockVersions.current_version,
diff_outcome: ThreeWayDiffOutcome.CustomizedValueNoUpdate,
merge_outcome: ThreeWayMergeOutcome.Current,
conflict: ThreeWayDiffConflict.NONE,
})
);
Expand All @@ -63,6 +66,7 @@ describe('numberDiffAlgorithm', () => {
expect.objectContaining({
merged_version: mockVersions.target_version,
diff_outcome: ThreeWayDiffOutcome.StockValueCanUpdate,
merge_outcome: ThreeWayMergeOutcome.Target,
conflict: ThreeWayDiffConflict.NONE,
})
);
Expand All @@ -81,6 +85,7 @@ describe('numberDiffAlgorithm', () => {
expect.objectContaining({
merged_version: mockVersions.current_version,
diff_outcome: ThreeWayDiffOutcome.CustomizedValueSameUpdate,
merge_outcome: ThreeWayMergeOutcome.Current,
conflict: ThreeWayDiffConflict.NONE,
})
);
Expand All @@ -99,6 +104,7 @@ describe('numberDiffAlgorithm', () => {
expect.objectContaining({
merged_version: mockVersions.current_version,
diff_outcome: ThreeWayDiffOutcome.CustomizedValueCanUpdate,
merge_outcome: ThreeWayMergeOutcome.Current,
conflict: ThreeWayDiffConflict.NON_SOLVABLE,
})
);
Expand All @@ -119,7 +125,8 @@ describe('numberDiffAlgorithm', () => {
has_base_version: false,
base_version: undefined,
merged_version: mockVersions.current_version,
diff_outcome: ThreeWayDiffOutcome.StockValueNoUpdate,
diff_outcome: ThreeWayDiffOutcome.MissingBaseNoUpdate,
merge_outcome: ThreeWayMergeOutcome.Current,
conflict: ThreeWayDiffConflict.NONE,
})
);
Expand All @@ -139,7 +146,8 @@ describe('numberDiffAlgorithm', () => {
has_base_version: false,
base_version: undefined,
merged_version: mockVersions.target_version,
diff_outcome: ThreeWayDiffOutcome.StockValueCanUpdate,
diff_outcome: ThreeWayDiffOutcome.MissingBaseCanUpdate,
merge_outcome: ThreeWayMergeOutcome.Target,
conflict: ThreeWayDiffConflict.SOLVABLE,
})
);
Expand Down
Loading

0 comments on commit 7851d36

Please sign in to comment.