Skip to content

Commit aa9e092

Browse files
authored
Merge pull request #1170 from CodexRaunak/improve-recipe
Modify the recipe UpdateJenkinsfileForJava25 to a general recipe
2 parents f1f1e18 + 98f4055 commit aa9e092

File tree

3 files changed

+92
-20
lines changed

3 files changed

+92
-20
lines changed

plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/recipes/UpdateJenkinsfileForJava25.java renamed to plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/recipes/UpdateJenkinsfileForJavaVersion.java

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.util.List;
99
import java.util.stream.Collectors;
1010
import org.openrewrite.ExecutionContext;
11+
import org.openrewrite.Option;
1112
import org.openrewrite.Recipe;
1213
import org.openrewrite.TreeVisitor;
1314
import org.openrewrite.groovy.GroovyVisitor;
@@ -17,18 +18,32 @@
1718
import org.slf4j.Logger;
1819
import org.slf4j.LoggerFactory;
1920

20-
public class UpdateJenkinsfileForJava25 extends Recipe {
21+
public class UpdateJenkinsfileForJavaVersion extends Recipe {
2122

22-
private static final Logger LOG = LoggerFactory.getLogger(UpdateJenkinsfileForJava25.class);
23+
private static final Logger LOG = LoggerFactory.getLogger(UpdateJenkinsfileForJavaVersion.class);
24+
25+
/**
26+
* The java version.
27+
*/
28+
@Option(displayName = "Version", description = "Java version.", example = "25")
29+
Integer javaVersion;
30+
31+
/**
32+
* Constructor.
33+
* @param javaVersion The java version.
34+
*/
35+
public UpdateJenkinsfileForJavaVersion(Integer javaVersion) {
36+
this.javaVersion = javaVersion;
37+
}
2338

2439
@Override
2540
public String getDisplayName() {
26-
return "Update Jenkinsfile for Java 25 Testing";
41+
return "Update Jenkinsfile for specefied Java Version";
2742
}
2843

2944
@Override
3045
public String getDescription() {
31-
return "Adds Java 25 to the buildPlugin configurations in Jenkinsfile for runtime testing on ci.jenkins.io.";
46+
return "Adds Java version to the buildPlugin configurations in Jenkinsfile for runtime testing on ci.jenkins.io.";
3247
}
3348

3449
@Override
@@ -41,21 +56,27 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
4156
return method;
4257
}
4358

59+
JDK jdkVersion = JDK.get(javaVersion);
60+
if (jdkVersion == null) {
61+
LOG.warn("Unsupported Java version: {}, probably not an LTS. No update will be made.", javaVersion);
62+
return method;
63+
}
64+
4465
// extract all existing arguments into a simple data model.
4566
JenkinsfileModel model = new JenkinsfileModel(method);
4667

47-
// see if java 25 is already present
48-
boolean hasJava25 = model.platformConfigs.stream()
68+
// see if java version is already present
69+
boolean hasJavaVersion = model.platformConfigs.stream()
4970
.anyMatch(p -> "linux".equalsIgnoreCase(p.name().toString())
50-
&& p.jdk().getMajor() == 25);
71+
&& p.jdk().getMajor() == javaVersion);
5172

