From 84630b0eff83327f84434c5510b6e9c78c8f6c82 Mon Sep 17 00:00:00 2001 From: Brad Smith Date: Tue, 28 May 2024 15:14:59 -0400 Subject: [PATCH] feat: add command to get aggregate report Relates to JIRA: DISCOVERY-611 --- qpc/cli.py | 2 ++ qpc/report/__init__.py | 2 ++ qpc/report/aggregate.py | 79 +++++++++++++++++++++++++++++++++++++++++ qpc/report/commands.py | 1 + 4 files changed, 84 insertions(+) create mode 100644 qpc/report/aggregate.py diff --git a/qpc/cli.py b/qpc/cli.py index 8bcdd166..d355086a 100644 --- a/qpc/cli.py +++ b/qpc/cli.py @@ -17,6 +17,7 @@ InsightsPublishCommand, ) from qpc.release import QPC_VAR_PROGRAM_NAME, VERSION, get_current_sha1 +from qpc.report.aggregate import ReportAggregateCommand from qpc.report.commands import ( ReportDeploymentsCommand, ReportDetailsCommand, @@ -162,6 +163,7 @@ def __init__(self, name="cli", usage=None, shortdesc=None, description=None): self._add_subcommand( report.SUBCOMMAND, [ + ReportAggregateCommand, ReportDeploymentsCommand, ReportDetailsCommand, ReportInsightsCommand, diff --git a/qpc/report/__init__.py b/qpc/report/__init__.py index d29e364d..4cfeff1e 100644 --- a/qpc/report/__init__.py +++ b/qpc/report/__init__.py @@ -5,6 +5,7 @@ DETAILS = "details" DEPLOYMENTS = "deployments" INSIGHTS = "insights" +AGGREGATE = "aggregate" MERGE = "merge" UPLOAD = "upload" MERGE_STATUS = "merge-status" @@ -12,6 +13,7 @@ REPORT_URI = "/api/v1/reports/" DETAILS_PATH_SUFFIX = "/details/" DEPLOYMENTS_PATH_SUFFIX = "/deployments/" +AGGREGATE_PATH_SUFFIX = "/aggregate/" INSIGHTS_PATH_SUFFIX = "/insights/" ASYNC_MERGE_URI = "/api/v1/reports/merge/" ASYNC_UPLOAD_URI = "/api/v1/reports/" diff --git a/qpc/report/aggregate.py b/qpc/report/aggregate.py new file mode 100644 index 00000000..c82204d8 --- /dev/null +++ b/qpc/report/aggregate.py @@ -0,0 +1,79 @@ +"""ReportAggregateCommand retrieves and outputs the aggregate report.""" + +import sys +from logging import getLogger + +from requests import codes + +from qpc import messages, report, scan +from qpc.clicommand import CliCommand +from qpc.request import GET, request +from qpc.translation import _ +from qpc.utils import pretty_format + +logger = getLogger(__name__) + + +class ReportAggregateCommand(CliCommand): + """Defines the command for showing the aggregate report.""" + + SUBCOMMAND = report.SUBCOMMAND + ACTION = report.AGGREGATE + + def __init__(self, subparsers): + """Create command.""" + CliCommand.__init__( + self, + self.SUBCOMMAND, + self.ACTION, + subparsers.add_parser(self.ACTION), + GET, + report.REPORT_URI, + [codes.ok], + ) + id_group = self.parser.add_mutually_exclusive_group(required=True) + id_group.add_argument( + "--scan-job", + dest="scan_job_id", + metavar="SCAN_JOB_ID", + help=_(messages.REPORT_SCAN_JOB_ID_HELP), + ) + id_group.add_argument( + "--report", + dest="report_id", + metavar="REPORT_ID", + help=_(messages.REPORT_REPORT_ID_HELP), + ) + self.report_id = None + + def _validate_args(self): # noqa: PLR0912 + CliCommand._validate_args(self) + + if not (report_id := self.args.report_id): + response = request( + parser=self.parser, + method=GET, + path=f"{scan.SCAN_JOB_URI}{self.args.scan_job_id}", + payload=None, + ) + if response.status_code == codes.ok: + json_data = response.json() + if not (report_id := json_data.get("report_id")): + logger.error( + _(messages.REPORT_NO_DEPLOYMENTS_REPORT_FOR_SJ), + self.args.scan_job_id, + ) + sys.exit(1) + else: + logger.error( + _(messages.REPORT_SJ_DOES_NOT_EXIST), self.args.scan_job_id + ) + sys.exit(1) + + self.report_id = report_id + self.req_path = f"{self.req_path}{self.report_id}{report.AGGREGATE_PATH_SUFFIX}" + + def _handle_response_success(self): + json_data = self.response.json() + data = pretty_format(json_data) + print(data) diff --git a/qpc/report/commands.py b/qpc/report/commands.py index 98b8dc47..0f2270fb 100644 --- a/qpc/report/commands.py +++ b/qpc/report/commands.py @@ -1,5 +1,6 @@ """Commands for import organization.""" +from qpc.report.aggregate import ReportAggregateCommand from qpc.report.deployments import ReportDeploymentsCommand from qpc.report.details import ReportDetailsCommand from qpc.report.download import ReportDownloadCommand