Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Docker Image reference, to be used for gitlab, docker, etc #4793

Merged
merged 38 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
04a0ed1
Basic implementation to find image over multiple sources
jevanlingen Dec 17, 2024
5dfc6d9
Formatting
jevanlingen Dec 17, 2024
a8f2e7d
Apply suggestions from code review
jevanlingen Dec 17, 2024
ebe33b9
Improvement
jevanlingen Dec 17, 2024
2ad2e6f
Improvement
jevanlingen Dec 17, 2024
f8fca6b
Add TODO removal comments
jevanlingen Dec 17, 2024
8e66538
Add DockerImageReference
jevanlingen Dec 17, 2024
de9632a
Add DockerImageReference
jevanlingen Dec 17, 2024
5482698
Add DockerImageReference
jevanlingen Dec 17, 2024
ab2a7eb
Introduce AbstractProvider
jevanlingen Dec 18, 2024
182ec9b
Improve DockerImageReference
jevanlingen Dec 18, 2024
80b611a
work on it
jevanlingen Dec 18, 2024
618cc63
first impl
jevanlingen Dec 19, 2024
bfd5050
Extra yaml test
jevanlingen Dec 19, 2024
cb1f8ed
working example
jevanlingen Dec 19, 2024
0f7e3ec
working example
jevanlingen Dec 19, 2024
c2abf2f
working example
jevanlingen Dec 19, 2024
6bbf0cb
revert lost
jevanlingen Dec 19, 2024
820da5f
Fix
jevanlingen Dec 19, 2024
78ff81d
Support --from and --platform and ignore comments for dockerfile
jevanlingen Dec 20, 2024
e506f0e
Merge branch 'main' into introduce-image-reference-and-recipe
jevanlingen Dec 20, 2024
c87164f
Merge branch 'main' into introduce-image-reference-and-recipe
jevanlingen Dec 20, 2024
7a60857
Remove files (will be places in rewrite-docker)
jevanlingen Dec 23, 2024
be20af2
Remove files (will be places in rewrite-docker)
jevanlingen Dec 23, 2024
3978669
Merge branch 'main' into introduce-image-reference-and-recipe
jevanlingen Dec 23, 2024
b2b88b2
Cleanup `findMatches`
jevanlingen Dec 23, 2024
97b5a1b
Make SourceFileWithReferences `abstract`, so we can define `getRefere…
jevanlingen Dec 23, 2024
70fd713
Make SourceFileWithReferences `abstract`, so we can define `getRefere…
jevanlingen Dec 23, 2024
ebb2ee6
Make SourceFileWithReferences `abstract`, so we can define `getRefere…
jevanlingen Dec 23, 2024
0a02c51
Make SourceFileWithReferences `abstract`, so we can define `getRefere…
jevanlingen Dec 23, 2024
411a811
Revert `SourceFileWithReferences` as interface
jevanlingen Dec 24, 2024
fae23b8
Make matchers static singletons in the provider classes
jevanlingen Dec 24, 2024
f65cf8c
Update description
timtebeek Dec 24, 2024
d6590f6
Merge branch 'main' into introduce-image-reference-and-recipe
jevanlingen Dec 27, 2024
f02c16a
Fix tests
jevanlingen Dec 27, 2024
59f1073
Revert implements of File class
jevanlingen Dec 27, 2024
f78bfbb
Merge branch 'main' into introduce-image-reference-and-recipe
timtebeek Dec 30, 2024
59db897
Pattern.asPredicate() acts as `find`, not `matches()`
timtebeek Dec 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,38 +21,40 @@
import org.jspecify.annotations.Nullable;
import org.openrewrite.trait.Reference;

import java.lang.ref.SoftReference;
import java.util.*;

