Skip to content

Commit

Permalink
CM-37160 - Remove legacy flow via detection count (#232)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarshalX authored Jun 18, 2024
1 parent e63c1db commit 0533c7d
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 59 deletions.
43 changes: 8 additions & 35 deletions cycode/cli/commands/scan/code_scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,10 +317,14 @@ def scan_documents(
errors, local_scan_results = run_parallel_batched_scan(
scan_batch_thread_func, documents_to_scan, progress_bar=progress_bar
)
aggregation_report_url = _try_get_aggregation_report_url_if_needed(
scan_parameters, context.obj['client'], context.obj['scan_type']
)
set_aggregation_report_url(context, aggregation_report_url)

if len(local_scan_results) > 1:
# if we used more than one batch, we need to fetch aggregate report url
aggregation_report_url = _try_get_aggregation_report_url_if_needed(
scan_parameters, context.obj['client'], context.obj['scan_type']
)
set_aggregation_report_url(context, aggregation_report_url)

progress_bar.set_section_length(ScanProgressBarSection.GENERATE_REPORT, 1)
progress_bar.update(ScanProgressBarSection.GENERATE_REPORT)
progress_bar.stop()
Expand Down Expand Up @@ -863,8 +867,6 @@ def _get_scan_result(
if not scan_details.detections_count:
return init_default_scan_result(cycode_client, scan_id, scan_type, should_get_report)

wait_for_detections_creation(cycode_client, scan_type, scan_id, scan_details.detections_count)

scan_detections = cycode_client.get_scan_detections(scan_type, scan_id)

return ZippedFileScanResult(
Expand Down Expand Up @@ -899,35 +901,6 @@ def _try_get_report_url_if_needed(
logger.debug('Failed to get report URL', exc_info=e)


def wait_for_detections_creation(
cycode_client: 'ScanClient', scan_type: str, scan_id: str, expected_detections_count: int
) -> None:
logger.debug('Waiting for detections to be created')

scan_persisted_detections_count = 0
polling_timeout = consts.DETECTIONS_COUNT_VERIFICATION_TIMEOUT_IN_SECONDS
end_polling_time = time.time() + polling_timeout

while time.time() < end_polling_time:
scan_persisted_detections_count = cycode_client.get_scan_detections_count(scan_type, scan_id)
logger.debug(
'Excepting %s detections, got %s detections (%s more; %s seconds left)',
expected_detections_count,
scan_persisted_detections_count,
expected_detections_count - scan_persisted_detections_count,
round(end_polling_time - time.time()),
)
if scan_persisted_detections_count == expected_detections_count:
return

time.sleep(consts.DETECTIONS_COUNT_VERIFICATION_WAIT_INTERVAL_IN_SECONDS)

logger.debug('%s detections has been created', scan_persisted_detections_count)
raise custom_exceptions.ScanAsyncError(
f'Failed to wait for detections to be created after {polling_timeout} seconds'
)


def _map_detections_per_file(detections: List[dict]) -> List[DetectionsPerFile]:
detections_per_files = {}
for detection in detections:
Expand Down
2 changes: 0 additions & 2 deletions cycode/cli/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,6 @@
SCAN_POLLING_WAIT_INTERVAL_IN_SECONDS = 5
DEFAULT_SCAN_POLLING_TIMEOUT_IN_SECONDS = 3600
SCAN_POLLING_TIMEOUT_IN_SECONDS_ENV_VAR_NAME = 'SCAN_POLLING_TIMEOUT_IN_SECONDS'
DETECTIONS_COUNT_VERIFICATION_TIMEOUT_IN_SECONDS = 600
DETECTIONS_COUNT_VERIFICATION_WAIT_INTERVAL_IN_SECONDS = 10
DEFAULT_SCA_PRE_COMMIT_TIMEOUT_IN_SECONDS = 600
SCA_PRE_COMMIT_TIMEOUT_IN_SECONDS_ENV_VAR_NAME = 'SCA_PRE_COMMIT_TIMEOUT_IN_SECONDS'

Expand Down
9 changes: 0 additions & 9 deletions cycode/cyclient/scan_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,15 +245,6 @@ def get_scan_detections(self, scan_type: str, scan_id: str) -> List[dict]:

return detections

def get_scan_detections_count_path(self, scan_type: str) -> str:
return f'{self.get_scan_detections_path(scan_type)}/count'

def get_scan_detections_count(self, scan_type: str, scan_id: str) -> int:
response = self.scan_cycode_client.get(
url_path=self.get_scan_detections_count_path(scan_type), params={'scan_id': scan_id}
)
return response.json().get('count', 0)

def commit_range_zipped_file_scan(
self, scan_type: str, zip_file: InMemoryZip, scan_id: str
) -> models.ZippedFileScanResult:
Expand Down
13 changes: 0 additions & 13 deletions tests/cyclient/mocked_responses/scan_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,18 +135,6 @@ def get_scan_details_response(url: str, scan_id: Optional[UUID] = None) -> respo
return responses.Response(method=responses.GET, url=url, json=json_response, status=200)


def get_scan_detections_count_url(scan_client: ScanClient) -> str:
api_url = scan_client.scan_cycode_client.api_url
service_url = scan_client.get_scan_detections_count_path()
return f'{api_url}/{service_url}'


def get_scan_detections_count_response(url: str) -> responses.Response:
json_response = {'count': 1}

return responses.Response(method=responses.GET, url=url, json=json_response, status=200)


def get_scan_detections_url(scan_client: ScanClient, scan_type: str) -> str:
api_url = scan_client.scan_cycode_client.api_url
service_url = scan_client.get_scan_detections_path(scan_type)
Expand Down Expand Up @@ -195,7 +183,6 @@ def mock_scan_async_responses(
)
responses_module.add(get_scan_details_response(get_scan_details_url(scan_id, scan_client), scan_id))
responses_module.add(get_detection_rules_response(get_detection_rules_url(scan_client)))
responses_module.add(get_scan_detections_count_response(get_scan_detections_count_url(scan_client)))
responses_module.add(
get_scan_detections_response(get_scan_detections_url(scan_client, scan_type), scan_id, zip_content_path)
)
Expand Down

0 comments on commit 0533c7d

Please sign in to comment.