diff --git a/Usage.md b/Usage.md index e80fea7b..9f4af800 100644 --- a/Usage.md +++ b/Usage.md @@ -122,6 +122,13 @@ keep_original_user_title = false ``` When a new PR is opened, PR-Agent will run the `describe` tool with the above parameters. +To cancel the automatic run of all the tools, set: +``` +[github_app] +pr_commands = [] +``` + + Note that a local `.pr_agent.toml` file enables you to edit and customize the default parameters of any tool, not just the ones that are run automatically. #### Editing the prompts diff --git a/pr_agent/algo/utils.py b/pr_agent/algo/utils.py index c7923d16..11f28e38 100644 --- a/pr_agent/algo/utils.py +++ b/pr_agent/algo/utils.py @@ -62,7 +62,7 @@ def convert_to_markdown(output_data: dict, gfm_supported: bool=True) -> str: markdown_text += f"- {emoji} **{key}:**\n\n" for item in value: if isinstance(item, dict) and key.lower() == 'code feedback': - markdown_text += parse_code_suggestion(item) + markdown_text += parse_code_suggestion(item, gfm_supported) elif item: markdown_text += f" - {item}\n" if key.lower() == 'code feedback': @@ -76,7 +76,7 @@ def convert_to_markdown(output_data: dict, gfm_supported: bool=True) -> str: return markdown_text -def parse_code_suggestion(code_suggestions: dict) -> str: +def parse_code_suggestion(code_suggestions: dict, gfm_supported: bool=True) -> str: """ Convert a dictionary of data into markdown format. @@ -99,6 +99,9 @@ def parse_code_suggestion(code_suggestions: dict) -> str: 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" markdown_text += "\n" return markdown_text diff --git a/pr_agent/git_providers/gitlab_provider.py b/pr_agent/git_providers/gitlab_provider.py index 33cbee2b..2e6f2140 100644 --- a/pr_agent/git_providers/gitlab_provider.py +++ b/pr_agent/git_providers/gitlab_provider.py @@ -1,3 +1,4 @@ +import hashlib import logging import re from typing import Optional, Tuple @@ -7,7 +8,7 @@ import gitlab from gitlab import GitlabGetError from ..algo.language_handler import is_valid_file -from ..algo.pr_processing import clip_tokens +from ..algo.pr_processing import clip_tokens, find_line_number_of_relevant_line_in_file from ..algo.utils import load_large_diff from ..config_loader import get_settings from .git_provider import EDIT_TYPE, FilePatchInfo, GitProvider @@ -175,8 +176,7 @@ class GitLabProvider(GitProvider): pos_obj['new_line'] = target_line_no - 1 pos_obj['old_line'] = source_line_no - 1 logging.debug(f"Creating comment in {self.id_mr} with body {body} and position {pos_obj}") - self.mr.discussions.create({'body': body, - 'position': pos_obj}) + self.mr.discussions.create({'body': body, 'position': pos_obj}) 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 @@ -386,3 +386,27 @@ class GitLabProvider(GitProvider): return pr_id except: return "" + + def generate_link_to_relevant_line_number(self, suggestion) -> str: + try: + relevant_file = suggestion['relevant file'].strip('`').strip("'") + relevant_line_str = suggestion['relevant line'] + if not relevant_line_str: + return "" + + position, absolute_position = find_line_number_of_relevant_line_in_file \ + (self.diff_files, relevant_file, relevant_line_str) + + if absolute_position != -1: + # link to right file only + link = f"https://gitlab.com/codiumai/pr-agent/-/blob/{self.mr.source_branch}/{relevant_file}?ref_type=heads#L{absolute_position}" + + # # link to diff + # sha_file = hashlib.sha1(relevant_file.encode('utf-8')).hexdigest() + # link = f"{self.pr.web_url}/diffs#{sha_file}_{absolute_position}_{absolute_position}" + return link + except Exception as e: + if get_settings().config.verbosity_level >= 2: + logging.info(f"Failed adding line link, error: {e}") + + return "" diff --git a/pr_agent/tools/pr_code_suggestions.py b/pr_agent/tools/pr_code_suggestions.py index 7f0b1264..e1d5206c 100644 --- a/pr_agent/tools/pr_code_suggestions.py +++ b/pr_agent/tools/pr_code_suggestions.py @@ -138,9 +138,9 @@ class PRCodeSuggestions: if get_settings().config.verbosity_level >= 2: logging.info(f"Could not parse suggestion: {d}") - is_successful = self.git_provider.publish_code_suggestions(code_suggestions) - if not is_successful: - logging.info("Failed to publish code suggestions, trying to publish each suggestion separately") + # is_successful = self.git_provider.publish_code_suggestions(code_suggestions) + if True: + # logging.info("Failed to publish code suggestions, trying to publish each suggestion separately") for code_suggestion in code_suggestions: self.git_provider.publish_code_suggestions([code_suggestion]) diff --git a/pr_agent/tools/pr_reviewer.py b/pr_agent/tools/pr_reviewer.py index 01e3f276..b96d5379 100644 --- a/pr_agent/tools/pr_reviewer.py +++ b/pr_agent/tools/pr_reviewer.py @@ -209,6 +209,22 @@ class PRReviewer: link = self.git_provider.generate_link_to_relevant_line_number(suggestion) if link: suggestion['relevant line'] = f"[{suggestion['relevant line']}]({link})" + else: + pass + # try: + # relevant_file = suggestion['relevant file'].strip('`').strip("'") + # relevant_line_str = suggestion['relevant line'] + # if not relevant_line_str: + # return "" + # + # position, absolute_position = find_line_number_of_relevant_line_in_file( + # self.git_provider.diff_files, relevant_file, relevant_line_str) + # if absolute_position != -1: + # suggestion[ + # 'relevant line'] = f"{suggestion['relevant line']} (line {absolute_position})" + # except: + # pass + # Add incremental review section if self.incremental.is_incremental: