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

Perform same newGroupId and newArtifactId validations for Gradle ChangeDependency recipe #3930

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.*;

Expand Down Expand Up @@ -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<Object> validate() {
Validated<Object> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,29 @@ 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<Object> validate() {
Validated<Object> validated = super.validate();
if (newVersion != null) {
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,
Expand All @@ -126,20 +140,10 @@ public Validated<Object> 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<?, ExecutionContext> getVisitor() {

Expand Down