Skip to content

Commit

Permalink
Add sanity checks (#711)
Browse files Browse the repository at this point in the history
* More robust version parsing

Based on what we've seen in real cases, e.g versions which start
with a dot.

* Make sure versions are starting with a digit
  • Loading branch information
melix authored Jul 18, 2024
1 parent 4616960 commit c06c787
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ class MicronautBasePlugin implements Plugin<Project> {
}

private void configureProjectVersion(Project project) {
project.version = project.providers.gradleProperty("projectVersion").orElse("undefined").get()
def version = project.providers.gradleProperty("projectVersion").orElse("undefined").get()
if (version.isEmpty() || !Character.isDigit(version.charAt(0))) {
throw new IllegalArgumentException("Version '" + version + "' is not a valid Micronaut version. It must start with a digit.")
}
project.version = version
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ private void updateCatalog(File inputCatalog, File outputCatalog, File logFile)
.cacheDynamicVersionsFor(0, TimeUnit.MINUTES);
var rejectedQualifiers = getRejectedQualifiers().get()
.stream()
.map(qualifier -> Pattern.compile("(?i).*[.-]" + qualifier + "[.\\d-+]*"))
.map(qualifier -> Pattern.compile("(?i)" + qualifier + "[.\\d-+]*"))
.toList();
var rejectedVersionsPerModule = getRejectedVersionsPerModule().get();
var ignoredModules = getIgnoredModules().get();
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/io/micronaut/build/utils/VersionParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public class VersionParser {

public static ComparableVersion parse(String version) {
try {
var matcher = SEMANTIC_VERSION.matcher(version);
var matchVersion = version.startsWith(".") ? "0" + version : version;
var matcher = SEMANTIC_VERSION.matcher(matchVersion);
if (matcher.find()) {
var major = matcher.group("major");
var minor = matcher.group("minor");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ class VersionParserTest extends Specification {
"1.5.7.45.0.1" | 1 | 5 | 7 | null | null | [45, 0, 1] | [1, 5, 7, 45, 0, 1]
"1.5.7.45.0.1-SNAPSHOT" | 1 | 5 | 7 | "SNAPSHOT" | null | [45, 0, 1] | [1, 5, 7, 45, 0, 1]
"1.1.0-9f31d6308e7ebbc3d7904b64ebb9f61f7e22a968" | 1 | 1 | 0 | null | null | [] | [1, 1, 0]
// The next items are not semantic versions, and we do best effort to return something reasonable. For example, if we considered
// that the qualifier for "1.0.0-jdk8" is "jdk8", then how do you make a difference with "1.2.0-beta8", where actually the qualifier is "beta"
// and the qualifier version is "8"? Instead of adding a dozen special cases, the parser will simply do best effort.
"2.2-pre-release-emit-jdk8-version.1" | 2 | 2 | null | "pre" | null | [] | [2, 2, 0]
".8.2" | 0 | 8 | 2 | null | null | [] | [0, 8, 2]
}

def "compares versions"() {
Expand Down

0 comments on commit c06c787

Please sign in to comment.