From 2eae45c646195b858c1d3fb4c6c6b722c4f2e8ae Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Mon, 15 Jul 2024 11:27:01 +0200 Subject: [PATCH 1/2] Strip multi-stage names from tags https://docs.docker.com/build/building/multi-stage/ --- build.gradle.kts | 2 ++ .../openrewrite/docker/trait/Dockerfile.java | 2 +- .../docker/FindDockerImagesUsedTest.java | 26 +++++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/build.gradle.kts b/build.gradle.kts index d82d0c7..f7bb43b 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -9,4 +9,6 @@ val rewriteVersion = rewriteRecipe.rewriteVersion.get() dependencies { implementation(platform("org.openrewrite:rewrite-bom:$rewriteVersion")) implementation("org.openrewrite:rewrite-core:$rewriteVersion") + + testImplementation("org.assertj:assertj-core:latest.release") } diff --git a/src/main/java/org/openrewrite/docker/trait/Dockerfile.java b/src/main/java/org/openrewrite/docker/trait/Dockerfile.java index a116441..712542d 100644 --- a/src/main/java/org/openrewrite/docker/trait/Dockerfile.java +++ b/src/main/java/org/openrewrite/docker/trait/Dockerfile.java @@ -37,7 +37,7 @@ public List getFroms() { String[] imageVersionStr = line.substring("FROM".length()).trim().split(":"); froms.add(new DockerImageVersion( imageVersionStr[0], - imageVersionStr.length > 1 ? imageVersionStr[1] : null + imageVersionStr.length > 1 ? imageVersionStr[1].split(" ")[0] : null )); } } diff --git a/src/test/java/org/openrewrite/docker/FindDockerImagesUsedTest.java b/src/test/java/org/openrewrite/docker/FindDockerImagesUsedTest.java index e26c0c0..47cfbb2 100644 --- a/src/test/java/org/openrewrite/docker/FindDockerImagesUsedTest.java +++ b/src/test/java/org/openrewrite/docker/FindDockerImagesUsedTest.java @@ -18,9 +18,11 @@ import org.junit.jupiter.api.Test; import org.openrewrite.DocumentExample; import org.openrewrite.docker.search.FindDockerImageUses; +import org.openrewrite.docker.table.DockerBaseImages; import org.openrewrite.test.RecipeSpec; import org.openrewrite.test.RewriteTest; +import static org.assertj.core.api.Assertions.assertThat; import static org.openrewrite.test.SourceSpecs.text; class FindDockerImagesUsedTest implements RewriteTest { @@ -52,4 +54,28 @@ void dockerfile() { ) ); } + + @Test + void multistageDockerfile() { + rewriteRun( + spec -> spec.dataTable(DockerBaseImages.Row.class, rows -> assertThat(rows) + .containsOnly(new DockerBaseImages.Row("nvidia/cuda", "11.8.0-cudnn8-devel-ubuntu20.04"))), + text( + //language=Dockerfile + """ + FROM nvidia/cuda:11.8.0-cudnn8-devel-ubuntu20.04 AS base + LABEL maintainer="Hugging Face" + ARG DEBIAN_FRONTEND=noninteractive + SHELL ["sh", "-lc"] + """, + """ + ~~(nvidia/cuda:11.8.0-cudnn8-devel-ubuntu20.04)~~>FROM nvidia/cuda:11.8.0-cudnn8-devel-ubuntu20.04 AS base + LABEL maintainer="Hugging Face" + ARG DEBIAN_FRONTEND=noninteractive + SHELL ["sh", "-lc"] + """, + spec -> spec.path("Dockerfile") + ) + ); + } } From 3b306c5cc0c742c05ca76eb8631373a75137ddc1 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Mon, 15 Jul 2024 11:30:16 +0200 Subject: [PATCH 2/2] Drop explicit dependency --- build.gradle.kts | 2 -- 1 file changed, 2 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index f7bb43b..d82d0c7 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -9,6 +9,4 @@ val rewriteVersion = rewriteRecipe.rewriteVersion.get() dependencies { implementation(platform("org.openrewrite:rewrite-bom:$rewriteVersion")) implementation("org.openrewrite:rewrite-core:$rewriteVersion") - - testImplementation("org.assertj:assertj-core:latest.release") }