Skip to content

Commit

Permalink
Ensure second execution work and fix 2.479.1 migration
Browse files Browse the repository at this point in the history
  • Loading branch information
jonesbusy committed Dec 27, 2024
1 parent fb67bf9 commit 4dfeb3f
Show file tree
Hide file tree
Showing 19 changed files with 927 additions and 45 deletions.
43 changes: 43 additions & 0 deletions plugin-modernizer-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,27 @@
<artifactId>logback-classic</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>6.1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>test</scope>
</dependency>

<!-- Recipes test dependencies -->
<dependency>
<groupId>org.kohsuke.stapler</groupId>
<artifactId>stapler</artifactId>
<version>1928.v9115fe47607f</version>
<scope>test</scope>
</dependency>

<!-- Test dependencies -->
<dependency>
<groupId>org.openrewrite</groupId>
Expand All @@ -177,6 +198,7 @@
<version>2.1.7</version>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand Down Expand Up @@ -206,6 +228,27 @@
</resource>
</resources>
<plugins>

<!-- Used to copy openrewrite test dependencies to a separate directory for running java recipes -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.8.1</version>
<executions>
<execution>
<id>copy-dependencies</id>
<goals>
<goal>copy-dependencies</goal>
</goals>
<phase>validate</phase>
<configuration>
<outputDirectory>${project.build.directory}/openrewrite-classpath</outputDirectory>
<!-- Adapt when testing java transformation -->
<includeGroupIds>javax.servlet,jakarta.servlet,org.kohsuke.stapler</includeGroupIds>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>gg.jte</groupId>
<artifactId>jte-maven-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,10 @@ private static Path getDefaultMavenLocalRepo() {
return readProperty("jenkins.parent.version", "versions.properties");
}

public static @Nullable String getBomVersion() {
return readProperty("bom.version", "versions.properties");
}

