Skip to content

Commit

Permalink
Fix go version extractor to accept valid semver with 2 dashes
Browse files Browse the repository at this point in the history
  • Loading branch information
nadav-y committed Jan 14, 2025
1 parent 530dd55 commit 4eee4b9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package org.jfrog.build.extractor.go.extractor;

import com.github.zafarkhaja.semver.Version;
import org.apache.commons.lang3.StringUtils;
import org.jfrog.build.api.util.Log;

import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Optional;

/**
* @author BarakH
Expand All @@ -14,30 +14,24 @@ public class GoVersionUtils {

public static final String INCOMPATIBLE = "+incompatible";
public static final int ZERO_OR_ONE = 0;
// The regular expression used here is derived from the SemVer specification: https://semver.org/
protected static final Pattern VERSION_PATTERN = Pattern.compile(
"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-]*)(?:\\." +
"(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\\+([0-9a-zA-Z-]+(?:\\.[0-9a-zA-Z-]+)*))?$");

private GoVersionUtils() {
}

/**
* @param version full version string
* @return The major version as an integer or 0 if couldn't parse it
*/
public static int getMajorVersion(String version, Log log) {
public static long getMajorVersion(String version, Log log) {
if (StringUtils.isEmpty(version)) {
return 0;
}
version = getCleanVersion(version);
Matcher matcher = VERSION_PATTERN.matcher(version);
if (matcher.matches()) {
String major = matcher.group(1);
if (!StringUtils.isEmpty(major)) {
try {
return Integer.parseInt(major);
} catch (NumberFormatException e) {
log.error("Failed to parse major version of " + version, e);
}
}
Optional<Version> parsedVersion = Version.tryParse(StringUtils.removeStart(version, "v"));
if (parsedVersion.isPresent()) {
return parsedVersion.get().majorVersion();
} else {
log.debug("Failed to parse major version of " + version);
}
return 0;
}
Expand Down Expand Up @@ -89,7 +83,7 @@ public static boolean isCompatibleGoModuleNaming(String projectName, String vers
if (StringUtils.isBlank(projectName) || StringUtils.isBlank(version)) {
return false;
}
int majorVersion = getMajorVersion(version, log);
long majorVersion = getMajorVersion(version, log);
if (majorVersion >= 2) {
return (projectName.endsWith("/v" + majorVersion) && !version.endsWith(INCOMPATIBLE));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public void getMajorVersion_ValidVersion_ReturnsMajorVersion() {
assertEquals(GoVersionUtils.getMajorVersion("v1.2.3----RC-SNAPSHOT.12.9.1--.12+788", log), 1);
assertEquals(GoVersionUtils.getMajorVersion("v1.2.3----R-S.12.9.1--.12+meta", log), 1);
assertEquals(GoVersionUtils.getMajorVersion("v2.2.0-beta.1", log), 2);
assertEquals(GoVersionUtils.getMajorVersion("v2.0.0-beta-1", log), 2);
}

@Test
Expand Down
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,10 @@ project('build-info-extractor-docker') {

project('build-info-extractor-go') {
description = 'JFrog Build-Info Go Extractor'

dependencies {
implementation group: 'com.github.zafarkhaja', name: 'java-semver', version: '0.10.2'
}
}

project('build-info-extractor-pip') {
Expand Down

0 comments on commit 4eee4b9

Please sign in to comment.