@Incubating(since = "8.39.0")
public interface SourceFileWithReferences extends SourceFile {

References getReferences();

default SoftReference<References> build(@Nullable SoftReference<@Nullable References> references) {
References cache = references == null ? null : references.get();
if (cache == null || cache.getSourceFile() != this) {
return new SoftReference<>(References.build(this));
}
return references;
}

@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
@Getter
class References {
@Getter(AccessLevel.PRIVATE)
private final SourceFile sourceFile;
@Getter
private final Set<Reference> references;

public Collection<Reference> findMatches(Reference.Matcher matcher) {
return findMatchesInternal(matcher, null);
}

public Collection<Reference> findMatches(Reference.Matcher matcher, Reference.Kind kind) {
return findMatchesInternal(matcher, kind);
}

private List<Reference> findMatchesInternal(Reference.Matcher matcher, Reference.@Nullable Kind kind) {
List<Reference> list = new ArrayList<>();
for (Reference ref : references) {
if ((kind == null || ref.getKind() == kind) && ref.matches(matcher) ) {
if (ref.matches(matcher)) {
list.add(ref);
}
}
return list;
}

public static References build(SourceFile sourceFile) {
private static References build(SourceFile sourceFile) {
Set<Reference> references = new HashSet<>();
ServiceLoader<Reference.Provider> loader = ServiceLoader.load(Reference.Provider.class);
loader.forEach(provider -> {
Expand Down
10 changes: 9 additions & 1 deletion rewrite-core/src/main/java/org/openrewrite/text/Find.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import lombok.EqualsAndHashCode;
import lombok.Value;
import org.apache.commons.lang3.BooleanUtils;
import org.jspecify.annotations.Nullable;
import org.openrewrite.*;
import org.openrewrite.binary.Binary;
Expand Down Expand Up @@ -91,6 +92,12 @@ public String getDescription() {
@Nullable
String filePattern;

@Option(displayName = "Description",
description = "Add the matched value(s) as description on the search result marker. Default `false`.",
required = false)
@Nullable
Boolean description;

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {

Expand Down Expand Up @@ -135,7 +142,8 @@ public Tree visit(@Nullable Tree tree, ExecutionContext ctx) {
do {
int matchStart = matcher.start();
snippets.add(snippet(rawText.substring(previousEnd, matchStart)));
snippets.add(SearchResult.found(snippet(rawText.substring(matchStart, matcher.end()))));
String text = rawText.substring(matchStart, matcher.end());
snippets.add(SearchResult.found(snippet(text), BooleanUtils.isTrue(description) ? text : null));
previousEnd = matcher.end();

// For the first match, search backwards
Expand Down
24 changes: 19 additions & 5 deletions rewrite-core/src/main/java/org/openrewrite/text/PlainText.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@
package org.openrewrite.text;

import lombok.*;
import lombok.experimental.NonFinal;
import org.jspecify.annotations.Nullable;
import org.openrewrite.*;
import org.openrewrite.marker.Markers;

import java.lang.ref.SoftReference;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.function.Predicate;

Expand All @@ -36,7 +39,7 @@
@Value
@Builder
@AllArgsConstructor
public class PlainText implements SourceFile, Tree {
public class PlainText implements SourceFileWithReferences, Tree {

@Builder.Default
@With
Expand Down Expand Up @@ -79,16 +82,27 @@ public SourceFile withCharset(Charset charset) {
@Builder.Default
String text = "";

List<Snippet> snippets;

@Nullable
@NonFinal
@ToString.Exclude
transient SoftReference<References> references;

@Override
public References getReferences() {
this.references = build(this.references);
return Objects.requireNonNull(this.references.get());
}

public PlainText withText(String text) {
if (!text.equals(this.text)) {
return new PlainText(this.id, this.sourcePath, this.markers, this.charsetName, this.charsetBomMarked,
this.fileAttributes, this.checksum, text, this.snippets);
this.fileAttributes, this.checksum, text, this.snippets, this.references);
}
return this;
}

List<Snippet> snippets;

@Override
public <P> boolean isAcceptable(TreeVisitor<?, P> v, P p) {
return v.isAdaptableTo(PlainTextVisitor.class);
Expand Down Expand Up @@ -123,7 +137,7 @@ public PlainText withSnippets(@Nullable List<Snippet> snippets) {
if (this.snippets == snippets) {
return this;
}
return new PlainText(id, sourcePath, markers, charsetName, charsetBomMarked, fileAttributes, checksum, text, snippets);
return new PlainText(id, sourcePath, markers, charsetName, charsetBomMarked, fileAttributes, checksum, text, snippets, null);
}

@Value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public Stream<SourceFile> parseInputs(Iterable<Input> sources, @Nullable Path re
input.getFileAttributes(),
null,
sourceStr,
null,
null
);
parsingListener.parsed(input, plainText);
Expand Down
18 changes: 17 additions & 1 deletion rewrite-core/src/main/java/org/openrewrite/trait/Reference.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@

import org.openrewrite.*;

import java.util.HashSet;
import java.util.Set;

@Incubating(since = "8.39.0")
public interface Reference extends Trait<Tree> {

enum Kind {
TYPE,
PACKAGE
PACKAGE,
IMAGE
}

Kind getKind();
Expand Down Expand Up @@ -57,6 +59,20 @@ interface Provider {
boolean isAcceptable(SourceFile sourceFile);
}

abstract class AbstractProvider<U extends Reference> implements Provider {
protected abstract SimpleTraitMatcher<U> getMatcher();

@Override
public Set<Reference> getReferences(SourceFile sourceFile) {
Set<Reference> references = new HashSet<>();
getMatcher().asVisitor(reference -> {
references.add(reference);
return reference.getTree();
}).visit(sourceFile, 0);
return references;
}
}

interface Matcher {

boolean matchesReference(Reference value);
Expand Down
35 changes: 25 additions & 10 deletions rewrite-core/src/test/java/org/openrewrite/text/FindTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class FindTest implements RewriteTest {
@Test
void dataTable() {
rewriteRun(
spec -> spec.recipe(new Find("text", null, null, null, null, null))
spec -> spec.recipe(new Find("text", null, null, null, null, null, null))
.dataTable(TextMatches.Row.class, rows -> {
assertThat(rows).hasSize(1);
assertThat(rows.get(0).getMatch()).isEqualTo("This is ~~>text.");
Expand All @@ -53,7 +53,7 @@ void dataTable() {
@Test
void regex() {
rewriteRun(
spec -> spec.recipe(new Find("[T\\s]", true, true, null, null, null)),
spec -> spec.recipe(new Find("[T\\s]", true, true, null, null, null, null)),
text(
"""
This is\ttext.
Expand All @@ -68,7 +68,7 @@ void regex() {
@Test
void plainText() {
rewriteRun(
spec -> spec.recipe(new Find("\\s", null, null, null, null, null)),
spec -> spec.recipe(new Find("\\s", null, null, null, null, null, null)),
text(
"""
This i\\s text.
Expand All @@ -83,7 +83,7 @@ void plainText() {
@Test
void caseInsensitive() {
rewriteRun(
spec -> spec.recipe(new Find("text", null, null, null, null, "**/foo/**;**/baz/**")),
spec -> spec.recipe(new Find("text", null, null, null, null, "**/foo/**;**/baz/**", null)),
dir("foo",
text(
"""
Expand Down Expand Up @@ -115,7 +115,7 @@ void caseInsensitive() {
@Test
void regexBasicMultiLine() {
rewriteRun(
spec -> spec.recipe(new Find("[T\\s]", true, true, true, null, null)),
spec -> spec.recipe(new Find("[T\\s]", true, true, true, null, null, null)),
text(
"""
This is\ttext.
Expand All @@ -132,7 +132,7 @@ void regexBasicMultiLine() {
@Test
void regexWithoutMultilineAndDotall() {
rewriteRun(
spec -> spec.recipe(new Find("^This.*below\\.$", true, true, false, false, null)),
spec -> spec.recipe(new Find("^This.*below\\.$", true, true, false, false, null, null)),
text(
"""
This is text.
Expand All @@ -148,7 +148,7 @@ void regexWithoutMultilineAndDotall() {
@Test
void regexMatchingWhitespaceWithoutMultilineWithDotall() {
rewriteRun(
spec -> spec.recipe(new Find("One.Two$", true, true, false, true, null)),
spec -> spec.recipe(new Find("One.Two$", true, true, false, true, null, null)),
//language=csv
text( // the `.` above matches the space character on the same line
"""
Expand All @@ -163,7 +163,7 @@ void regexMatchingWhitespaceWithoutMultilineWithDotall() {
@Test
void regexWithoutMultilineAndWithDotAll() {
rewriteRun(
spec -> spec.recipe(new Find("^This.*below\\.$", true, true, false, true, null)),
spec -> spec.recipe(new Find("^This.*below\\.$", true, true, false, true, null, null)),
text(
"""
This is text.
Expand All @@ -186,7 +186,7 @@ void regexWithoutMultilineAndWithDotAll() {
@Test
void regexWithMultilineAndWithoutDotall() {
rewriteRun(
spec -> spec.recipe(new Find("^This.*below\\.$", true, true, true, false, null)),
spec -> spec.recipe(new Find("^This.*below\\.$", true, true, true, false, null, null)),
text(
"""
This is text.
Expand All @@ -209,7 +209,7 @@ void regexWithMultilineAndWithoutDotall() {
@Test
void regexWithBothMultilineAndDotAll() {
rewriteRun(
spec -> spec.recipe(new Find("^This.*below\\.$", true, true, true, true, null)),
spec -> spec.recipe(new Find("^This.*below\\.$", true, true, true, true, null, null)),
text(
"""
The first line.
Expand All @@ -228,4 +228,19 @@ void regexWithBothMultilineAndDotAll() {
)
);
}

@Test
void description() {
rewriteRun(
spec -> spec.recipe(new Find("text", null, null, null, null, null, true)),
text(
"""
This is text.
""",
"""
This is ~~(text)~~>text.
"""
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public boolean isAcceptable(SourceFile sourceFile, ExecutionContext ctx) {
SourceFileWithReferences.References references = sourceFile.getReferences();
TypeMatcher matcher = new TypeMatcher(oldFullyQualifiedTypeName);
Map<Tree, Reference> matches = new HashMap<>();
for (Reference ref : references.findMatches(matcher, Reference.Kind.TYPE)) {
for (Reference ref : references.findMatches(matcher)) {
matches.put(ref.getTree(), ref);
}
return new ReferenceChangeTypeVisitor(matches, matcher.createRenamer(newFullyQualifiedTypeName)).visit(tree, ctx, requireNonNull(getCursor().getParent()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.openrewrite.java.table.TypeUses;
import org.openrewrite.java.tree.*;
import org.openrewrite.marker.SearchResult;
import org.openrewrite.trait.Reference;
import org.openrewrite.trait.Trait;

import java.util.HashSet;
Expand Down Expand Up @@ -80,7 +79,7 @@ public boolean isAcceptable(SourceFile sourceFile, ExecutionContext ctx) {
SourceFileWithReferences sourceFile = (SourceFileWithReferences) tree;
SourceFileWithReferences.References references = sourceFile.getReferences();
TypeMatcher matcher = new TypeMatcher(fullyQualifiedTypeName);
Set<Tree> matches = references.findMatches(matcher, Reference.Kind.TYPE).stream().map(Trait::getTree).collect(Collectors.toSet());
Set<Tree> matches = references.findMatches(matcher).stream().map(Trait::getTree).collect(Collectors.toSet());
return new ReferenceVisitor(matches).visit(tree, ctx);
}
return tree;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public boolean isAcceptable(SourceFile sourceFile, P p) {
SourceFileWithReferences sourceFile = (SourceFileWithReferences) tree;
SourceFileWithReferences.References references = sourceFile.getReferences();
TypeMatcher matcher = typeMatcher != null ? typeMatcher : new TypeMatcher(fullyQualifiedType);
for (Reference ignored : references.findMatches(matcher, Reference.Kind.TYPE)) {
for (Reference ignored : references.findMatches(matcher)) {
return SearchResult.found(sourceFile);
}
}
Expand Down
Loading
Loading