Skip to content

Commit

Permalink
Merge pull request #1091 from thc202/sort-version-sbom
Browse files Browse the repository at this point in the history
  • Loading branch information
kingthorin authored Jan 9, 2024
2 parents 70c2693 + cc2c4c1 commit 8b2d2ab
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.snakeyaml.engine.v2.api.Dump;
import org.snakeyaml.engine.v2.api.DumpSettings;
import org.snakeyaml.engine.v2.api.RepresentToNode;
Expand Down Expand Up @@ -114,9 +115,9 @@ String getVersion() {
static class SbomData {
private final String bomFormat;
private final String downloadUrl;
private final List<SbomDataComponent> components;
private final Set<SbomDataComponent> components;

SbomData(String bomFormat, String downloadUrl, List<SbomDataComponent> components) {
SbomData(String bomFormat, String downloadUrl, Set<SbomDataComponent> components) {
this.bomFormat = bomFormat;
this.downloadUrl = downloadUrl;
this.components = components;
Expand All @@ -130,12 +131,12 @@ public String getDownloadUrl() {
return downloadUrl;
}

public List<SbomDataComponent> getComponents() {
public Set<SbomDataComponent> getComponents() {
return components;
}
}

static class SbomDataComponent {
static class SbomDataComponent implements Comparable<SbomDataComponent> {
private final String name;
private final String version;
private final String licenses;
Expand All @@ -158,6 +159,19 @@ public String getVersion() {
public String getLicenses() {
return licenses;
}

@Override
public int compareTo(SbomDataComponent o) {
int result = name.compareTo(o.name);
if (result != 0) {
return result;
}
result = version.compareTo(o.version);
if (result != 0) {
return result;
}
return licenses.compareTo(o.licenses);
}
}

private static class PageFrontMatterRepresenter extends StandardRepresenter {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

Expand All @@ -48,13 +47,9 @@ public static void generate(
throws Exception {
PageFrontMatter frontMatter = new PageFrontMatter("sbom", pageTitle, 1);
JsonNode bomJson = MAPPER.readTree(bomPath.toFile());
List<PageFrontMatter.SbomDataComponent> resultComponents = new ArrayList<>();
Set<PageFrontMatter.SbomDataComponent> resultComponents = new TreeSet<>();
var componentsJsonArray = (ArrayNode) bomJson.get("components");
List<JsonNode> sortedComponentsList =
StreamSupport.stream(componentsJsonArray.spliterator(), false)
.sorted(Comparator.comparing(jsonNode -> jsonNode.get("name").asText()))
.collect(Collectors.toList());
for (JsonNode component : sortedComponentsList) {
for (JsonNode component : componentsJsonArray) {
resultComponents.add(
new PageFrontMatter.SbomDataComponent(
component.get("name").asText(),
Expand Down

0 comments on commit 8b2d2ab

Please sign in to comment.