From 20b554ea884d3497b760ff598266ee791de170b5 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Wed, 21 Aug 2024 19:20:50 +0200 Subject: [PATCH] Handle nullable distribution correctly in scmCheckout --- .../jenkins/UpgradeJavaVersion.java | 2 +- .../jenkins/UpgradeJavaVersionTest.java | 34 +++++++++++++++++-- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/openrewrite/jenkins/UpgradeJavaVersion.java b/src/main/java/org/openrewrite/jenkins/UpgradeJavaVersion.java index f0f4d5c..01c6156 100644 --- a/src/main/java/org/openrewrite/jenkins/UpgradeJavaVersion.java +++ b/src/main/java/org/openrewrite/jenkins/UpgradeJavaVersion.java @@ -111,7 +111,7 @@ public J.Lambda visitLambda(J.Lambda lambda, ExecutionContext ctx) { return l; } J.Assignment as = GroovyParser.builder().build() - .parse("java_version = '" + distribution + version + "'") + .parse("java_version = '" + (distribution == null ? "" : distribution) + version + "'") .findFirst() .map(G.CompilationUnit.class::cast) .map(cu -> cu.getStatements().get(0)) diff --git a/src/test/java/org/openrewrite/jenkins/UpgradeJavaVersionTest.java b/src/test/java/org/openrewrite/jenkins/UpgradeJavaVersionTest.java index 5669fb7..8b03b76 100644 --- a/src/test/java/org/openrewrite/jenkins/UpgradeJavaVersionTest.java +++ b/src/test/java/org/openrewrite/jenkins/UpgradeJavaVersionTest.java @@ -104,23 +104,53 @@ void scmCheckout () { spec -> spec.recipe(new UpgradeJavaVersion(21, "jdk")), //language=groovy groovy( - """ + """ + node('cicd-build') { + stage("Checkout") { + scmCheckout { + deleteWorkspace = 'false' + maven_version = "maven 3.5" + } + } + } + """, + """ node('cicd-build') { stage("Checkout") { scmCheckout { deleteWorkspace = 'false' maven_version = "maven 3.5" + java_version = 'jdk21' } } } """, + spec -> spec.path("Jenkinsfile")) + ); + } + + @Test + void nullDistributionInScmCheckout() { + rewriteRun( + //language=groovy + groovy( + """ + node('cicd-build') { + stage("Checkout") { + scmCheckout { + deleteWorkspace = 'false' + maven_version = "maven 3.5" + } + } + } + """, """ node('cicd-build') { stage("Checkout") { scmCheckout { deleteWorkspace = 'false' maven_version = "maven 3.5" - java_version = 'jdk21' + java_version = '17' } } }