From 92d904ee100594df758b369bba65739357c83398 Mon Sep 17 00:00:00 2001 From: Michael Hohn Date: Mon, 6 Dec 2021 14:24:08 -0800 Subject: [PATCH] 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 --- bin/sarif-results-summary | 8 ++++++++ sarif_cli/__init__.py | 6 ++++++ 2 files changed, 14 insertions(+) diff --git a/bin/sarif-results-summary b/bin/sarif-results-summary index 5a4e361..127e751 100755 --- a/bin/sarif-results-summary +++ b/bin/sarif-results-summary @@ -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 diff --git a/sarif_cli/__init__.py b/sarif_cli/__init__.py index 94270e4..70cd3b3 100644 --- a/sarif_cli/__init__.py +++ b/sarif_cli/__init__.py @@ -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. """