Skip to content

Commit

Permalink
feat: add command to get aggregate report
Browse files Browse the repository at this point in the history
Relates to JIRA: DISCOVERY-611
  • Loading branch information
infinitewarp committed May 28, 2024
1 parent b967c77 commit 84630b0
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 0 deletions.
2 changes: 2 additions & 0 deletions qpc/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -162,6 +163,7 @@ def __init__(self, name="cli", usage=None, shortdesc=None, description=None):
self._add_subcommand(
report.SUBCOMMAND,
[
ReportAggregateCommand,
ReportDeploymentsCommand,
ReportDetailsCommand,
ReportInsightsCommand,
Expand Down
2 changes: 2 additions & 0 deletions qpc/report/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
DETAILS = "details"
DEPLOYMENTS = "deployments"
INSIGHTS = "insights"
AGGREGATE = "aggregate"
MERGE = "merge"
UPLOAD = "upload"
MERGE_STATUS = "merge-status"
DOWNLOAD = "download"
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/"
79 changes: 79 additions & 0 deletions qpc/report/aggregate.py
Original file line number Diff line number Diff line change
@@ -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)
1 change: 1 addition & 0 deletions qpc/report/commands.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 84630b0

Please sign in to comment.