From 234717b470a9be17bef507b22d5a1dae652a3ed7 Mon Sep 17 00:00:00 2001 From: Koby Meir Date: Sun, 11 Jun 2023 15:09:33 +0300 Subject: [PATCH] fixing unbound local variable on search dependencies (#27327) --- Tests/Marketplace/search_and_install_packs.py | 65 +++++++------------ 1 file changed, 24 insertions(+), 41 deletions(-) diff --git a/Tests/Marketplace/search_and_install_packs.py b/Tests/Marketplace/search_and_install_packs.py index c8b9cbaaf1ed..1db60a840b0a 100644 --- a/Tests/Marketplace/search_and_install_packs.py +++ b/Tests/Marketplace/search_and_install_packs.py @@ -134,45 +134,31 @@ def get_pack_dependencies(client: demisto_client, pack_data: dict, lock: Lock): pack_id = pack_data['id'] logging.debug(f'Getting dependencies for pack {pack_id}') try: - try: - response_data, status_code, _ = demisto_client.generic_request_func( - client, - path='/contentpacks/marketplace/search/dependencies', - method='POST', - body=[pack_data], - accept='application/json', - _request_timeout=None, - response_type='object' - ) - except ApiException as ex: - try: - logging.exception(f'Exception trying to get pack {pack_id} dependencies.' - f' Exception: {ex.status}, {ex.body}') - except Exception: - logging.error(f'An error occurred while parsing the dependencies result.' - f' Rrror: {str(ex)}') - raise ex - except Exception as ex: - logging.exception(f'Exception trying to get pack {pack_id} dependencies.' - f' Exception: {ex}') - + response_data, status_code, _ = demisto_client.generic_request_func( + client, + path='/contentpacks/marketplace/search/dependencies', + method='POST', + body=[pack_data], + accept='application/json', + _request_timeout=None, + response_type='object' + ) if 200 <= status_code < 300: dependencies_data: list = [] dependants_ids = [pack_id] response_data = response_data.get('dependencies', []) create_dependencies_data_structure(response_data, dependants_ids, dependencies_data, dependants_ids) - dependencies_str = ', '.join([dep['id'] for dep in dependencies_data]) if dependencies_data: + dependencies_str = ', '.join([dep['id'] for dep in dependencies_data]) logging.debug(f'Found the following dependencies for pack {pack_id}: {dependencies_str}') return dependencies_data if status_code == 400: logging.error(f'Unable to find dependencies for {pack_id}.') return [] - else: - msg = response_data.get('message', '') - raise Exception(f'Failed to get pack {pack_id} dependencies - with status code {status_code}\n{msg}\n') - except Exception: - logging.exception(f'The request to get pack {pack_id} dependencies has failed. {status_code=}.') + msg = response_data.get('message', '') + raise Exception(f'status code {status_code}\n{msg}\n') + except Exception as ex: + logging.exception(f'The request to get pack {pack_id} dependencies has failed. {ex}.') lock.acquire() global SUCCESS_FLAG @@ -205,11 +191,10 @@ def search_pack(client: demisto_client, if 200 <= status_code < 300: if response_data and response_data.get('currentVersion'): logging.debug(f'Found pack "{pack_display_name}" by its ID "{pack_id}" in bucket!') - pack_data = { + return { 'id': response_data.get('id'), - 'version': response_data.get('currentVersion') + 'version': response_data.get('currentVersion'), } - return pack_data else: raise Exception(f'Did not find pack "{pack_display_name}" by its ID "{pack_id}" in bucket.') else: @@ -263,7 +248,7 @@ def find_malformed_pack_id(body: str) -> List: def handle_malformed_pack_ids(malformed_pack_ids, packs_to_install): """ - Handles the case where the malformed id failed the installation but it was not a part of the initial installaion. + Handles the case where the malformed id failed the installation, but it was not a part of the initial installation. This is in order to prevent an infinite loop for this such edge case. Args: malformed_pack_ids: the ids found from the error msg @@ -285,7 +270,7 @@ def install_packs_from_artifacts(client: demisto_client, host: str, test_pack_pa :param client: Demisto-py client to connect to the server. :param host: FQDN of the server. - :param test_pack_path: Path the the test pack directory. + :param test_pack_path: Path to the test pack directory. :param pack_ids_to_install: List of pack IDs to install. :return: None. Call to server waits until a successful response. """ @@ -335,6 +320,7 @@ def install_packs(client: demisto_client, packs_to_install (list): A list of the packs to install. request_timeout (int): Timeout settings for the installation request. """ + global SUCCESS_FLAG class GCPTimeOutException(ApiException): def __init__(self, error): @@ -415,7 +401,6 @@ def call_install_packs_request(packs, attempts_count=1): except Exception as e: logging.exception(f'The request to install packs has failed. Additional info: {str(e)}') - global SUCCESS_FLAG SUCCESS_FLAG = False finally: @@ -514,18 +499,16 @@ def get_latest_version_from_bucket(pack_id: str, production_bucket: Bucket) -> s logging.debug(f'Found the following zips for {pack_id} pack: {pack_versions}') if pack_versions: - pack_latest_version = str(max(pack_versions)) - return pack_latest_version - else: - logging.error(f'Could not find any versions for pack {pack_id} in bucket path {pack_bucket_path}') - return '' + return str(max(pack_versions)) + logging.error(f'Could not find any versions for pack {pack_id} in bucket path {pack_bucket_path}') + return '' def get_pack_installation_request_data(pack_id: str, pack_version: str): """ Returns the installation request data of a given pack and its version. The request must have the ID and Version. - :param pack_id: Id of the pack to add. + :param pack_id: ID of the pack to add. :param pack_version: Version of the pack to add. :return: The request data part of the pack """ @@ -683,7 +666,7 @@ def search_and_install_packs_and_their_dependencies(pack_ids: list, A list of the installed packs' ids, or an empty list if is_nightly == True. A flag that indicates if the operation succeeded or not. """ - host = hostname if hostname else client.api_client.configuration.host + host = hostname or client.api_client.configuration.host logging.info(f'Starting to search and install packs in server: {host}')