Skip to content

Commit

Permalink
fix: fixes the version number discrepancy for gradle (#107)
Browse files Browse the repository at this point in the history
## Description

> fixes the version number discrepancy between manifest file and html
report

**Related issue (if any):** fixes [TC-1558922782
](https://issues.redhat.com/browse/TC-1558)

## Checklist

- [x ] I have followed this repository's contributing guidelines.
- [x ] I will adhere to the project's code of conduct.

## Additional information

> Anything else?

---------

Signed-off-by: Olga Lavtar <[email protected]>
  • Loading branch information
olavtar authored Jul 2, 2024
1 parent 711b039 commit 1146394
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 1,516 deletions.
58 changes: 56 additions & 2 deletions src/main/java/com/redhat/exhort/providers/GradleProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -263,15 +263,69 @@ private Sbom buildSbomFromTextFormat(
}
}
// remove duplicates for component analysis
if (List.of("api", "implementation", "compile").contains(configName)) {
if (List.of("api", "implementation", "compileOnly").contains(configName)) {
removeDuplicateIfExists(arrayForSbom, textFormatFile);
arrayForSbom = performManifestVersionsCheck(arrayForSbom, textFormatFile);
}

String[] array = arrayForSbom.toArray(new String[0]);
parseDependencyTree(root, 0, array, sbom);
return sbom;
}

private List<String> performManifestVersionsCheck(List<String> arrayForSbom, Path textFormatFile)
throws IOException {

List<String> runtimeClasspathLines = extractLines(textFormatFile, "runtimeClasspath");
Map<String, String> runtimeClasspathVersions = parseDependencyVersions(runtimeClasspathLines);
List<String> updatedLines = updateDependencies(arrayForSbom, runtimeClasspathVersions);

return updatedLines;
}

private Map<String, String> parseDependencyVersions(List<String> lines) {
Map<String, String> dependencyVersions = new HashMap<>();

for (String line : lines) {
if (line.contains("->")) {
String[] splitLine = line.split("---");
if (splitLine.length > 1) {
String dependencyPart = splitLine[1].trim();
String[] parts = dependencyPart.split("-> ");
// Extract the dependency name (without the version) and the resolved version
String dependency = parts[0].substring(0, parts[0].lastIndexOf(':')).trim();
String version = parts[1].split(" ")[0].trim();
dependencyVersions.put(dependency, version);
}
}
}

return dependencyVersions;
}

private List<String> updateDependencies(
List<String> lines, Map<String, String> runtimeClasspathVersions) {
List<String> updatedLines = new ArrayList<>();
for (String line : lines) {
PackageURL packageURL = parseDep(line);
String[] parts = line.split(":");
if (parts.length >= 4) {
String dependencyKey =
packageURL.getNamespace() + ":" + packageURL.getName(); // Extract dependency key
if (runtimeClasspathVersions.containsKey(dependencyKey)) {
String newVersion = runtimeClasspathVersions.get(dependencyKey);
parts[3] = newVersion; // Replace version with the resolved version
updatedLines.add(String.join(":", parts));
} else {
updatedLines.add(line); // Keep the original line if no update is needed
}
} else {
updatedLines.add(line); // Keep the original line if it doesn't match the expected pattern
}
}
return updatedLines;
}

private void removeDuplicateIfExists(List<String> arrayForSbom, Path theContent) {
Consumer<String> removeDuplicateFunction =
dependency -> {
Expand Down Expand Up @@ -413,7 +467,7 @@ public Content provideComponent(Path manifestPath) throws IOException {
Path tempFile = getDependencies(manifestPath);
Map<String, String> propertiesMap = extractProperties(manifestPath);

String[] configurationNames = {"api", "implementation", "compile"};
String[] configurationNames = {"api", "implementation", "compileOnly", "runtimeOnly"};

String configName = null;
for (String configurationName : configurationNames) {
Expand Down
Loading

0 comments on commit 1146394

Please sign in to comment.