Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

snyk: added snyk stats to metadata #152

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions py/common/snyk.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# standard imports
import json


def snyk_write_analysis_meta(results, results_file):

Check warning

Code scanning / vcs-diff-lint

snyk_write_analysis_meta: Missing function or method docstring Warning

snyk_write_analysis_meta: Missing function or method docstring
try:
with open(results_file) as snyk_results_file:

Check warning

Code scanning / vcs-diff-lint

snyk_write_analysis_meta: Using open without explicitly specifying an encoding Warning

snyk_write_analysis_meta: Using open without explicitly specifying an encoding
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:

Check warning

Code scanning / vcs-diff-lint

snyk_write_analysis_meta: Catching too general exception Exception Warning

snyk_write_analysis_meta: Catching too general exception Exception
results.error(f"snyk-scan: error parsing results from snyk-results.sarif file: {e}")
return 1
8 changes: 7 additions & 1 deletion py/plugins/snyk.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import os

from csmock.common.snyk import snyk_write_analysis_meta

Check warning

Code scanning / vcs-diff-lint

Unable to import 'csmock.common.snyk' Warning

Unable to import 'csmock.common.snyk'


# default URL to download snyk binary executable
SNYK_BIN_URL = "https://static.snyk.io/cli/latest/snyk-linux"
Expand Down Expand Up @@ -204,4 +206,8 @@
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]