Skip to content

Commit 9c457f8

Browse files
authored
Annotate_vdr: fix reading metadata.get("tools", {}).get("components", {}), create vdr file in report folder (#441)
* Fix annotate_vdr to get components only if the section exists Signed-off-by: michelar68 <[email protected]> * Set report folder path for vdr file Signed-off-by: michelar68 <[email protected]> * ruff format * Ruff format on cli.py --------- Signed-off-by: michelar68 <[email protected]>
1 parent 5cf736b commit 9c457f8

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

depscan/cli.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
from depscan.lib.logger import DEBUG, LOG, SPINNER, console, IS_CI
5656

5757
from reporting_lib.htmlgen import ReportGenerator
58+
5859
if sys.platform == "win32" and os.environ.get("PYTHONIOENCODING") is None:
5960
sys.stdin.reconfigure(encoding="utf-8")
6061
sys.stdout.reconfigure(encoding="utf-8")
@@ -101,6 +102,7 @@ def vdr_analyze_summarize(
101102
scoped_pkgs,
102103
bom_file,
103104
bom_dir,
105+
reports_dir,
104106
pkg_list,
105107
reachability_analyzer,
106108
reachability_options,
@@ -116,6 +118,7 @@ def vdr_analyze_summarize(
116118
:param scoped_pkgs: Dict containing package scopes.
117119
:param bom_file: Single BOM file.
118120
:param bom_dir: Directory containining bom files.
121+
:param reports_dir: Directory containining report files.
119122
:param pkg_list: Direct list of packages when the bom file is empty.
120123
:param reachability_analyzer: Reachability Analyzer specified.
121124
:param reachability_options: Reachability Analyzer options.
@@ -166,7 +169,11 @@ def vdr_analyze_summarize(
166169
)
167170
ds_version = get_version()
168171
vdr_result = VDRAnalyzer(vdr_options=options).process()
169-
vdr_file = bom_file.replace(".cdx.json", ".vdr.json") if bom_file else None
172+
# Set vdr_file in report folder
173+
vdr_file = (
174+
os.path.join(reports_dir, os.path.basename(bom_file)) if bom_file else None
175+
)
176+
vdr_file = vdr_file.replace(".cdx.json", ".vdr.json") if vdr_file else None
170177
if not vdr_file and bom_dir:
171178
vdr_file = os.path.join(bom_dir, DEPSCAN_DEFAULT_VDR_FILE)
172179
if vdr_result.success:
@@ -931,6 +938,7 @@ def run_depscan(args):
931938
scoped_pkgs=scoped_pkgs,
932939
bom_file=bom_files[0] if len(bom_files) == 1 else None,
933940
bom_dir=args.bom_dir,
941+
reports_dir=args.reports_dir,
934942
pkg_list=pkg_list,
935943
reachability_analyzer=reachability_analyzer,
936944
reachability_options=reachability_options,
@@ -973,7 +981,11 @@ def run_depscan(args):
973981
)
974982
console.save_text(txt_report_file, clear=False)
975983
# Prettify the rich html report
976-
html_report_generator = ReportGenerator(input_rich_html_path=html_report_file, report_output_path=html_report_file, raw_content=False)
984+
html_report_generator = ReportGenerator(
985+
input_rich_html_path=html_report_file,
986+
report_output_path=html_report_file,
987+
raw_content=False,
988+
)
977989
html_report_generator.parse_and_generate_report()
978990
# This logic needs refactoring
979991
# render report into template if wished

depscan/lib/bom.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,11 @@ def annotate_vdr(vdr_file, txt_report_file):
556556
return
557557
vdr = json_load(vdr_file)
558558
metadata = vdr.get("metadata", {})
559-
tools = metadata.get("tools", {}).get("components", {})
559+
# Some cyclonedx sbom don't containg tools.components
560+
if "components" in metadata.get("tools"):
561+
tools = metadata.get("tools", {}).get("components", {})
562+
else:
563+
tools = {}
560564
with open(txt_report_file, errors="ignore", encoding="utf-8") as txt_fp:
561565
report = txt_fp.read()
562566
annotations = vdr.get("annotations", []) or []

0 commit comments

Comments
 (0)