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

Handle missing license in SBOMs #1044

Merged
merged 1 commit into from
Oct 12, 2023
Merged
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
Expand Up @@ -55,21 +55,11 @@ public static void generate(
.sorted(Comparator.comparing(jsonNode -> jsonNode.get("name").asText()))
.collect(Collectors.toList());
for (JsonNode component : sortedComponentsList) {
var licenses = (ArrayNode) component.get("licenses");
String licensesStr =
StreamSupport.stream(licenses.spliterator(), false)
.map(l -> l.get("license"))
.map(
l ->
l.has("id")
? l.get("id").asText()
: l.has("name") ? l.get("name").asText() : "")
.collect(Collectors.joining(", "));
resultComponents.add(
new PageFrontMatter.SbomDataComponent(
component.get("name").asText(),
component.get("version").asText(),
licensesStr));
createLicensesString(component)));
}
frontMatter.setSbomData(
new PageFrontMatter.SbomData(
Expand All @@ -79,4 +69,35 @@ public static void generate(
frontMatter.writeTo(NOTICE, writer);
Files.write(outputFile, writer.toString().getBytes(StandardCharsets.UTF_8));
}

private static String createLicensesString(JsonNode component) {
var licenses = (ArrayNode) component.get("licenses");
if (licenses == null) {
return "";
}

return StreamSupport.stream(licenses.spliterator(), false)
.map(WebsiteSbomPageGenerator::licenseObjectToString)
.filter(e -> e != null)
.collect(Collectors.joining(", "));
}

private static String licenseObjectToString(JsonNode l) {
if (!l.has("license")) {
return get(l, "expression");
}
var license = l.get("license");
var id = get(license, "id");
if (id != null) {
return id;
}
return get(license, "name");
}

private static String get(JsonNode node, String property) {
if (node.has(property)) {
return node.get(property).asText();
}
return null;
}
}