Skip to content

Commit 752126f

Browse files
committed
Fix go version extractor to accept valid semver with 2 dashes
1 parent 530dd55 commit 752126f

File tree

3 files changed

+15
-16
lines changed

3 files changed

+15
-16
lines changed

build-info-extractor-go/src/main/java/org/jfrog/build/extractor/go/extractor/GoVersionUtils.java

+10-16
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package org.jfrog.build.extractor.go.extractor;
22

3+
import com.github.zafarkhaja.semver.Version;
34
import org.apache.commons.lang3.StringUtils;
45
import org.jfrog.build.api.util.Log;
56

67
import java.io.File;
7-
import java.util.regex.Matcher;
8-
import java.util.regex.Pattern;
8+
import java.util.Optional;
99

1010
/**
1111
* @author BarakH
@@ -14,10 +14,9 @@ public class GoVersionUtils {
1414

1515
public static final String INCOMPATIBLE = "+incompatible";
1616
public static final int ZERO_OR_ONE = 0;
17-
// The regular expression used here is derived from the SemVer specification: https://semver.org/
18-
protected static final Pattern VERSION_PATTERN = Pattern.compile(
19-
"v(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)(?:-((?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\\." +
20-
"(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\\+([0-9a-zA-Z-]+(?:\\.[0-9a-zA-Z-]+)*))?$");
17+
18+
private GoVersionUtils() {
19+
}
2120

2221
/**
2322
* @param version full version string
@@ -28,16 +27,11 @@ public static int getMajorVersion(String version, Log log) {
2827
return 0;
2928
}
3029
version = getCleanVersion(version);
31-
Matcher matcher = VERSION_PATTERN.matcher(version);
32-
if (matcher.matches()) {
33-
String major = matcher.group(1);
34-
if (!StringUtils.isEmpty(major)) {
35-
try {
36-
return Integer.parseInt(major);
37-
} catch (NumberFormatException e) {
38-
log.error("Failed to parse major version of " + version, e);
39-
}
40-
}
30+
Optional<Version> parsedVersion = Version.tryParse(StringUtils.removeStart(version, "v"));
31+
if (parsedVersion.isPresent()) {
32+
return (int) parsedVersion.get().majorVersion();
33+
} else {
34+
log.debug("Failed to parse major version of " + version);
4135
}
4236
return 0;
4337
}

build-info-extractor-go/src/test/java/org/jfrog/build/extractor/go/extractor/GoVersionUtilsTest.java

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public void getMajorVersion_ValidVersion_ReturnsMajorVersion() {
3333
assertEquals(GoVersionUtils.getMajorVersion("v1.2.3----RC-SNAPSHOT.12.9.1--.12+788", log), 1);
3434
assertEquals(GoVersionUtils.getMajorVersion("v1.2.3----R-S.12.9.1--.12+meta", log), 1);
3535
assertEquals(GoVersionUtils.getMajorVersion("v2.2.0-beta.1", log), 2);
36+
assertEquals(GoVersionUtils.getMajorVersion("v2.0.0-beta-1", log), 2);
3637
}
3738

3839
@Test

build.gradle

+4
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,10 @@ project('build-info-extractor-docker') {
478478

479479
project('build-info-extractor-go') {
480480
description = 'JFrog Build-Info Go Extractor'
481+
482+
dependencies {
483+
implementation group: 'com.github.zafarkhaja', name: 'java-semver', version: '0.10.2'
484+
}
481485
}
482486

483487
project('build-info-extractor-pip') {

0 commit comments

Comments
 (0)