diff --git a/src/main/java/org/openrewrite/docker/search/FindDockerImageUses.java b/src/main/java/org/openrewrite/docker/search/FindDockerImageUses.java index ef06c33..dea8ee6 100644 --- a/src/main/java/org/openrewrite/docker/search/FindDockerImageUses.java +++ b/src/main/java/org/openrewrite/docker/search/FindDockerImageUses.java @@ -90,25 +90,4 @@ public Tree postVisit(Tree tree, ExecutionContext ctx) { return tree; } } - - ; - - /*@Override - public TreeVisitor getVisitor() { - return dockerfile().asVisitor((docker, ctx) -> { - List froms = docker.getFroms(); - if (!froms.isEmpty()) { - for (DockerImageVersion from : froms) { - dockerBaseImages.insertRow(ctx, new DockerBaseImages.Row( - from.getImageName(), - from.getVersion() == null ? "" : from.getVersion() - )); - } - return SearchResult.found(docker.getTree(), - froms.stream().map(DockerImageVersion::toString) - .collect(Collectors.joining(", "))); - } - return docker.getTree(); - }); - }*/ } diff --git a/src/main/java/org/openrewrite/docker/trait/DockerfileImageReference.java b/src/main/java/org/openrewrite/docker/trait/DockerfileImageReference.java index b9c7452..7fc5059 100644 --- a/src/main/java/org/openrewrite/docker/trait/DockerfileImageReference.java +++ b/src/main/java/org/openrewrite/docker/trait/DockerfileImageReference.java @@ -46,13 +46,13 @@ public Set getReferences(SourceFile sourceFile) { for (int i = 0, wordsLength = words.length; i < wordsLength; i++) { if ("from".equalsIgnoreCase(words[i])) { String image = words[i + 1].startsWith("--platform") ? words[i + 2] : words[i + 1]; - references.add(new org.openrewrite.text.DockerImageReference(c, image)); + references.add(new DockerfileImageReference(c, image)); } else if ("as".equalsIgnoreCase(words[i])) { imageVariables.add(words[i + 1]); } else if (words[i].startsWith("--from") && words[i].split("=").length == 2) { String image = words[i].split("=")[1]; if (!imageVariables.contains(image) && !StringUtils.isNumeric(image)) { - references.add(new org.openrewrite.text.DockerImageReference(c, image)); + references.add(new DockerfileImageReference(c, image)); } } } diff --git a/src/main/resources/services/org.openrewrite.trait.Reference$Provider b/src/main/resources/META-INF/services/org.openrewrite.trait.Reference$Provider similarity index 100% rename from src/main/resources/services/org.openrewrite.trait.Reference$Provider rename to src/main/resources/META-INF/services/org.openrewrite.trait.Reference$Provider diff --git a/src/test/java/org/openrewrite/docker/FindDockerImagesUsedTest.java b/src/test/java/org/openrewrite/docker/FindDockerImagesUsedTest.java index 9ca4628..23ae9f9 100644 --- a/src/test/java/org/openrewrite/docker/FindDockerImagesUsedTest.java +++ b/src/test/java/org/openrewrite/docker/FindDockerImagesUsedTest.java @@ -271,12 +271,81 @@ void dockerFileIgnoreComment() { ); } + @Test + void gitlabCIFile() { + rewriteRun( + assertImages("maven:latest"), + //language=yaml + yaml( + """ + image: maven:latest + + variables: + MAVEN_CLI_OPTS: "-s .m2/settings.xml --batch-mode" + MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository" + + cache: + paths: + - .m2/repository/ + - target/ + + build: + stage: build + script: + - mvn $MAVEN_CLI_OPTS compile + + test: + stage: test + script: + - mvn $MAVEN_CLI_OPTS test + + deploy: + stage: deploy + script: + - mvn $MAVEN_CLI_OPTS deploy + only: + - master + """, + """ + image: ~~(maven:latest)~~>maven:latest + + variables: + MAVEN_CLI_OPTS: "-s .m2/settings.xml --batch-mode" + MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository" + + cache: + paths: + - .m2/repository/ + - target/ + + build: + stage: build + script: + - mvn $MAVEN_CLI_OPTS compile + + test: + stage: test + script: + - mvn $MAVEN_CLI_OPTS test + + deploy: + stage: deploy + script: + - mvn $MAVEN_CLI_OPTS deploy + only: + - master + """, + spec -> spec.path(".gitlab-ci") + ) + ); + } + private static Consumer assertImages(String... expected) { return spec -> spec.recipe(new FindDockerImageUses()) .dataTable(DockerBaseImages.Row.class,rows -> assertThat(rows) .hasSize(expected.length) - .extracting(it -> it.getImageName() + ":" + it.getTag()) + .extracting(it -> it.getImageName() + (it.getTag().isEmpty() ? "" : ":" + it.getTag())) .containsExactlyInAnyOrder(expected) ); }