Skip to content

Commit

Permalink
Make ParentPomInsight recursive (#4529)
Browse files Browse the repository at this point in the history
* HasMavenAncestry recipe

* Merge with ParentPomInsight

* Invert conditional to reduce nesting and diff

---------

Co-authored-by: Tim te Beek <[email protected]>
  • Loading branch information
DidierLoiseau and timtebeek authored Oct 21, 2024
1 parent cb67df7 commit 5c07699
Show file tree
Hide file tree
Showing 3 changed files with 765 additions and 25 deletions.
8 changes: 8 additions & 0 deletions rewrite-core/src/main/java/org/openrewrite/semver/Semver.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ public static boolean isVersion(@Nullable String version) {
return LatestRelease.RELEASE_PATTERN.matcher(version).matches();
}

/**
* Validates the given version against an optional pattern
*
* @param toVersion the version to validate. Node-style [version selectors](https://docs.openrewrite.org/reference/dependency-version-selectors) may be used.
* @param metadataPattern optional metadata appended to the version. Allows version selection to be extended beyond the original Node Semver semantics. So for example,
* Setting 'version' to "25-29" can be paired with a metadata pattern of "-jre" to select Guava 29.0-jre
* @return the validation result
*/
public static Validated<VersionComparator> validate(String toVersion, @Nullable String metadataPattern) {
return Validated.<VersionComparator, String>testNone(
"metadataPattern",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,18 @@
import org.jspecify.annotations.Nullable;
import org.openrewrite.*;
import org.openrewrite.marker.SearchResult;
import org.openrewrite.maven.MavenDownloadingException;
import org.openrewrite.maven.MavenIsoVisitor;
import org.openrewrite.maven.internal.MavenPomDownloader;
import org.openrewrite.maven.table.ParentPomsInUse;
import org.openrewrite.maven.tree.MavenResolutionResult;
import org.openrewrite.maven.tree.Parent;
import org.openrewrite.maven.tree.ResolvedPom;
import org.openrewrite.semver.Semver;
import org.openrewrite.semver.VersionComparator;
import org.openrewrite.xml.tree.Xml;

import static java.util.Collections.emptyList;
import static org.openrewrite.internal.StringUtils.matchesGlob;

@EqualsAndHashCode(callSuper = false)
Expand All @@ -52,6 +58,12 @@ public class ParentPomInsight extends Recipe {
@Nullable
String version;

@Option(displayName = "Recursive",
description = "Whether to search recursively through the parents. True by default.",
required = false)
@Nullable
Boolean recursive;

@Override
public String getDisplayName() {
return "Maven parent insight";
Expand Down Expand Up @@ -79,26 +91,43 @@ public Validated<Object> validate() {
@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new MavenIsoVisitor<ExecutionContext>() {
@Nullable
final VersionComparator versionComparator = version == null ? null : Semver.validate(version, null).getValue();

@Override
public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
Xml.Tag t = super.visitTag(tag, ctx);
if (isParentTag()) {
ResolvedPom resolvedPom = getResolutionResult().getPom();
String groupId = resolvedPom.getValue(tag.getChildValue("groupId").orElse(null));
String artifactId = resolvedPom.getValue(tag.getChildValue("artifactId").orElse(null));
if (!isParentTag()) {
return t;
}

MavenResolutionResult mrr = getResolutionResult();
MavenPomDownloader mpd = new MavenPomDownloader(mrr.getProjectPoms(), ctx, mrr.getMavenSettings(), mrr.getActiveProfiles());

Parent ancestor = mrr.getPom().getRequested().getParent();
String relativePath = tag.getChildValue("relativePath").orElse(null);
while (ancestor != null) {
String groupId = ancestor.getGroupId();
String artifactId = ancestor.getArtifactId();
if (matchesGlob(groupId, groupIdPattern) && matchesGlob(artifactId, artifactIdPattern)) {
String parentVersion = resolvedPom.getValue(tag.getChildValue("version").orElse(null));
if (version != null) {
if (!Semver.validate(version, null).getValue()
.isValid(null, parentVersion)) {
return t;
}
String parentVersion = ancestor.getVersion();
if (versionComparator == null || versionComparator.isValid(null, parentVersion)) {
// Found a parent pom that matches the criteria
inUse.insertRow(ctx, new ParentPomsInUse.Row(
mrr.getPom().getArtifactId(), groupId, artifactId, parentVersion, relativePath));
return SearchResult.found(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, parentVersion, relativePath));
return SearchResult.found(t);
}
if (Boolean.FALSE.equals(recursive)) {
return t;
}
try {
ResolvedPom ancestorPom = mpd.download(ancestor.getGav(), null, null, mrr.getPom().getRepositories())
.resolve(emptyList(), mpd, ctx);
ancestor = ancestorPom.getRequested().getParent();
relativePath = null;
} catch (MavenDownloadingException e) {
return e.warn(t);
}
}
return t;
Expand Down
Loading

0 comments on commit 5c07699

Please sign in to comment.