|
| 1 | +from typing import TYPE_CHECKING, Optional |
| 2 | + |
| 3 | +import typer |
| 4 | + |
| 5 | +from cycode.logger import get_logger |
| 6 | + |
| 7 | +if TYPE_CHECKING: |
| 8 | + from cycode.cyclient.scan_client import ScanClient |
| 9 | + |
| 10 | +logger = get_logger('Aggregation Report URL') |
| 11 | + |
| 12 | + |
| 13 | +def _set_aggregation_report_url(ctx: typer.Context, aggregation_report_url: Optional[str] = None) -> None: |
| 14 | + ctx.obj['aggregation_report_url'] = aggregation_report_url |
| 15 | + |
| 16 | + |
| 17 | +def try_get_aggregation_report_url_if_needed( |
| 18 | + scan_parameters: dict, cycode_client: 'ScanClient', scan_type: str |
| 19 | +) -> Optional[str]: |
| 20 | + if not scan_parameters.get('report', False): |
| 21 | + return None |
| 22 | + |
| 23 | + aggregation_id = scan_parameters.get('aggregation_id') |
| 24 | + if aggregation_id is None: |
| 25 | + return None |
| 26 | + |
| 27 | + try: |
| 28 | + report_url_response = cycode_client.get_scan_aggregation_report_url(aggregation_id, scan_type) |
| 29 | + return report_url_response.report_url |
| 30 | + except Exception as e: |
| 31 | + logger.debug('Failed to get aggregation report url: %s', str(e)) |
| 32 | + |
| 33 | + |
| 34 | +def try_set_aggregation_report_url_if_needed( |
| 35 | + ctx: typer.Context, scan_parameters: dict, cycode_client: 'ScanClient', scan_type: str |
| 36 | +) -> None: |
| 37 | + aggregation_report_url = try_get_aggregation_report_url_if_needed(scan_parameters, cycode_client, scan_type) |
| 38 | + if aggregation_report_url: |
| 39 | + _set_aggregation_report_url(ctx, aggregation_report_url) |
| 40 | + logger.debug('Aggregation report URL set successfully', {'aggregation_report_url': aggregation_report_url}) |
| 41 | + else: |
| 42 | + logger.debug('No aggregation report URL found or report generation is disabled') |
0 commit comments