Skip to content

Commit 6397545

Browse files
sacca97copernico
authored andcommitted
fixed json report generation to handle sets and file extensions when default is set
1 parent 0db9a46 commit 6397545

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

prospector/client/cli/json_report.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,24 @@
66

77
_logger = log.util.init_local_logger()
88

9+
class SetEncoder(json.JSONEncoder):
10+
def default(self, obj):
11+
if isinstance(obj, set):
12+
return list(obj)
13+
return json.JSONEncoder.default(self, obj)
14+
915

1016
def report_as_json(
1117
results: "list[Commit]",
1218
advisory_record: AdvisoryRecord,
1319
filename: str = "prospector-report.json",
1420
):
15-
21+
# Need to convert the Sets to Lists for JSON serialization
1622
data = {
1723
"advisory_record": advisory_record.dict(),
1824
"commits": [r.dict() for r in results],
1925
}
2026
_logger.info("Writing results to " + filename)
2127
with open(filename, "w", encoding="utf8") as json_file:
22-
json.dump(data, json_file, ensure_ascii=True, indent=4)
28+
json.dump(data, json_file, ensure_ascii=True, indent=4, cls=SetEncoder)
2329
return filename

prospector/client/cli/main.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ def parseArguments(args):
135135

136136
parser.add_argument(
137137
"--report-filename",
138-
default="prospector-report.html",
138+
default="prospector-report",
139139
type=str,
140140
help="File where to save the report",
141141
)
@@ -311,11 +311,11 @@ def main(argv): # noqa: C901
311311
report_on_console(results, advisory_record, log.config.level < logging.INFO)
312312
elif report == "json":
313313
report_file = report_as_json(
314-
results, advisory_record, str(args.report_filename)
314+
results, advisory_record, args.report_filename + ".json"
315315
)
316316
elif report == "html":
317317
report_file = report_as_html(
318-
results, advisory_record, str(args.report_filename)
318+
results, advisory_record, args.report_filename + ".html"
319319
)
320320
else:
321321
_logger.warning("Invalid report type specified, using 'console'")

0 commit comments

Comments
 (0)