From ce6c7dc0f31a1a81301972915dfcac9603a2076a Mon Sep 17 00:00:00 2001 From: jperezdealgaba <124347725+jperezdealgaba@users.noreply.github.com> Date: Thu, 15 Feb 2024 12:15:39 -0800 Subject: [PATCH] snyk: added snyk stats to metadata Related: https://issues.redhat.com/browse/OSH-347 Reproducer: csmock -t snyk --force -r rhel-8-x86_64 osbuild-106-1.el10+4.src.rpm Added the stats from snyk results (snyk coverage rate, analyzed files and total of files) to the metadata file. --- py/common/snyk.py | 29 +++++++++++++++++++++++++++++ py/plugins/snyk.py | 8 +++++++- 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 py/common/snyk.py diff --git a/py/common/snyk.py b/py/common/snyk.py new file mode 100644 index 0000000..deebc44 --- /dev/null +++ b/py/common/snyk.py @@ -0,0 +1,29 @@ +# standard imports +import json + + +def snyk_write_analysis_meta(results, results_file): + try: + with open(results_file) as snyk_results_file: + data = json.load(snyk_results_file) + coverage_stats = data["runs"][0]["properties"]["coverage"] + total_files = 0 + supported_files = 0 + for lang in coverage_stats: + total_files += lang["files"] + if lang["type"] == "SUPPORTED": + supported_files += lang["files"] + + coverage_ratio = 0 + if total_files > 0: + coverage_ratio = int(supported_files * 100 / total_files) + + results.ini_writer.append("snyk-scanned-files-coverage", coverage_ratio) + results.ini_writer.append("snyk-scanned-files-success", supported_files) + results.ini_writer.append("snyk-scanned-files-total", total_files) + + return 0 + + except Exception as e: + results.error(f"snyk-scan: error parsing results from snyk-results.sarif file: {e}") + return 1 diff --git a/py/plugins/snyk.py b/py/plugins/snyk.py index c22f006..cdba5be 100644 --- a/py/plugins/snyk.py +++ b/py/plugins/snyk.py @@ -17,6 +17,8 @@ import os +from csmock.common.snyk import snyk_write_analysis_meta + # default URL to download snyk binary executable SNYK_BIN_URL = "https://static.snyk.io/cli/latest/snyk-linux" @@ -204,4 +206,8 @@ def filter_hook(results): cmd = FILTER_CMD % (src, dst) return results.exec_cmd(cmd, shell=True) - props.post_process_hooks += [filter_hook] + def write_snyk_stats_metadata(results): + results_file = results.dbgdir_raw + SNYK_OUTPUT + return snyk_write_analysis_meta(results, results_file) + + props.post_process_hooks += [write_snyk_stats_metadata, filter_hook]