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

Enhancement of Code Feedback Formatting in utils.py #515

Merged
merged 5 commits into from
Dec 11, 2023
Merged

Conversation

mrT23
Copy link
Collaborator

@mrT23 mrT23 commented Dec 10, 2023

Type

Enhancement


Description

This PR introduces changes to the convert_to_markdown and parse_code_suggestion functions in the utils.py file. The main enhancements include:

  • Modification of the code feedback markdown formatting.
  • Addition of enumeration to the list items in the convert_to_markdown function.
  • Addition of an index parameter to the parse_code_suggestion function.
  • Inclusion of a condition to check if the index is 0 and add a horizontal rule in the parse_code_suggestion function.
  • Addition of a table format for the code suggestions when GitHub Flavored Markdown (GFM) is supported.

PR changes walkthrough

Relevant files                                                                                                                                 
Enhancement
1 files
utils.py                                                                                                       
    pr_agent/algo/utils.py

    The `convert_to_markdown` function has been updated to
    modify the markdown formatting for code feedback and add
    enumeration to list items. The `parse_code_suggestion`
    function has been updated to include an index parameter, add
    a horizontal rule when the index is 0, and format the code
    suggestions in a table format when GFM is supported.
+48/-18

Copy link
Contributor

github-actions bot commented Dec 10, 2023

PR Analysis

(review updated until commit 654938f)

  • 🎯 Main theme: Enhancing the formatting of code feedback in markdown in the utils.py file.
  • 📝 PR summary: This PR focuses on improving the markdown formatting of code feedback in the utils.py file. It introduces changes to the 'convert_to_markdown' and 'parse_code_suggestion' functions to better handle the formatting of code suggestions, especially when GitHub Flavored Markdown (GFM) is supported. The changes include the use of tables for better readability and the inclusion of hyperlinks for relevant lines of code.
  • 📌 Type of PR: Enhancement
  • 🧪 Relevant tests added: No
  • ⏱️ Estimated effort to review [1-5]: 3, because the PR involves changes to the markdown formatting logic which requires a good understanding of markdown syntax and the existing codebase. Also, the changes are quite extensive and would require careful review to ensure they don't break existing functionality.
  • 🔒 Security concerns: No security concerns found

PR Feedback

  • 💡 General suggestions: The changes made in this PR are quite extensive and could potentially affect the readability and structure of the generated markdown. It would be beneficial to include some unit tests to verify the correctness of the new formatting logic. Additionally, the use of exception handling could be improved to provide more specific error messages and to avoid the use of a generic pass statement.

  • 🤖 Code feedback:
    relevant filepr_agent/algo/utils.py
    suggestion      Consider adding more specific error handling in the try-except block. Instead of catching a generic Exception, catch specific exceptions that you expect might occur. This will make debugging easier if an error occurs. Also, avoid using 'pass' in the except block. Instead, consider re-raising the exception or handling it in a way that doesn't silence it. [important]
    relevant lineexcept Exception as e:

    relevant filepr_agent/algo/utils.py
    suggestion      The 'relevant_file' variable is assigned but never used within the 'if gfm_supported and 'relevant line' in code_suggestions:' block. If it's not needed, consider removing it to improve code readability. [medium]
    relevant linerelevant_file = ''

    relevant filepr_agent/algo/utils.py
    suggestion      The 'else' clause after 'if gfm_supported and 'relevant line' in code_suggestions:' seems to be repeating some logic from the 'if' clause. Consider refactoring this to avoid code duplication. [medium]
    relevant lineelse:

    relevant filepr_agent/algo/utils.py
    suggestion      The 'try_fix_json' function is not modified in this PR. If it's not relevant to the changes, consider removing it from the PR to keep the changes focused and easier to review. [medium]
    relevant linedef try_fix_json(review, max_iter=10, code_suggestions=False):

@mrT23
Copy link
Collaborator Author

mrT23 commented Dec 10, 2023

Persistent review updated to latest commit 654938f

Copy link
Contributor

PR Code Suggestions

💡 Suggestion:

Instead of using string concatenation for building the markdown text, consider using a list of strings and join them at the end. This can improve performance as string concatenation in Python creates a new string and can be costly for large strings.

File: pr_agent/algo/utils.py (94-140)

Example code:

Existing code:

markdown_text = ""
if gfm_supported and 'relevant line' in code_suggestions:
    markdown_text += '<table>'
    relevant_file = ''
    for sub_key, sub_value in code_suggestions.items():
        try:
            if sub_key.lower() == 'relevant file':
                relevant_file = sub_value.strip('`').strip('"').strip("'")
                markdown_text += f"<tr><td>{sub_key}</td><td>{relevant_file}</td></tr>"
            elif sub_key.lower() == 'suggestion':
                markdown_text += f"<tr><td>{sub_key} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td><strong>{sub_value}</strong></td></tr>"
            elif sub_key.lower() == 'relevant line':
                markdown_text += f"<tr><td>relevant line</td>"
                sub_value_list = sub_value.split('](')
                relevant_line = sub_value_list[0].lstrip('`').lstrip('[')
                if len(sub_value_list) > 1:
                    link = sub_value_list[1].rstrip(')').strip('`')
                    markdown_text += f"<td><a href={link}>{relevant_line}</a></td>"
                else:
                    markdown_text += f"<td>{relevant_line}</td>"
                markdown_text += "</tr>"
        except Exception as e:
            get_logger().exception(f"Failed to parse code suggestion: {e}")
            pass
    markdown_text += '</table>'
    markdown_text += "<hr>"
