Ejemplo n.º 1
0
    def json_output(self, file_path: str, error_code: str, error_message: str,
                    warning: bool) -> None:
        """Adds an error's info to the output JSON file

        Args:
            file_path (str): The file path where the error ocurred.
            error_code (str): The error code
            error_message (str): The error message
            warning (bool): Whether the error is defined as a warning
        """
        if not self.json_file_path:
            return

        error_data = get_error_object(error_code)

        output = {
            'severity': 'warning' if warning else 'error',
            'errorCode': error_code,
            'message': error_message,
            'ui': error_data.get('ui_applicable'),
            'relatedField': error_data.get('related_field'),
            'linter': 'validate'
        }

        json_contents = []
        existing_json = ''
        if os.path.exists(self.json_file_path):
            try:
                existing_json = get_json(self.json_file_path)
            except ValueError:
                pass
            if isinstance(existing_json, list):
                json_contents = existing_json

        file_type = find_type(file_path)
        entity_type = file_type.value if file_type else 'pack'

        # handling unified yml image errors
        if entity_type == FileType.INTEGRATION.value and error_code.startswith(
                'IM'):
            entity_type = FileType.IMAGE.value

        formatted_error_output = {
            'filePath': file_path,
            'fileType': os.path.splitext(file_path)[1].replace('.', ''),
            'entityType': entity_type,
            'errorType': 'Settings',
            'name': get_file_displayed_name(file_path),
            'linter': 'validate',
            **output
        }
        json_contents.append(formatted_error_output)
        with open(self.json_file_path, 'w') as f:
            json.dump(json_contents, f, indent=4)
 def name_does_not_contain_excluded_word(self) -> bool:
     """
     Checks whether given object contains excluded word.
     Returns:
         (bool) False if display name corresponding to file path contains excluded word, true otherwise.
     """
     name = get_file_displayed_name(self.file_path)
     if not name:
         return True
     lowercase_name = name.lower()
     if any(excluded_word in lowercase_name for excluded_word in EXCLUDED_DISPLAY_NAME_WORDS):
         error_message, error_code = Errors.entity_name_contains_excluded_word(name,
                                                                               EXCLUDED_DISPLAY_NAME_WORDS)
         if self.handle_error(error_message, error_code, file_path=self.file_path):
             return False
     return True
Ejemplo n.º 3
0
def test_get_file_displayed_name__image(repo):
    """
    Given
    - The path to an image.

    When
    - Running get_file_displayed_name.

    Then:
    - Ensure the returned name is the file name.
    """
    pack = repo.create_pack('MyPack')
    integration = pack.create_integration('MyInt')
    integration.create_default_integration()
    with ChangeCWD(repo.path):
        display_name = get_file_displayed_name(integration.image.path)
        assert display_name == os.path.basename(integration.image.rel_path)
Ejemplo n.º 4
0
def test_get_file_displayed_name__layout(repo):
    """
    Given
    - The path to a layout.

    When
    - Running get_file_displayed_name.

    Then:
    - Ensure the returned name is the TypeName field.
    """
    pack = repo.create_pack('MyPack')
    layout = pack.create_layout('MyLay', content=LAYOUT)
    json_content = layout.read_json_as_dict()
    json_content['TypeName'] = 'MyDisplayName'
    layout.write_json(json_content)
    with ChangeCWD(repo.path):
        display_name = get_file_displayed_name(layout.path)
        assert display_name == 'MyDisplayName'
Ejemplo n.º 5
0
def test_get_file_displayed_name__old_classifier(repo):
    """
    Given
    - The path to an old classifier.

    When
    - Running get_file_displayed_name.

    Then:
    - Ensure the returned name is the brandName field.
    """
    pack = repo.create_pack('MyPack')
    old_classifier = pack.create_classifier('MyClas', content=OLD_CLASSIFIER)
    json_content = old_classifier.read_json_as_dict()
    json_content['brandName'] = 'MyDisplayName'
    old_classifier.write_json(json_content)
    with ChangeCWD(repo.path):
        display_name = get_file_displayed_name(old_classifier.path)
        assert display_name == 'MyDisplayName'
Ejemplo n.º 6
0
def test_get_file_displayed_name__mapper(repo):
    """
    Given
    - The path to a mapper.

    When
    - Running get_file_displayed_name.

    Then:
    - Ensure the returned name is the name field.
    """
    pack = repo.create_pack('MyPack')
    mapper = pack.create_mapper('MyMap', content=MAPPER)
    json_content = mapper.read_json_as_dict()
    json_content['name'] = 'MyDisplayName'
    mapper.write_json(json_content)
    with ChangeCWD(repo.path):
        display_name = get_file_displayed_name(mapper.path)
        assert display_name == 'MyDisplayName'
