Skip to content

Commit

Permalink
Do not upgrade jenkins.version if not supported by Jenkinsfile
Browse files Browse the repository at this point in the history
  • Loading branch information
jonesbusy committed Jan 13, 2025
1 parent 5afca12 commit 8a4c213
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
package io.jenkins.tools.pluginmodernizer.core.recipes;

import io.jenkins.tools.pluginmodernizer.core.extractor.ArchetypeCommonFile;
import io.jenkins.tools.pluginmodernizer.core.extractor.JenkinsfileVisitor;
import io.jenkins.tools.pluginmodernizer.core.extractor.PluginMetadata;
import io.jenkins.tools.pluginmodernizer.core.model.JDK;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Option;
import org.openrewrite.Recipe;
import org.openrewrite.ScanningRecipe;
import org.openrewrite.SourceFile;
import org.openrewrite.Tree;
import org.openrewrite.TreeVisitor;
import org.openrewrite.jenkins.UpgradeVersionProperty;
import org.openrewrite.maven.MavenIsoVisitor;
Expand All @@ -15,7 +24,7 @@
* 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 {
public class UpgradeJenkinsVersion extends ScanningRecipe<UpgradeJenkinsVersion.PlatformConfigAccumulator> {

/**
* LOGGER.
Expand Down Expand Up @@ -47,12 +56,22 @@ public String getDescription() {
}

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

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

Set<JDK> jenkinsfileJdks = acc.getJdks();
List<JDK> jenkinsJdks = JDK.get(minimumVersion);

// All jenkinsfile JDK must be supported by the new Jenkins version
if (jenkinsfileJdks.stream().anyMatch(jdk -> !jenkinsJdks.contains(jdk))) {
LOG.warn(
"Jenkinsfile JDKs are not supported by the new Jenkins version. Will not change the version.");
return document;
}

// Return another tree with jenkins version updated
document = (Xml.Document) new UpgradeVersionProperty("jenkins.version", minimumVersion)
.getVisitor()
Expand All @@ -61,4 +80,38 @@ public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) {
}
};
}

@Override
public PlatformConfigAccumulator getInitialValue(ExecutionContext ctx) {
return new PlatformConfigAccumulator();
}

@Override
public TreeVisitor<?, ExecutionContext> getScanner(PlatformConfigAccumulator acc) {
return new TreeVisitor<>() {
@Override
public Tree visit(Tree tree, ExecutionContext ctx) {
tree = super.visit(tree, ctx);
if (tree instanceof SourceFile
&& ArchetypeCommonFile.JENKINSFILE.same(((SourceFile) tree).getSourcePath())) {
acc.setPlatform(new JenkinsfileVisitor()
.reduce(tree, new PluginMetadata())
.getJdks());
}
return tree;
}
};
}

public static class PlatformConfigAccumulator {
private Set<JDK> jdks = new HashSet<>();

public void setPlatform(Set<JDK> jdks) {
this.jdks = jdks;
}

public Set<JDK> getJdks() {
return jdks;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1152,6 +1152,19 @@ void upgradeNextMajorParentVersionTest() {
"io.jenkins.tools.pluginmodernizer.UpgradeNextMajorParentVersion")
.parser(parser);
},
groovy(
"""
buildPlugin(
forkCount: '1C', // Run a JVM per core in tests
useContainerAgent: true, // Set to `false` if you need to use Docker for containerized tests
configurations: [
[platform: 'linux', jdk: 21],
[platform: 'windows', jdk: 17],
])
""",
sourceSpecs -> {
sourceSpecs.path(ArchetypeCommonFile.JENKINSFILE.getPath());
}),
mavenProject("test"),
// language=xml
srcMainResources(text(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package io.jenkins.tools.pluginmodernizer.core.recipes;

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

import io.jenkins.tools.pluginmodernizer.core.config.Settings;
import io.jenkins.tools.pluginmodernizer.core.extractor.ArchetypeCommonFile;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;
Expand Down Expand Up @@ -93,6 +95,59 @@ void testPerformUpgradeWithoutBom() {
"""));
}

@Test
void testNoUpgradeIfJenkinsFileDoesNotSupportJava17() {
rewriteRun(
spec -> spec.recipe(new UpgradeJenkinsVersion("2.452.4")),
groovy(
"""
buildPlugin(
forkCount: '1C', // Run a JVM per core in tests
useContainerAgent: true, // Set to `false` if you need to use Docker for containerized tests
configurations: [
[platform: 'linux', jdk: 8],
[platform: 'windows', jdk: 11],
])
""",
sourceSpecs -> {
sourceSpecs.path(ArchetypeCommonFile.JENKINSFILE.getPath());
}),
// 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.87</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>
<repositories>
<repository>
<id>repo.jenkins-ci.org</id>
<url>https://repo.jenkins-ci.org/public/</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>repo.jenkins-ci.org</id>
<url>https://repo.jenkins-ci.org/public/</url>
</pluginRepository>
</pluginRepositories>
</project>
"""));
}

@Test
void testPerformUpgradeWithBaselineWithoutBom() {
rewriteRun(
Expand Down

0 comments on commit 8a4c213

Please sign in to comment.