From 241f23d2d6eff7778fcbb6b92109ab66229a9e3e Mon Sep 17 00:00:00 2001 From: Pranav Gaikwad Date: Wed, 20 Sep 2023 10:06:38 -0400 Subject: [PATCH] :sparkles: add file pattern to filecontent rules (#92) * :sparkles: add file pattern to filecontent rules Signed-off-by: Pranav Gaikwad * :sparkles: fix more regexes Signed-off-by: Pranav Gaikwad --------- Signed-off-by: Pranav Gaikwad --- pkg/conversion/convert.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/pkg/conversion/convert.go b/pkg/conversion/convert.go index b7c35045..a6d7b574 100644 --- a/pkg/conversion/convert.go +++ b/pkg/conversion/convert.go @@ -381,8 +381,8 @@ func convertWindupWhenToAnalyzer(windupWhen windup.When, where map[string]string //} condition := map[string]interface{}{ "builtin.filecontent": map[string]interface{}{ - "pattern": convertWindupRegex(substituteWhere(where, fc.Pattern)), - //"filePattern": files, + "pattern": convertWindupRegex(substituteWhere(where, fc.Pattern)), + "filePattern": escapeDots(convertWindupRegex(substituteWhere(where, fc.Filename))), }, } if fc.As != "" { @@ -405,9 +405,15 @@ func convertWindupWhenToAnalyzer(windupWhen windup.When, where map[string]string pattern = strings.Replace(pattern, `(.*)?.`, ".*", -1) pattern = strings.Replace(pattern, `.[^.]+`, "*", -1) pattern = strings.Replace(pattern, `[^.]+`, "*", -1) + pattern = strings.Replace(pattern, `[^.()]+`, "*", -1) pattern = strings.Replace(pattern, `(.[^.]*)*`, "*", -1) pattern = strings.Replace(pattern, `+[^.]*?`, "*", -1) pattern = strings.Replace(pattern, `[*]`, `\[*\]`, -1) + pattern = strings.Replace(pattern, `([a-z]+.)`, `*`, -1) + // remove open paranthesis which will be otherwise interpreted as wildcards + pattern = regexp.MustCompile(`\(\*$`).ReplaceAllString(pattern, "*") + pattern = regexp.MustCompile(`\(\)`).ReplaceAllString(pattern, "*") + pattern = regexp.MustCompile(`\[\]`).ReplaceAllString(pattern, "*") // cascade multiple dots and stars pattern = regexp.MustCompile(`[\.]{2,}\*`).ReplaceAllString(pattern, ".*") pattern = regexp.MustCompile(`[\*]{2,}`).ReplaceAllString(pattern, "*")