Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix go version extractor to accept valid semver with 2 dashes #815

Merged
merged 1 commit into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
RobiNino marked this conversation as resolved.
Show resolved Hide resolved
}
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
Loading