From f4c8b44a60b1584e378fd992cc7454861d245794 Mon Sep 17 00:00:00 2001 From: Sam Snyder Date: Tue, 21 May 2024 17:47:51 -0700 Subject: [PATCH] Add UpgradeJavaVersion recipe --- build.gradle.kts | 1 + .../jenkins/UpgradeJavaVersion.java | 95 +++++++++++++++++++ .../jenkins/UpgradeJavaVersionTest.java | 95 +++++++++++++++++++ 3 files changed, 191 insertions(+) create mode 100644 src/main/java/org/openrewrite/jenkins/UpgradeJavaVersion.java create mode 100644 src/test/java/org/openrewrite/jenkins/UpgradeJavaVersionTest.java diff --git a/build.gradle.kts b/build.gradle.kts index a9d916d..028bf99 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -13,6 +13,7 @@ dependencies { implementation(platform("org.openrewrite:rewrite-bom:$rewriteVersion")) + implementation("org.openrewrite:rewrite-groovy") implementation("org.openrewrite:rewrite-java") implementation("org.openrewrite:rewrite-maven") implementation("org.openrewrite:rewrite-yaml") diff --git a/src/main/java/org/openrewrite/jenkins/UpgradeJavaVersion.java b/src/main/java/org/openrewrite/jenkins/UpgradeJavaVersion.java new file mode 100644 index 0000000..9c71bd5 --- /dev/null +++ b/src/main/java/org/openrewrite/jenkins/UpgradeJavaVersion.java @@ -0,0 +1,95 @@ +/* + * Copyright 2024 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openrewrite.jenkins; + +import lombok.EqualsAndHashCode; +import lombok.Value; +import org.openrewrite.*; +import org.openrewrite.groovy.GroovyIsoVisitor; +import org.openrewrite.internal.StringUtils; +import org.openrewrite.internal.lang.Nullable; +import org.openrewrite.java.tree.J; +import org.openrewrite.marker.Markup; + +@Value +@EqualsAndHashCode(callSuper = false) +public class UpgradeJavaVersion extends Recipe { + + @Option(displayName = "Java version", + description = "The Java version to upgrade to.", + example = "17") + int version; + + @Option(displayName = "Distribution", + description = "The distribution of Java to use. When omitted the current distribution is maintained.", + example = "openjdk") + @Nullable + String distribution; + + @Override + public String getDisplayName() { + return "Upgrade Jenkins Java version"; + } + + @Override + public String getDescription() { + return "Upgrades the version of java specified in Jenkins groovy scripts. " + + "Will not downgrade if the version is newer than the specified version."; + } + + @Override + public TreeVisitor getVisitor() { + return Preconditions.check(new FindSourceFiles("**/Jenkinsfile"), new GroovyIsoVisitor() { + @Override + public J.Assignment visitAssignment(J.Assignment assignment, ExecutionContext executionContext) { + J.Assignment a = super.visitAssignment(assignment, executionContext); + if(!(a.getVariable() instanceof J.Identifier) || !(a.getAssignment() instanceof J.Literal)) { + return a; + } + J.Identifier id = (J.Identifier) a.getVariable(); + J.Literal value = (J.Literal) a.getAssignment(); + if( !("java_version".equals(id.getSimpleName()) || "javaVersion".equals(id.getSimpleName())) || !(value.getValue() instanceof String)) { + return a; + } + String currentJdkString = ((String) value.getValue()).trim(); + int versionBeginsIndex = StringUtils.indexOf(currentJdkString, Character::isDigit); + String currentJdkDistribution = currentJdkString.substring(0, versionBeginsIndex); + String currentJdkVersion = currentJdkString.substring(versionBeginsIndex); + int jdkVersion; + try { + jdkVersion = Integer.parseInt(currentJdkVersion); + } catch (NumberFormatException e) { + return Markup.warn(a, new IllegalStateException("Unable to parse JDK version", e)); + } + String targetVersion = currentJdkVersion; + if(jdkVersion < version) { + targetVersion = String.valueOf(version); + } + String targetDistribution = currentJdkDistribution; + if(distribution != null) { + targetDistribution = distribution; + } + String targetJdkString = targetDistribution + targetVersion; + if(!targetJdkString.equals(currentJdkString)) { + char quote = value.getValueSource() == null ? '\'' : value.getValueSource().charAt(0); + a = a.withAssignment(value.withValue(targetJdkString) + .withValueSource(quote + targetJdkString + quote)); + } + return a; + } + }); + } +} diff --git a/src/test/java/org/openrewrite/jenkins/UpgradeJavaVersionTest.java b/src/test/java/org/openrewrite/jenkins/UpgradeJavaVersionTest.java new file mode 100644 index 0000000..45dcc13 --- /dev/null +++ b/src/test/java/org/openrewrite/jenkins/UpgradeJavaVersionTest.java @@ -0,0 +1,95 @@ +/* + * Copyright 2024 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openrewrite.jenkins; + +import org.junit.jupiter.api.Test; +import org.openrewrite.test.RecipeSpec; +import org.openrewrite.test.RewriteTest; + +import static org.openrewrite.groovy.Assertions.groovy; + +public class UpgradeJavaVersionTest implements RewriteTest { + + @Override + public void defaults(RecipeSpec spec) { + spec.recipe(new UpgradeJavaVersion(17, null)); + } + + @Test + void openJdk() { + rewriteRun( + //language=groovy + groovy(""" + #!/usr/bin/env groovy + + stage("Checkout") { + scmCheckout { + java_version = "openjdk11" + deleteWorkspace = 'false' + } + } + """, + """ + #!/usr/bin/env groovy + + stage("Checkout") { + scmCheckout { + java_version = "openjdk17" + deleteWorkspace = 'false' + } + } + """, + spec -> spec.path("Jenkinsfile")) + ); + } + + @Test + void jdk() { + rewriteRun( + spec -> spec.recipe(new UpgradeJavaVersion(17, "openjdk")), + //language=groovy + groovy(""" + node('cicd-build') { + stage ("Titan") { + titan { + gitSource = "github" + projectType = "java" + mavenVersion = '3.5' + javaVersion = 'jdk11' + minimumCodeCoverage = 86 + codeCoverageFilePath = "/target/site/jacoco/index.html" + } + } + } + """, + """ + node('cicd-build') { + stage ("Titan") { + titan { + gitSource = "github" + projectType = "java" + mavenVersion = '3.5' + javaVersion = 'openjdk17' + minimumCodeCoverage = 86 + codeCoverageFilePath = "/target/site/jacoco/index.html" + } + } + } + """, + spec -> spec.path("Jenkinsfile")) + ); + } +}