Skip to content

Commit 038ae0a

Browse files
authored
Convert extracted go module versions to KB compatible versions (#1578)
* use kb compatible versions with go mod file * Updated docs for go mod file detector
1 parent d1e8779 commit 038ae0a

File tree

4 files changed

+57
-1
lines changed

4 files changed

+57
-1
lines changed

detectable/src/main/java/com/blackduck/integration/detectable/detectables/go/gomodfile/parse/GoModFileParser.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.blackduck.integration.detectable.detectables.go.gomodfile.parse.model.GoModFileContent;
88
import com.blackduck.integration.detectable.detectables.go.gomodfile.parse.model.GoModuleInfo;
99
import com.blackduck.integration.detectable.detectables.go.gomodfile.parse.model.GoReplaceDirective;
10+
import com.blackduck.integration.detectable.util.KBComponentHelpers;
1011

1112
import java.util.ArrayList;
1213
import java.util.Arrays;
@@ -28,6 +29,7 @@
2829
*/
2930
public class GoModFileParser {
3031
private final Logger logger = LoggerFactory.getLogger(this.getClass());
32+
private final KBComponentHelpers kbComponentHelpers = new KBComponentHelpers();
3133

3234
// Regular expressions for parsing different sections
3335
private static final Pattern MODULE_PATTERN = Pattern.compile("^module\\s+(.+)$");
@@ -252,6 +254,7 @@ private GoModuleInfo parseDependencyLine(String line) {
252254

253255
// Clean up version (remove +incompatible, %2Bincompatible suffixes)
254256
version = cleanVersion(version);
257+
version = kbComponentHelpers.getKbCompatibleVersion(version);
255258

256259
// Check if it's an indirect dependency
257260
boolean isIndirect = comment != null && comment.contains("indirect");
@@ -264,6 +267,7 @@ private GoModuleInfo parseDependencyLine(String line) {
264267
if (parts.length >= 1) {
265268
String moduleName = parts[0];
266269
String version = parts.length > 1 ? cleanVersion(parts[1]) : "";
270+
version = kbComponentHelpers.getKbCompatibleVersion(version);
267271
boolean isIndirect = line.contains("// indirect");
268272
return new GoModuleInfo(moduleName, version, isIndirect);
269273
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.blackduck.integration.detectable.util;
2+
3+
import java.util.Optional;
4+
import java.util.regex.Matcher;
5+
import java.util.regex.Pattern;
6+
7+
import org.apache.commons.lang3.StringUtils;
8+
9+
public class KBComponentHelpers {
10+
11+
private static final String INCOMPATIBLE_SUFFIX = "+incompatible";
12+
private static final String SHA1_REGEX = "[a-fA-F0-9]{40}";
13+
private static final String SHORT_SHA1_REGEX = "[a-fA-F0-9]{12}";
14+
private static final String GIT_VERSION_FORMAT = ".*(%s).*";
15+
private static final Pattern SHA1_VERSION_PATTERN = Pattern.compile(String.format(GIT_VERSION_FORMAT, SHA1_REGEX));
16+
private static final Pattern SHORT_SHA1_VERSION_PATTERN = Pattern.compile(String.format(GIT_VERSION_FORMAT, SHORT_SHA1_REGEX));
17+
18+
public String getKbCompatibleVersion(String version) {
19+
String kbCompatibleVersion;
20+
kbCompatibleVersion = handleGitHash(version);
21+
kbCompatibleVersion = removeIncompatibleSuffix(kbCompatibleVersion);
22+
return kbCompatibleVersion;
23+
}
24+
25+
// When a version contains a commit hash, the KB only accepts the git hash, so we must strip out the rest.
26+
private String handleGitHash(String version) {
27+
return getVersionFromPattern(version, SHA1_VERSION_PATTERN)
28+
.orElseGet(() ->
29+
getVersionFromPattern(version, SHORT_SHA1_VERSION_PATTERN)
30+
.orElse(version)
31+
);
32+
}
33+
34+
private Optional<String> getVersionFromPattern(String version, Pattern versionPattern) {
35+
Matcher matcher = versionPattern.matcher(version);
36+
if (matcher.matches()) {
37+
return Optional.ofNullable(StringUtils.trim(matcher.group(1)));
38+
}
39+
return Optional.empty();
40+
}
41+
42+
// https://golang.org/ref/mod#incompatible-versions
43+
private String removeIncompatibleSuffix(String version) {
44+
if (version.endsWith(INCOMPATIBLE_SUFFIX)) {
45+
// Trim incompatible suffix so that KB can match component
46+
version = version.substring(0, version.length() - INCOMPATIBLE_SUFFIX.length());
47+
}
48+
return version;
49+
}
50+
51+
}

documentation/src/main/markdown/currentreleasenotes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
* Added a new property [detect.go.forge](properties/detectors/go.md#go-forge-url) to customize the Go registry URL used for fetching dependency information. Defaults to `https://proxy.golang.org`.
3030
* Added a new property [detect.go.forge.connection.timeout](properties/detectors/go.md#go-forge-connection-timeout) to customize the connection timeout limit while connecting to the Go registry. Defaults to 30 seconds.
3131
* Added a new property [detect.go.forge.read.timeout](properties/detectors/go.md#go-forge-read-timeout) to customize the read timeout limit while fetching go.mod file of a dependency from Go registry. Defaults to 60 seconds.
32+
* Dependency exclusions (via [detect.go.mod.dependency.types.excluded](properties/detectors/go.md#go-mod-dependency-types-excluded) property) are not supported by this detector.
3233

3334
### Changed features
3435

documentation/src/main/markdown/packagemgrs/golang.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ the [go mod why documentation](https://go.dev/ref/mod#go-mod-why) for additional
4444
* Attempts to run on your project if a go.mod file is found in your source directory.
4545
* Parses go.mod file to gather dependency information.
4646
* Computes transitive dependency graph by fetching go.mod files of direct dependencies from proxy.golang.org or custom Go proxy supplied via [detect.go.forge](../properties/detectors/go.md#go-forge) property. If the proxy is not reachable, the transitive dependencies are listed as additional components under the root module.
47-
* Dependency exclusions (unused, vendored) are supported in this detector.
47+
* Dependency exclusions (via [detect.go.mod.dependency.types.excluded](../properties/detectors/go.md#go-mod-dependency-types-excluded) property) are not supported by this detector.
4848

4949
## Go Lock (GO_DEP) detector
5050

0 commit comments

Comments
 (0)