Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Depend on our windup-rulesets fork, some fixes around conversion #114

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ RUN wget -qO /tmp/windup.zip $WINDUP \
&& rm /tmp/windup.zip \
&& ln -s /windup/tackle-cli-*/bin/windup-cli /usr/bin/windup-cli

RUN git clone https://github.com/windup/windup-rulesets.git -b 6.2.3.Final /windup-rulesets \
RUN git clone https://github.com/konveyor-ecosystem/windup-rulesets.git -b konveyor /windup-rulesets \
&& git clone https://github.com/konveyor/example-applications /example-applications

COPY --from=java-builder /usr/local/openjdk-11 /java-11-openjdk
Expand Down
60 changes: 34 additions & 26 deletions pkg/conversion/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,30 +392,7 @@ func convertWindupWhenToAnalyzer(windupWhen windup.When, where map[string]string
if windupWhen.Javaclass != nil {
for _, jc := range windupWhen.Javaclass {
customVars = convertWhereToCustomVars(where, jc.References)
pattern := strings.Replace(substituteWhere(where, jc.References), "{*}", "*", -1)
pattern = strings.Replace(pattern, "(*)", "*", -1)
pattern = strings.Replace(pattern, ".*", "*", -1)
pattern = strings.Replace(pattern, `\`, "", -1)
// Make some .* regesx's more generic.
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, "*")
pattern = strings.Replace(pattern, ".(*)?*", ".*", -1)
// when there are wildcards in the middle of the pattern, make them .*
// see https://github.com/konveyor/analyzer-lsp/issues/481
pattern = regexp.MustCompile(`([A-Za-z])\*([A-Za-z])`).ReplaceAllString(pattern, `$1.*$2`)
pattern := convertJavaPattern(where, jc.References)
// when pattern ends with * and a location is not specified
// we guess the location based on defined pattern
if strings.HasSuffix(pattern, "*") && jc.Location == nil {
Expand Down Expand Up @@ -634,10 +611,41 @@ func convertWindupWhenToAnalyzer(windupWhen windup.When, where map[string]string
return conditions, customVars
}

func convertJavaPattern(where map[string]string, references string) string {
pattern := substituteWhere(where, references)
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)
// Make some .* regesx's more generic.
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 parenthesis 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, "*")
pattern = strings.Replace(pattern, ".(*)?*", ".*", -1)
// when there are wildcards in the middle of the pattern, make them .*
// see https://github.com/konveyor/analyzer-lsp/issues/481
pattern = regexp.MustCompile(`([A-Za-z])\*([A-Za-z])`).ReplaceAllString(pattern, `$1.*$2`)
return pattern
}

func convertWhereToCustomVars(whereMap map[string]string, fullPattern string) []map[string]interface{} {
l := []map[string]interface{}{}
newString := fullPattern
// escape any empty paranthesis to avoid interpreting them as wildcards
// escape any empty parenthesis to avoid interpreting them as wildcards
newString = strings.Replace(newString, `[]`, `\[\]`, -1)
newString = strings.Replace(newString, `()`, `\(\)`, -1)
// cascade multiple dots and stars
Expand Down Expand Up @@ -886,7 +894,7 @@ func convertWindupPerformToAnalyzer(perform windup.Iteration, where map[string]s
func substituteWhere(where map[string]string, pattern string) string {
newString := pattern
for k, v := range where {
newString = strings.ReplaceAll(newString, "{"+k+"}", v)
newString = strings.ReplaceAll(newString, "{"+k+"}", "("+v+")")
}
return newString
}
Expand Down
25 changes: 25 additions & 0 deletions pkg/conversion/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,28 @@ func Test_trimMessage(t *testing.T) {
})
}
}

func Test_convertJavaPattern(t *testing.T) {
tests := []struct {
name string
where map[string]string
references string
want string
}{
{
name: "Java pattern substitution should be enclosed by parenthesis",
where: map[string]string{
"scope": "Application|Request|Session",
},
references: "javax.faces.bean.{scope}Scoped",
want: "javax.faces.bean.(Application|Request|Session)Scoped",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := convertJavaPattern(tt.where, tt.references); got != tt.want {
t.Errorf("convertJavaPattern() = %v, want %v", got, tt.want)
}
})
}
}
Loading