Skip to content

Commit

Permalink
Catch non-semver compatible exception in case of version update (#4656)
Browse files Browse the repository at this point in the history
* Catch the exception that gets thrown in case of a non semver version number

* Catch the exception that gets thrown in case of a non semver version number

Fixes #4655

* Catch the exception that gets thrown in case of a non semver version number

Fixes #4655
  • Loading branch information
Jenson3210 authored Nov 11, 2024
1 parent 24b41bb commit 4391062
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,19 +134,24 @@ public class DependencyVersionSelector {
"current version.");
}

VersionComparator versionComparator = StringUtils.isBlank(version) ?
new LatestRelease(versionPattern) :
requireNonNull(Semver.validate(version, versionPattern).getValue());
try {
VersionComparator versionComparator = StringUtils.isBlank(version) ?
new LatestRelease(versionPattern) :
requireNonNull(Semver.validate(version, versionPattern).getValue());

if (versionComparator instanceof ExactVersion) {
return versionComparator.upgrade(gav.getVersion(), singletonList(version)).orElse(null);
} else if (versionComparator instanceof LatestPatch &&
!versionComparator.isValid(gav.getVersion(), gav.getVersion())) {
// in the case of "latest.patch", a new version can only be derived if the
// current version is a semantic version
if (versionComparator instanceof ExactVersion) {
return versionComparator.upgrade(gav.getVersion(), singletonList(version)).orElse(null);
} else if (versionComparator instanceof LatestPatch &&
!versionComparator.isValid(gav.getVersion(), gav.getVersion())) {
// in the case of "latest.patch", a new version can only be derived if the
// current version is a semantic version
return null;
} else {
return findNewerVersion(gav, configuration, versionComparator, ctx).orElse(null);
}
} catch (IllegalStateException e) {
// this can happen when we encounter exotic versions
return null;
} else {
return findNewerVersion(gav, configuration, versionComparator, ctx).orElse(null);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1049,4 +1049,27 @@ void dependenciesBlockInFreestandingScript() {
)
);
}

@Test
@Issue("https://github.com/openrewrite/rewrite/issues/4655")
void issue4655() {
rewriteRun(
buildGradle(
"""
plugins {
id 'java-library'
}
repositories {
mavenCentral()
}
version='ORC-246-1-SNAPSHOT'
dependencies {
implementation "com.veon.eurasia.oraculum:jira-api:$version"
}
"""
)
);
}
}

0 comments on commit 4391062

Please sign in to comment.