-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
force version non conflict diff when base version is missing
- Loading branch information
Showing
4 changed files
with
130 additions
and
1 deletion.
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
...built_rules/logic/diff/calculation/algorithms/force_target_version_diff_algorithm.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
import type { ThreeVersionsOf } from '../../../../../../../../common/api/detection_engine'; | ||
import { | ||
ThreeWayMergeOutcome, | ||
MissingVersion, | ||
ThreeWayDiffConflict, | ||
} from '../../../../../../../../common/api/detection_engine'; | ||
import { forceTargetVersionDiffAlgorithm } from './force_target_version_diff_algorithm'; | ||
|
||
describe('forceTargetVersionDiffAlgorithm', () => { | ||
describe('when base version exists', () => { | ||
it('returns a NON conflict diff', () => { | ||
const mockVersions: ThreeVersionsOf<number> = { | ||
base_version: 1, | ||
current_version: 1, | ||
target_version: 2, | ||
}; | ||
|
||
const result = forceTargetVersionDiffAlgorithm(mockVersions); | ||
|
||
expect(result).toMatchObject({ | ||
conflict: ThreeWayDiffConflict.NONE, | ||
}); | ||
}); | ||
|
||
it('return merge outcome TARGET', () => { | ||
const mockVersions: ThreeVersionsOf<number> = { | ||
base_version: 1, | ||
current_version: 1, | ||
target_version: 2, | ||
}; | ||
|
||
const result = forceTargetVersionDiffAlgorithm(mockVersions); | ||
|
||
expect(result).toMatchObject({ | ||
has_base_version: true, | ||
merge_outcome: ThreeWayMergeOutcome.Target, | ||
}); | ||
}); | ||
}); | ||
|
||
describe('when base version missing', () => { | ||
it('returns a NON conflict diff', () => { | ||
const mockVersions: ThreeVersionsOf<number> = { | ||
base_version: MissingVersion, | ||
current_version: 1, | ||
target_version: 2, | ||
}; | ||
|
||
const result = forceTargetVersionDiffAlgorithm(mockVersions); | ||
|
||
expect(result).toMatchObject({ | ||
conflict: ThreeWayDiffConflict.NONE, | ||
}); | ||
}); | ||
|
||
it('return merge outcome TARGET', () => { | ||
const mockVersions: ThreeVersionsOf<number> = { | ||
base_version: MissingVersion, | ||
current_version: 1, | ||
target_version: 2, | ||
}; | ||
|
||
const result = forceTargetVersionDiffAlgorithm(mockVersions); | ||
|
||
expect(result).toMatchObject({ | ||
has_base_version: false, | ||
merge_outcome: ThreeWayMergeOutcome.Target, | ||
}); | ||
}); | ||
}); | ||
}); |
45 changes: 45 additions & 0 deletions
45
...e/prebuilt_rules/logic/diff/calculation/algorithms/force_target_version_diff_algorithm.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
import type { | ||
ThreeVersionsOf, | ||
ThreeWayDiff, | ||
} from '../../../../../../../../common/api/detection_engine/prebuilt_rules'; | ||
import { | ||
MissingVersion, | ||
ThreeWayDiffConflict, | ||
ThreeWayMergeOutcome, | ||
determineDiffOutcome, | ||
} from '../../../../../../../../common/api/detection_engine/prebuilt_rules'; | ||
|
||
/** | ||
* Diff algorithm forcing target version. Useful for special fields like `version`. | ||
*/ | ||
export const forceTargetVersionDiffAlgorithm = <TValue>( | ||
versions: ThreeVersionsOf<TValue> | ||
): ThreeWayDiff<TValue> => { | ||
const { | ||
base_version: baseVersion, | ||
current_version: currentVersion, | ||
target_version: targetVersion, | ||
} = versions; | ||
const hasBaseVersion = baseVersion !== MissingVersion; | ||
const hasUpdate = targetVersion !== currentVersion; | ||
|
||
return { | ||
has_base_version: hasBaseVersion, | ||
base_version: hasBaseVersion ? baseVersion : undefined, | ||
current_version: currentVersion, | ||
target_version: targetVersion, | ||
merged_version: targetVersion, | ||
merge_outcome: ThreeWayMergeOutcome.Target, | ||
|
||
diff_outcome: determineDiffOutcome(baseVersion, currentVersion, targetVersion), | ||
has_update: hasUpdate, | ||
conflict: ThreeWayDiffConflict.NONE, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters