Skip to content

Commit

Permalink
Set java_version on scmCheckout even if the assignment is not already…
Browse files Browse the repository at this point in the history
… present.
  • Loading branch information
sambsnyd committed Aug 8, 2024
1 parent 476b1e7 commit 59b4168
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 4 deletions.
35 changes: 35 additions & 0 deletions src/main/java/org/openrewrite/jenkins/UpgradeJavaVersion.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
import lombok.Value;
import org.openrewrite.*;
import org.openrewrite.groovy.GroovyIsoVisitor;
import org.openrewrite.groovy.GroovyParser;
import org.openrewrite.groovy.tree.G;
import org.openrewrite.internal.ListUtils;
import org.openrewrite.internal.StringUtils;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.java.tree.J;
Expand Down Expand Up @@ -83,13 +86,45 @@ public J.Assignment visitAssignment(J.Assignment assignment, ExecutionContext ct
targetDistribution = distribution;
}
String targetJdkString = targetDistribution + targetVersion;
getCursor().putMessageOnFirstEnclosing(J.Lambda.class, "TARGET_JDK_ALERADY_CONFIGURED", true);
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;
}


@Override
public J.Lambda visitLambda(J.Lambda lambda, ExecutionContext ctx) {
J.Lambda l = super.visitLambda(lambda, ctx);
if (!(getCursor().getParentTreeCursor().getValue() instanceof J.MethodInvocation)) {
return l;
}
J.MethodInvocation m = getCursor().getParentTreeCursor().getValue();
if (!m.getSimpleName().equals("scmCheckout")) {
return l;
}

if (!(l.getBody() instanceof J.Block) || getCursor().pollMessage("TARGET_JDK_ALERADY_CONFIGURED") != null) {
return l;
}
J.Assignment as = GroovyParser.builder().build()
.parse("java_version = '" + distribution + version + "'")
.findFirst()
.map(G.CompilationUnit.class::cast)
.map(cu -> cu.getStatements().get(0))
.map(J.Assignment.class::cast)
.orElse(null);
if (as == null) {
return l;
}
J.Block body = (J.Block) l.getBody();
as = autoFormat(as, ctx, new Cursor(getCursor(), body));
l = l.withBody(body.withStatements(ListUtils.concat(body.getStatements(), as)));
return l;
}
});
}
}
37 changes: 33 additions & 4 deletions src/test/java/org/openrewrite/jenkins/UpgradeJavaVersionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

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

@SuppressWarnings("GroovyAssignabilityCheck")
class UpgradeJavaVersionTest implements RewriteTest {

@Override
Expand All @@ -34,8 +35,7 @@ public void defaults(RecipeSpec spec) {
void openJdk() {
rewriteRun(
//language=groovy
groovy(
"""
groovy("""
#!/usr/bin/env groovy
stage("Checkout") {
Expand Down Expand Up @@ -64,8 +64,7 @@ void jdk() {
rewriteRun(
spec -> spec.recipe(new UpgradeJavaVersion(17, "openjdk")),
//language=groovy
groovy(
"""
groovy("""
node('cicd-build') {
stage ("Titan") {
titan {
Expand Down Expand Up @@ -96,4 +95,34 @@ void jdk() {
spec -> spec.path("Jenkinsfile"))
);
}

@Test
void scmCheckout () {
rewriteRun(
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"))
);
}
}

0 comments on commit 59b4168

Please sign in to comment.