Skip to content

Commit

Permalink
Support optional version selector in ParentPomInsight (#3715)
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek authored Nov 21, 2023
1 parent 552a4a8 commit 8282949
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,15 @@

import lombok.EqualsAndHashCode;
import lombok.Value;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Option;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.*;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.marker.SearchResult;
import org.openrewrite.maven.MavenIsoVisitor;
import org.openrewrite.maven.table.ParentPomsInUse;
import org.openrewrite.maven.tree.ResolvedPom;
import org.openrewrite.semver.Semver;
import org.openrewrite.xml.tree.Xml;

import java.util.UUID;

import static org.openrewrite.Tree.randomId;
import static org.openrewrite.internal.StringUtils.matchesGlob;

@EqualsAndHashCode(callSuper = true)
Expand All @@ -47,7 +43,14 @@ public class ParentPomInsight extends Recipe {
example = "spring-boot-starter-*")
String artifactIdPattern;

UUID searchId = randomId();
@Option(displayName = "Version",
description = "Match only dependencies with the specified version. " +
"Node-style [version selectors](https://docs.openrewrite.org/reference/dependency-version-selectors) may be used." +
"All versions are searched by default.",
example = "1.x",
required = false)
@Nullable
String version;

@Override
public String getDisplayName() {
Expand All @@ -59,6 +62,16 @@ public String getDescription() {
return "Find Maven parents matching a `groupId` and `artifactId`.";
}


@Override
public Validated<Object> validate() {
Validated<Object> v = super.validate();
if (version != null) {
v = v.and(Semver.validate(version, null));
}
return v;
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new MavenIsoVisitor<ExecutionContext>() {
Expand All @@ -70,10 +83,17 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
String groupId = resolvedPom.getValue(tag.getChildValue("groupId").orElse(null));
String artifactId = resolvedPom.getValue(tag.getChildValue("artifactId").orElse(null));
if (matchesGlob(groupId, groupIdPattern) && matchesGlob(artifactId, artifactIdPattern)) {
String version = resolvedPom.getValue(tag.getChildValue("version").orElse(null));
String parentVersion = resolvedPom.getValue(tag.getChildValue("version").orElse(null));
if (version != null) {
if (!Semver.validate(version, null).getValue()
.isValid(null, parentVersion)) {
return t;
}
}
// Found a parent pom that matches the criteria
String relativePath = tag.getChildValue("relativePath").orElse(null);
inUse.insertRow(ctx, new ParentPomsInUse.Row(
resolvedPom.getArtifactId(), groupId, artifactId, version, relativePath));
resolvedPom.getArtifactId(), groupId, artifactId, parentVersion, relativePath));
return SearchResult.found(t);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
class ParentPomInsightTest implements RewriteTest {
@Override
public void defaults(RecipeSpec spec) {
spec.recipe(new ParentPomInsight("org.springframework.boot", "spring-boot-starter-parent"));
spec.recipe(new ParentPomInsight("org.springframework.boot", "spring-boot-starter-parent", null));
}

@DocumentExample
Expand Down Expand Up @@ -89,7 +89,7 @@ void findParent() {
void multiModuleOnlyRoot() {
rewriteRun(
spec -> spec
.recipe(new ParentPomInsight("*", "*"))
.recipe(new ParentPomInsight("*", "*", null))
.dataTableAsCsv(ParentPomsInUse.class.getName(), """
projectArtifactId,groupId,artifactId,version,relativePath
sample,org.springframework.boot,"spring-boot-starter-parent",2.5.0,
Expand Down Expand Up @@ -203,4 +203,95 @@ void multiModuleOnlyRoot() {
)
);
}

@Test
void matchNonSnapshot() {
rewriteRun(
spec -> spec
.recipe(new ParentPomInsight("*", "*", "~2"))
.dataTableAsCsv(ParentPomsInUse.class.getName(), """
projectArtifactId,groupId,artifactId,version,relativePath
sample,org.springframework.boot,"spring-boot-starter-parent",2.5.0,
"""),
mavenProject("sample",
pomXml(
"""
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.sample</groupId>
<artifactId>sample</artifactId>
<version>1.0.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.0</version>
</parent>
<modules>
<module>module1</module>
<module>module2</module>
</modules>
</project>
""",
"""
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.sample</groupId>
<artifactId>sample</artifactId>
<version>1.0.0-SNAPSHOT</version>
<!--~~>--><parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.0</version>
</parent>
<modules>
<module>module1</module>
<module>module2</module>
</modules>
</project>
"""
),
mavenProject("module1",
pomXml(
"""
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.sample</groupId>
<artifactId>sample</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../</relativePath>
</parent>
<artifactId>module1</artifactId>
</project>
""",
spec -> spec.path("module1/pom.xml")
)),
mavenProject("module2",
pomXml(
"""
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.sample</groupId>
<artifactId>sample</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../</relativePath>
</parent>
<artifactId>module2</artifactId>
</project>
""",
spec -> spec.path("module2/pom.xml")
)
)
)
);
}
}

0 comments on commit 8282949

Please sign in to comment.