Skip to content

Commit

Permalink
Handle jenkins.baseline on AddPluginsPom
Browse files Browse the repository at this point in the history
  • Loading branch information
jonesbusy authored and Valentin Delaye committed Nov 11, 2024
1 parent d099a27 commit 9d915d6
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 5 deletions.
3 changes: 3 additions & 0 deletions src/main/java/org/openrewrite/jenkins/AddPluginsBom.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
if (isManagedDependencyTag()) {
String groupId = tag.getChildValue("groupId").orElse("");
String artifactId = tag.getChildValue("artifactId").orElse("");
if (artifactId.equals("bom-${jenkins.baseline}.x")) {
artifactId = "bom-" + getResolutionResult().getPom().getProperties().get("jenkins.baseline") + ".x";
}
if (PLUGINS_BOM_GROUP_ID.equals(groupId) && !artifactId.isEmpty()) {
List<Xml.Tag> pluginBoms = getCursor().getNearestMessage(PLUGIN_BOMS_KEY, new LinkedList<>());
pluginBoms.add(t);
Expand Down
16 changes: 11 additions & 5 deletions src/main/java/org/openrewrite/jenkins/Jenkins.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
* Utility class
*/
class Jenkins {
private static final Predicate<String> LTS_PATTERN = Pattern.compile("^\\d\\.\\d+\\.\\d$").asPredicate();
private static final Predicate<String> LTS_PATTERN = Pattern.compile("^\\d\\.(\\d+)\\.\\d$").asPredicate();
private static final Predicate<String> LTS_BASELINE_PATTERN = Pattern.compile("^\\$\\{jenkins.baseline\\}.\\d$").asPredicate();

/**
* Determines if this is a Jenkins Plugin Pom by checking for a managed version
Expand All @@ -46,10 +47,15 @@ class Jenkins {

@NonNull
public static String bomNameForJenkinsVersion(@NonNull String version) {
if (LTS_PATTERN.test(version)) {
int lastIndex = version.lastIndexOf(".");
String prefix = version.substring(0, lastIndex);
return "bom-" + prefix + ".x";
if (LTS_PATTERN.test(version) || LTS_BASELINE_PATTERN.test(version)) {
if (version.startsWith("${jenkins.baseline}")) {
return "bom-${jenkins.baseline}.x";
}
else {
int lastIndex = version.lastIndexOf(".");
String prefix = version.substring(0, lastIndex);
return "bom-" + prefix + ".x";
}
}
return "bom-weekly";
}
Expand Down
87 changes: 87 additions & 0 deletions src/test/java/org/openrewrite/jenkins/AddPluginsBomTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,47 @@ void shouldAddBomIfManagedDependencies() {
));
}

@Test
void shouldAddBomWithBaselineIfManagedDependencies() {
// language=xml
rewriteRun(pomXml(
"""
<project>
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>4.86</version>
<relativePath/>
</parent>
<artifactId>foo</artifactId>
<properties>
<jenkins.baseline>2.440</jenkins.baseline>
<jenkins.version>${jenkins.baseline}.3</jenkins.version>
</properties>
<repositories>
<repository>
<id>repo.jenkins-ci.org</id>
<url>https://repo.jenkins-ci.org/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>ant</artifactId>
<version>1.9</version>
</dependency>
</dependencies>
</project>
""",
spec -> spec.after(after -> {
ModernizePluginTest.Versions versionsAfter = ModernizePluginTest.Versions.parse(after);
assertThat(versionsAfter.bomArtifactId()).isEqualTo("bom-${jenkins.baseline}.x");
assertThat(versionsAfter.bomVersion()).isNotEmpty();
return after;
})
));
}

@Test
void shouldLeaveBomVersionIfAlreadyPresent() {
// language=xml
Expand Down Expand Up @@ -176,6 +217,52 @@ void shouldLeaveBomVersionIfAlreadyPresent() {
));
}

@Test
void shouldLeaveBomWithBaselineVersionIfAlreadyPresent() {
// language=xml
rewriteRun(pomXml(
"""
<project>
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>5.2</version>
<relativePath/>
</parent>
<artifactId>foo</artifactId>
<properties>
<jenkins.baseline>2.479</jenkins.baseline>
<jenkins.version>${jenkins.baseline}.1</jenkins.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.jenkins.tools.bom</groupId>
<artifactId>bom-${jenkins.baseline}.x</artifactId>
<version>3613.v584fca_12cf5c</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>
<dependencies>
<dependency>
<groupId>io.jenkins.plugins</groupId>
<artifactId>commons-text-api</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
"""
));
}

@Test
void shouldFixOutdatedPluginsBom() {
String bomArtifactId = "bom-2.346.x";
Expand Down
9 changes: 9 additions & 0 deletions src/test/java/org/openrewrite/jenkins/JenkinsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.openrewrite.jenkins;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
Expand All @@ -38,6 +39,14 @@ void shouldGenerateBomNameFromJenkinsVersion(String jenkinsVersion, String bomVe
assertThat(actual).isEqualTo(bomVersion);
}

@Test
void shouldGenerateBomNameWithBaseline() {
String actual = Jenkins.bomNameForJenkinsVersion("${jenkins.baseline}.3");
assertThat(actual).isEqualTo("bom-${jenkins.baseline}.x");
actual = Jenkins.bomNameForJenkinsVersion("${jenkins.baseline}.1");
assertThat(actual).isEqualTo("bom-${jenkins.baseline}.x");
}

static Stream<Arguments> versionToBom() {
return Stream.of(
arguments("2.277.3", "bom-2.277.x"),
Expand Down

0 comments on commit 9d915d6

Please sign in to comment.