52-
if (hasJava25) {
53-
LOG.info("Java 25 configuration already exists. No update needed.");
73+
if (hasJavaVersion) {
74+
LOG.info("Java {} configuration already exists. No update needed.", javaVersion);
5475
return method;
5576
}
5677

57-
// add the new java 25 configuration to the model.
58-
model.platformConfigs.add(PlatformConfig.build(Platform.LINUX, JDK.JAVA_25));
78+
// add the java version configuration to the model.
79+
model.platformConfigs.add(PlatformConfig.build(Platform.LINUX, jdkVersion));
5980

6081
// We pass it the complete, updated configuration, and it will overwrite the old method call.
6182
// in future at some point remove jdk 17 from the configurations

plugin-modernizer-core/src/main/resources/META-INF/rewrite/recipes.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ recipeList:
148148
# - org.openrewrite.java.migrate.RemoveSecurityManager
149149
# - org.openrewrite.java.migrate.SystemGetSecurityManagerToNull
150150
- io.jenkins.tools.pluginmodernizer.SetupJenkinsfile
151-
- io.jenkins.tools.pluginmodernizer.core.recipes.UpdateJenkinsfileForJava25
151+
- io.jenkins.tools.pluginmodernizer.core.recipes.UpdateJenkinsfileForJavaVersion:
152+
javaVersion: 25
152153
---
153154
type: specs.openrewrite.org/v1beta/recipe
154155
name: io.jenkins.tools.pluginmodernizer.MigrateToJenkinsBaseLineProperty

plugin-modernizer-core/src/test/java/io/jenkins/tools/pluginmodernizer/core/recipes/UpdateJenkinsfileForJava25Test.java renamed to plugin-modernizer-core/src/test/java/io/jenkins/tools/pluginmodernizer/core/recipes/UpdateJenkinsfileForJavaVersionTest.java

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
import org.openrewrite.test.RewriteTest;
1010

1111
/**
12-
* Test for {@link UpdateJenkinsfileForJava25}.
12+
* Test for {@link UpdateJenkinsfileForJavaVersion}.
1313
*/
1414
@Execution(ExecutionMode.CONCURRENT)
15-
public class UpdateJenkinsfileForJava25Test implements RewriteTest {
15+
public class UpdateJenkinsfileForJavaVersionTest implements RewriteTest {
1616

1717
@Test
1818
void shouldUpgradeLegacyConfigAndAddJava25() {
1919
rewriteRun(
20-
spec -> spec.recipe(new UpdateJenkinsfileForJava25()),
20+
spec -> spec.recipe(new UpdateJenkinsfileForJavaVersion(25)),
2121
// language=groovy
2222
groovy(
2323
"""
@@ -51,7 +51,7 @@ void shouldUpgradeLegacyConfigAndAddJava25() {
5151
@Test
5252
void shouldAddJava25ToModernConfigurations() {
5353
rewriteRun(
54-
spec -> spec.recipe(new UpdateJenkinsfileForJava25()),
54+
spec -> spec.recipe(new UpdateJenkinsfileForJavaVersion(25)),
5555
// language=groovy
5656
groovy(
5757
"""
@@ -81,9 +81,59 @@ void shouldAddJava25ToModernConfigurations() {
8181
}
8282

8383
@Test
84-
void shouldNotAddJava25WhenAlreadyPresent() {
84+
void shouldAddJava21ToModernConfigurations() {
8585
rewriteRun(
86-
spec -> spec.recipe(new UpdateJenkinsfileForJava25()),
86+
spec -> spec.recipe(new UpdateJenkinsfileForJavaVersion(21)),
87+
// language=groovy
88+
groovy(
89+
"""
90+
buildPlugin(
91+
useContainerAgent: true,
92+
configurations: [
93+
[platform: 'linux', jdk: 17],
94+
[platform: 'windows', jdk: 17]
95+
]
96+
)
97+
""",
98+
"""
99+
/*
100+
See the documentation for more options:
101+
https://github.com/jenkins-infra/pipeline-library/
102+
*/
103+
buildPlugin(
104+
useContainerAgent: true,
105+
forkCount: '1C', // Set to `false` if you need to use Docker for containerized tests
106+
configurations: [
107+
[platform: 'linux', jdk: 17],
108+
[platform: 'windows', jdk: 17],
109+
[platform: 'linux', jdk: 21],
110+
])
111+
""",
112+
sourceSpecs -> sourceSpecs.path(ArchetypeCommonFile.JENKINSFILE.getPath())));
113+
}
114+
115+
@Test
116+
void shouldNotAddJavaVersionToModernConfigurationsAsNotLTS() {
117+
rewriteRun(
118+
spec -> spec.recipe(new UpdateJenkinsfileForJavaVersion(18)),
119+
// language=groovy
120+
groovy(
121+
"""
122+
buildPlugin(
123+
useContainerAgent: true,
124+
configurations: [
125+
[platform: 'linux', jdk: 17],
126+
[platform: 'windows', jdk: 17]
127+
]
128+
)
129+
""",
130+
sourceSpecs -> sourceSpecs.path(ArchetypeCommonFile.JENKINSFILE.getPath())));
131+
}
132+
133+
@Test
134+
void shouldNotAddJavaVersionWhenAlreadyPresent() {
135+
rewriteRun(
136+
spec -> spec.recipe(new UpdateJenkinsfileForJavaVersion(25)),
87137
// language=groovy
88138
groovy(
89139
"""
@@ -98,9 +148,9 @@ void shouldNotAddJava25WhenAlreadyPresent() {
98148
}
99149

100150
@Test
101-
void shouldAddConfigurationsBlockWhenMissing() {
151+
void shouldAddConfigurationsBlockForJavaVersionWhenMissing() {
102152
rewriteRun(
103-
spec -> spec.recipe(new UpdateJenkinsfileForJava25()),
153+
spec -> spec.recipe(new UpdateJenkinsfileForJavaVersion(25)),
104154
// language=groovy
105155
groovy(
106156
"""

0 commit comments

Comments
 (0)