From 8a4c213fdf38baa81fd9cdd6ca43e22c92df3f7a Mon Sep 17 00:00:00 2001 From: Valentin Delaye Date: Mon, 13 Jan 2025 18:13:11 +0100 Subject: [PATCH] Do not upgrade jenkins.version if not supported by Jenkinsfile --- .../core/recipes/UpgradeJenkinsVersion.java | 59 ++++++++++++++++++- .../core/recipes/DeclarativeRecipesTest.java | 13 ++++ .../recipes/UpgradeJenkinsVersionTest.java | 55 +++++++++++++++++ 3 files changed, 124 insertions(+), 3 deletions(-) diff --git a/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/recipes/UpgradeJenkinsVersion.java b/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/recipes/UpgradeJenkinsVersion.java index bae33410..1de172e6 100644 --- a/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/recipes/UpgradeJenkinsVersion.java +++ b/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/recipes/UpgradeJenkinsVersion.java @@ -1,8 +1,17 @@ package io.jenkins.tools.pluginmodernizer.core.recipes; +import io.jenkins.tools.pluginmodernizer.core.extractor.ArchetypeCommonFile; +import io.jenkins.tools.pluginmodernizer.core.extractor.JenkinsfileVisitor; +import io.jenkins.tools.pluginmodernizer.core.extractor.PluginMetadata; +import io.jenkins.tools.pluginmodernizer.core.model.JDK; +import java.util.HashSet; +import java.util.List; +import java.util.Set; import org.openrewrite.ExecutionContext; import org.openrewrite.Option; -import org.openrewrite.Recipe; +import org.openrewrite.ScanningRecipe; +import org.openrewrite.SourceFile; +import org.openrewrite.Tree; import org.openrewrite.TreeVisitor; import org.openrewrite.jenkins.UpgradeVersionProperty; import org.openrewrite.maven.MavenIsoVisitor; @@ -15,7 +24,7 @@ * Take care of updating the bom or adding the bom if it's not present. * Not changing anything if the version is already higher than the minimum version. */ -public class UpgradeJenkinsVersion extends Recipe { +public class UpgradeJenkinsVersion extends ScanningRecipe { /** * LOGGER. @@ -47,12 +56,22 @@ public String getDescription() { } @Override - public TreeVisitor getVisitor() { + public TreeVisitor getVisitor(PlatformConfigAccumulator acc) { return new MavenIsoVisitor<>() { @Override public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) { + Set jenkinsfileJdks = acc.getJdks(); + List jenkinsJdks = JDK.get(minimumVersion); + + // All jenkinsfile JDK must be supported by the new Jenkins version + if (jenkinsfileJdks.stream().anyMatch(jdk -> !jenkinsJdks.contains(jdk))) { + LOG.warn( + "Jenkinsfile JDKs are not supported by the new Jenkins version. Will not change the version."); + return document; + } + // Return another tree with jenkins version updated document = (Xml.Document) new UpgradeVersionProperty("jenkins.version", minimumVersion) .getVisitor() @@ -61,4 +80,38 @@ public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) { } }; } + + @Override + public PlatformConfigAccumulator getInitialValue(ExecutionContext ctx) { + return new PlatformConfigAccumulator(); + } + + @Override + public TreeVisitor getScanner(PlatformConfigAccumulator acc) { + return new TreeVisitor<>() { + @Override + public Tree visit(Tree tree, ExecutionContext ctx) { + tree = super.visit(tree, ctx); + if (tree instanceof SourceFile + && ArchetypeCommonFile.JENKINSFILE.same(((SourceFile) tree).getSourcePath())) { + acc.setPlatform(new JenkinsfileVisitor() + .reduce(tree, new PluginMetadata()) + .getJdks()); + } + return tree; + } + }; + } + + public static class PlatformConfigAccumulator { + private Set jdks = new HashSet<>(); + + public void setPlatform(Set jdks) { + this.jdks = jdks; + } + + public Set getJdks() { + return jdks; + } + } } diff --git a/plugin-modernizer-core/src/test/java/io/jenkins/tools/pluginmodernizer/core/recipes/DeclarativeRecipesTest.java b/plugin-modernizer-core/src/test/java/io/jenkins/tools/pluginmodernizer/core/recipes/DeclarativeRecipesTest.java index 71051361..01cd9f4f 100644 --- a/plugin-modernizer-core/src/test/java/io/jenkins/tools/pluginmodernizer/core/recipes/DeclarativeRecipesTest.java +++ b/plugin-modernizer-core/src/test/java/io/jenkins/tools/pluginmodernizer/core/recipes/DeclarativeRecipesTest.java @@ -1152,6 +1152,19 @@ void upgradeNextMajorParentVersionTest() { "io.jenkins.tools.pluginmodernizer.UpgradeNextMajorParentVersion") .parser(parser); }, + groovy( + """ + buildPlugin( + forkCount: '1C', // Run a JVM per core in tests + useContainerAgent: true, // Set to `false` if you need to use Docker for containerized tests + configurations: [ + [platform: 'linux', jdk: 21], + [platform: 'windows', jdk: 17], + ]) + """, + sourceSpecs -> { + sourceSpecs.path(ArchetypeCommonFile.JENKINSFILE.getPath()); + }), mavenProject("test"), // language=xml srcMainResources(text( diff --git a/plugin-modernizer-core/src/test/java/io/jenkins/tools/pluginmodernizer/core/recipes/UpgradeJenkinsVersionTest.java b/plugin-modernizer-core/src/test/java/io/jenkins/tools/pluginmodernizer/core/recipes/UpgradeJenkinsVersionTest.java index 9bbb0ea6..feaf687b 100644 --- a/plugin-modernizer-core/src/test/java/io/jenkins/tools/pluginmodernizer/core/recipes/UpgradeJenkinsVersionTest.java +++ b/plugin-modernizer-core/src/test/java/io/jenkins/tools/pluginmodernizer/core/recipes/UpgradeJenkinsVersionTest.java @@ -1,8 +1,10 @@ package io.jenkins.tools.pluginmodernizer.core.recipes; +import static org.openrewrite.groovy.Assertions.groovy; import static org.openrewrite.maven.Assertions.pomXml; import io.jenkins.tools.pluginmodernizer.core.config.Settings; +import io.jenkins.tools.pluginmodernizer.core.extractor.ArchetypeCommonFile; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.parallel.Execution; import org.junit.jupiter.api.parallel.ExecutionMode; @@ -93,6 +95,59 @@ void testPerformUpgradeWithoutBom() { """)); } + @Test + void testNoUpgradeIfJenkinsFileDoesNotSupportJava17() { + rewriteRun( + spec -> spec.recipe(new UpgradeJenkinsVersion("2.452.4")), + groovy( + """ + buildPlugin( + forkCount: '1C', // Run a JVM per core in tests + useContainerAgent: true, // Set to `false` if you need to use Docker for containerized tests + configurations: [ + [platform: 'linux', jdk: 8], + [platform: 'windows', jdk: 11], + ]) + """, + sourceSpecs -> { + sourceSpecs.path(ArchetypeCommonFile.JENKINSFILE.getPath()); + }), + // language=xml + pomXml( + """ + + + 4.0.0 + + org.jenkins-ci.plugins + plugin + 4.87 + + + io.jenkins.plugins + empty + 1.0.0-SNAPSHOT + hpi + Empty Plugin + + 2.440.3 + + + + repo.jenkins-ci.org + https://repo.jenkins-ci.org/public/ + + + + + repo.jenkins-ci.org + https://repo.jenkins-ci.org/public/ + + + + """)); + } + @Test void testPerformUpgradeWithBaselineWithoutBom() { rewriteRun(