Skip to content

Commit 6051110

Browse files
authored
Merge pull request #496 from jonesbusy/feature/cleanup-collector
MetadataCollector is not anymore a scanning recipe and various cleanup
2 parents a594dc3 + 183f521 commit 6051110

19 files changed

Lines changed: 997 additions & 409 deletions

File tree

plugin-modernizer-cli/src/test/resources/logback-test.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414
<root level="TRACE">
1515
<appender-ref ref="CONSOLE" />
1616
</root>
17+
<logger name="io.micrometer.common.util.internal.logging" level="INFO" />
1718
<logger name="io.jenkins.tools.pluginmodernizer" level="TRACE" />
1819
<logger name="io.jenkins.tools.pluginmodernizer.cli.utils" level="INFO" />
20+
<logger name="org.apache.sshd.common.util" level="WARN" />
1921
<logger name="org.testcontainers" level="INFO" />
2022
<logger name="com.github.sparsick.testcontainers" level="INFO" />
21-
<logger name="tc.rockstorm/git-server:2.45" level="WARN" />
23+
<logger name="tc.rockstorm/git-server:2.47" level="WARN" />
2224
<logger name="tc.testcontainers/ryuk:0.9.0" level="WARN" />
2325
<logger name="com.github.dockerjava" level="INFO" />
2426
</configuration>
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package io.jenkins.tools.pluginmodernizer.core.extractor;
2+
3+
import io.jenkins.tools.pluginmodernizer.core.model.JDK;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
import java.util.stream.Stream;
8+
import org.openrewrite.groovy.GroovyIsoVisitor;
9+
import org.openrewrite.groovy.tree.G;
10+
import org.openrewrite.java.tree.Expression;
11+
import org.openrewrite.java.tree.J;
12+
import org.slf4j.Logger;
13+
import org.slf4j.LoggerFactory;
14+
15+
/**
16+
* Visitor for a Jenkinsfile
17+
*/
18+
public class JenkinsfileVisitor extends GroovyIsoVisitor<PluginMetadata> {
19+
20+
/**
21+
* LOGGER.
22+
*/
23+
public static final Logger LOG = LoggerFactory.getLogger(JenkinsfileVisitor.class);
24+
25+
private final Map<String, Object> variableMap = new HashMap<>();
26+
27+
@Override
28+
public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations v, PluginMetadata pluginMetadata) {
29+
LOG.debug("Visiting variable declarations {}", v);
30+
J.VariableDeclarations variableDeclarations = super.visitVariableDeclarations(v, pluginMetadata);
31+
32+
for (J.VariableDeclarations.NamedVariable variable : variableDeclarations.getVariables()) {
33+
variableMap.put(variable.getSimpleName(), variable.getInitializer());
34+
}
35+
36+
return variableDeclarations;
37+
}
38+
39+
@Override
40+
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, PluginMetadata pluginMetadata) {
41+
LOG.debug("Visiting method invocation {}", method);
42+
method = super.visitMethodInvocation(method, pluginMetadata);
43+
if ("buildPlugin".equals(method.getSimpleName())) {
44+
List<Expression> args = method.getArguments();
45+
46+
List<Integer> jdkVersions =
47+
args.stream().flatMap(this::extractJdkVersions).distinct().toList();
48+
49+
LOG.info("JDK versions found: {}", jdkVersions);
50+
51+
jdkVersions.forEach(jdkVersion -> pluginMetadata.addJdk(JDK.get(jdkVersion)));
52+
}
53+
54+
return method;
55+
}
56+
57+
private Stream<Integer> extractJdkVersions(Expression arg) {
58+
if (arg instanceof G.MapEntry) {
59+
return Stream.of(arg)
60+
.map(G.MapEntry.class::cast)
61+
.filter(entry -> "configurations".equals(((J.Literal) entry.getKey()).getValue()))
62+
.map(entry -> resolveConfigurations(entry.getValue()))
63+
.filter(value -> value instanceof G.ListLiteral)
64+
.flatMap(value -> ((G.ListLiteral) value).getElements().stream())
65+
.filter(expression -> expression instanceof G.MapLiteral)
66+
.flatMap(expression -> ((G.MapLiteral) expression).getElements().stream())
67+
.filter(mapExpr -> mapExpr instanceof G.MapEntry)
68+
.map(G.MapEntry.class::cast)
69+
.filter(mapEntry -> "jdk".equals(((J.Literal) mapEntry.getKey()).getValue()))
70+
.map(mapEntry -> Integer.parseInt(
71+
((J.Literal) mapEntry.getValue()).getValue().toString()));
72+
} else {
73+
Expression resolvedArg = resolveVariable(arg);
74+
return Stream.of(resolvedArg)
75+
.filter(resolved -> resolved instanceof G.MapLiteral)
76+
.flatMap(resolved -> ((G.MapLiteral) resolved).getElements().stream())
77+
.filter(entry -> entry instanceof G.MapEntry)
78+
.map(G.MapEntry.class::cast)
79+
.filter(entry -> "configurations".equals(((J.Literal) entry.getKey()).getValue()))
80+
.map(entry -> resolveConfigurations(entry.getValue()))
81+
.filter(value -> value instanceof G.ListLiteral)
82+
.flatMap(value -> ((G.ListLiteral) value).getElements().stream())
83+
.filter(expression -> expression instanceof G.MapLiteral)
84+
.flatMap(expression -> ((G.MapLiteral) expression).getElements().stream())
85+
.filter(mapExpr -> mapExpr instanceof G.MapEntry)
86+
.map(G.MapEntry.class::cast)
87+
.filter(mapEntry -> "jdk".equals(((J.Literal) mapEntry.getKey()).getValue()))
88+
.map(mapEntry -> Integer.parseInt(
89+
((J.Literal) mapEntry.getValue()).getValue().toString()));
90+
}
91+
}
92+
93+
private Expression resolveVariable(Expression expression) {
94+
if (expression instanceof J.Identifier) {
95+
String variableName = ((J.Identifier) expression).getSimpleName();
96+
if (variableMap.containsKey(variableName)) {
97+
return (Expression) variableMap.get(variableName);
98+
}
99+
}
100+
return expression;
101+
}
102+
103+
private Expression resolveConfigurations(Expression entry) {
104+
return entry instanceof G.ListLiteral ? entry : resolveVariable(entry);
105+
}
106+
}

0 commit comments

Comments
 (0)