Skip to content

Commit 802bbf3

Browse files
jperezdealgabakdudka
authored andcommitted
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.
1 parent b2c7201 commit 802bbf3

3 files changed

Lines changed: 60 additions & 1 deletion

File tree

py/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ install(FILES ${src_dir}/__init__.py DESTINATION ${dst_dir})
2727
install(FILES ${src_dir}/common/__init__.py DESTINATION ${dst_dir}/common)
2828
install(FILES ${src_dir}/common/cflags.py DESTINATION ${dst_dir}/common)
2929
install(FILES ${src_dir}/common/results.py DESTINATION ${dst_dir}/common)
30+
install(FILES ${src_dir}/common/snyk.py DESTINATION ${dst_dir}/common)
3031
install(FILES ${src_dir}/common/util.py DESTINATION ${dst_dir}/common)
3132

3233
macro(install_executable FILE_NAME)

py/common/snyk.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Copyright (C) 2024 Red Hat, Inc.
2+
#
3+
# This file is part of csmock.
4+
#
5+
# csmock is free software: you can redistribute it and/or modify
6+
# it under the terms of the GNU General Public License as published by
7+
# the Free Software Foundation, either version 3 of the License, or
8+
# any later version.
9+
#
10+
# csmock is distributed in the hope that it will be useful,
11+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
# GNU General Public License for more details.
14+
#
15+
# You should have received a copy of the GNU General Public License
16+
# along with csmock. If not, see <http://www.gnu.org/licenses/>.
17+
18+
import json
19+
20+
21+
def snyk_write_analysis_meta(results, raw_results_file):
22+
"""write snyk stats on metadata file. At the time, we write the total number of files,
23+
the number of supported files and the coverage ratio."""
24+
25+
try:
26+
with open(raw_results_file) as snyk_results_file:
27+
data = json.load(snyk_results_file)
28+
coverage_stats = data["runs"][0]["properties"]["coverage"]
29+
total_files = 0
30+
supported_files = 0
31+
for lang in coverage_stats:
32+
total_files += lang["files"]
33+
if lang["type"] == "SUPPORTED":
34+
supported_files += lang["files"]
35+
36+
coverage_ratio = 0
37+
if total_files > 0:
38+
coverage_ratio = int(supported_files * 100 / total_files)
39+
40+
results.ini_writer.append("snyk-scanned-files-coverage", coverage_ratio)
41+
results.ini_writer.append("snyk-scanned-files-success", supported_files)
42+
results.ini_writer.append("snyk-scanned-files-total", total_files)
43+
44+
return 0
45+
46+
except OSError as e:
47+
results.error(f"snyk-scan: failed to read {raw_results_file}: {e}")
48+
return 1
49+
50+
except KeyError as e:
51+
results.error(f"snyk-scan: error parsing results from snyk-results.sarif file: {e}")
52+
return 1

py/plugins/snyk.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
import os
1919

20+
from csmock.common.snyk import snyk_write_analysis_meta
21+
2022

2123
# default URL to download snyk binary executable
2224
SNYK_BIN_URL = "https://static.snyk.io/cli/latest/snyk-linux"
@@ -204,4 +206,8 @@ def filter_hook(results):
204206
cmd = FILTER_CMD % (src, dst)
205207
return results.exec_cmd(cmd, shell=True)
206208

207-
props.post_process_hooks += [filter_hook]
209+
def write_snyk_stats_metadata(results):
210+
raw_results_file = results.dbgdir_raw + SNYK_OUTPUT
211+
return snyk_write_analysis_meta(results, raw_results_file)
212+
213+
props.post_process_hooks += [write_snyk_stats_metadata, filter_hook]

0 commit comments

Comments
 (0)