else:
    for sub_key, sub_value in code_suggestions.items():
        if isinstance(sub_value, dict):  # "code example"
            markdown_text += f"  - **{sub_key}:**\n"
            for code_key, code_value in sub_value.items():  # 'before' and 'after' code
                code_str = f"```\n{code_value}\n```"
                code_str_indented = textwrap.indent(code_str, '        ')
                markdown_text += f"    - **{code_key}:**\n{code_str_indented}\n"
        else:
            if "relevant file" in sub_key.lower():
                markdown_text += f"\n  - **{sub_key}:** {sub_value}  \n"
            else:
                markdown_text += f"   **{sub_key}:** {sub_value}  \n"
            if not gfm_supported:
                if "relevant line" not in sub_key.lower():  # nicer presentation
                    markdown_text = markdown_text.rstrip('\n') + "   \n"  # works for gitlab and bitbucker
    markdown_text += "\n"
return markdown_text

Improved code:

markdown_text = []
if gfm_supported and 'relevant line' in code_suggestions:
    markdown_text.append('<table>')
    relevant_file = ''
    for sub_key, sub_value in code_suggestions.items():
        try:
            if sub_key.lower() == 'relevant file':
                relevant_file = sub_value.strip('`').strip('"').strip("'")
                markdown_text.append(f"<tr><td>{sub_key}</td><td>{relevant_file}</td></tr>")
            elif sub_key.lower() == 'suggestion':
                markdown_text.append(f"<tr><td>{sub_key} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td><strong>{sub_value}</strong></td></tr>")
            elif sub_key.lower() == 'relevant line':
                markdown_text.append(f"<tr><td>relevant line</td>")
                sub_value_list = sub_value.split('](')
                relevant_line = sub_value_list[0].lstrip('`').lstrip('[')
                if len(sub_value_list) > 1:
                    link = sub_value_list[1].rstrip(')').strip('`')
                    markdown_text.append(f"<td><a href={link}>{relevant_line}</a></td>")
                else:
                    markdown_text.append(f"<td>{relevant_line}</td>")
                markdown_text.append("</tr>")
        except Exception as e:
            get_logger().exception(f"Failed to parse code suggestion: {e}")
            pass
    markdown_text.append('</table>')
    markdown_text.append("<hr>")
else:
    for sub_key, sub_value in code_suggestions.items():
        if isinstance(sub_value, dict):  # "code example"
            markdown_text.append(f"  - **{sub_key}:**\n")
            for code_key, code_value in sub_value.items():  # 'before' and 'after' code
                code_str = f"```\n{code_value}\n```"
                code_str_indented = textwrap.indent(code_str, '        ')
                markdown_text.append(f"    - **{code_key}:**\n{code_str_indented}\n")
        else:
            if "relevant file" in sub_key.lower():
                markdown_text.append(f"\n  - **{sub_key}:** {sub_value}  \n")
            else:
                markdown_text.append(f"   **{sub_key}:** {sub_value}  \n")
            if not gfm_supported:
                if "relevant line" not in sub_key.lower():  # nicer presentation
                    markdown_text.append(markdown_text.pop().rstrip('\n') + "   \n")  # works for gitlab and bitbucker
    markdown_text.append("\n")
return ''.join(markdown_text)

@mrT23
Copy link
Collaborator Author

mrT23 commented Dec 10, 2023

Persistent review updated to latest commit 654938f

1 similar comment
@mrT23
Copy link
Collaborator Author

mrT23 commented Dec 10, 2023

Persistent review updated to latest commit 654938f

@mrT23 mrT23 requested a review from hussam789 December 10, 2023 15:47
@mrT23
Copy link
Collaborator Author

mrT23 commented Dec 10, 2023

/describe

@github-actions github-actions bot changed the title feat: Enhance code feedback formatting in utils.py Enhancement of Code Feedback Formatting in utils.py Dec 10, 2023
@github-actions github-actions bot added the enhancement New feature or request label Dec 10, 2023
Copy link
Contributor

PR Description updated to latest commit (a247fc3)

@mrT23 mrT23 merged commit f7a6348 into main Dec 11, 2023
4 checks passed
@mrT23 mrT23 deleted the tr/review_graphics branch December 11, 2023 06:37
yochail pushed a commit to yochail/pr-agent that referenced this pull request Feb 11, 2024
Enhancement of Code Feedback Formatting in utils.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants