2023-07-06 00:21:08 +03:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-07-18 23:14:47 +03:00
|
|
|
import difflib
|
2023-07-11 22:11:42 +03:00
|
|
|
import json
|
|
|
|
import re
|
2023-07-06 00:21:08 +03:00
|
|
|
import textwrap
|
2023-08-01 14:43:26 +03:00
|
|
|
from datetime import datetime
|
|
|
|
from typing import Any, List
|
2023-07-06 00:21:08 +03:00
|
|
|
|
2023-08-09 08:50:15 +03:00
|
|
|
import yaml
|
2023-08-01 14:43:26 +03:00
|
|
|
from starlette_context import context
|
|
|
|
from pr_agent.config_loader import get_settings, global_settings
|
2023-10-16 14:56:00 +03:00
|
|
|
from pr_agent.log import get_logger
|
2023-08-01 14:43:26 +03:00
|
|
|
|
|
|
|
|
|
|
|
def get_setting(key: str) -> Any:
|
|
|
|
try:
|
|
|
|
key = key.upper()
|
|
|
|
return context.get("settings", global_settings).get(key, global_settings.get(key, None))
|
|
|
|
except Exception:
|
|
|
|
return global_settings.get(key, None)
|
2023-07-06 00:21:08 +03:00
|
|
|
|
2023-09-12 07:43:15 +03:00
|
|
|
def convert_to_markdown(output_data: dict, gfm_supported: bool=True) -> str:
|
2023-07-20 10:51:21 +03:00
|
|
|
"""
|
|
|
|
Convert a dictionary of data into markdown format.
|
|
|
|
Args:
|
|
|
|
output_data (dict): A dictionary containing data to be converted to markdown format.
|
|
|
|
Returns:
|
|
|
|
str: The markdown formatted text generated from the input dictionary.
|
|
|
|
"""
|
2023-07-06 00:21:08 +03:00
|
|
|
markdown_text = ""
|
|
|
|
|
|
|
|
emojis = {
|
|
|
|
"Main theme": "🎯",
|
2023-08-22 20:10:36 +03:00
|
|
|
"PR summary": "📝",
|
2023-07-06 00:21:08 +03:00
|
|
|
"Type of PR": "📌",
|
2023-07-18 16:27:42 +03:00
|
|
|
"Score": "🏅",
|
2023-07-06 00:21:08 +03:00
|
|
|
"Relevant tests added": "🧪",
|
|
|
|
"Unrelated changes": "⚠️",
|
2023-07-11 08:50:28 +03:00
|
|
|
"Focused PR": "✨",
|
2023-07-06 00:21:08 +03:00
|
|
|
"Security concerns": "🔒",
|
2023-08-22 20:10:36 +03:00
|
|
|
"General suggestions": "💡",
|
2023-07-18 16:32:51 +03:00
|
|
|
"Insights from user's answers": "📝",
|
2023-08-05 10:34:09 +03:00
|
|
|
"Code feedback": "🤖",
|
2023-09-17 17:08:02 +03:00
|
|
|
"Estimated effort to review [1-5]": "⏱️",
|
2023-07-06 00:21:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for key, value in output_data.items():
|
2023-08-22 20:21:52 +03:00
|
|
|
if value is None or value == '' or value == {}:
|
2023-07-06 00:21:08 +03:00
|
|
|
continue
|
|
|
|
if isinstance(value, dict):
|
|
|
|
markdown_text += f"## {key}\n\n"
|
2023-09-10 14:06:13 +03:00
|
|
|
markdown_text += convert_to_markdown(value, gfm_supported)
|
2023-07-06 00:21:08 +03:00
|
|
|
elif isinstance(value, list):
|
2023-07-19 15:12:50 +03:00
|
|
|
emoji = emojis.get(key, "")
|
2023-08-22 20:10:36 +03:00
|
|
|
if key.lower() == 'code feedback':
|
2023-09-10 14:06:13 +03:00
|
|
|
if gfm_supported:
|
|
|
|
markdown_text += f"\n\n- **<details><summary> { emoji } Code feedback:**</summary>\n\n"
|
|
|
|
else:
|
|
|
|
markdown_text += f"\n\n- **{emoji} Code feedback:**\n\n"
|
2023-08-22 20:10:36 +03:00
|
|
|
else:
|
|
|
|
markdown_text += f"- {emoji} **{key}:**\n\n"
|
2023-07-06 00:21:08 +03:00
|
|
|
for item in value:
|
2023-08-05 10:34:09 +03:00
|
|
|
if isinstance(item, dict) and key.lower() == 'code feedback':
|
2023-10-05 18:08:02 +03:00
|
|
|
markdown_text += parse_code_suggestion(item, gfm_supported)
|
2023-07-06 00:21:08 +03:00
|
|
|
elif item:
|
|
|
|
markdown_text += f" - {item}\n"
|
2023-08-22 20:10:36 +03:00
|
|
|
if key.lower() == 'code feedback':
|
2023-09-10 14:06:13 +03:00
|
|
|
if gfm_supported:
|
|
|
|
markdown_text += "</details>\n\n"
|
|
|
|
else:
|
|
|
|
markdown_text += "\n\n"
|
2023-07-06 00:21:08 +03:00
|
|
|
elif value != 'n/a':
|
2023-07-19 15:12:50 +03:00
|
|
|
emoji = emojis.get(key, "")
|
2023-07-06 00:21:08 +03:00
|
|
|
markdown_text += f"- {emoji} **{key}:** {value}\n"
|
|
|
|
return markdown_text
|
|
|
|
|
|
|
|
|
2023-10-05 18:08:02 +03:00
|
|
|
def parse_code_suggestion(code_suggestions: dict, gfm_supported: bool=True) -> str:
|
2023-07-20 10:51:21 +03:00
|
|
|
"""
|
|
|
|
Convert a dictionary of data into markdown format.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
code_suggestions (dict): A dictionary containing data to be converted to markdown format.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
str: A string containing the markdown formatted text generated from the input dictionary.
|
|
|
|
"""
|
2023-07-06 00:21:08 +03:00
|
|
|
markdown_text = ""
|
|
|
|
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:
|
2023-07-13 08:10:36 +03:00
|
|
|
if "relevant file" in sub_key.lower():
|
2023-07-06 12:49:10 +03:00
|
|
|
markdown_text += f"\n - **{sub_key}:** {sub_value}\n"
|
2023-07-06 00:21:08 +03:00
|
|
|
else:
|
2023-07-06 12:49:10 +03:00
|
|
|
markdown_text += f" **{sub_key}:** {sub_value}\n"
|
2023-10-05 18:08:02 +03:00
|
|
|
if not gfm_supported:
|
|
|
|
if "relevant line" not in sub_key.lower(): # nicer presentation
|
2023-10-29 17:59:46 +02:00
|
|
|
# markdown_text = markdown_text.rstrip('\n') + "\\\n" # works for gitlab
|
|
|
|
markdown_text = markdown_text.rstrip('\n') + " \n" # works for gitlab and bitbucker
|
2023-07-06 12:49:10 +03:00
|
|
|
|
2023-07-06 00:21:08 +03:00
|
|
|
markdown_text += "\n"
|
|
|
|
return markdown_text
|
|
|
|
|
2023-07-11 22:11:42 +03:00
|
|
|
|
2023-07-17 01:44:40 +03:00
|
|
|
def try_fix_json(review, max_iter=10, code_suggestions=False):
|
2023-07-20 10:51:21 +03:00
|
|
|
"""
|
|
|
|
Fix broken or incomplete JSON messages and return the parsed JSON data.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
- review: A string containing the JSON message to be fixed.
|
|
|
|
- max_iter: An integer representing the maximum number of iterations to try and fix the JSON message.
|
2023-08-05 10:34:09 +03:00
|
|
|
- code_suggestions: A boolean indicating whether to try and fix JSON messages with code feedback.
|
2023-07-20 10:51:21 +03:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
- data: A dictionary containing the parsed JSON data.
|
|
|
|
|
|
|
|
The function attempts to fix broken or incomplete JSON messages by parsing until the last valid code suggestion.
|
2023-08-01 14:43:26 +03:00
|
|
|
If the JSON message ends with a closing bracket, the function calls the fix_json_escape_char function to fix the
|
|
|
|
message.
|
2023-08-05 10:34:09 +03:00
|
|
|
If code_suggestions is True and the JSON message contains code feedback, the function tries to fix the JSON
|
2023-08-01 14:43:26 +03:00
|
|
|
message by parsing until the last valid code suggestion.
|
|
|
|
The function uses regular expressions to find the last occurrence of "}," with any number of whitespaces or
|
|
|
|
newlines.
|
2023-07-20 10:51:21 +03:00
|
|
|
It tries to parse the JSON message with the closing bracket and checks if it is valid.
|
|
|
|
If the JSON message is valid, the parsed JSON data is returned.
|
2023-08-01 14:43:26 +03:00
|
|
|
If the JSON message is not valid, the last code suggestion is removed and the process is repeated until a valid JSON
|
|
|
|
message is obtained or the maximum number of iterations is reached.
|
2023-07-20 10:51:21 +03:00
|
|
|
If a valid JSON message is not obtained, an error is logged and an empty dictionary is returned.
|
|
|
|
"""
|
|
|
|
|
2023-07-17 01:44:40 +03:00
|
|
|
if review.endswith("}"):
|
|
|
|
return fix_json_escape_char(review)
|
2023-07-20 10:51:21 +03:00
|
|
|
|
2023-07-11 22:11:42 +03:00
|
|
|
data = {}
|
2023-07-17 01:44:40 +03:00
|
|
|
if code_suggestions:
|
|
|
|
closing_bracket = "]}"
|
|
|
|
else:
|
|
|
|
closing_bracket = "]}}"
|
2023-07-20 10:51:21 +03:00
|
|
|
|
2023-08-05 10:34:09 +03:00
|
|
|
if (review.rfind("'Code feedback': [") > 0 or review.rfind('"Code feedback": [') > 0) or \
|
|
|
|
(review.rfind("'Code suggestions': [") > 0 or review.rfind('"Code suggestions": [') > 0) :
|
2023-07-11 22:11:42 +03:00
|
|
|
last_code_suggestion_ind = [m.end() for m in re.finditer(r"\}\s*,", review)][-1] - 1
|
|
|
|
valid_json = False
|
2023-07-11 22:22:08 +03:00
|
|
|
iter_count = 0
|
2023-07-20 10:51:21 +03:00
|
|
|
|
2023-07-11 22:22:08 +03:00
|
|
|
while last_code_suggestion_ind > 0 and not valid_json and iter_count < max_iter:
|
2023-07-11 22:11:42 +03:00
|
|
|
try:
|
2023-07-17 01:44:40 +03:00
|
|
|
data = json.loads(review[:last_code_suggestion_ind] + closing_bracket)
|
2023-07-11 22:11:42 +03:00
|
|
|
valid_json = True
|
2023-07-17 01:44:40 +03:00
|
|
|
review = review[:last_code_suggestion_ind].strip() + closing_bracket
|
2023-07-11 22:11:42 +03:00
|
|
|
except json.decoder.JSONDecodeError:
|
|
|
|
review = review[:last_code_suggestion_ind]
|
|
|
|
last_code_suggestion_ind = [m.end() for m in re.finditer(r"\}\s*,", review)][-1] - 1
|
2023-07-11 22:22:08 +03:00
|
|
|
iter_count += 1
|
2023-07-20 10:51:21 +03:00
|
|
|
|
2023-07-11 22:11:42 +03:00
|
|
|
if not valid_json:
|
2023-10-16 14:56:00 +03:00
|
|
|
get_logger().error("Unable to decode JSON response from AI")
|
2023-07-11 22:11:42 +03:00
|
|
|
data = {}
|
2023-07-20 10:51:21 +03:00
|
|
|
|
2023-07-11 22:11:42 +03:00
|
|
|
return data
|
2023-07-17 01:44:40 +03:00
|
|
|
|
2023-07-18 11:34:57 +03:00
|
|
|
|
2023-07-17 01:44:40 +03:00
|
|
|
def fix_json_escape_char(json_message=None):
|
2023-07-20 10:51:21 +03:00
|
|
|
"""
|
|
|
|
Fix broken or incomplete JSON messages and return the parsed JSON data.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
json_message (str): A string containing the JSON message to be fixed.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
dict: A dictionary containing the parsed JSON data.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
None
|
|
|
|
|
2023-09-05 08:40:05 +03:00
|
|
|
"""
|
2023-07-17 01:44:40 +03:00
|
|
|
try:
|
|
|
|
result = json.loads(json_message)
|
|
|
|
except Exception as e:
|
|
|
|
# Find the offending character index:
|
|
|
|
idx_to_replace = int(str(e).split(' ')[-1].replace(')', ''))
|
|
|
|
# Remove the offending character:
|
|
|
|
json_message = list(json_message)
|
|
|
|
json_message[idx_to_replace] = ' '
|
|
|
|
new_message = ''.join(json_message)
|
2023-07-18 11:34:57 +03:00
|
|
|
return fix_json_escape_char(json_message=new_message)
|
|
|
|
return result
|
2023-07-18 23:14:47 +03:00
|
|
|
|
|
|
|
|
|
|
|
def convert_str_to_datetime(date_str):
|
2023-07-20 10:51:21 +03:00
|
|
|
"""
|
|
|
|
Convert a string representation of a date and time into a datetime object.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
date_str (str): A string representation of a date and time in the format '%a, %d %b %Y %H:%M:%S %Z'
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
datetime: A datetime object representing the input date and time.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
>>> convert_str_to_datetime('Mon, 01 Jan 2022 12:00:00 UTC')
|
|
|
|
datetime.datetime(2022, 1, 1, 12, 0, 0)
|
2023-09-05 08:40:05 +03:00
|
|
|
"""
|
2023-07-18 23:14:47 +03:00
|
|
|
datetime_format = '%a, %d %b %Y %H:%M:%S %Z'
|
|
|
|
return datetime.strptime(date_str, datetime_format)
|
|
|
|
|
|
|
|
|
2023-08-03 22:14:05 +03:00
|
|
|
def load_large_diff(filename, new_file_content_str: str, original_file_content_str: str) -> str:
|
2023-07-20 10:51:21 +03:00
|
|
|
"""
|
2023-08-01 14:43:26 +03:00
|
|
|
Generate a patch for a modified file by comparing the original content of the file with the new content provided as
|
|
|
|
input.
|
2023-07-20 10:51:21 +03:00
|
|
|
|
|
|
|
Args:
|
|
|
|
new_file_content_str: The new content of the file as a string.
|
|
|
|
original_file_content_str: The original content of the file as a string.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
The generated or provided patch string.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
None.
|
|
|
|
"""
|
2023-08-03 22:14:05 +03:00
|
|
|
patch = ""
|
|
|
|
try:
|
|
|
|
diff = difflib.unified_diff(original_file_content_str.splitlines(keepends=True),
|
|
|
|
new_file_content_str.splitlines(keepends=True))
|
|
|
|
if get_settings().config.verbosity_level >= 2:
|
2023-10-16 14:56:00 +03:00
|
|
|
get_logger().warning(f"File was modified, but no patch was found. Manually creating patch: {filename}.")
|
2023-08-03 22:14:05 +03:00
|
|
|
patch = ''.join(diff)
|
|
|
|
except Exception:
|
|
|
|
pass
|
2023-07-18 23:14:47 +03:00
|
|
|
return patch
|
2023-07-30 11:43:44 +03:00
|
|
|
|
|
|
|
|
2023-08-01 14:43:26 +03:00
|
|
|
def update_settings_from_args(args: List[str]) -> List[str]:
|
2023-07-30 12:14:26 +03:00
|
|
|
"""
|
|
|
|
Update the settings of the Dynaconf object based on the arguments passed to the function.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
args: A list of arguments passed to the function.
|
2023-07-30 12:16:43 +03:00
|
|
|
Example args: ['--pr_code_suggestions.extra_instructions="be funny',
|
|
|
|
'--pr_code_suggestions.num_code_suggestions=3']
|
2023-07-30 12:14:26 +03:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
None
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
ValueError: If the argument is not in the correct format.
|
|
|
|
|
|
|
|
"""
|
2023-08-01 14:43:26 +03:00
|
|
|
other_args = []
|
2023-07-30 12:14:26 +03:00
|
|
|
if args:
|
2023-07-30 11:43:44 +03:00
|
|
|
for arg in args:
|
2023-08-01 14:43:26 +03:00
|
|
|
arg = arg.strip()
|
|
|
|
if arg.startswith('--'):
|
2023-07-30 11:43:44 +03:00
|
|
|
arg = arg.strip('-').strip()
|
2023-08-20 10:03:57 +03:00
|
|
|
vals = arg.split('=', 1)
|
2023-07-30 12:14:26 +03:00
|
|
|
if len(vals) != 2:
|
2023-08-22 09:42:59 +03:00
|
|
|
if len(vals) > 2: # --extended is a valid argument
|
2023-10-16 14:56:00 +03:00
|
|
|
get_logger().error(f'Invalid argument format: {arg}')
|
2023-08-01 14:43:26 +03:00
|
|
|
other_args.append(arg)
|
|
|
|
continue
|
2023-08-20 10:03:57 +03:00
|
|
|
key, value = _fix_key_value(*vals)
|
2023-08-01 14:43:26 +03:00
|
|
|
get_settings().set(key, value)
|
2023-10-16 14:56:00 +03:00
|
|
|
get_logger().info(f'Updated setting {key} to: "{value}"')
|
2023-08-01 14:43:26 +03:00
|
|
|
else:
|
|
|
|
other_args.append(arg)
|
|
|
|
return other_args
|
2023-08-09 08:50:15 +03:00
|
|
|
|
|
|
|
|
2023-08-20 10:03:57 +03:00
|
|
|
def _fix_key_value(key: str, value: str):
|
|
|
|
key = key.strip().upper()
|
|
|
|
value = value.strip()
|
|
|
|
try:
|
|
|
|
value = yaml.safe_load(value)
|
|
|
|
except Exception as e:
|
2023-10-16 14:56:00 +03:00
|
|
|
get_logger().error(f"Failed to parse YAML for config override {key}={value}", exc_info=e)
|
2023-08-20 10:03:57 +03:00
|
|
|
return key, value
|
|
|
|
|
|
|
|
|
2023-08-09 08:50:15 +03:00
|
|
|
def load_yaml(review_text: str) -> dict:
|
2023-08-11 18:35:34 +03:00
|
|
|
review_text = review_text.removeprefix('```yaml').rstrip('`')
|
2023-08-09 08:50:15 +03:00
|
|
|
try:
|
2023-08-28 09:48:43 +03:00
|
|
|
data = yaml.safe_load(review_text)
|
2023-08-09 08:50:15 +03:00
|
|
|
except Exception as e:
|
2023-10-16 14:56:00 +03:00
|
|
|
get_logger().error(f"Failed to parse AI prediction: {e}")
|
2023-08-09 08:50:15 +03:00
|
|
|
data = try_fix_yaml(review_text)
|
|
|
|
return data
|
|
|
|
|
|
|
|
def try_fix_yaml(review_text: str) -> dict:
|
|
|
|
review_text_lines = review_text.split('\n')
|
|
|
|
data = {}
|
|
|
|
for i in range(1, len(review_text_lines)):
|
|
|
|
review_text_lines_tmp = '\n'.join(review_text_lines[:-i])
|
|
|
|
try:
|
|
|
|
data = yaml.load(review_text_lines_tmp, Loader=yaml.SafeLoader)
|
2023-10-16 14:56:00 +03:00
|
|
|
get_logger().info(f"Successfully parsed AI prediction after removing {i} lines")
|
2023-08-09 08:50:15 +03:00
|
|
|
break
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
return data
|
2023-10-24 22:28:57 +03:00
|
|
|
|
|
|
|
|
2023-10-26 23:28:33 +03:00
|
|
|
def set_custom_labels(variables):
|
2023-10-29 14:58:36 +02:00
|
|
|
if not get_settings().config.enable_custom_labels:
|
|
|
|
return
|
|
|
|
|
2023-10-24 22:28:57 +03:00
|
|
|
labels = get_settings().custom_labels
|
|
|
|
if not labels:
|
|
|
|
# set default labels
|
|
|
|
labels = ['Bug fix', 'Tests', 'Bug fix with tests', 'Refactoring', 'Enhancement', 'Documentation', 'Other']
|
|
|
|
labels_list = "\n - ".join(labels) if labels else ""
|
|
|
|
labels_list = f" - {labels_list}" if labels_list else ""
|
|
|
|
variables["custom_labels"] = labels_list
|
|
|
|
return
|
|
|
|
final_labels = ""
|
|
|
|
for k, v in labels.items():
|
|
|
|
final_labels += f" - {k} ({v['description']})\n"
|
|
|
|
variables["custom_labels"] = final_labels
|
2023-10-26 23:28:33 +03:00
|
|
|
variables["custom_labels_examples"] = f" - {list(labels.keys())[0]}"
|
2023-11-06 15:14:08 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get_user_labels(current_labels):
|
|
|
|
## Only keep labels that has been added by the user
|
|
|
|
if current_labels is None:
|
|
|
|
current_labels = []
|
|
|
|
user_labels = []
|
|
|
|
for label in current_labels:
|
|
|
|
if label in ['Bug fix', 'Tests', 'Refactoring', 'Enhancement', 'Documentation', 'Other']:
|
|
|
|
continue
|
|
|
|
if get_settings().config.enable_custom_labels:
|
|
|
|
if label in get_settings().custom_labels:
|
|
|
|
continue
|
|
|
|
user_labels.append(label)
|
|
|
|
if user_labels:
|
|
|
|
get_logger().info(f"Keeping user labels: {user_labels}")
|
|
|
|
return user_labels
|