Add quick check to verify that input is serif

An occasional output from LGTM is
    {"code":404,"error":"The specified analysis could not be found"}

With this patch, the csv output is now
    "ERROR","invalid json contents %s","some-file.json"

and the plain text output becomes
    ERROR: invalid json contents in some-file.json
This commit is contained in:
Michael Hohn
2021-12-06 14:24:08 -08:00
committed by =Michael Hohn
parent 120e673424
commit 92d904ee10
2 changed files with 14 additions and 0 deletions

View File

@@ -23,6 +23,14 @@ with open(args.file, 'r') if args.file != '-' else sys.stdin as fp:
if args.csv:
cw = S.get_csv_writer()
if not S.is_sarif_struct(sarif_struct):
if args.csv:
S.write_csv(cw, "ERROR", "invalid json contents %s", args.file)
else:
S.msg("ERROR: invalid json contents in %s\n" % (args.file))
S.dbg("invalid json contents in %s\n" % (args.file))
sys.exit(0) # No failure, just a warning
for runi in S.indices(sarif_struct, 'runs'):
num_results = len(S.get(sarif_struct, 'runs', runi, 'results'))
if num_results == 0: continue

View File

@@ -18,6 +18,12 @@ class NoFile:
whole file is to be used.
"""
def is_sarif_struct(struct):
"""A quick check to verify that `struct` is in fact a SARIF tree.
"""
return type(struct) == dict and "$schema" in struct and \
"sarif" in struct["$schema"] and "version" in struct
def get_csv_writer():
""" Set up and return the default csv writer on stdout.
"""