Skip to content

Commit

Permalink
feat: added check for duplicated dependency (#4515)
Browse files Browse the repository at this point in the history
* feat: added check for duplicated dependency

Refs: #4514

* chore: renaming

Refs: #4514

* refactor: moved delete old recipe after super() call

Refs: #4514

* Add issue references to tests

* Apply formatter

---------

Co-authored-by: Andrei Shakirin <[email protected]>
Co-authored-by: Tim te Beek <[email protected]>
  • Loading branch information
3 people authored Oct 11, 2024
1 parent d2f3de2 commit 637266b
Show file tree
Hide file tree
Showing 4 changed files with 479 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@
import org.jspecify.annotations.Nullable;
import org.openrewrite.*;
import org.openrewrite.maven.table.MavenMetadataFailures;
import org.openrewrite.maven.tree.MavenMetadata;
import org.openrewrite.maven.tree.MavenResolutionResult;
import org.openrewrite.maven.tree.ResolvedManagedDependency;
import org.openrewrite.maven.tree.Scope;
import org.openrewrite.maven.tree.*;
import org.openrewrite.semver.Semver;
import org.openrewrite.semver.VersionComparator;
import org.openrewrite.xml.AddToTagVisitor;
Expand Down Expand Up @@ -153,9 +150,11 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
final VersionComparator versionComparator = newVersion != null ? Semver.validate(newVersion, versionPattern).getValue() : null;
@Nullable
private Collection<String> availableVersions;
private boolean isNewDependencyPresent;

@Override
public Xml visitDocument(Xml.Document document, ExecutionContext ctx) {
isNewDependencyPresent = checkIfNewDependencyPresents(newGroupId, newArtifactId, newVersion);
// Any managed dependency change is unlikely to use the same version, so only update selectively.
if ((changeManagedDependency == null || changeManagedDependency) && newVersion != null || versionPattern != null) {
doAfterVisit(new ChangeManagedDependencyGroupIdAndArtifactId(
Expand All @@ -170,7 +169,13 @@ public Xml visitDocument(Xml.Document document, ExecutionContext ctx) {
@Override
public Xml visitTag(Xml.Tag tag, ExecutionContext ctx) {
Xml.Tag t = (Xml.Tag) super.visitTag(tag, ctx);
if (isDependencyTag(oldGroupId, oldArtifactId)) {
boolean isOldDependencyTag = isDependencyTag(oldGroupId, oldArtifactId);
if (isOldDependencyTag && isNewDependencyPresent) {
doAfterVisit(new RemoveContentVisitor<>(tag, true));
maybeUpdateModel();
return t;
}
if (isOldDependencyTag) {
String groupId = newGroupId;
if (groupId != null) {
t = changeChildTagValue(t, "groupId", groupId, ctx);
Expand Down Expand Up @@ -224,6 +229,16 @@ public Xml visitTag(Xml.Tag tag, ExecutionContext ctx) {
return t;
}

private boolean checkIfNewDependencyPresents(@Nullable String groupId, @Nullable String artifactId, @Nullable String version) {
if ((groupId == null) || (artifactId == null)) {
return false;
}
List<ResolvedDependency> dependencies = findDependencies(groupId, artifactId);
return dependencies.stream()
.filter(ResolvedDependency::isDirect)
.anyMatch(rd -> (version == null) || version.equals(rd.getVersion()));
}

private Xml.Tag changeChildTagValue(Xml.Tag tag, String childTagName, String newValue, ExecutionContext ctx) {
Optional<Xml.Tag> childTag = tag.getChild(childTagName);
if (childTag.isPresent() && !newValue.equals(childTag.get().getValue().orElse(null))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
import org.openrewrite.*;
import org.openrewrite.maven.table.MavenMetadataFailures;
import org.openrewrite.maven.tree.MavenMetadata;
import org.openrewrite.maven.tree.ResolvedManagedDependency;
import org.openrewrite.semver.Semver;
import org.openrewrite.semver.VersionComparator;
import org.openrewrite.xml.ChangeTagValueVisitor;
import org.openrewrite.xml.RemoveContentVisitor;
import org.openrewrite.xml.tree.Xml;

import java.util.*;
Expand Down Expand Up @@ -103,8 +105,7 @@ public Validated<Object> validate() {
if (newVersion != null) {
validated = validated.and(Semver.validate(newVersion, versionPattern));
}
validated =
validated.and(test(
validated = validated.and(test(
"coordinates",
"newGroupId OR newArtifactId must be different from before",
this,
Expand All @@ -113,7 +114,7 @@ public Validated<Object> validate() {
boolean sameArtifactId = isBlank(r.newArtifactId) || Objects.equals(r.oldArtifactId, r.newArtifactId);
return !(sameGroupId && sameArtifactId);
}
));
));
return validated;
}

Expand All @@ -134,6 +135,14 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
final VersionComparator versionComparator = newVersion != null ? Semver.validate(newVersion, versionPattern).getValue() : null;
@Nullable
private Collection<String> availableVersions;
private boolean isNewDependencyPresent;

@Override
public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) {
isNewDependencyPresent = checkIfNewDependencyPresents(newGroupId, newArtifactId, newVersion);
return super.visitDocument(document, ctx);
}

@Override
public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {

Expand All @@ -160,18 +169,45 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
t = (Xml.Tag) new ChangeTagValueVisitor<>(versionTag.get(), resolvedNewVersion).visitNonNull(t, 0, getCursor().getParentOrThrow());
}
changed = true;
} catch(MavenDownloadingException e) {
} catch (MavenDownloadingException e) {
return e.warn(t);
}
}
if (changed) {
maybeUpdateModel();
doAfterVisit(new RemoveRedundantDependencyVersions(null, null, (RemoveRedundantDependencyVersions.Comparator) null, null).getVisitor());
if (isNewDependencyPresent) {
doAfterVisit(new RemoveContentVisitor<>(t, true));
maybeUpdateModel();
}
}
}
return t;
}

private boolean checkIfNewDependencyPresents(@Nullable String groupId, @Nullable String artifactId, @Nullable String version) {
if ((groupId == null) || (artifactId == null)) {
return false;
}
ResolvedManagedDependency managedDependency = findManagedDependency(groupId, artifactId);
if (managedDependency != null) {
return compareVersions(version, managedDependency.getVersion());
} else {
return false;
}
}

private boolean compareVersions(@Nullable String targetVersion, @Nullable String foundVersion) {
if (targetVersion == null) {
return true;
}
if ((versionComparator != null) && (foundVersion != null)) {
return versionComparator.isValid(targetVersion, foundVersion);
} else {
return targetVersion.equals(foundVersion);
}
}

@SuppressWarnings("ConstantConditions")
private String resolveSemverVersion(ExecutionContext ctx, String groupId, String artifactId, @Nullable String currentVersion) throws MavenDownloadingException {
if (versionComparator == null) {
Expand Down
Loading

0 comments on commit 637266b

Please sign in to comment.