diff --git a/pr_agent/algo/utils.py b/pr_agent/algo/utils.py index f92309c4..3a555dca 100644 --- a/pr_agent/algo/utils.py +++ b/pr_agent/algo/utils.py @@ -304,14 +304,14 @@ def try_fix_yaml(response_text: str) -> dict: for i in range(0, len(response_text_lines_copy)): for key in keys: if key in response_text_lines_copy[i] and not '|-' in response_text_lines_copy[i]: - response_text_lines_copy[i] = response_text_lines_copy[i].replace(f'{key}: ', - f'{key}: |-\n ') + response_text_lines_copy[i] = response_text_lines_copy[i].replace(f'{key}', + f'{key} |-\n ') try: data = yaml.safe_load('\n'.join(response_text_lines_copy)) get_logger().info(f"Successfully parsed AI prediction after adding |-\n") return data except: - get_logger().debug(f"Failed to parse AI prediction after adding |-\n") + get_logger().info(f"Failed to parse AI prediction after adding |-\n") # second fallback - try to remove last lines data = {} diff --git a/tests/unittest/test_load_yaml.py b/tests/unittest/test_load_yaml.py index f1d274c2..a77c847b 100644 --- a/tests/unittest/test_load_yaml.py +++ b/tests/unittest/test_load_yaml.py @@ -2,6 +2,9 @@ # Generated by CodiumAI import pytest +import yaml +from yaml.scanner import ScannerError + from pr_agent.algo.utils import load_yaml @@ -26,15 +29,23 @@ PR Feedback: Code feedback: - relevant file: pr_agent/settings/pr_description_prompts.toml suggestion: Consider using a more descriptive variable name than 'user' for the command prompt. A more descriptive name would make the code more readable and maintainable. [medium] - relevant line: 'user="""PR Info:' + relevant line: user="""PR Info: aaa Security concerns: No''' - expected_output = {'PR Analysis': {'Main theme': 'Enhancing the `/describe` command prompt by adding title and description', 'Type of PR': 'Enhancement', 'Relevant tests added': False, 'Focused PR': 'Yes, the PR is focused on enhancing the `/describe` command prompt.'}, 'PR Feedback': {'General suggestions': 'The PR seems to be well-structured and focused on a specific enhancement. However, it would be beneficial to add tests to ensure the new feature works as expected.', 'Code feedback': [{'relevant file': 'pr_agent/settings/pr_description_prompts.toml', 'suggestion': "Consider using a more descriptive variable name than 'user' for the command prompt. A more descriptive name would make the code more readable and maintainable. [medium]", 'relevant line': 'user="""PR Info:'}], 'Security concerns': False}} + with pytest.raises(ScannerError): + yaml.safe_load(yaml_str) + + expected_output = {'PR Analysis': {'Main theme': 'Enhancing the `/describe` command prompt by adding title and description', 'Type of PR': 'Enhancement', 'Relevant tests added': False, 'Focused PR': 'Yes, the PR is focused on enhancing the `/describe` command prompt.'}, 'PR Feedback': {'General suggestions': 'The PR seems to be well-structured and focused on a specific enhancement. However, it would be beneficial to add tests to ensure the new feature works as expected.', 'Code feedback': [{'relevant file': 'pr_agent/settings/pr_description_prompts.toml', 'suggestion': "Consider using a more descriptive variable name than 'user' for the command prompt. A more descriptive name would make the code more readable and maintainable. [medium]", 'relevant line': 'user="""PR Info: aaa'}], 'Security concerns': False}} assert load_yaml(yaml_str) == expected_output def test_load_invalid_yaml2(self): yaml_str = '''\ -- relevant file: src/app.py - suggestion content: The print statement is outside inside the if __name__ == \ +- relevant file: src/app.py: + suggestion content: The print statement is outside inside the if __name__ ==: \ ''' - expected_output =[{'relevant file': 'src/app.py', 'suggestion content': 'The print statement is outside inside the if __name__ =='}] + with pytest.raises(ScannerError): + yaml.safe_load(yaml_str) + + expected_output =[{'relevant file': 'src/app.py:', + 'suggestion content': 'The print statement is outside inside the if __name__ ==: '}] assert load_yaml(yaml_str) == expected_output +