diff --git a/src/main/java/org/openrewrite/jenkins/AddPluginsBom.java b/src/main/java/org/openrewrite/jenkins/AddPluginsBom.java index cb79fe1..886b879 100644 --- a/src/main/java/org/openrewrite/jenkins/AddPluginsBom.java +++ b/src/main/java/org/openrewrite/jenkins/AddPluginsBom.java @@ -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 pluginBoms = getCursor().getNearestMessage(PLUGIN_BOMS_KEY, new LinkedList<>()); pluginBoms.add(t); diff --git a/src/main/java/org/openrewrite/jenkins/Jenkins.java b/src/main/java/org/openrewrite/jenkins/Jenkins.java index 3756b21..def4d90 100644 --- a/src/main/java/org/openrewrite/jenkins/Jenkins.java +++ b/src/main/java/org/openrewrite/jenkins/Jenkins.java @@ -27,7 +27,8 @@ * Utility class */ class Jenkins { - private static final Predicate LTS_PATTERN = Pattern.compile("^\\d\\.\\d+\\.\\d$").asPredicate(); + private static final Predicate LTS_PATTERN = Pattern.compile("^\\d\\.(\\d+)\\.\\d$").asPredicate(); + private static final Predicate LTS_BASELINE_PATTERN = Pattern.compile("^\\$\\{jenkins.baseline\\}.\\d$").asPredicate(); /** * Determines if this is a Jenkins Plugin Pom by checking for a managed version @@ -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"; } diff --git a/src/test/java/org/openrewrite/jenkins/AddPluginsBomTest.java b/src/test/java/org/openrewrite/jenkins/AddPluginsBomTest.java index 8b3f9d4..6e435e0 100644 --- a/src/test/java/org/openrewrite/jenkins/AddPluginsBomTest.java +++ b/src/test/java/org/openrewrite/jenkins/AddPluginsBomTest.java @@ -27,153 +27,253 @@ class AddPluginsBomTest implements RewriteTest { @Override public void defaults(RecipeSpec spec) { - spec.recipe(new AddPluginsBom()); + spec.recipe(new AddPluginsBom() + ); } @Test void shouldNotAddBomIfNoDependencies() { // language=xml - rewriteRun(pomXml( - """ - - - org.jenkins-ci.plugins - plugin - 4.86 - - - foo - - 2.440.3 - - - - repo.jenkins-ci.org - https://repo.jenkins-ci.org/public/ - - - + rewriteRun( + pomXml( """ - )); + + + org.jenkins-ci.plugins + plugin + 4.86 + + + foo + + 2.440.3 + + + + repo.jenkins-ci.org + https://repo.jenkins-ci.org/public/ + + + + """ + ) + ); } @Test void shouldNotAddBomIfNoManagedDependencies() { // language=xml - rewriteRun(pomXml( - """ - - - org.jenkins-ci.plugins - plugin - 4.86 - - - foo - - 2.440.3 - - - - repo.jenkins-ci.org - https://repo.jenkins-ci.org/public/ - - - - - com.lmax - disruptor - 3.4.4 - - - + rewriteRun( + pomXml( """ - )); + + + org.jenkins-ci.plugins + plugin + 4.86 + + + foo + + 2.440.3 + + + + repo.jenkins-ci.org + https://repo.jenkins-ci.org/public/ + + + + + com.lmax + disruptor + 3.4.4 + + + + """ + ) + ); } @Test @DocumentExample void shouldAddBomIfManagedDependencies() { // language=xml - rewriteRun(pomXml( - """ - - - org.jenkins-ci.plugins - plugin - 4.86 - - - foo - - 2.440.3 - - - - repo.jenkins-ci.org - https://repo.jenkins-ci.org/public/ - - - - - org.jenkins-ci.plugins - ant - 1.9 - - - - """, - spec -> spec.after(after -> { - ModernizePluginTest.Versions versionsAfter = ModernizePluginTest.Versions.parse(after); - assertThat(versionsAfter.bomArtifactId()).isNotEmpty(); - assertThat(versionsAfter.bomVersion()).isNotEmpty(); - return after; - }) - )); + rewriteRun( + pomXml( + """ + + + org.jenkins-ci.plugins + plugin + 4.86 + + + foo + + 2.440.3 + + + + repo.jenkins-ci.org + https://repo.jenkins-ci.org/public/ + + + + + org.jenkins-ci.plugins + ant + 1.9 + + + + """, + spec -> spec.after(after -> { + ModernizePluginTest.Versions versionsAfter = ModernizePluginTest.Versions.parse(after); + assertThat(versionsAfter.bomArtifactId()).isNotEmpty(); + assertThat(versionsAfter.bomVersion()).isNotEmpty(); + return after; + }) + ) + ); + } + + @Test + void shouldAddBomWithBaselineIfManagedDependencies() { + // language=xml + rewriteRun( + pomXml( + """ + + + org.jenkins-ci.plugins + plugin + 4.86 + + + foo + + 2.440 + ${jenkins.baseline}.3 + + + + repo.jenkins-ci.org + https://repo.jenkins-ci.org/public/ + + + + + org.jenkins-ci.plugins + ant + 1.9 + + + + """, + 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 - rewriteRun(pomXml( - """ - - - org.jenkins-ci.plugins - plugin - 4.86 - - - foo - - 2.440.3 - - - - - io.jenkins.tools.bom - bom-2.440.x - 3221.ve8f7b_fdd149d - pom - import - - - - - - repo.jenkins-ci.org - https://repo.jenkins-ci.org/public/ - - - - - org.jenkins-ci.plugins - ant - - - + rewriteRun( + pomXml( + """ + + + org.jenkins-ci.plugins + plugin + 4.86 + + + foo + + 2.440.3 + + + + + io.jenkins.tools.bom + bom-2.440.x + 3221.ve8f7b_fdd149d + pom + import + + + + + + repo.jenkins-ci.org + https://repo.jenkins-ci.org/public/ + + + + + org.jenkins-ci.plugins + ant + + + + """ + ) + ); + } + + @Test + void shouldLeaveBomWithBaselineVersionIfAlreadyPresent() { + // language=xml + rewriteRun( + pomXml( """ - )); + + + org.jenkins-ci.plugins + plugin + 5.2 + + + foo + + 2.479 + ${jenkins.baseline}.1 + + + + + io.jenkins.tools.bom + bom-${jenkins.baseline}.x + 3613.v584fca_12cf5c + pom + import + + + + + + repo.jenkins-ci.org + https://repo.jenkins-ci.org/public/ + + + + + io.jenkins.plugins + commons-text-api + test + + + + """ + ) + ); } @Test @@ -181,58 +281,60 @@ void shouldFixOutdatedPluginsBom() { String bomArtifactId = "bom-2.346.x"; String bomVersion = "1706.vc166d5f429f8"; // language=xml - rewriteRun(pomXml( - """ - - - org.jenkins-ci.plugins - plugin - 4.86 - - - foo - - 2.440.3 - - - - - io.jenkins.tools.bom - %s - %s - pom - import - - - io.jenkins.tools.bom - bom-2.319.x - 1135.va_4eeca_ea_21c1 - pom - import - - - - - - repo.jenkins-ci.org - https://repo.jenkins-ci.org/public/ - - - - - org.jenkins-ci.plugins - ant - - - - """.formatted(bomArtifactId, bomVersion), - spec -> spec.after(after -> { - ModernizePluginTest.Versions versionsAfter = ModernizePluginTest.Versions.parse(after); - assertThat(versionsAfter.bomArtifactId()).isGreaterThan(bomArtifactId); - assertThat(versionsAfter.bomVersion()).isGreaterThan(bomVersion); - return after; - }) - )); + rewriteRun( + pomXml( + """ + + + org.jenkins-ci.plugins + plugin + 4.86 + + + foo + + 2.440.3 + + + + + io.jenkins.tools.bom + %s + %s + pom + import + + + io.jenkins.tools.bom + bom-2.319.x + 1135.va_4eeca_ea_21c1 + pom + import + + + + + + repo.jenkins-ci.org + https://repo.jenkins-ci.org/public/ + + + + + org.jenkins-ci.plugins + ant + + + + """.formatted(bomArtifactId, bomVersion), + spec -> spec.after(after -> { + ModernizePluginTest.Versions versionsAfter = ModernizePluginTest.Versions.parse(after); + assertThat(versionsAfter.bomArtifactId()).isGreaterThan(bomArtifactId); + assertThat(versionsAfter.bomVersion()).isGreaterThan(bomVersion); + return after; + }) + ) + ); } @Test @@ -244,14 +346,19 @@ void shouldFixOutdatedPluginsBomPropertiesBelowManagedDependencies() { "1706.vc166d5f429f8" ); // language=xml - rewriteRun(pomXml( + rewriteRun( + pomXml( versionsBefore.asPomXml(), spec -> spec.after(after -> { ModernizePluginTest.Versions versionsAfter = ModernizePluginTest.Versions.parse(after); - assertThat(versionsAfter.parentVersion()).isGreaterThanOrEqualTo(versionsBefore.parentVersion()); - assertThat(versionsAfter.propertyVersion()).isGreaterThanOrEqualTo(versionsBefore.propertyVersion()); - assertThat(versionsAfter.bomArtifactId()).isGreaterThan(versionsBefore.bomArtifactId()); - assertThat(versionsAfter.bomVersion()).isGreaterThan(versionsBefore.bomVersion()); + assertThat(versionsAfter.parentVersion()).isGreaterThanOrEqualTo(versionsBefore.parentVersion() + ); + assertThat(versionsAfter.propertyVersion()).isGreaterThanOrEqualTo(versionsBefore.propertyVersion() + ); + assertThat(versionsAfter.bomArtifactId()).isGreaterThan(versionsBefore.bomArtifactId() + ); + assertThat(versionsAfter.bomVersion()).isGreaterThan(versionsBefore.bomVersion() + ); return versionsAfter.asPomXml(); }) ) @@ -263,215 +370,220 @@ void shouldFixOutdatedPluginsBomEvenIfUnused() { String bomArtifactId = "bom-2.346.x"; String bomVersion = "1706.vc166d5f429f8"; // language=xml - rewriteRun(pomXml( - """ - - - org.jenkins-ci.plugins - plugin - 4.86 - - - foo - - 2.440.3 - - - - - io.jenkins.tools.bom - %s - %s - pom - import - - - - - - repo.jenkins-ci.org - https://repo.jenkins-ci.org/public/ - - - - - com.lmax - disruptor - 3.4.4 - - - - """.formatted(bomArtifactId, bomVersion), - spec -> spec.after(after -> { - ModernizePluginTest.Versions versionsAfter = ModernizePluginTest.Versions.parse(after); - assertThat(versionsAfter.bomArtifactId()).isGreaterThan(bomArtifactId); - assertThat(versionsAfter.bomVersion()).isGreaterThan(bomVersion); - return after; - }) - )); + rewriteRun( + pomXml( + """ + + + org.jenkins-ci.plugins + plugin + 4.86 + + + foo + + 2.440.3 + + + + + io.jenkins.tools.bom + %s + %s + pom + import + + + + + + repo.jenkins-ci.org + https://repo.jenkins-ci.org/public/ + + + + + com.lmax + disruptor + 3.4.4 + + + + """.formatted(bomArtifactId, bomVersion), + spec -> spec.after(after -> { + ModernizePluginTest.Versions versionsAfter = ModernizePluginTest.Versions.parse(after); + assertThat(versionsAfter.bomArtifactId()).isGreaterThan(bomArtifactId); + assertThat(versionsAfter.bomVersion()).isGreaterThan(bomVersion); + return after; + }) + ) + ); } @Test void shouldLeaveOtherBomsAlone() { // language=xml - rewriteRun(pomXml( - """ - - - org.jenkins-ci.plugins - plugin - 4.86 - - - foo - - 2.440.3 - - - - - org.testcontainers - testcontainers-bom - 1.18.3 - pom - import - - - - - - repo.jenkins-ci.org - https://repo.jenkins-ci.org/public/ - - - - - org.testcontainers - testcontainers - test - - - + rewriteRun( + pomXml( """ - )); + + + org.jenkins-ci.plugins + plugin + 4.86 + + + foo + + 2.440.3 + + + + + org.testcontainers + testcontainers-bom + 1.18.3 + pom + import + + + + + + repo.jenkins-ci.org + https://repo.jenkins-ci.org/public/ + + + + + org.testcontainers + testcontainers + test + + + + """ + ) + ); } @Test void shouldHandleCommentsInManagedDependency() { // language=xml - rewriteRun(pomXml( - """ - - - org.jenkins-ci.plugins - plugin - 4.86 - - - - golang - 1.5-SNAPSHOT - hpi + rewriteRun( + pomXml( + """ + + + org.jenkins-ci.plugins + plugin + 4.86 + + - - 2.346.3 - + golang + 1.5-SNAPSHOT + hpi - - - - org.jenkins-ci.plugins.workflow - workflow-api - 2.6 - test - - + + 2.346.3 + - - + - io.jenkins.tools.bom - bom-2.190.x - 16 - pom - import + org.jenkins-ci.plugins.workflow + workflow-api + 2.6 + test - - - - - repo.jenkins-ci.org - https://repo.jenkins-ci.org/public/ - - + + + + + io.jenkins.tools.bom + bom-2.190.x + 16 + pom + import + + + - - - repo.jenkins-ci.org - https://repo.jenkins-ci.org/public/ - - - - """, - """ - - - org.jenkins-ci.plugins - plugin - 4.86 - - + + + + repo.jenkins-ci.org + https://repo.jenkins-ci.org/public/ + + - golang - 1.5-SNAPSHOT - hpi + + + repo.jenkins-ci.org + https://repo.jenkins-ci.org/public/ + + + + """, + """ + + + org.jenkins-ci.plugins + plugin + 4.86 + + - - 2.346.3 - + golang + 1.5-SNAPSHOT + hpi - - - - org.jenkins-ci.plugins.workflow - workflow-api - test - - + + 2.346.3 + - - + - io.jenkins.tools.bom - bom-2.346.x - 1763.v092b_8980a_f5e - pom - import + org.jenkins-ci.plugins.workflow + workflow-api + test - - - - - repo.jenkins-ci.org - https://repo.jenkins-ci.org/public/ - - + + + + + io.jenkins.tools.bom + bom-2.346.x + 1763.v092b_8980a_f5e + pom + import + + + - - - repo.jenkins-ci.org - https://repo.jenkins-ci.org/public/ - - - - """ + + + + repo.jenkins-ci.org + https://repo.jenkins-ci.org/public/ + + - )); + + + repo.jenkins-ci.org + https://repo.jenkins-ci.org/public/ + + + + """ + ) + ); } } diff --git a/src/test/java/org/openrewrite/jenkins/JenkinsTest.java b/src/test/java/org/openrewrite/jenkins/JenkinsTest.java index 61f247b..1a4ae21 100644 --- a/src/test/java/org/openrewrite/jenkins/JenkinsTest.java +++ b/src/test/java/org/openrewrite/jenkins/JenkinsTest.java @@ -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; @@ -33,9 +34,13 @@ class JenkinsTest { @ParameterizedTest @MethodSource("versionToBom") void shouldGenerateBomNameFromJenkinsVersion(String jenkinsVersion, String bomVersion) { - String actual = Jenkins.bomNameForJenkinsVersion(jenkinsVersion); + assertThat(Jenkins.bomNameForJenkinsVersion(jenkinsVersion)).isEqualTo(bomVersion); + } - assertThat(actual).isEqualTo(bomVersion); + @Test + void shouldGenerateBomNameWithBaseline() { + assertThat(Jenkins.bomNameForJenkinsVersion("${jenkins.baseline}.3")).isEqualTo("bom-${jenkins.baseline}.x"); + assertThat(Jenkins.bomNameForJenkinsVersion("${jenkins.baseline}.1")).isEqualTo("bom-${jenkins.baseline}.x"); } static Stream versionToBom() {