Skip to content

Commit

Permalink
fixing defect when using latest.patch version syntax with ChangeDep…
Browse files Browse the repository at this point in the history
…endencyGroupIdAndArtifactId and ChangeManagedDependencyGroupIdAndArtifactId (#4001)
  • Loading branch information
nmck257 authored Feb 19, 2024
1 parent ee451a0 commit dab9144
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,10 @@ public Xml visitTag(Xml.Tag tag, ExecutionContext ctx) {
} else {
artifactId = t.getChildValue("artifactId").orElseThrow(NoSuchElementException::new);
}
String currentVersion = t.getChildValue("version").orElse(null);
if (newVersion != null) {
try {
String resolvedNewVersion = resolveSemverVersion(ctx, groupId, artifactId);
String resolvedNewVersion = resolveSemverVersion(ctx, groupId, artifactId, currentVersion);
Optional<Xml.Tag> scopeTag = t.getChild("scope");
Scope scope = scopeTag.map(xml -> Scope.fromName(xml.getValue().orElse("compile"))).orElse(Scope.Compile);
Optional<Xml.Tag> versionTag = t.getChild("version");
Expand Down Expand Up @@ -242,15 +243,16 @@ private boolean isDependencyManaged(Scope scope, String groupId, String artifact
}

@SuppressWarnings("ConstantConditions")
private String resolveSemverVersion(ExecutionContext ctx, String groupId, String artifactId) throws MavenDownloadingException {
private String resolveSemverVersion(ExecutionContext ctx, String groupId, String artifactId, @Nullable String currentVersion) throws MavenDownloadingException {
if (versionComparator == null) {
return newVersion;
}
String finalCurrentVersion = currentVersion != null ? currentVersion : newVersion;
if (availableVersions == null) {
availableVersions = new ArrayList<>();
MavenMetadata mavenMetadata = metadataFailures.insertRows(ctx, () -> downloadMetadata(groupId, artifactId, ctx));
for (String v : mavenMetadata.getVersioning().getVersions()) {
if (versionComparator.isValid(newVersion, v)) {
if (versionComparator.isValid(finalCurrentVersion, v)) {
availableVersions.add(v);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,10 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
}
if (newVersion != null) {
try {
String resolvedNewVersion = resolveSemverVersion(ctx, newGroupId, newArtifactId);

Optional<Xml.Tag> versionTag = t.getChild("version");

if (versionTag.isPresent()) {
String resolvedNewVersion = resolveSemverVersion(ctx, newGroupId, newArtifactId, versionTag.get().getValue().orElse(null));
t = (Xml.Tag) new ChangeTagValueVisitor<>(versionTag.get(), resolvedNewVersion).visitNonNull(t, 0, getCursor().getParentOrThrow());
}
changed = true;
Expand All @@ -174,15 +173,16 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
}

@SuppressWarnings("ConstantConditions")
private String resolveSemverVersion(ExecutionContext ctx, String groupId, String artifactId) throws MavenDownloadingException {
private String resolveSemverVersion(ExecutionContext ctx, String groupId, String artifactId, @Nullable String currentVersion) throws MavenDownloadingException {
if (versionComparator == null) {
return newVersion;
}
String finalCurrentVersion = currentVersion != null ? currentVersion : newVersion;
if (availableVersions == null) {
availableVersions = new ArrayList<>();
MavenMetadata mavenMetadata = metadataFailures.insertRows(ctx, () -> downloadMetadata(groupId, artifactId, ctx));
for (String v : mavenMetadata.getVersioning().getVersions()) {
if (versionComparator.isValid(newVersion, v)) {
if (versionComparator.isValid(finalCurrentVersion, v)) {
availableVersions.add(v);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,52 @@ void unmanagedToManagedExternalizedDepMgmt() {
);
}

@Test
void latestPatch() {
rewriteRun(
spec -> spec.recipe(new ChangeDependencyGroupIdAndArtifactId(
"javax.activation",
"javax.activation-api",
"jakarta.activation",
"jakarta.activation-api",
"latest.patch",
null
)),
pomXml(
"""
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
<dependencies>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>javax.activation-api</artifactId>
<version>1.2.0</version>
</dependency>
</dependencies>
</project>
""",
"""
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
<dependencies>
<dependency>
<groupId>jakarta.activation</groupId>
<artifactId>jakarta.activation-api</artifactId>
<version>1.2.2</version>
</dependency>
</dependencies>
</project>
"""
)
);
}

@Test
void changeOnlyArtifactId() {
rewriteRun(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,54 @@ void changeManagedDependencyWithDynamicVersion() {
)
);
}

@Test
void latestPatch() {
rewriteRun(
spec -> spec.recipe(new ChangeManagedDependencyGroupIdAndArtifactId(
"javax.activation",
"javax.activation-api",
"jakarta.activation",
"jakarta.activation-api",
"latest.patch",
null
)),
pomXml(
"""
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>javax.activation-api</artifactId>
<version>1.2.0</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
""",
"""
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>jakarta.activation</groupId>
<artifactId>jakarta.activation-api</artifactId>
<version>1.2.2</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
"""
)
);
}
}

0 comments on commit dab9144

Please sign in to comment.