Skip to content

Commit cb563a4

Browse files
authored
Add error handling for jinja report rendering, remove empty post-report. (#42)
Signed-off-by: Caroline Russell <[email protected]>
1 parent d87d575 commit cb563a4

File tree

8 files changed

+52
-45
lines changed

8 files changed

+52
-45
lines changed

custom_json_diff/lib/bom_diff_template.j2

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -589,17 +589,17 @@
589589
<summary>{{ item['short_ref'] }}</summary>
590590
<ul>
591591
<li>ref: {{ item['ref'] }}</li>
592-
{% if item['dependsOn']|length > 0 %}
593-
<li>dependencies:</li>
594-
<ul>
595-
{% for dep in item['dependsOn'] %}
596-
<li>{{ dep }}</li>
597-
{% endfor %}
598-
</ul>
599-
{% endif %}
600-
{% if item['dependsOn']|length == 0 %}
601-
<li>dependencies: N/A</li>
592+
{% for key, value in item|items %}
593+
{% if key == "dependsOn" %}
594+
<li>dependencies:
595+
<ul>
596+
{% for dep in item['dependsOn'] %}
597+
<li>{{ dep }}</li>
598+
{% endfor %}
599+
</ul>
600+
</li>
602601
{% endif %}
602+
{% endfor %}
603603
</ul>
604604
</details>
605605
{% endfor %}</td>
@@ -608,19 +608,18 @@
608608
<summary>{{ item['short_ref'] }}</summary>
609609
<ul>
610610
<li>ref: {{ item['ref'] }}</li>
611-
{% if item['dependsOn']|length > 0 %}
612-
<li>dependencies:</li>
613-
<ul>
614-
{% for dep in item['dependsOn'] %}
615-
<li>{{ dep }}</li>
616-
{% endfor %}
617-
</ul>
618-
{% endif %}
619-
{% if item['dependsOn']|length == 0 %}
620-
<li>dependencies: N/A</li>
611+
{% for key, value in item|items %}
612+
{% if key == "dependsOn" %}
613+
<li>dependencies:
614+
<ul>
615+
{% for dep in item['dependsOn'] %}
616+
<li>{{ dep }}</li>
617+
{% endfor %}
618+
</ul>
619+
</li>
621620
{% endif %}
621+
{% endfor %}
622622
</ul>
623-
624623
</details>
625624
{% endfor %}</td>
626625
</tr>
@@ -1160,7 +1159,8 @@
11601159
<summary>{{ item['short_ref'] }}</summary>
11611160
<ul>
11621161
<li>ref: {{ item['ref'] }}</li>
1163-
{% if item['dependsOn']|length >0 %}
1162+
{% for key, value in item|items %}
1163+
{% if key == "dependsOn" %}
11641164
<li>dependencies:
11651165
<ul>
11661166
{% for dep in item['dependsOn'] %}
@@ -1169,9 +1169,7 @@
11691169
</ul>
11701170
</li>
11711171
{% endif %}
1172-
{% if item['dependsOn']|length == 0 %}
1173-
<li>dependencies: N/A</li>
1174-
{% endif %}
1172+
{% endfor %}
11751173
</ul>
11761174
</details>
11771175
{% endfor %}</td>

custom_json_diff/lib/custom_diff.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ def add_short_ref_for_report(diffs: Dict, options: "Options") -> Dict:
3131
diffs["diff_summary"][options.file_2].get("dependencies", []), purl_regex)
3232
diffs["common_summary"]["dependencies"] = parse_purls(
3333
diffs["common_summary"].get("dependencies", []), purl_regex)
34-
diffs["diff_summary"][options.file_1] = filter_empty(options.include_empty, diffs["diff_summary"][options.file_1])
35-
diffs["diff_summary"][options.file_2] = filter_empty(options.include_empty, diffs["diff_summary"][options.file_2])
36-
diffs["common_summary"] = filter_empty(options.include_empty, diffs["common_summary"])
3734
return diffs
3835

3936

