Skip to content

Commit

Permalink
fix: add and fix missing configurations for gradle ca
Browse files Browse the repository at this point in the history
retrofits RHEcosystemAppEng/exhort-javascript-api#144 to exhort-java-api
fixes: https://issues.redhat.com/browse/TC-1557

Signed-off-by: Zvi Grinberg <zgrinber@redhat.com>
  • Loading branch information
zvigrinberg committed Jul 29, 2024
1 parent 1146394 commit a683dfc
Showing 1 changed file with 16 additions and 19 deletions.
35 changes: 16 additions & 19 deletions src/main/java/com/redhat/exhort/providers/GradleProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.function.Consumer;
import java.util.logging.Logger;
import java.util.regex.Matcher;
Expand All @@ -49,6 +46,9 @@
*/
public final class GradleProvider extends BaseJavaProvider {

public static final String[] COMPONENT_ANALYSIS_CONFIGURATIONS = {
"api", "implementation", "compileOnlyApi", "compileOnly", "runtimeOnly"
};
private Logger log = LoggersFactory.getLogger(this.getClass().getName());

public GradleProvider() {
Expand All @@ -67,7 +67,7 @@ public Content provideStack(final Path manifestPath) throws IOException {
}
Map<String, String> propertiesMap = extractProperties(manifestPath);

var sbom = buildSbomFromTextFormat(tempFile, propertiesMap, "runtimeClasspath");
var sbom = buildSbomFromTextFormat(tempFile, propertiesMap, new String[] {"runtimeClasspath"});
var ignored = getIgnoredDeps(manifestPath);

return new Content(
Expand Down Expand Up @@ -241,14 +241,20 @@ private Path getProperties(Path manifestPath) throws IOException {
}

private Sbom buildSbomFromTextFormat(
Path textFormatFile, Map<String, String> propertiesMap, String configName)
Path textFormatFile, Map<String, String> propertiesMap, String[] configNames)
throws IOException {
var sbom = SbomFactory.newInstance(Sbom.BelongingCondition.PURL, "sensitive");
String root = getRoot(textFormatFile, propertiesMap);

var rootPurl = parseDep(root);
sbom.addRoot(rootPurl);
List<String> lines = extractLines(textFormatFile, configName);
List<String> lines = new ArrayList<>();

for (String configName : configNames) {
List<String> deps = extractLines(textFormatFile, configName);
lines.addAll(deps);
}

List<String> arrayForSbom = new ArrayList<>();

for (String line : lines) {
Expand All @@ -263,7 +269,7 @@ private Sbom buildSbomFromTextFormat(
}
}
// remove duplicates for component analysis
if (List.of("api", "implementation", "compileOnly").contains(configName)) {
if (Arrays.equals(configNames, COMPONENT_ANALYSIS_CONFIGURATIONS)) {
removeDuplicateIfExists(arrayForSbom, textFormatFile);
arrayForSbom = performManifestVersionsCheck(arrayForSbom, textFormatFile);
}
Expand Down Expand Up @@ -467,20 +473,11 @@ public Content provideComponent(Path manifestPath) throws IOException {
Path tempFile = getDependencies(manifestPath);
Map<String, String> propertiesMap = extractProperties(manifestPath);

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

String configName = null;
for (String configurationName : configurationNames) {
List<String> directDependencies = extractLines(tempFile, configurationName);

// Check if dependencies are found for the current configuration
if (!directDependencies.isEmpty()) {
configName = configurationName;
break;
}
}

var sbom = buildSbomFromTextFormat(tempFile, propertiesMap, configName);
var sbom = buildSbomFromTextFormat(tempFile, propertiesMap, configurationNames);
var ignored = getIgnoredDeps(manifestPath);

return new Content(
Expand Down

0 comments on commit a683dfc

Please sign in to comment.