Skip to content

Commit

Permalink
Merge pull request #539 from jonesbusy/feature/filter-incrementals
Browse files Browse the repository at this point in the history
Filter incrementals on `UpdateBomVersion` visitor
  • Loading branch information
jonesbusy authored Dec 30, 2024
2 parents 9fcfba1 + a8e62d2 commit 68a18ae
Show file tree
Hide file tree
Showing 4 changed files with 326 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ private RecipesConsts() {

public static final String PLUGINS_BOM_GROUP_ID = "io.jenkins.tools.bom";
public static final String VERSION_METADATA_PATTERN = "\\.v[a-f0-9_]+";
public static final String INCREMENTAL_REPO_ID = "incrementals";
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ public Xml.Document visitDocument(Xml.Document document, PluginMetadata pluginMe
}
// Lookup by group ID to set the BOM version if any
pom.getDependencyManagement().stream()
.peek(dependency -> LOG.debug("Dependency: {}", dependency))
.filter(dependency -> RecipesConsts.PLUGINS_BOM_GROUP_ID.equals(dependency.getGroupId()))
.findFirst()
.ifPresent(dependency -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package io.jenkins.tools.pluginmodernizer.core.visitors;

import io.jenkins.tools.pluginmodernizer.core.config.RecipesConsts;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import org.openrewrite.ExecutionContext;
import org.openrewrite.maven.*;
import org.openrewrite.maven.MavenDownloadingException;
import org.openrewrite.maven.MavenIsoVisitor;
import org.openrewrite.maven.internal.MavenPomDownloader;
import org.openrewrite.maven.table.MavenMetadataFailures;
import org.openrewrite.maven.trait.MavenDependency;
import org.openrewrite.maven.tree.GroupArtifact;
import org.openrewrite.maven.tree.MavenMetadata;
import org.openrewrite.maven.tree.MavenResolutionResult;
import org.openrewrite.semver.LatestRelease;
import org.openrewrite.semver.VersionComparator;
import org.openrewrite.semver.Semver;
import org.openrewrite.xml.ChangeTagValueVisitor;
import org.openrewrite.xml.tree.Xml;
import org.slf4j.Logger;
Expand All @@ -22,7 +29,16 @@ public class UpdateBomVersionVisitor extends MavenIsoVisitor<ExecutionContext> {
*/
private static final Logger LOG = LoggerFactory.getLogger(UpdateBomVersionVisitor.class);

private transient MavenMetadataFailures metadataFailures;
/**
* The metadata failures from recipe
*/
private final transient MavenMetadataFailures metadataFailures;

/**
* The version comparator for the bom
*/
private final transient LatestRelease latestBomReleaseComparator =
new LatestRelease(RecipesConsts.VERSION_METADATA_PATTERN);

/**
* Contructor
Expand All @@ -49,14 +65,14 @@ public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) {
"bom-" + getProperties.getChildValue("jenkins.baseline").orElseThrow() + ".x";
}

String newBomVersionAvailable = findNewerBomVersion(artifactId, version, ctx);
if (newBomVersionAvailable == null) {
String newBomVersion = getLatestBomVersion(artifactId, version, ctx);
if (newBomVersion == null) {
LOG.debug("No newer version available for {}", artifactId);
return document;
}
LOG.debug("Newer version available for {}: {}", artifactId, newBomVersion);

return (Xml.Document)
new ChangeTagValueVisitor<>(versionTag, newBomVersionAvailable).visitNonNull(document, ctx);
return (Xml.Document) new ChangeTagValueVisitor<>(versionTag, newBomVersion).visitNonNull(document, ctx);
}

/**
Expand All @@ -66,23 +82,52 @@ public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) {
* @param ctx The execution context
* @return The newer bom version
*/
private String findNewerBomVersion(String artifactId, String currentVersion, ExecutionContext ctx) {
VersionComparator latestRelease = new LatestRelease(RecipesConsts.VERSION_METADATA_PATTERN);
public String getLatestBomVersion(String artifactId, String currentVersion, ExecutionContext ctx) {
try {
return MavenDependency.findNewerVersion(
RecipesConsts.PLUGINS_BOM_GROUP_ID,
artifactId,
currentVersion,
getResolutionResult(),
metadataFailures,
latestRelease,
ctx);
return getLatestBomVersion(artifactId, currentVersion, getResolutionResult(), ctx);
} catch (MavenDownloadingException e) {
LOG.warn("Failed to download metadata for {}", artifactId, e);
return null;
}
}

/**
* Get the latest bom version
* @param artifactId The artifact id
* @param currentVersion The current version
* @param mrr The maven resolution result
* @param ctx The execution context
* @return The latest
*/
private String getLatestBomVersion(
String artifactId, String currentVersion, MavenResolutionResult mrr, ExecutionContext ctx)
throws MavenDownloadingException {

// Since 'incrementals' repository is always enabled with -Pconsume-incrementals
// the only way to exclude incrementals bom version is to exclude the repository
MavenMetadata mavenMetadata = metadataFailures.insertRows(ctx, () -> (new MavenPomDownloader(ctx))
.downloadMetadata(
new GroupArtifact(RecipesConsts.PLUGINS_BOM_GROUP_ID, artifactId),
null,
mrr.getPom().getRepositories().stream()
.filter(r -> !Objects.equals(r.getId(), RecipesConsts.INCREMENTAL_REPO_ID))
.toList()));

// Keep track of version found
List<String> versions = new ArrayList<>();
for (String v : mavenMetadata.getVersioning().getVersions()) {
if (latestBomReleaseComparator.isValid(currentVersion, v)) {
versions.add(v);
}
}
if (!Semver.isVersion(currentVersion) && !versions.isEmpty()) {
versions.sort(latestBomReleaseComparator);
return versions.get(versions.size() - 1);
} else {
return latestBomReleaseComparator.upgrade(currentVersion, versions).orElse(null);
}
}

/**
* Get the bom tag from the document
* @param document The document
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,262 @@
package io.jenkins.tools.pluginmodernizer.core.recipes;

import static org.openrewrite.maven.Assertions.pomXml;

import org.junit.jupiter.api.Test;
import org.openrewrite.Issue;
import org.openrewrite.maven.MavenParser;
import org.openrewrite.test.RewriteTest;

/**
* Test for {@link UpdateBom}.
*/
public class UpdateBomTest implements RewriteTest {

@Test
void shouldSkipIfNoBom() {
rewriteRun(
spec -> spec.recipe(new UpdateBom()),
// language=xml
pomXml(
"""
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.jenkins.plugins</groupId>
<artifactId>empty</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>hpi</packaging>
<name>Empty Plugin</name>
<properties>
<jenkins.version>2.440</jenkins.version>
</properties>
</project>
"""));
}

@Test
void shouldUpdateToLatestReleasedWithoutMavenConfig() {
rewriteRun(
spec -> spec.recipe(new UpdateBom()),
// language=xml
pomXml(
"""
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.jenkins.plugins</groupId>
<artifactId>empty</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>hpi</packaging>
<name>Empty Plugin</name>
<properties>
<jenkins.version>2.440.3</jenkins.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.jenkins.tools.bom</groupId>
<artifactId>bom-2.440.x</artifactId>
<version>2746.vb_79a_1d3e7b_c8</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<repositories>
<repository>
<id>repo.jenkins-ci.org</id>
<url>https://repo.jenkins-ci.org/public/</url>
</repository>
</repositories>
</project>
""",
"""
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.jenkins.plugins</groupId>
<artifactId>empty</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>hpi</packaging>
<name>Empty Plugin</name>
<properties>
<jenkins.version>2.440.3</jenkins.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.jenkins.tools.bom</groupId>
<artifactId>bom-2.440.x</artifactId>
<version>3435.v238d66a_043fb_</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<repositories>
<repository>
<id>repo.jenkins-ci.org</id>
<url>https://repo.jenkins-ci.org/public/</url>
</repository>
</repositories>
</project>
"""));
}

@Test
@Issue("https://github.com/jenkins-infra/plugin-modernizer-tool/issues/534")
void shouldUpdateToLatestReleasedWithIncrementalsEnabled() {
rewriteRun(
spec -> {
spec.parser(MavenParser.builder().activeProfiles("consume-incrementals"));
spec.recipe(new UpdateBom());
},
// language=xml
pomXml(
"""
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>4.88</version>
<relativePath />
</parent>
<groupId>io.jenkins.plugins</groupId>
<artifactId>empty</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>hpi</packaging>
<name>Empty Plugin</name>
<properties>
<jenkins.version>2.440.3</jenkins.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.jenkins.tools.bom</groupId>
<artifactId>bom-2.440.x</artifactId>
<version>2746.vb_79a_1d3e7b_c8</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<repositories>
<repository>
<id>repo.jenkins-ci.org</id>
<url>https://repo.jenkins-ci.org/public/</url>
</repository>
</repositories>
</project>
""",
"""
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>4.88</version>
<relativePath />
</parent>
<groupId>io.jenkins.plugins</groupId>
<artifactId>empty</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>hpi</packaging>
<name>Empty Plugin</name>
<properties>
<jenkins.version>2.440.3</jenkins.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.jenkins.tools.bom</groupId>
<artifactId>bom-2.440.x</artifactId>
<version>3435.v238d66a_043fb_</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<repositories>
<repository>
<id>repo.jenkins-ci.org</id>
<url>https://repo.jenkins-ci.org/public/</url>
</repository>
</repositories>
</project>
"""));
}

@Test
void shouldUpdateToLatestIncrementalsWithoutMavenConfig() {
rewriteRun(
spec -> spec.recipe(new UpdateBom()),
// language=xml
pomXml(
"""
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.jenkins.plugins</groupId>
<artifactId>empty</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>hpi</packaging>
<name>Empty Plugin</name>
<properties>
<jenkins.version>2.440.3</jenkins.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.jenkins.tools.bom</groupId>
<artifactId>bom-2.440.x</artifactId>
<version>2747.v42d8f18d1741</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<repositories>
<repository>
<id>repo.jenkins-ci.org</id>
<url>https://repo.jenkins-ci.org/incrementals/</url>
</repository>
</repositories>
</project>
""",
"""
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.jenkins.plugins</groupId>
<artifactId>empty</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>hpi</packaging>
<name>Empty Plugin</name>
<properties>
<jenkins.version>2.440.3</jenkins.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.jenkins.tools.bom</groupId>
<artifactId>bom-2.440.x</artifactId>
<version>3440.v7ed0db_c20c5e</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<repositories>
<repository>
<id>repo.jenkins-ci.org</id>
<url>https://repo.jenkins-ci.org/incrementals/</url>
</repository>
</repositories>
</project>
"""));
}
}

0 comments on commit 68a18ae

Please sign in to comment.