Skip to content

Commit b21a389

Browse files
authored
Merge pull request #715 from jonesbusy/bugfix/old-bom
Ensure old bom version can be upgraded
2 parents da7ccd5 + 490cceb commit b21a389

File tree

3 files changed

+115
-2
lines changed

3 files changed

+115
-2
lines changed

plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/config/RecipesConsts.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ private RecipesConsts() {
1414

1515
public static final String PLUGIN_POM_GROUP_ID = "org.jenkins-ci.plugins";
1616
public static final String PLUGINS_BOM_GROUP_ID = "io.jenkins.tools.bom";
17-
public static final String ANALYSIS_POM_GROUP_ID = "org.jvnet.hudson.plugins";
1817
public static final String ANALYSIS_POM_ARTIFACT_ID = "analysis-pom";
1918
public static final String VERSION_METADATA_PATTERN = "\\.v[a-f0-9_]+";
19+
public static final String OLD_BOM_VERSION_PATTERN =
20+
""; // Old release line like 2.150.x, no need to filter by pattern
2021
public static final String INCREMENTAL_REPO_ID = "incrementals";
2122
}

plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/visitors/UpdateBomVersionVisitor.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ public class UpdateBomVersionVisitor extends MavenIsoVisitor<ExecutionContext> {
4040
private final transient LatestRelease latestBomReleaseComparator =
4141
new LatestRelease(RecipesConsts.VERSION_METADATA_PATTERN);
4242

43+
/**
44+
* Old bom version comparator that where not using JEP-229
45+
*/
46+
private final transient LatestRelease oldBomReleaseComparator =
47+
new LatestRelease(RecipesConsts.OLD_BOM_VERSION_PATTERN);
48+
4349
/**
4450
* Contructor
4551
*/
@@ -154,16 +160,27 @@ private String getLatestBomVersion(
154160

155161
// Keep track of version found
156162
List<String> versions = new ArrayList<>();
163+
List<String> oldVersions = new ArrayList<>();
157164
for (String v : mavenMetadata.getVersioning().getVersions()) {
158165
if (latestBomReleaseComparator.isValid(currentVersion, v)) {
159166
versions.add(v);
160167
}
168+
if (oldBomReleaseComparator.isValid(currentVersion, v)) {
169+
oldVersions.add(v);
170+
}
161171
}
162172

163173
// Take latest version available. Allow to downgrade from incrementals to release
164174
if (!Semver.isVersion(currentVersion) && !versions.isEmpty() || (!versions.contains(currentVersion))) {
165175
versions.sort(latestBomReleaseComparator);
166-
return versions.get(versions.size() - 1);
176+
oldVersions.sort(oldBomReleaseComparator);
177+
if (!versions.isEmpty()) {
178+
return versions.get(versions.size() - 1);
179+
}
180+
if (!oldVersions.isEmpty()) {
181+
return oldVersions.get(oldVersions.size() - 1);
182+
}
183+
return currentVersion;
167184
} else {
168185
return latestBomReleaseComparator.upgrade(currentVersion, versions).orElse(null);
169186
}

plugin-modernizer-core/src/test/java/io/jenkins/tools/pluginmodernizer/core/recipes/DeclarativeRecipesTest.java

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,101 @@ void testUpgradeBomVersion() {
320320
.formatted(Settings.getJenkinsParentVersion(), Settings.getBomVersion())));
321321
}
322322

323+
@Test
324+
void testUpgradeOldBomVersionFormat() {
325+
rewriteRun(
326+
spec -> spec.recipeFromResource(
327+
"/META-INF/rewrite/recipes.yml", "io.jenkins.tools.pluginmodernizer.UpgradeBomVersion"),
328+
// language=xml
329+
pomXml(
330+
"""
331+
<?xml version="1.0" encoding="UTF-8"?>
332+
<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">
333+
<modelVersion>4.0.0</modelVersion>
334+
<parent>
335+
<groupId>org.jenkins-ci.plugins</groupId>
336+
<artifactId>plugin</artifactId>
337+
<version>4.0</version>
338+
<relativePath />
339+
</parent>
340+
<groupId>io.jenkins.plugins</groupId>
341+
<artifactId>empty</artifactId>
342+
<version>1.0.0-SNAPSHOT</version>
343+
<packaging>hpi</packaging>
344+
<name>Empty Plugin</name>
345+
<properties>
346+
<jenkins.version>2.164.3</jenkins.version>
347+
</properties>
348+
<dependencyManagement>
349+
<dependencies>
350+
<dependency>
351+
<groupId>io.jenkins.tools.bom</groupId>
352+
<artifactId>bom-2.164.x</artifactId>
353+
<version>3</version>
354+
<type>pom</type>
355+
<scope>import</scope>
356+
</dependency>
357+
</dependencies>
358+
</dependencyManagement>
359+
<repositories>
360+
<repository>
361+
<id>repo.jenkins-ci.org</id>
362+
<url>https://repo.jenkins-ci.org/public/</url>
363+
</repository>
364+
</repositories>
365+
<pluginRepositories>
366+
<pluginRepository>
367+
<id>repo.jenkins-ci.org</id>
368+
<url>https://repo.jenkins-ci.org/public/</url>
369+
</pluginRepository>
370+
</pluginRepositories>
371+
</project>
372+
""",
373+
"""
374+
<?xml version="1.0" encoding="UTF-8"?>
375+
<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">
376+
<modelVersion>4.0.0</modelVersion>
377+
<parent>
378+
<groupId>org.jenkins-ci.plugins</groupId>
379+
<artifactId>plugin</artifactId>
380+
<version>4.0</version>
381+
<relativePath />
382+
</parent>
383+
<groupId>io.jenkins.plugins</groupId>
384+
<artifactId>empty</artifactId>
385+
<version>1.0.0-SNAPSHOT</version>
386+
<packaging>hpi</packaging>
387+
<name>Empty Plugin</name>
388+
<properties>
389+
<jenkins.version>2.164.3</jenkins.version>
390+
</properties>
391+
<dependencyManagement>
392+
<dependencies>
393+
<dependency>
394+
<groupId>io.jenkins.tools.bom</groupId>
395+
<artifactId>bom-2.164.x</artifactId>
396+
<version>10</version>
397+
<type>pom</type>
398+
<scope>import</scope>
399+
</dependency>
400+
</dependencies>
401+
</dependencyManagement>
402+
<repositories>
403+
<repository>
404+
<id>repo.jenkins-ci.org</id>
405+
<url>https://repo.jenkins-ci.org/public/</url>
406+
</repository>
407+
</repositories>
408+
<pluginRepositories>
409+
<pluginRepository>
410+
<id>repo.jenkins-ci.org</id>
411+
<url>https://repo.jenkins-ci.org/public/</url>
412+
</pluginRepository>
413+
</pluginRepositories>
414+
</project>
415+
"""));
416+
}
417+
323418
@Test
324419
void testRemoveDependenciesOverride() {
325420
rewriteRun(

0 commit comments

Comments
 (0)