From 26f85e8d03cc585a7f23cdac8106d4e02b917387 Mon Sep 17 00:00:00 2001 From: Pranav Gaikwad Date: Tue, 6 Feb 2024 15:03:54 -0500 Subject: [PATCH] :bug: AND chain items more intelligently Signed-off-by: Pranav Gaikwad --- pkg/conversion/convert.go | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/pkg/conversion/convert.go b/pkg/conversion/convert.go index 4caf460..c76ed39 100644 --- a/pkg/conversion/convert.go +++ b/pkg/conversion/convert.go @@ -135,7 +135,11 @@ func ConvertWindupRulesetToAnalyzer(ruleset windup.Ruleset) []map[string]interfa if len(when) == 1 { rule["when"] = when[0] } else if len(when) > 1 { - rule["when"] = map[string]interface{}{"or": when} + if canUseAndToChain(when) { + rule["when"] = map[string]interface{}{"and": when} + } else { + rule["when"] = map[string]interface{}{"or": when} + } } else { continue } @@ -983,3 +987,28 @@ func escapeParens(s string) string { s = strings.Replace(s, ")", "\\)", -1) return s } + +// canUseAndToChain when converting chained conditions, we check if they can +// be ANDed...it improves accuracy: https://github.com/konveyor/rulesets/issues/41 +// we can only do this for builtin conditions reliably +func canUseAndToChain(when []map[string]interface{}) bool { + canDetermine := true + fromUsed := false + asUsed := false + for _, cond := range when { + for key := range cond { + // if any one condition is java, we can't make this an AND + if strings.HasPrefix(key, "java") { + canDetermine = false + break + } + if key == "from" { + fromUsed = true + } + if key == "as" { + asUsed = true + } + } + } + return canDetermine && fromUsed && asUsed +}