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

Make ParentPomInsight recursive #4529

Merged
merged 4 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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;

timtebeek marked this conversation as resolved.
Show resolved Hide resolved
@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);
timtebeek marked this conversation as resolved.
Show resolved Hide resolved
}
}
return t;
Expand Down
Loading