From 92035ce281a46cf386031691b6cce8634d302708 Mon Sep 17 00:00:00 2001 From: Valentin Delaye Date: Wed, 25 Dec 2024 16:06:01 +0100 Subject: [PATCH] Add visitor to add and remove a comment for properties --- .../core/extractor/PluginMetadata.java | 7 + .../pluginmodernizer/core/model/Recipe.java | 15 ++ .../MigrateToJenkinsBaseLineProperty.java | 80 ++++--- .../visitors/AddBeforePropertyVisitor.java | 84 ++++++++ .../visitors/AddPropertyCommentVisitor.java | 80 +++++++ .../RemovePropertyCommentVisitor.java | 43 ++++ .../resources/META-INF/rewrite/recipes.yml | 8 +- .../core/recipes/DeclarativeRecipesTest.java | 145 +++++++++++++ .../MigrateToJenkinsBaseLinePropertyTest.java | 200 ++++++++++++++++++ .../core/visitors/AddBeforePropertyTest.java | 154 ++++++++++++++ .../AddPropertyCommentVisitorTest.java | 153 ++++++++++++++ .../RemovePropertyCommentVisitorTest.java | 64 ++++++ 12 files changed, 1006 insertions(+), 27 deletions(-) create mode 100644 plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/visitors/AddBeforePropertyVisitor.java create mode 100644 plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/visitors/AddPropertyCommentVisitor.java create mode 100644 plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/visitors/RemovePropertyCommentVisitor.java create mode 100644 plugin-modernizer-core/src/test/java/io/jenkins/tools/pluginmodernizer/core/visitors/AddBeforePropertyTest.java create mode 100644 plugin-modernizer-core/src/test/java/io/jenkins/tools/pluginmodernizer/core/visitors/AddPropertyCommentVisitorTest.java create mode 100644 plugin-modernizer-core/src/test/java/io/jenkins/tools/pluginmodernizer/core/visitors/RemovePropertyCommentVisitorTest.java diff --git a/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/extractor/PluginMetadata.java b/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/extractor/PluginMetadata.java index e462de53..e3c72a3f 100644 --- a/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/extractor/PluginMetadata.java +++ b/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/extractor/PluginMetadata.java @@ -254,6 +254,13 @@ public void setProperties(Map properties) { this.properties = properties; } + public void addProperty(String key, String value) { + if (properties == null) { + properties = new HashMap<>(); + } + properties.put(key, value); + } + @Override public UUID getId() { return id; diff --git a/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/model/Recipe.java b/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/model/Recipe.java index e4b40d23..f8708139 100644 --- a/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/model/Recipe.java +++ b/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/model/Recipe.java @@ -19,6 +19,9 @@ public class Recipe { @JsonIgnore private Object preconditions; // Use Object to avoid mapping complex nested structures. + @JsonIgnore + private Boolean causesAnotherCycle; + public String getName() { return name; } @@ -70,4 +73,16 @@ public void setRecipeList(Object recipeList) { public Object getPreconditions() { return preconditions; } + + public void setPreconditions(Object preconditions) { + this.preconditions = preconditions; + } + + public Boolean getCausesAnotherCycle() { + return causesAnotherCycle; + } + + public void setCausesAnotherCycle(Boolean causesAnotherCycle) { + this.causesAnotherCycle = causesAnotherCycle; + } } diff --git a/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/recipes/MigrateToJenkinsBaseLineProperty.java b/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/recipes/MigrateToJenkinsBaseLineProperty.java index 238cf224..a611f146 100644 --- a/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/recipes/MigrateToJenkinsBaseLineProperty.java +++ b/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/recipes/MigrateToJenkinsBaseLineProperty.java @@ -2,9 +2,11 @@ import io.jenkins.tools.pluginmodernizer.core.extractor.PluginMetadata; import io.jenkins.tools.pluginmodernizer.core.extractor.PomVisitor; -import org.openrewrite.ExecutionContext; -import org.openrewrite.Recipe; -import org.openrewrite.TreeVisitor; +import io.jenkins.tools.pluginmodernizer.core.visitors.AddBeforePropertyVisitor; +import io.jenkins.tools.pluginmodernizer.core.visitors.AddPropertyCommentVisitor; +import org.jspecify.annotations.Nullable; +import org.openrewrite.*; +import org.openrewrite.Cursor; import org.openrewrite.maven.AddPropertyVisitor; import org.openrewrite.maven.MavenIsoVisitor; import org.openrewrite.xml.ChangeTagValueVisitor; @@ -36,41 +38,67 @@ public TreeVisitor getVisitor() { */ private static class MigrateToJenkinsBaseLinePropertyVisitor extends MavenIsoVisitor { + @Override + public @Nullable Xml visit(@Nullable Tree tree, ExecutionContext executionContext, Cursor parent) { + return super.visit(tree, executionContext, parent); + } + @Override public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) { Xml.Document d = super.visitDocument(document, ctx); + // Skip if not bom or weekly bom PluginMetadata pluginMetadata = new PomVisitor().reduce(document, new PluginMetadata()); - - // Keep only major and minor and ignore patch version - String jenkinsBaseline = pluginMetadata.getJenkinsVersion(); - String jenkinsPatch = null; - if (pluginMetadata.getJenkinsVersion().matches("\\d+\\.\\d+\\.\\d+")) { - jenkinsBaseline = jenkinsBaseline.substring(0, jenkinsBaseline.lastIndexOf('.')); - jenkinsPatch = pluginMetadata - .getJenkinsVersion() - .substring(pluginMetadata.getJenkinsVersion().lastIndexOf('.') + 1); - } - LOG.debug("Jenkins baseline version is {}", jenkinsBaseline); - LOG.debug("Jenkins patch version is {}", jenkinsPatch); - if (pluginMetadata.getBomArtifactId() == null || "bom-weekly".equals(pluginMetadata.getBomArtifactId())) { LOG.debug("Project is using Jenkins weekly bom or not bom, skipping"); return d; } - performUpdate(document, jenkinsBaseline, jenkinsPatch); + performUpdate(document); return document; } - private void performUpdate(Xml.Document document, String jenkinsBaseline, String jenkinsPatch) { + private void performUpdate(Xml.Document document) { + + Xml.Tag jenkinsVersionTag = document.getRoot() + .getChild("properties") + .flatMap(props -> props.getChild("jenkins.version")) + .orElse(null); + + if (jenkinsVersionTag == null) { + return; + } + + // Keep only major and minor and ignore patch version + String jenkinsVersion = jenkinsVersionTag.getValue().get(); + String jenkinsBaseline = jenkinsVersionTag.getValue().get(); + String jenkinsPatch = null; + + // It's a LTS, extract patch + if (jenkinsVersion.matches("\\d+\\.\\d+\\.\\d+") + || jenkinsVersion.matches("\\$\\{jenkins.baseline}\\.\\d+")) { + jenkinsBaseline = jenkinsBaseline.substring(0, jenkinsBaseline.lastIndexOf('.')); + jenkinsPatch = jenkinsVersion.substring(jenkinsVersion.lastIndexOf('.') + 1); + } + LOG.debug("Jenkins baseline version is {}", jenkinsBaseline); + LOG.debug("Jenkins patch version is {}", jenkinsPatch); + + String expectedVersion = + jenkinsPatch != null ? "${jenkins.baseline}." + jenkinsPatch : "${jenkins.baseline}"; // Add or changes properties - doAfterVisit(new AddPropertyVisitor("jenkins.baseline", jenkinsBaseline, false)); - if (jenkinsPatch != null) { - doAfterVisit(new AddPropertyVisitor("jenkins.version", "${jenkins.baseline}." + jenkinsPatch, false)); - } else { - doAfterVisit(new AddPropertyVisitor("jenkins.version", "${jenkins.baseline}", false)); + doAfterVisit(new AddBeforePropertyVisitor("jenkins.version", "jenkins.baseline", jenkinsBaseline)); + doAfterVisit(new AddPropertyCommentVisitor( + "jenkins.baseline", + " https://www.jenkins.io/doc/developer/plugin-development/choosing-jenkins-baseline/ ")); + + LOG.debug("Jenkins version is {}", jenkinsVersionTag.getValue().get()); + LOG.debug("Expected version is {}", expectedVersion); + + // Change the jenkins version + if (!expectedVersion.equals(jenkinsVersionTag.getValue().get())) { + doAfterVisit(new AddPropertyVisitor("jenkins.version", expectedVersion, false)); + maybeUpdateModel(); } // Change the bom artifact ID @@ -84,7 +112,11 @@ private void performUpdate(Xml.Document document, String jenkinsBaseline, String .flatMap(dep -> dep.getChild("artifactId")) .orElseThrow(); - doAfterVisit(new ChangeTagValueVisitor<>(artifactIdTag, "bom-${jenkins.baseline}.x")); + LOG.debug("Artifact ID is {}", artifactIdTag.getValue().get()); + + if (!artifactIdTag.getValue().get().equals("bom-${jenkins.baseline}.x")) { + doAfterVisit(new ChangeTagValueVisitor<>(artifactIdTag, "bom-${jenkins.baseline}.x")); + } } } } diff --git a/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/visitors/AddBeforePropertyVisitor.java b/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/visitors/AddBeforePropertyVisitor.java new file mode 100644 index 00000000..91928acc --- /dev/null +++ b/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/visitors/AddBeforePropertyVisitor.java @@ -0,0 +1,84 @@ +package io.jenkins.tools.pluginmodernizer.core.visitors; + +import java.util.ArrayList; +import java.util.List; +import org.openrewrite.ExecutionContext; +import org.openrewrite.maven.MavenIsoVisitor; +import org.openrewrite.xml.tree.Content; +import org.openrewrite.xml.tree.Xml; + +/** + * A visitor that add a maven property before another property. + * If the property already exists, it will update its value. + * If the previous property does not exist, the new property will not be added. + */ +public class AddBeforePropertyVisitor extends MavenIsoVisitor { + + /** + * The previous property name to add the new property before. + */ + private final String previousPropertyName; + + /** + * The property name to add. + */ + private final String propertyName; + + /** + * The property value to add. + */ + private final String propertyValue; + + /** + * Constructor of the visitor. + * @param previousPropertyName The previous property name to add the new property before. + * @param propertyName The property name to add. + * @param propertyValue The property value to add. + */ + public AddBeforePropertyVisitor(String previousPropertyName, String propertyName, String propertyValue) { + this.previousPropertyName = previousPropertyName; + this.propertyName = propertyName; + this.propertyValue = propertyValue; + } + + @Override + public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext executionContext) { + tag = super.visitTag(tag, executionContext); + if (tag.getName().equals("properties")) { + + // Ensure previous + Xml.Tag previousPropertyTag = tag.getChild(previousPropertyName).orElse(null); + if (previousPropertyTag == null) { + return tag; + } + + // Ensure value if exists + Xml.Tag existingPropertyTag = tag.getChild(propertyName).orElse(null); + if (existingPropertyTag != null && existingPropertyTag.getValue().isPresent()) { + // doAfterVisit(new ChangeTagValueVisitor<>(existingPropertyTag, propertyValue)); + return tag; + } + + if (tag.getContent() == null || tag.getContent().isEmpty()) { + return tag; + } + + List contents = new ArrayList<>(tag.getContent()); + int propertyIndex = contents.indexOf(previousPropertyTag); + + // If there are comments leave them as they are + while (propertyIndex > 0 && contents.get(propertyIndex - 1) instanceof Xml.Comment) { + propertyIndex--; + } + + // Place the tag just before the property or on first element of the sequence + Xml.Tag propertyTag = Xml.Tag.build("<" + propertyName + ">" + propertyValue + ""); + propertyTag = propertyTag.withPrefix(previousPropertyTag.getPrefix()); + contents.add(propertyIndex, propertyTag); + + tag = tag.withContent(contents); + } + + return tag; + } +} diff --git a/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/visitors/AddPropertyCommentVisitor.java b/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/visitors/AddPropertyCommentVisitor.java new file mode 100644 index 00000000..c6bd80fe --- /dev/null +++ b/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/visitors/AddPropertyCommentVisitor.java @@ -0,0 +1,80 @@ +package io.jenkins.tools.pluginmodernizer.core.visitors; + +import java.util.ArrayList; +import java.util.List; +import org.openrewrite.ExecutionContext; +import org.openrewrite.Tree; +import org.openrewrite.marker.Markers; +import org.openrewrite.maven.MavenIsoVisitor; +import org.openrewrite.xml.tree.Content; +import org.openrewrite.xml.tree.Xml; + +/** + * A visitor that add a comment before a maven property. + * If a comment already exists, it's updated. + */ +public class AddPropertyCommentVisitor extends MavenIsoVisitor { + + /** + * The property name to add the comment before. + */ + private final String propertyName; + + /** + * The comment to add. + */ + private final String comment; + + /** + * Constructor of the visitor. + * @param propertyName The property name to add the comment before. + * @param comment The comment to add. + */ + public AddPropertyCommentVisitor(String propertyName, String comment) { + this.propertyName = propertyName; + this.comment = comment; + } + + @Override + public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext executionContext) { + tag = super.visitTag(tag, executionContext); + if (tag.getName().equals("properties")) { + + // Ensure property exists + Xml.Tag propertyTag = tag.getChild(propertyName).orElse(null); + if (propertyTag == null) { + return tag; + } + + if (tag.getContent() == null) { + return tag; + } + + List contents = new ArrayList<>(tag.getContent()); + int propertyIndex = contents.indexOf(propertyTag); + + // Add or update comment + if (propertyTag.getContent() != null) { + boolean containsComment = contents.stream() + .anyMatch(c -> c instanceof Xml.Comment && comment.equals(((Xml.Comment) c).getText())); + + // Add comment if not exists + if (!containsComment) { + + // If there is a comment just before, remove it + if (propertyIndex > 0 && contents.get(propertyIndex - 1) instanceof Xml.Comment xmlComment) { + propertyIndex--; + contents.remove(propertyIndex); + doAfterVisit(new RemovePropertyCommentVisitor(xmlComment.getText())); + } + + Xml.Comment customComment = new Xml.Comment( + Tree.randomId(), contents.get(propertyIndex).getPrefix(), Markers.EMPTY, comment); + contents.add(propertyIndex, customComment); + tag = tag.withContent(contents); + } + } + } + return tag; + } +} diff --git a/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/visitors/RemovePropertyCommentVisitor.java b/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/visitors/RemovePropertyCommentVisitor.java new file mode 100644 index 00000000..c737cc42 --- /dev/null +++ b/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/visitors/RemovePropertyCommentVisitor.java @@ -0,0 +1,43 @@ +package io.jenkins.tools.pluginmodernizer.core.visitors; + +import java.util.ArrayList; +import java.util.List; +import org.openrewrite.ExecutionContext; +import org.openrewrite.maven.MavenIsoVisitor; +import org.openrewrite.xml.tree.Content; +import org.openrewrite.xml.tree.Xml; + +public class RemovePropertyCommentVisitor extends MavenIsoVisitor { + + private String comment; + + public RemovePropertyCommentVisitor(String comment) { + this.comment = comment; + } + + @Override + public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext executionContext) { + tag = super.visitTag(tag, executionContext); + if (tag.getName().equals("properties")) { + + List contents = new ArrayList<>(tag.getContent()); + + // Remove the comment if needed + boolean containsComment = contents.stream() + .anyMatch(c -> c instanceof Xml.Comment && comment.equals(((Xml.Comment) c).getText())); + + // Add comment if not exists + if (containsComment) { + for (int i = 0; i < contents.size(); i++) { + if (contents.get(i) instanceof Xml.Comment + && comment.equals(((Xml.Comment) contents.get(i)).getText())) { + contents.remove(i); + break; + } + } + tag = tag.withContent(contents); + } + } + return tag; + } +} diff --git a/plugin-modernizer-core/src/main/resources/META-INF/rewrite/recipes.yml b/plugin-modernizer-core/src/main/resources/META-INF/rewrite/recipes.yml index a2f8f192..9e68c72b 100644 --- a/plugin-modernizer-core/src/main/resources/META-INF/rewrite/recipes.yml +++ b/plugin-modernizer-core/src/main/resources/META-INF/rewrite/recipes.yml @@ -27,7 +27,6 @@ description: Migrate pom to using jenkins.baseline property if bom is present. tags: ['chore'] preconditions: - io.jenkins.tools.pluginmodernizer.core.recipes.IsUsingBom - - io.jenkins.tools.pluginmodernizer.core.recipes.IsMissingJenkinsBaselineProperty recipeList: - io.jenkins.tools.pluginmodernizer.core.recipes.MigrateToJenkinsBaseLineProperty --- @@ -57,6 +56,7 @@ displayName: Upgrade to the next major parent version (5.X) requiring Jenkins 2. description: Upgrade to the next major parent version (5.X) requiring Jenkins 2.479 and Java 17. tags: ['dependencies'] recipeList: + - io.jenkins.tools.pluginmodernizer.MigrateToJenkinsBaseLineProperty - org.openrewrite.maven.UpgradeParentVersion: groupId: org.jenkins-ci.plugins artifactId: plugin @@ -281,6 +281,7 @@ displayName: Upgrade to latest recommended core version and ensure the bom is ma description: Upgrade to latest recommended core version and ensure the bom is matching the core version. tags: ['developer'] recipeList: + - io.jenkins.tools.pluginmodernizer.MigrateToJenkinsBaseLineProperty - io.jenkins.tools.pluginmodernizer.UpgradeParentVersion - org.openrewrite.jenkins.UpgradeVersionProperty: key: jenkins.version @@ -291,13 +292,14 @@ recipeList: type: specs.openrewrite.org/v1beta/recipe name: io.jenkins.tools.pluginmodernizer.UpgradeToLatestJava11CoreVersion displayName: Upgrade to latest LTS core version supporting Java 11 -description: Upgrade to latest LTS core version supporting Java 11 +description: Upgrade to latest LTS core version supporting Java 11. tags: ['developer'] recipeList: + - io.jenkins.tools.pluginmodernizer.MigrateToJenkinsBaseLineProperty + - io.jenkins.tools.pluginmodernizer.UpgradeParentVersion - org.openrewrite.jenkins.UpgradeVersionProperty: key: jenkins.version minimumVersion: 2.462.3 - - io.jenkins.tools.pluginmodernizer.UpgradeParentVersion - io.jenkins.tools.pluginmodernizer.RemoveDependencyVersionOverride - io.jenkins.tools.pluginmodernizer.RemoveExtraMavenProperties --- 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 c425cee6..09c029ca 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 @@ -111,6 +111,17 @@ void upgradeToRecommendCoreVersionTestWithBaseline() { 2.440 ${jenkins.baseline}.3 + + + + io.jenkins.tools.bom + bom-${jenkins.baseline}.x + 3435.v238d66a_043fb_ + pom + import + + + repo.jenkins-ci.org @@ -141,9 +152,118 @@ void upgradeToRecommendCoreVersionTestWithBaseline() { hpi Empty Plugin + 2.452 ${jenkins.baseline}.4 + + + + io.jenkins.tools.bom + bom-${jenkins.baseline}.x + 3435.v238d66a_043fb_ + pom + import + + + + + + repo.jenkins-ci.org + https://repo.jenkins-ci.org/public/ + + + + + repo.jenkins-ci.org + https://repo.jenkins-ci.org/public/ + + + + """)); + } + + @Test + void upgradeToUpgradeToLatestJava11CoreVersion() { + rewriteRun( + spec -> spec.recipeFromResource( + "/META-INF/rewrite/recipes.yml", + "io.jenkins.tools.pluginmodernizer.UpgradeToLatestJava11CoreVersion"), + pomXml( + """ + + + 4.0.0 + + org.jenkins-ci.plugins + plugin + 4.55 + + + io.jenkins.plugins + empty + 1.0.0-SNAPSHOT + hpi + Empty Plugin + + 2.440.3 + + + + + io.jenkins.tools.bom + bom-2.440.x + 3435.v238d66a_043fb_ + pom + import + + + + + + repo.jenkins-ci.org + https://repo.jenkins-ci.org/public/ + + + + + repo.jenkins-ci.org + https://repo.jenkins-ci.org/public/ + + + + """, + """ + + + 4.0.0 + + org.jenkins-ci.plugins + plugin + 4.88 + + + io.jenkins.plugins + empty + 1.0.0-SNAPSHOT + hpi + Empty Plugin + + + 2.462 + ${jenkins.baseline}.3 + + + + + io.jenkins.tools.bom + bom-${jenkins.baseline}.x + 3435.v238d66a_043fb_ + pom + import + + + repo.jenkins-ci.org @@ -261,6 +381,17 @@ void upgradeNextMajorParentVersionTestWithBaseline() { 2.462 ${jenkins.baseline}.3 + + + + io.jenkins.tools.bom + bom-${jenkins.baseline}.x + 3435.v238d66a_043fb_ + pom + import + + + repo.jenkins-ci.org @@ -291,9 +422,21 @@ void upgradeNextMajorParentVersionTestWithBaseline() { hpi Empty Plugin + 2.479 ${jenkins.baseline}.1 + + + + io.jenkins.tools.bom + bom-${jenkins.baseline}.x + 3435.v238d66a_043fb_ + pom + import + + + repo.jenkins-ci.org @@ -372,6 +515,7 @@ void addPluginBomTest() { hpi Empty Plugin + 2.440 ${jenkins.baseline}.3 @@ -474,6 +618,7 @@ void migrateToJenkinsBaseLinePropertyTest() { hpi Empty Plugin + 2.440 ${jenkins.baseline}.3 diff --git a/plugin-modernizer-core/src/test/java/io/jenkins/tools/pluginmodernizer/core/recipes/MigrateToJenkinsBaseLinePropertyTest.java b/plugin-modernizer-core/src/test/java/io/jenkins/tools/pluginmodernizer/core/recipes/MigrateToJenkinsBaseLinePropertyTest.java index 3532620c..8e263230 100644 --- a/plugin-modernizer-core/src/test/java/io/jenkins/tools/pluginmodernizer/core/recipes/MigrateToJenkinsBaseLinePropertyTest.java +++ b/plugin-modernizer-core/src/test/java/io/jenkins/tools/pluginmodernizer/core/recipes/MigrateToJenkinsBaseLinePropertyTest.java @@ -28,6 +28,7 @@ void testNoChanges() { hpi Empty Plugin + 2.452 ${jenkins.baseline}.4 @@ -122,6 +123,204 @@ void testAddBaseline() { hpi Empty Plugin + + 2.452 + ${jenkins.baseline}.4 + + + + + io.jenkins.tools.bom + bom-${jenkins.baseline}.x + 3814.v9563d972079a_ + pom + import + + + + + + repo.jenkins-ci.org + https://repo.jenkins-ci.org/public/ + + + + + repo.jenkins-ci.org + https://repo.jenkins-ci.org/public/ + + + + """)); + } + + @Test + void testAddComment() { + rewriteRun( + spec -> spec.recipe(new MigrateToJenkinsBaseLineProperty()), + pomXml( + """ + + + 4.0.0 + + org.jenkins-ci.plugins + plugin + 4.88 + + + io.jenkins.plugins + empty + 1.0.0-SNAPSHOT + hpi + Empty Plugin + + 2.452 + ${jenkins.baseline}.4 + + + + + io.jenkins.tools.bom + bom-2.452.x + 3814.v9563d972079a_ + pom + import + + + + + + repo.jenkins-ci.org + https://repo.jenkins-ci.org/public/ + + + + + repo.jenkins-ci.org + https://repo.jenkins-ci.org/public/ + + + + """, + """ + + + 4.0.0 + + org.jenkins-ci.plugins + plugin + 4.88 + + + io.jenkins.plugins + empty + 1.0.0-SNAPSHOT + hpi + Empty Plugin + + + 2.452 + ${jenkins.baseline}.4 + + + + + io.jenkins.tools.bom + bom-${jenkins.baseline}.x + 3814.v9563d972079a_ + pom + import + + + + + + repo.jenkins-ci.org + https://repo.jenkins-ci.org/public/ + + + + + repo.jenkins-ci.org + https://repo.jenkins-ci.org/public/ + + + + """)); + } + + @Test + void testAddBaselineWithOtherProperties() { + rewriteRun( + spec -> spec.recipe(new MigrateToJenkinsBaseLineProperty()), + pomXml( + """ + + + 4.0.0 + + org.jenkins-ci.plugins + plugin + 4.88 + + + io.jenkins.plugins + empty + 1.0.0-SNAPSHOT + hpi + Empty Plugin + + + + baz + 2.452.4 + + + + + io.jenkins.tools.bom + bom-2.452.x + 3814.v9563d972079a_ + pom + import + + + + + + repo.jenkins-ci.org + https://repo.jenkins-ci.org/public/ + + + + + repo.jenkins-ci.org + https://repo.jenkins-ci.org/public/ + + + + """, + """ + + + 4.0.0 + + org.jenkins-ci.plugins + plugin + 4.88 + + + io.jenkins.plugins + empty + 1.0.0-SNAPSHOT + hpi + Empty Plugin + + + + baz + 2.452 ${jenkins.baseline}.4 @@ -216,6 +415,7 @@ void testFixBom() { hpi Empty Plugin + 2.479 ${jenkins.baseline}.1 diff --git a/plugin-modernizer-core/src/test/java/io/jenkins/tools/pluginmodernizer/core/visitors/AddBeforePropertyTest.java b/plugin-modernizer-core/src/test/java/io/jenkins/tools/pluginmodernizer/core/visitors/AddBeforePropertyTest.java new file mode 100644 index 00000000..d37b3e61 --- /dev/null +++ b/plugin-modernizer-core/src/test/java/io/jenkins/tools/pluginmodernizer/core/visitors/AddBeforePropertyTest.java @@ -0,0 +1,154 @@ +package io.jenkins.tools.pluginmodernizer.core.visitors; + +import static org.openrewrite.maven.Assertions.pomXml; +import static org.openrewrite.test.RewriteTest.toRecipe; + +import org.junit.jupiter.api.Test; +import org.openrewrite.ExecutionContext; +import org.openrewrite.maven.MavenIsoVisitor; +import org.openrewrite.test.RewriteTest; +import org.openrewrite.xml.tree.Xml; + +/** + * A visitor that add a maven property before another property. + */ +public class AddBeforePropertyTest implements RewriteTest { + + @Test + void addProperty() { + rewriteRun( + spec -> spec.recipe(toRecipe(() -> new MavenIsoVisitor<>() { + @Override + public Xml.Document visitDocument(Xml.Document x, ExecutionContext ctx) { + doAfterVisit(new AddBeforePropertyVisitor("jenkins.version", "jenkins.baseline", "2.440")); + return super.visitDocument(x, ctx); + } + })), + pomXml( + """ + + + 4.0.0 + io.jenkins.plugins + empty + 1.0.0-SNAPSHOT + hpi + Empty Plugin + + 2.440 + + + """, + """ + + + 4.0.0 + io.jenkins.plugins + empty + 1.0.0-SNAPSHOT + hpi + Empty Plugin + + 2.440 + 2.440 + + + """)); + } + + @Test + void addPropertyWithOtherProperties() { + rewriteRun( + spec -> spec.recipe(toRecipe(() -> new MavenIsoVisitor<>() { + @Override + public Xml.Document visitDocument(Xml.Document x, ExecutionContext ctx) { + doAfterVisit(new AddBeforePropertyVisitor("jenkins.version", "jenkins.baseline", "2.440")); + return super.visitDocument(x, ctx); + } + })), + pomXml( + """ + + + 4.0.0 + io.jenkins.plugins + empty + 1.0.0-SNAPSHOT + hpi + Empty Plugin + + baz + 2.440 + + + """, + """ + + + 4.0.0 + io.jenkins.plugins + empty + 1.0.0-SNAPSHOT + hpi + Empty Plugin + + baz + 2.440 + 2.440 + + + """)); + } + + @Test + void addPropertyWithOtherPropertiesWithComment() { + rewriteRun( + spec -> spec.recipe(toRecipe(() -> new MavenIsoVisitor<>() { + @Override + public Xml.Document visitDocument(Xml.Document x, ExecutionContext ctx) { + doAfterVisit(new AddBeforePropertyVisitor("jenkins.version", "jenkins.baseline", "2.440")); + return super.visitDocument(x, ctx); + } + })), + pomXml( + """ + + + 4.0.0 + io.jenkins.plugins + empty + 1.0.0-SNAPSHOT + hpi + Empty Plugin + + + + baz + foo + + 2.440 + + + """, + """ + + + 4.0.0 + io.jenkins.plugins + empty + 1.0.0-SNAPSHOT + hpi + Empty Plugin + + + + baz + foo + 2.440 + + 2.440 + + + """)); + } +} diff --git a/plugin-modernizer-core/src/test/java/io/jenkins/tools/pluginmodernizer/core/visitors/AddPropertyCommentVisitorTest.java b/plugin-modernizer-core/src/test/java/io/jenkins/tools/pluginmodernizer/core/visitors/AddPropertyCommentVisitorTest.java new file mode 100644 index 00000000..edc82b5d --- /dev/null +++ b/plugin-modernizer-core/src/test/java/io/jenkins/tools/pluginmodernizer/core/visitors/AddPropertyCommentVisitorTest.java @@ -0,0 +1,153 @@ +package io.jenkins.tools.pluginmodernizer.core.visitors; + +import static org.openrewrite.maven.Assertions.pomXml; +import static org.openrewrite.test.RewriteTest.toRecipe; + +import org.junit.jupiter.api.Test; +import org.openrewrite.ExecutionContext; +import org.openrewrite.maven.MavenIsoVisitor; +import org.openrewrite.test.RewriteTest; +import org.openrewrite.xml.tree.Xml; + +/** + * Tests for {@link AddPropertyCommentVisitor}. + */ +public class AddPropertyCommentVisitorTest implements RewriteTest { + + @Test + void addPropertyComment() { + rewriteRun( + spec -> spec.recipe(toRecipe(() -> new MavenIsoVisitor<>() { + @Override + public Xml.Document visitDocument(Xml.Document x, ExecutionContext ctx) { + doAfterVisit(new AddPropertyCommentVisitor("jenkins.version", " This is the jenkins version ")); + return super.visitDocument(x, ctx); + } + })), + pomXml( + """ + + + 4.0.0 + io.jenkins.plugins + empty + 1.0.0-SNAPSHOT + hpi + Empty Plugin + + 2.440 + + + """, + """ + + + 4.0.0 + io.jenkins.plugins + empty + 1.0.0-SNAPSHOT + hpi + Empty Plugin + + + 2.440 + + + """)); + } + + @Test + void addPropertyCommentWithOtherProperties() { + rewriteRun( + spec -> spec.recipe(toRecipe(() -> new MavenIsoVisitor<>() { + @Override + public Xml.Document visitDocument(Xml.Document x, ExecutionContext ctx) { + doAfterVisit(new AddPropertyCommentVisitor("jenkins.version", " This is the jenkins version ")); + return super.visitDocument(x, ctx); + } + })), + pomXml( + """ + + + 4.0.0 + io.jenkins.plugins + empty + 1.0.0-SNAPSHOT + hpi + Empty Plugin + + + value + 2.440 + + + """, + """ + + + 4.0.0 + io.jenkins.plugins + empty + 1.0.0-SNAPSHOT + hpi + Empty Plugin + + + value + + 2.440 + + + """)); + } + + @Test + void replacePropertyComment() { + rewriteRun( + spec -> spec.recipe(toRecipe(() -> new MavenIsoVisitor<>() { + @Override + public Xml.Document visitDocument(Xml.Document x, ExecutionContext ctx) { + doAfterVisit(new AddPropertyCommentVisitor("jenkins.version", " This is the jenkins version ")); + return super.visitDocument(x, ctx); + } + })), + pomXml( + """ + + + 4.0.0 + io.jenkins.plugins + empty + 1.0.0-SNAPSHOT + hpi + Empty Plugin + + + + value + + 2.440 + + + """, + """ + + + 4.0.0 + io.jenkins.plugins + empty + 1.0.0-SNAPSHOT + hpi + Empty Plugin + + + + value + + 2.440 + + + """)); + } +} diff --git a/plugin-modernizer-core/src/test/java/io/jenkins/tools/pluginmodernizer/core/visitors/RemovePropertyCommentVisitorTest.java b/plugin-modernizer-core/src/test/java/io/jenkins/tools/pluginmodernizer/core/visitors/RemovePropertyCommentVisitorTest.java new file mode 100644 index 00000000..c28c1acf --- /dev/null +++ b/plugin-modernizer-core/src/test/java/io/jenkins/tools/pluginmodernizer/core/visitors/RemovePropertyCommentVisitorTest.java @@ -0,0 +1,64 @@ +package io.jenkins.tools.pluginmodernizer.core.visitors; + +import static org.openrewrite.maven.Assertions.pomXml; +import static org.openrewrite.test.RewriteTest.toRecipe; + +import org.junit.jupiter.api.Test; +import org.openrewrite.ExecutionContext; +import org.openrewrite.maven.MavenIsoVisitor; +import org.openrewrite.test.RewriteTest; +import org.openrewrite.xml.tree.Xml; + +/** + * A visitor that remove a property comment. + */ +public class RemovePropertyCommentVisitorTest implements RewriteTest { + + @Test + void replacePropertyComment() { + rewriteRun( + spec -> spec.recipe(toRecipe(() -> new MavenIsoVisitor<>() { + @Override + public Xml.Document visitDocument(Xml.Document x, ExecutionContext ctx) { + doAfterVisit(new RemovePropertyCommentVisitor(" My other property ")); + doAfterVisit(new RemovePropertyCommentVisitor(" Unrelated comment ")); + return super.visitDocument(x, ctx); + } + })), + pomXml( + """ + + + 4.0.0 + io.jenkins.plugins + empty + 1.0.0-SNAPSHOT + hpi + Empty Plugin + + + + value + + 2.440 + + + """, + """ + + + 4.0.0 + io.jenkins.plugins + empty + 1.0.0-SNAPSHOT + hpi + Empty Plugin + + + value + 2.440 + + + """)); + } +}