private static @Nullable URL getUpdateCenterUrl() throws MalformedURLException {
String url = System.getenv("JENKINS_UC");
if (url != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public Tree visit(Tree tree, ExecutionContext executionContext) {
// Extract metadata from POM
else if (PathUtils.matchesGlob(sourceFile.getSourcePath(), "**/pom.xml")) {
LOG.debug("Visiting POM {}", sourceFile.getSourcePath());
PluginMetadata pomMetadata = new PomVisitor().reduce(tree, pluginMetadata);
PluginMetadata pomMetadata = new PomResolutionVisitor().reduce(tree, pluginMetadata);
LOG.debug("POM metadata: {}", JsonUtils.toJson(pomMetadata));
executionContext.putMessage("pomMetadata", pomMetadata); // Is there better than context messaging ?
return tree;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,24 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PomVisitor extends MavenIsoVisitor<PluginMetadata> {
/**
* A pom visitor that accumulate PluginMetadata using maven resolution result.
* Maven resolution might not get updated if the tree is modified by other visitor
* So it's best used in preconditons recipes to avoid side effect
* An other implementation of this visitor could be extraction from the tree instead of maven resolution result
*/
public class PomResolutionVisitor extends MavenIsoVisitor<PluginMetadata> {

private static final Logger LOG = LoggerFactory.getLogger(PomVisitor.class);
/**
* LOGGER.
*/
private static final Logger LOG = LoggerFactory.getLogger(PomResolutionVisitor.class);

@Override
public Xml.Document visitDocument(Xml.Document document, PluginMetadata pluginMetadata) {

document = super.visitDocument(document, pluginMetadata);

Markers markers = getCursor().firstEnclosingOrThrow(Xml.Document.class).getMarkers();

// Ensure maven resolution result is present
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.jenkins.tools.pluginmodernizer.core.recipes;

import io.jenkins.tools.pluginmodernizer.core.extractor.PluginMetadata;
import io.jenkins.tools.pluginmodernizer.core.extractor.PomVisitor;
import io.jenkins.tools.pluginmodernizer.core.extractor.PomResolutionVisitor;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
Expand Down Expand Up @@ -37,7 +37,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
@Override
public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) {

PluginMetadata pluginMetadata = new PomVisitor().reduce(document, new PluginMetadata());
PluginMetadata pluginMetadata = new PomResolutionVisitor().reduce(document, new PluginMetadata());

if (pluginMetadata.getBomVersion() == null) {
LOG.info("Project is missing Jenkins BOM");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.jenkins.tools.pluginmodernizer.core.recipes;

import io.jenkins.tools.pluginmodernizer.core.extractor.PluginMetadata;
import io.jenkins.tools.pluginmodernizer.core.extractor.PomVisitor;
import io.jenkins.tools.pluginmodernizer.core.extractor.PomResolutionVisitor;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
Expand Down Expand Up @@ -37,7 +37,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
@Override
public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) {

PluginMetadata pluginMetadata = new PomVisitor().reduce(document, new PluginMetadata());
PluginMetadata pluginMetadata = new PomResolutionVisitor().reduce(document, new PluginMetadata());

if (!pluginMetadata.getProperties().containsKey("jenkins.baseline")) {
LOG.info("Project is missing jenkins.baseline property");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.jenkins.tools.pluginmodernizer.core.recipes;

import io.jenkins.tools.pluginmodernizer.core.extractor.PluginMetadata;
import io.jenkins.tools.pluginmodernizer.core.extractor.PomVisitor;
import io.jenkins.tools.pluginmodernizer.core.extractor.PomResolutionVisitor;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
Expand Down Expand Up @@ -37,7 +37,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
@Override
public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) {

PluginMetadata pluginMetadata = new PomVisitor().reduce(document, new PluginMetadata());
PluginMetadata pluginMetadata = new PomResolutionVisitor().reduce(document, new PluginMetadata());

if (pluginMetadata.getBomVersion() != null) {
LOG.info("Project is using Jenkins bom at version {}", pluginMetadata.getBomVersion());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,40 @@
package io.jenkins.tools.pluginmodernizer.core.recipes;

import io.jenkins.tools.pluginmodernizer.core.extractor.PluginMetadata;
import io.jenkins.tools.pluginmodernizer.core.extractor.PomVisitor;
import io.jenkins.tools.pluginmodernizer.core.extractor.PomResolutionVisitor;
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.ChangeManagedDependencyGroupIdAndArtifactId;
import org.openrewrite.maven.MavenIsoVisitor;
import org.openrewrite.xml.ChangeTagValueVisitor;
import org.openrewrite.xml.tree.Xml;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* A recipe to migrate the pom.xml to use the jenkins.baseLine property.
* Doesn't upgrade the jenkins version or bom version.
* If the plugin is not using the bom or using the weekly baseline, it will skip the update.
* This will also fix the bom version if it doesn't match the jenkins.version.
*/
public class MigrateToJenkinsBaseLineProperty extends Recipe {

/**
* Logger for the class.
*/
public static final Logger LOG = LoggerFactory.getLogger(MigrateToJenkinsBaseLineProperty.class);

@Override
public String getDisplayName() {
return "Migrate to Jenkins baseline property";
return "Update the pom.xml to use newly introduced jenkins.baseline property";
}

@Override
public String getDescription() {
return "Migrate to Jenkins baseline property.";
return "Update the pom.xml to use newly introduced jenkins.baseline property.";
}

@Override
Expand All @@ -47,8 +56,8 @@ private static class MigrateToJenkinsBaseLinePropertyVisitor extends MavenIsoVis
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());
// Skip if not using bom or using weekly baseline
PluginMetadata pluginMetadata = new PomResolutionVisitor().reduce(document, new PluginMetadata());
if (pluginMetadata.getBomArtifactId() == null || "bom-weekly".equals(pluginMetadata.getBomArtifactId())) {
LOG.debug("Project is using Jenkins weekly bom or not bom, skipping");
return d;
Expand All @@ -58,6 +67,10 @@ public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) {
return document;
}

/**
* Perform the update.
* @param document The document to update.
*/
private void performUpdate(Xml.Document document) {

Xml.Tag jenkinsVersionTag = document.getRoot()
Expand All @@ -71,7 +84,7 @@ private void performUpdate(Xml.Document document) {

// Keep only major and minor and ignore patch version
String jenkinsVersion = jenkinsVersionTag.getValue().get();
String jenkinsBaseline = jenkinsVersionTag.getValue().get();
String jenkinsBaseline = jenkinsVersion;
String jenkinsPatch = null;

// It's a LTS, extract patch
Expand Down Expand Up @@ -102,20 +115,31 @@ private void performUpdate(Xml.Document document) {
}

// Change the bom artifact ID
Xml.Tag artifactIdTag = document.getRoot()
Xml.Tag bom = document.getRoot()
.getChild("dependencyManagement")
.flatMap(dm -> dm.getChild("dependencies"))
.flatMap(deps -> deps.getChild("dependency"))
.filter(dep -> dep.getChildValue("groupId")
.map("io.jenkins.tools.bom"::equals)
.orElse(false))
.flatMap(dep -> dep.getChild("artifactId"))
.orElseThrow();

Xml.Tag artifactIdTag = bom.getChild("artifactId").orElseThrow();
Xml.Tag version = bom.getChild("version").orElseThrow();

LOG.debug("Artifact ID is {}", artifactIdTag.getValue().get());
LOG.debug("Version is {}", version.getValue().get());

// Change the artifact and perform upgrade
if (!artifactIdTag.getValue().get().equals("bom-${jenkins.baseline}.x")) {
doAfterVisit(new ChangeTagValueVisitor<>(artifactIdTag, "bom-${jenkins.baseline}.x"));
doAfterVisit(new ChangeManagedDependencyGroupIdAndArtifactId(
"io.jenkins.tools.bom",
artifactIdTag.getValue().get(),
"io.jenkins.tools.bom",
"bom-${jenkins.baseline}.x",
version.getValue().get(),
"\\.v[a-f0-9_]+")
.getVisitor());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.jenkins.tools.pluginmodernizer.core.recipes;

import io.jenkins.tools.pluginmodernizer.core.visitors.UpdateBomVersionVisitor;
import org.openrewrite.*;
import org.openrewrite.maven.table.MavenMetadataFailures;

/**
* A recipe that update the bom version to latest available.
*/
public class UpdateBom extends Recipe {

@Override
public @NlsRewrite.DisplayName String getDisplayName() {
return "Update bom recipe";
}

@Override
public @NlsRewrite.Description String getDescription() {
return "Update bom recipe.";
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(new IsUsingBom(), new UpdateBomVersionVisitor(new MavenMetadataFailures(this)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package io.jenkins.tools.pluginmodernizer.core.recipes;

import org.openrewrite.*;
import org.openrewrite.jenkins.UpgradeVersionProperty;
import org.openrewrite.maven.MavenIsoVisitor;
import org.openrewrite.xml.tree.Xml;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* A recipe to upgrade the Jenkins version in the pom.xml.
* 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 {

/**
* LOGGER.
*/
private static final Logger LOG = LoggerFactory.getLogger(UpgradeJenkinsVersion.class);

/**
* The minimum version.
*/
@Option(displayName = "Version", description = "The version.", example = "2.452.4")
String minimumVersion;

/**
* Constructor.
* @param minimumVersion The minimum version.
*/
public UpgradeJenkinsVersion(String minimumVersion) {
this.minimumVersion = minimumVersion;
}

@Override
public String getDisplayName() {
return "Upgrade Jenkins version";
}

@Override
public String getDescription() {
return "Upgrade Jenkins version.";
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new MavenIsoVisitor<>() {

@Override
public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) {

// Return another tree with jenkins version updated
document = (Xml.Document) new UpgradeVersionProperty("jenkins.version", minimumVersion)
.getVisitor()
.visitNonNull(document, ctx);
return (Xml.Document) new UpdateBom().getVisitor().visitNonNull(document, ctx);
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,21 @@
import org.openrewrite.xml.tree.Content;
import org.openrewrite.xml.tree.Xml;

/**
* A visitor that remove a comment from a property in a maven pom file.
* Only a comment with exact match will be removed inside the properties block.
*/
public class RemovePropertyCommentVisitor extends MavenIsoVisitor<ExecutionContext> {

private String comment;
/**
* The comment to remove.
*/
private final String comment;

/**
* Constructor of the visitor.
* @param comment The comment to remove.
*/
public RemovePropertyCommentVisitor(String comment) {
this.comment = comment;
}
Expand All @@ -20,7 +31,7 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext executionContext) {
tag = super.visitTag(tag, executionContext);
if (tag.getName().equals("properties")) {

List<Content> contents = new ArrayList<>(tag.getContent());
List<Content> contents = new ArrayList<>(tag.getContent() != null ? tag.getContent() : List.of());

// Remove the comment if needed
boolean containsComment = contents.stream()
Expand Down
Loading

0 comments on commit 4dfeb3f

Please sign in to comment.