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: fixes the version number discrepancy for gradle #107

Merged
merged 5 commits into from
Jul 2, 2024
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
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
Loading