Skip to content

Commit

Permalink
Implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
jevanlingen committed Dec 23, 2024
1 parent b7adeba commit ee296fd
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,25 +90,4 @@ public Tree postVisit(Tree tree, ExecutionContext ctx) {
return tree;
}
}

;

/*@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return dockerfile().asVisitor((docker, ctx) -> {
List<DockerImageVersion> 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();
});
}*/
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ public Set<Reference> 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));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<RecipeSpec> 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)
);
}
Expand Down

0 comments on commit ee296fd

Please sign in to comment.