Skip to content

Commit

Permalink
Add UpgradeJavaVersion recipe
Browse files Browse the repository at this point in the history
  • Loading branch information
sambsnyd committed May 22, 2024
1 parent e6253c1 commit f4c8b44
Show file tree
Hide file tree
Showing 3 changed files with 191 additions and 0 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
95 changes: 95 additions & 0 deletions src/main/java/org/openrewrite/jenkins/UpgradeJavaVersion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* 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
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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<?, ExecutionContext> getVisitor() {
return Preconditions.check(new FindSourceFiles("**/Jenkinsfile"), new GroovyIsoVisitor<ExecutionContext>() {
@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;
}
});
}
}
95 changes: 95 additions & 0 deletions src/test/java/org/openrewrite/jenkins/UpgradeJavaVersionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* 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
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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"))
);
}
}

0 comments on commit f4c8b44

Please sign in to comment.