forked from CycloneDX/cdxgen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
postgen.js
92 lines (91 loc) · 2.62 KB
/
postgen.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
export const postProcess = (bomNSData, options) => {
let jsonPayload = bomNSData.bomJson;
if (
typeof bomNSData.bomJson === "string" ||
bomNSData.bomJson instanceof String
) {
jsonPayload = JSON.parse(bomNSData.bomJson);
}
bomNSData.bomJson = filterBom(jsonPayload, options);
return bomNSData;
};
export const filterBom = (bomJson, options) => {
const newPkgMap = {};
let filtered = false;
for (const comp of bomJson.components) {
if (
options.requiredOnly &&
comp.scope &&
["optional", "excluded"].includes(comp.scope)
) {
filtered = true;
continue;
} else if (options.only && options.only.length) {
if (!Array.isArray(options.only)) {
options.only = [options.only];
}
let purlfiltered = false;
for (const filterstr of options.only) {
if (filterstr.length && !comp.purl.toLowerCase().includes(filterstr)) {
filtered = true;
purlfiltered = true;
continue;
}
}
if (!purlfiltered) {
newPkgMap[comp["bom-ref"]] = comp;
}
} else if (options.filter && options.filter.length) {
if (!Array.isArray(options.filter)) {
options.filter = [options.filter];
}
let purlfiltered = false;
for (const filterstr of options.filter) {
if (filterstr.length && comp.purl.toLowerCase().includes(filterstr)) {
filtered = true;
purlfiltered = true;
continue;
}
}
if (!purlfiltered) {
newPkgMap[comp["bom-ref"]] = comp;
}
} else {
newPkgMap[comp["bom-ref"]] = comp;
}
}
if (filtered) {
const newcomponents = [];
const newdependencies = [];
for (const aref of Object.keys(newPkgMap).sort()) {
newcomponents.push(newPkgMap[aref]);
}
for (const adep of bomJson.dependencies) {
if (newPkgMap[adep.ref]) {
const newdepson = (adep.dependsOn || []).filter((d) => newPkgMap[d]);
newdependencies.push({
ref: adep.ref,
dependsOn: newdepson
});
}
}
bomJson.components = newcomponents;
bomJson.dependencies = newdependencies;
// We set the compositions.aggregate to incomplete by default
if (
options.specVersion >= 1.5 &&
options.autoCompositions &&
bomJson.metadata &&
bomJson.metadata.component
) {
if (!bomJson.compositions) {
bomJson.compositions = [];
}
bomJson.compositions.push({
"bom-ref": bomJson.metadata.component["bom-ref"],
aggregate: options.only ? "incomplete_first_party_only" : "incomplete"
});
}
}
return bomJson;
};