Ejemplo n.º 7
0
    def json_output(self, file_path: str, error_code: str, error_message: str,
                    warning: bool) -> None:
        """Adds an error's info to the output JSON file

        Args:
            file_path (str): The file path where the error ocurred.
            error_code (str): The error code
            error_message (str): The error message
            warning (bool): Whether the error is defined as a warning
        """
        if not self.json_file_path:
            return

        error_data = get_error_object(error_code)

        output = {
            "severity": "warning" if warning else "error",
            "code": error_code,
            "message": error_message,
            "ui": error_data.get('ui_applicable'),
            'related-field': error_data.get('related_field')
        }

        if os.path.exists(self.json_file_path):
            json_contents = get_json(self.json_file_path)

        else:
            json_contents = {}

        file_type = find_type(file_path)
        if file_path in json_contents:
            if output in json_contents[file_path].get('outputs'):
                return
            json_contents[file_path]['outputs'].append(output)
        else:
            json_contents[file_path] = {
                "file-type": os.path.splitext(file_path)[1].replace('.', ''),
                "entity-type": file_type.value if file_type else 'pack',
                "display-name": get_file_displayed_name(file_path),
                "outputs": [output]
            }
        with open(self.json_file_path, 'w') as f:
            json.dump(json_contents, f, indent=4)
Ejemplo n.º 8
0
def test_get_file_displayed_name__playbook(repo):
    """
    Given
    - The path to a playbook.

    When
    - Running get_file_displayed_name.

    Then:
    - Ensure the returned name is the name field.
    """
    pack = repo.create_pack('MyPack')
    playbook = pack.create_playbook('MyPlay')
    playbook.create_default_playbook()
    yml_content = playbook.yml.read_dict()
    yml_content['name'] = 'MyDisplayName'
    playbook.yml.write_dict(yml_content)
    with ChangeCWD(repo.path):
        display_name = get_file_displayed_name(playbook.yml.path)
        assert display_name == 'MyDisplayName'
Ejemplo n.º 9
0
def test_get_file_displayed_name__script(repo):
    """
    Given
    - The path to a script.

    When
    - Running get_file_displayed_name.

    Then:
    - Ensure the returned name is the name field.
    """
    pack = repo.create_pack('MyPack')
    script = pack.create_script('MyScr')
    script.create_default_script()
    yml_content = script.yml.read_dict()
    yml_content['name'] = 'MyDisplayName'
    script.yml.write_dict(yml_content)
    with ChangeCWD(repo.path):
        display_name = get_file_displayed_name(script.yml.path)
        assert display_name == 'MyDisplayName'
Ejemplo n.º 10
0
def test_get_file_displayed_name__integration(repo):
    """
    Given
    - The path to an integration.

    When
    - Running get_file_displayed_name.

    Then:
    - Ensure the returned name is the display field.
    """
    pack = repo.create_pack('MyPack')
    integration = pack.create_integration('MyInt')
    integration.create_default_integration()
    yml_content = integration.yml.read_dict()
    yml_content['display'] = 'MyDisplayName'
    integration.yml.write_dict(yml_content)
    with ChangeCWD(repo.path):
        display_name = get_file_displayed_name(integration.yml.path)
        assert display_name == 'MyDisplayName'
Ejemplo n.º 11
0
def test_get_file_displayed_name__reputation(repo):
    """
    Given
    - The path to a reputation.

    When
    - Running get_file_displayed_name.

    Then:
    - Ensure the returned name is the id field.
    """
    pack = repo.create_pack('MyPack')
    reputation = pack._create_json_based('MyRep',
                                         content=REPUTATION,
                                         prefix='reputation')
    json_content = reputation.read_json_as_dict()
    json_content['id'] = 'MyDisplayName'
    reputation.write_json(json_content)
    with ChangeCWD(repo.path):
        display_name = get_file_displayed_name(reputation.path)
        assert display_name == 'MyDisplayName'
Ejemplo n.º 12
0
    def add_to_json_outputs(output: Dict, file_path: str,
                            json_contents: Dict) -> None:
        """Adds an error entry to the JSON file contents

        Args:
            output (Dict): The information about an error entry
            file_path (str): The file path where the error occurred
            json_contents (Dict): The JSON file outputs
        """
        yml_file_path = file_path.replace('.py',
                                          '.yml').replace('.ps1', '.yml')
        file_type = find_type(yml_file_path)
        if file_path in json_contents:
            if output in json_contents[file_path]['outputs']:
                return
            json_contents[file_path]['outputs'].append(output)

        else:
            json_contents[file_path] = {
                'file-type': os.path.splitext(file_path)[1].replace('.', ''),
                'entity-type': file_type.value if file_type else '',
                "display-name": get_file_displayed_name(yml_file_path),
                'outputs': [output]
            }