mirror of
https://github.com/qodo-ai/pr-agent.git
synced 2025-07-04 12:50:38 +08:00
Merge pull request #515 from Codium-ai/tr/review_graphics
Enhancement of Code Feedback Formatting in utils.py
This commit is contained in:
@ -59,14 +59,14 @@ def convert_to_markdown(output_data: dict, gfm_supported: bool=True) -> str:
|
|||||||
if key.lower() == 'code feedback':
|
if key.lower() == 'code feedback':
|
||||||
if gfm_supported:
|
if gfm_supported:
|
||||||
markdown_text += f"\n\n- "
|
markdown_text += f"\n\n- "
|
||||||
markdown_text += f"<details><summary> { emoji } Code feedback:</summary>\n\n"
|
markdown_text += f"<details><summary> { emoji } Code feedback:</summary>"
|
||||||
else:
|
else:
|
||||||
markdown_text += f"\n\n- **{emoji} Code feedback:**\n\n"
|
markdown_text += f"\n\n- **{emoji} Code feedback:**\n\n"
|
||||||
else:
|
else:
|
||||||
markdown_text += f"- {emoji} **{key}:**\n\n"
|
markdown_text += f"- {emoji} **{key}:**\n\n"
|
||||||
for item in value:
|
for i, item in enumerate(value):
|
||||||
if isinstance(item, dict) and key.lower() == 'code feedback':
|
if isinstance(item, dict) and key.lower() == 'code feedback':
|
||||||
markdown_text += parse_code_suggestion(item, gfm_supported)
|
markdown_text += parse_code_suggestion(item, i, gfm_supported)
|
||||||
elif item:
|
elif item:
|
||||||
markdown_text += f" - {item}\n"
|
markdown_text += f" - {item}\n"
|
||||||
if key.lower() == 'code feedback':
|
if key.lower() == 'code feedback':
|
||||||
@ -80,7 +80,7 @@ def convert_to_markdown(output_data: dict, gfm_supported: bool=True) -> str:
|
|||||||
return markdown_text
|
return markdown_text
|
||||||
|
|
||||||
|
|
||||||
def parse_code_suggestion(code_suggestions: dict, gfm_supported: bool=True) -> str:
|
def parse_code_suggestion(code_suggestions: dict, i: int = 0, gfm_supported: bool = True) -> str:
|
||||||
"""
|
"""
|
||||||
Convert a dictionary of data into markdown format.
|
Convert a dictionary of data into markdown format.
|
||||||
|
|
||||||
@ -91,24 +91,52 @@ def parse_code_suggestion(code_suggestions: dict, gfm_supported: bool=True) -> s
|
|||||||
str: A string containing the markdown formatted text generated from the input dictionary.
|
str: A string containing the markdown formatted text generated from the input dictionary.
|
||||||
"""
|
"""
|
||||||
markdown_text = ""
|
markdown_text = ""
|
||||||
for sub_key, sub_value in code_suggestions.items():
|
if gfm_supported and 'relevant line' in code_suggestions:
|
||||||
if isinstance(sub_value, dict): # "code example"
|
if i == 0:
|
||||||
markdown_text += f" - **{sub_key}:**\n"
|
markdown_text += "<hr>"
|
||||||
for code_key, code_value in sub_value.items(): # 'before' and 'after' code
|
markdown_text += '<table>'
|
||||||
code_str = f"```\n{code_value}\n```"
|
for sub_key, sub_value in code_suggestions.items():
|
||||||
code_str_indented = textwrap.indent(code_str, ' ')
|
try:
|
||||||
markdown_text += f" - **{code_key}:**\n{code_str_indented}\n"
|
if sub_key.lower() == 'relevant file':
|
||||||
else:
|
relevant_file = sub_value.strip('`').strip('"').strip("'")
|
||||||
if "relevant file" in sub_key.lower():
|
markdown_text += f"<tr><td>{sub_key}</td><td>{relevant_file}</td></tr>"
|
||||||
markdown_text += f"\n - **{sub_key}:** {sub_value} \n"
|
# continue
|
||||||
|
elif sub_key.lower() == 'suggestion':
|
||||||
|
markdown_text += f"<tr><td>{sub_key} </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:
|
else:
|
||||||
markdown_text += f" **{sub_key}:** {sub_value} \n"
|
if "relevant file" in sub_key.lower():
|
||||||
if not gfm_supported:
|
markdown_text += f"\n - **{sub_key}:** {sub_value} \n"
|
||||||
if "relevant line" not in sub_key.lower(): # nicer presentation
|
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
|
# markdown_text = markdown_text.rstrip('\n') + "\\\n" # works for gitlab
|
||||||
markdown_text = markdown_text.rstrip('\n') + " \n" # works for gitlab and bitbucker
|
markdown_text = markdown_text.rstrip('\n') + " \n" # works for gitlab and bitbucker
|
||||||
|
|
||||||
markdown_text += "\n"
|
markdown_text += "\n"
|
||||||
return markdown_text
|
return markdown_text
|
||||||
|
|
||||||
|
|
||||||
|
@ -211,7 +211,11 @@ class GitLabProvider(GitProvider):
|
|||||||
pos_obj['new_line'] = target_line_no - 1
|
pos_obj['new_line'] = target_line_no - 1
|
||||||
pos_obj['old_line'] = source_line_no - 1
|
pos_obj['old_line'] = source_line_no - 1
|
||||||
get_logger().debug(f"Creating comment in {self.id_mr} with body {body} and position {pos_obj}")
|
get_logger().debug(f"Creating comment in {self.id_mr} with body {body} and position {pos_obj}")
|
||||||
self.mr.discussions.create({'body': body, 'position': pos_obj})
|
try:
|
||||||
|
self.mr.discussions.create({'body': body, 'position': pos_obj})
|
||||||
|
except Exception as e:
|
||||||
|
get_logger().debug(
|
||||||
|
f"Failed to create comment in {self.id_mr} with position {pos_obj} (probably not a '+' line)")
|
||||||
|
|
||||||
def get_relevant_diff(self, relevant_file: str, relevant_line_in_file: int) -> Optional[dict]:
|
def get_relevant_diff(self, relevant_file: str, relevant_line_in_file: int) -> Optional[dict]:
|
||||||
changes = self.mr.changes() # Retrieve the changes for the merge request once
|
changes = self.mr.changes() # Retrieve the changes for the merge request once
|
||||||
|
@ -71,7 +71,7 @@ class TestConvertToMarkdown:
|
|||||||
- 📌 **Type of PR:** Test type\n\
|
- 📌 **Type of PR:** Test type\n\
|
||||||
- 🧪 **Relevant tests added:** no\n\
|
- 🧪 **Relevant tests added:** no\n\
|
||||||
- ✨ **Focused PR:** Yes\n\
|
- ✨ **Focused PR:** Yes\n\
|
||||||
- **General PR suggestions:** general suggestion...\n\n\n- <details><summary> 🤖 Code feedback:</summary>\n\n - **Code example:**\n - **Before:**\n ```\n Code before\n ```\n - **After:**\n ```\n Code after\n ```\n\n - **Code example:**\n - **Before:**\n ```\n Code before 2\n ```\n - **After:**\n ```\n Code after 2\n ```\n\n</details>\
|
- **General PR suggestions:** general suggestion...\n\n\n- <details><summary> 🤖 Code feedback:</summary> - **Code example:**\n - **Before:**\n ```\n Code before\n ```\n - **After:**\n ```\n Code after\n ```\n\n - **Code example:**\n - **Before:**\n ```\n Code before 2\n ```\n - **After:**\n ```\n Code after 2\n ```\n\n</details>\
|
||||||
"""
|
"""
|
||||||
assert convert_to_markdown(input_data).strip() == expected_output.strip()
|
assert convert_to_markdown(input_data).strip() == expected_output.strip()
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user