From f93d7b45173c36b8521f532e2887565d04f46a56 Mon Sep 17 00:00:00 2001 From: Shannon Pamperl Date: Wed, 17 Jan 2024 09:51:07 -0600 Subject: [PATCH] Perform same newGroupId and newArtifactId validations for Gradle ChangeDependency recipe --- .../openrewrite/gradle/ChangeDependency.java | 26 ++++++++++++++-- .../ChangeDependencyGroupIdAndArtifactId.java | 30 +++++++++++-------- 2 files changed, 41 insertions(+), 15 deletions(-) diff --git a/rewrite-gradle/src/main/java/org/openrewrite/gradle/ChangeDependency.java b/rewrite-gradle/src/main/java/org/openrewrite/gradle/ChangeDependency.java index f8fbaa9d7ea..d9ce58be98f 100644 --- a/rewrite-gradle/src/main/java/org/openrewrite/gradle/ChangeDependency.java +++ b/rewrite-gradle/src/main/java/org/openrewrite/gradle/ChangeDependency.java @@ -16,7 +16,8 @@ package org.openrewrite.gradle; import com.fasterxml.jackson.annotation.JsonCreator; -import lombok.*; +import lombok.EqualsAndHashCode; +import lombok.Value; import org.openrewrite.*; import org.openrewrite.gradle.marker.GradleDependencyConfiguration; import org.openrewrite.gradle.marker.GradleProject; @@ -37,6 +38,7 @@ import org.openrewrite.maven.tree.MavenRepository; import org.openrewrite.maven.tree.ResolvedGroupArtifactVersion; import org.openrewrite.semver.DependencyMatcher; +import org.openrewrite.semver.Semver; import java.util.*; @@ -127,7 +129,27 @@ public String getInstanceNameSuffix() { @Override public String getDescription() { - return "Change a Gradle dependency coordinates."; + return "Change a Gradle dependency coordinates. The `newGroupId` or `newArtifactId` **MUST** be different from before."; + } + + @Override + public Validated validate() { + Validated validated = super.validate(); + if (newVersion != null) { + validated = validated.and(Semver.validate(newVersion, versionPattern)); + } + validated = validated.and(Validated.required("newGroupId", newGroupId).or(Validated.required("newArtifactId", newArtifactId))); + validated = validated.and(Validated.test( + "coordinates", + "newGroupId OR newArtifactId must be different from before", + this, + r -> { + boolean sameGroupId = StringUtils.isBlank(r.newGroupId) || Objects.equals(r.oldGroupId, r.newGroupId); + boolean sameArtifactId = StringUtils.isBlank(r.newArtifactId) || Objects.equals(r.oldArtifactId, r.newArtifactId); + return !(sameGroupId && sameArtifactId); + } + )); + return validated; } @Override diff --git a/rewrite-maven/src/main/java/org/openrewrite/maven/ChangeDependencyGroupIdAndArtifactId.java b/rewrite-maven/src/main/java/org/openrewrite/maven/ChangeDependencyGroupIdAndArtifactId.java index 0a1caa2d2f7..23e38023218 100755 --- a/rewrite-maven/src/main/java/org/openrewrite/maven/ChangeDependencyGroupIdAndArtifactId.java +++ b/rewrite-maven/src/main/java/org/openrewrite/maven/ChangeDependencyGroupIdAndArtifactId.java @@ -109,6 +109,21 @@ public ChangeDependencyGroupIdAndArtifactId(String oldGroupId, String oldArtifac this.overrideManagedVersion = overrideManagedVersion; } + @Override + public String getDisplayName() { + return "Change Maven dependency"; + } + + @Override + public String getInstanceNameSuffix() { + return String.format("`%s:%s`", oldGroupId, oldArtifactId); + } + + @Override + public String getDescription() { + return "Change a Maven dependency coordinates. The `newGroupId` or `newArtifactId` **MUST** be different from before."; + } + @Override public Validated validate() { Validated validated = super.validate(); @@ -116,8 +131,7 @@ public Validated validate() { validated = validated.and(Semver.validate(newVersion, versionPattern)); } validated = validated.and(required("newGroupId", newGroupId).or(required("newArtifactId", newArtifactId))); - validated = - validated.and(test( + validated = validated.and(test( "coordinates", "newGroupId OR newArtifactId must be different from before", this, @@ -126,20 +140,10 @@ public Validated validate() { boolean sameArtifactId = isBlank(r.newArtifactId) || Objects.equals(r.oldArtifactId, r.newArtifactId); return !(sameGroupId && sameArtifactId); } - )); + )); return validated; } - @Override - public String getDisplayName() { - return "Change Maven dependency groupId, artifactId and optionally the version"; - } - - @Override - public String getDescription() { - return "Change the groupId, artifactId and optionally the version of a specified Maven dependency."; - } - @Override public TreeVisitor getVisitor() {