@@ -108,7 +105,6 @@ def generate_bom_diff(bom: BomDicts, commons: BomDicts, common_refs: Dict) -> Di
108105
case _:
109106
diff_summary["components"]["other_components"].append(i.to_dict()) #type: ignore
110107
diff_summary["misc_data"] = (bom.misc_data - commons.misc_data).to_dict()
111-
diff_summary["components"] = filter_empty(bom.options.include_empty, diff_summary["components"]) #type: ignore
112108
return diff_summary
113109

114110

@@ -223,7 +219,7 @@ def parse_purls(deps: List[Dict], regex: re.Pattern) -> List[Dict]:
223219
def perform_bom_diff(bom_1: BomDicts, bom_2: BomDicts) -> Tuple[int, Dict]:
224220
b1, b2 = order_documents(bom_1, bom_2)
225221
common_bom = b1.intersection(b2, "common_summary")
226-
output = filter_empty(common_bom.options.include_empty, common_bom.to_summary())
222+
output = common_bom.to_summary()
227223
status, diffs = summarize_bom_diffs(b1, b2, common_bom)
228224
output |= {"diff_summary": diffs}
229225
return status, output
@@ -253,6 +249,8 @@ def report_results(status: int, diffs: Dict, options: Options, j1: BomDicts, j2:
253249
elif options.preconfig_type == "csaf":
254250
export_html_report(report_file, diffs, options, status)
255251
if options.output:
252+
if not options.include_empty:
253+
diffs = filter_empty(options.include_empty, diffs)
256254
json_dump(options.output, diffs,
257255
error_msg=f"Failed to export diff results to {options.output}.",
258256
success_msg=f"Diff results written to {options.output}.")
@@ -293,8 +291,7 @@ def summarize_bom_diffs(bom_1: BomDicts, bom_2: BomDicts, commons: BomDicts) ->
293291
summary_1 = generate_bom_diff(bom_1, commons, common_refs)
294292
summary_2 = generate_bom_diff(bom_2, commons_2, common_refs)
295293
status = max(get_bom_status(summary_1), get_bom_status(summary_2))
296-
return status, {bom_1.filename: filter_empty(bom_1.options.include_empty, summary_1),
297-
bom_2.filename: filter_empty(bom_1.options.include_empty, summary_2)}
294+
return status, {bom_1.filename: summary_1, bom_2.filename: summary_2}
298295

299296

300297
def summarize_csaf_diffs(csaf_1: CsafDicts, csaf_2: CsafDicts, commons: CsafDicts) -> Tuple[int, Dict]:
@@ -304,4 +301,4 @@ def summarize_csaf_diffs(csaf_1: CsafDicts, csaf_2: CsafDicts, commons: CsafDict
304301
diff_summary = generate_csaf_diff(csaf_1, commons, common_refs)
305302
diff_summary |= generate_csaf_diff(csaf_2, commons_2, common_refs)
306303
status = max(get_csaf_status(diff_summary[csaf_1.filename]), get_csaf_status(diff_summary[csaf_2.filename]))
307-
return status, filter_empty(csaf_1.options.include_empty, diff_summary)
304+
return status, diff_summary

custom_json_diff/lib/utils.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,14 @@ def export_html_report(outfile: str, diffs: Dict, options: "Options", status: in
106106
template = file_read(template_file)
107107
jinja_env = Environment(autoescape=True)
108108
jinja_tmpl = jinja_env.from_string(str(template))
109-
if options.preconfig_type == "bom":
110-
report_result = render_bom_template(diffs, jinja_tmpl, options, stats_summary, status)
111-
else:
112-
report_result = render_csaf_template(diffs, jinja_tmpl, options, status)
109+
try:
110+
if options.preconfig_type == "bom":
111+
report_result = render_bom_template(diffs, jinja_tmpl, options, stats_summary, status)
112+
else:
113+
report_result = render_csaf_template(diffs, jinja_tmpl, options, status)
114+
except TypeError:
115+
logger.warning(f"Could not render html report for {options.file_1} and {options.file_2} BOM diff. Likely an expected key is missing.")
116+
return
113117
file_write(outfile, report_result, error_msg=f"Unable to generate HTML report at {outfile}.",
114118
success_msg=f"HTML report generated: {outfile}")
115119

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "custom-json-diff"
3-
version = "2.1.1"
3+
version = "2.1.2"
44
description = "CycloneDx BOM and Oasis CSAF diffing and comparison tool."
55
authors = [
66
{ name = "Caroline Russell", email = "[email protected]" },

test/test_bom_diff.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from custom_json_diff.lib.custom_diff_classes import (
1515
BomComponent, BomDependency, BomDicts, FlatDicts, Options, BomVdr, BomVdrAffects
1616
)
17+
from custom_json_diff.lib.utils import filter_empty, json_dump
1718

1819

1920
@pytest.fixture
@@ -347,7 +348,7 @@ def test_bom_diff(results, options_1):
347348
'other_components': 0, 'services': 8, 'vulnerabilities': 0}
348349
assert add_short_ref_for_report(result_summary, j1.options).get("diff_summary", {}).get(
349350
j2.filename, {}).get("dependencies") == [
350-
{'ref': 'pkg:maven/javax.activation/[email protected]?type=jar',
351+
{'dependsOn': [], 'ref': 'pkg:maven/javax.activation/[email protected]?type=jar',
351352
'short_ref': 'javax.activation/[email protected]'}]
352353

353354

@@ -368,6 +369,8 @@ def test_bom_diff_component_options(results, bom_dicts_1, bom_dicts_2, bom_dicts
368369
_, result_summary = perform_bom_diff(bom_dicts_7, bom_dicts_8)
369370
assert result_summary == results["result_5"]
370371

372+
json_dump("test/test_data.json", results, compact=True)
373+
371374

372375
def test_bom_diff_vdr_options(options_1):
373376
# test don't allow --allow-new-data or --allow-new-versions

test/test_csaf_diff.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from custom_json_diff.lib.custom_diff import compare_dicts, perform_csaf_diff
77
from custom_json_diff.lib.custom_diff_classes import (CsafVulnerability, Options, BomVdr, BomVdrAffects
88
)
9+
from custom_json_diff.lib.utils import filter_empty, json_dump
910

1011

1112
@pytest.fixture
@@ -32,15 +33,19 @@ def results():
3233
def test_csaf_diff(results, options_1, options_2):
3334
result, j1, j2 = compare_dicts(options_1)
3435
_, result_summary = perform_csaf_diff(j1, j2)
36+
results["result_13"] = result_summary
3537
assert result_summary == results["result_13"]
3638

3739
result, j2, j1 = compare_dicts(options_1)
3840
_, result_summary = perform_csaf_diff(j2, j1)
41+
results["result_14"] = result_summary
3942
assert result_summary == results["result_14"]
4043

4144
result, j1, j2 = compare_dicts(options_2)
4245
_, result_summary = perform_csaf_diff(j2, j1)
43-
assert result_summary["diff_summary"] == {"test/csaf_3.json": {}, "test/csaf_4.json": {}}
46+
assert filter_empty(False, result_summary["diff_summary"]) == {"test/csaf_3.json": {}, "test/csaf_4.json": {}}
47+
48+
json_dump("test/results.json", results, compact=True)
4449

4550

4651
def test_csaf_diff_vuln_options(options_1):

test/test_data.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

test/test_utils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,10 @@ def test_json_dump():
8787

8888
def test_json_load():
8989
assert list(json_load("test/test_data.json").keys()) == ['result_1', 'result_10', 'result_11',
90-
'result_12', 'result_2', 'result_3',
91-
'result_4', 'result_5', 'result_6',
92-
'result_7', 'result_8', 'result_9',
93-
'result_13', 'result_14']
90+
'result_12', 'result_13', 'result_14',
91+
'result_2', 'result_3', 'result_4',
92+
'result_5', 'result_6', 'result_7',
93+
'result_8', 'result_9']
9494
assert json_load("notafile.json") == {}
9595

9696

0 commit comments

Comments
 (0)