Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixing unbound local variable on search dependencies #27327

Merged
merged 1 commit into from
Jun 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 24 additions & 41 deletions Tests/Marketplace/search_and_install_packs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
kobymeir marked this conversation as resolved.
Show resolved Hide resolved
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=}.')
kobymeir marked this conversation as resolved.
Show resolved Hide resolved
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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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.
"""
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
"""
Expand Down Expand Up @@ -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}')

Expand Down