From 3456c8e0398c067a10e191be7a877517ecd96d11 Mon Sep 17 00:00:00 2001 From: mrT23 Date: Fri, 16 Aug 2024 08:58:51 +0300 Subject: [PATCH 1/2] Add 'original_suggestion' parameter to publish_inline_comment methods across git providers for enhanced inline comment handling --- pr_agent/git_providers/azuredevops_provider.py | 2 +- pr_agent/git_providers/bitbucket_provider.py | 2 +- pr_agent/git_providers/bitbucket_server_provider.py | 2 +- pr_agent/git_providers/codecommit_provider.py | 2 +- pr_agent/git_providers/gerrit_provider.py | 2 +- pr_agent/git_providers/git_provider.py | 2 +- pr_agent/git_providers/github_provider.py | 2 +- pr_agent/git_providers/gitlab_provider.py | 12 ++++++++---- pr_agent/git_providers/local_git_provider.py | 2 +- pr_agent/settings/configuration.toml | 2 +- pr_agent/tools/pr_reviewer.py | 2 +- 11 files changed, 18 insertions(+), 14 deletions(-) diff --git a/pr_agent/git_providers/azuredevops_provider.py b/pr_agent/git_providers/azuredevops_provider.py index cf488cbee..6c45019fc 100644 --- a/pr_agent/git_providers/azuredevops_provider.py +++ b/pr_agent/git_providers/azuredevops_provider.py @@ -444,7 +444,7 @@ def remove_initial_comment(self): except Exception as e: get_logger().exception(f"Failed to remove temp comments, error: {e}") - def publish_inline_comment(self, body: str, relevant_file: str, relevant_line_in_file: str): + def publish_inline_comment(self, body: str, relevant_file: str, relevant_line_in_file: str, original_suggestion=None): self.publish_inline_comments([self.create_inline_comment(body, relevant_file, relevant_line_in_file)]) diff --git a/pr_agent/git_providers/bitbucket_provider.py b/pr_agent/git_providers/bitbucket_provider.py index 1c990dbab..dd9bfc4c3 100644 --- a/pr_agent/git_providers/bitbucket_provider.py +++ b/pr_agent/git_providers/bitbucket_provider.py @@ -331,7 +331,7 @@ def create_inline_comment(self, body: str, relevant_file: str, relevant_line_in_ return dict(body=body, path=path, position=absolute_position) if subject_type == "LINE" else {} - def publish_inline_comment(self, comment: str, from_line: int, file: str): + def publish_inline_comment(self, comment: str, from_line: int, file: str, original_suggestion=None): comment = self.limit_output_characters(comment, self.max_comment_length) payload = json.dumps( { "content": { diff --git a/pr_agent/git_providers/bitbucket_server_provider.py b/pr_agent/git_providers/bitbucket_server_provider.py index 6c56ef84a..b8358176f 100644 --- a/pr_agent/git_providers/bitbucket_server_provider.py +++ b/pr_agent/git_providers/bitbucket_server_provider.py @@ -224,7 +224,7 @@ def create_inline_comment(self, body: str, relevant_file: str, relevant_line_in_ path = relevant_file.strip() return dict(body=body, path=path, position=absolute_position) if subject_type == "LINE" else {} - def publish_inline_comment(self, comment: str, from_line: int, file: str): + def publish_inline_comment(self, comment: str, from_line: int, file: str, original_suggestion=None): payload = { "text": comment, "severity": "NORMAL", diff --git a/pr_agent/git_providers/codecommit_provider.py b/pr_agent/git_providers/codecommit_provider.py index 41de6bb31..89a0254df 100644 --- a/pr_agent/git_providers/codecommit_provider.py +++ b/pr_agent/git_providers/codecommit_provider.py @@ -225,7 +225,7 @@ def remove_initial_comment(self): def remove_comment(self, comment): return "" # not implemented yet - def publish_inline_comment(self, body: str, relevant_file: str, relevant_line_in_file: str): + def publish_inline_comment(self, body: str, relevant_file: str, relevant_line_in_file: str, original_suggestion=None): # https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/codecommit/client/post_comment_for_compared_commit.html raise NotImplementedError("CodeCommit provider does not support publishing inline comments yet") diff --git a/pr_agent/git_providers/gerrit_provider.py b/pr_agent/git_providers/gerrit_provider.py index 8e33151a9..8ec1be135 100644 --- a/pr_agent/git_providers/gerrit_provider.py +++ b/pr_agent/git_providers/gerrit_provider.py @@ -376,7 +376,7 @@ def publish_inline_comments(self, comments: list[dict]): 'provider') def publish_inline_comment(self, body: str, relevant_file: str, - relevant_line_in_file: str): + relevant_line_in_file: str, original_suggestion=None): raise NotImplementedError( 'Publishing inline comments is not implemented for the gerrit ' 'provider') diff --git a/pr_agent/git_providers/git_provider.py b/pr_agent/git_providers/git_provider.py index 6a0d1b694..ab0b57157 100644 --- a/pr_agent/git_providers/git_provider.py +++ b/pr_agent/git_providers/git_provider.py @@ -182,7 +182,7 @@ def publish_persistent_comment_full(self, pr_comment: str, @abstractmethod - def publish_inline_comment(self, body: str, relevant_file: str, relevant_line_in_file: str): + def publish_inline_comment(self, body: str, relevant_file: str, relevant_line_in_file: str, original_suggestion=None): pass def create_inline_comment(self, body: str, relevant_file: str, relevant_line_in_file: str, diff --git a/pr_agent/git_providers/github_provider.py b/pr_agent/git_providers/github_provider.py index 3a5b48e88..51e8d6b62 100644 --- a/pr_agent/git_providers/github_provider.py +++ b/pr_agent/git_providers/github_provider.py @@ -267,7 +267,7 @@ def publish_comment(self, pr_comment: str, is_temporary: bool = False): self.pr.comments_list.append(response) return response - def publish_inline_comment(self, body: str, relevant_file: str, relevant_line_in_file: str): + def publish_inline_comment(self, body: str, relevant_file: str, relevant_line_in_file: str, original_suggestion=None): body = self.limit_output_characters(body, self.max_comment_chars) self.publish_inline_comments([self.create_inline_comment(body, relevant_file, relevant_line_in_file)]) diff --git a/pr_agent/git_providers/gitlab_provider.py b/pr_agent/git_providers/gitlab_provider.py index b76528b5c..5e6fe52fc 100644 --- a/pr_agent/git_providers/gitlab_provider.py +++ b/pr_agent/git_providers/gitlab_provider.py @@ -211,12 +211,12 @@ def reply_to_comment_from_comment_id(self, comment_id: int, body: str): discussion = self.mr.discussions.get(comment_id) discussion.notes.create({'body': body}) - def publish_inline_comment(self, body: str, relevant_file: str, relevant_line_in_file: str): + def publish_inline_comment(self, body: str, relevant_file: str, relevant_line_in_file: str, original_suggestion=None): body = self.limit_output_characters(body, self.max_comment_chars) edit_type, found, source_line_no, target_file, target_line_no = self.search_line(relevant_file, relevant_line_in_file) self.send_inline_comment(body, edit_type, found, relevant_file, relevant_line_in_file, source_line_no, - target_file, target_line_no) + target_file, target_line_no, original_suggestion) def create_inline_comment(self, body: str, relevant_file: str, relevant_line_in_file: str, absolute_position: int = None): raise NotImplementedError("Gitlab provider does not support creating inline comments yet") @@ -230,7 +230,8 @@ def get_comment_body_from_comment_id(self, comment_id: int): def send_inline_comment(self, body: str, edit_type: str, found: bool, relevant_file: str, relevant_line_in_file: str, - source_line_no: int, target_file: str, target_line_no: int, original_suggestion) -> None: + source_line_no: int, target_file: str, target_line_no: int, + original_suggestion=None) -> None: if not found: get_logger().info(f"Could not find position for {relevant_file} {relevant_line_in_file}") else: @@ -326,7 +327,10 @@ def get_relevant_diff(self, relevant_file: str, relevant_line_in_file: str) -> O def publish_code_suggestions(self, code_suggestions: list) -> bool: for suggestion in code_suggestions: try: - original_suggestion = suggestion['original_suggestion'] + if 'original_suggestion' in suggestion: + original_suggestion = suggestion['original_suggestion'] + else: + original_suggestion = suggestion body = suggestion['body'] relevant_file = suggestion['relevant_file'] relevant_lines_start = suggestion['relevant_lines_start'] diff --git a/pr_agent/git_providers/local_git_provider.py b/pr_agent/git_providers/local_git_provider.py index 678a519ca..c104224ac 100644 --- a/pr_agent/git_providers/local_git_provider.py +++ b/pr_agent/git_providers/local_git_provider.py @@ -119,7 +119,7 @@ def publish_comment(self, pr_comment: str, is_temporary: bool = False): # Write the string to the file file.write(pr_comment) - def publish_inline_comment(self, body: str, relevant_file: str, relevant_line_in_file: str): + def publish_inline_comment(self, body: str, relevant_file: str, relevant_line_in_file: str, original_suggestion=None): raise NotImplementedError('Publishing inline comments is not implemented for the local git provider') def publish_inline_comments(self, comments: list[dict]): diff --git a/pr_agent/settings/configuration.toml b/pr_agent/settings/configuration.toml index 5bfc5071e..67273c46d 100644 --- a/pr_agent/settings/configuration.toml +++ b/pr_agent/settings/configuration.toml @@ -1,7 +1,7 @@ [config] # models model="gpt-4-turbo-2024-04-09" -model_turbo="gpt-4o" +model_turbo="gpt-4o-2024-08-06" fallback_models=["gpt-4-0125-preview"] # CLI git_provider="github" diff --git a/pr_agent/tools/pr_reviewer.py b/pr_agent/tools/pr_reviewer.py index 41f6287a9..b7c39faca 100644 --- a/pr_agent/tools/pr_reviewer.py +++ b/pr_agent/tools/pr_reviewer.py @@ -287,7 +287,7 @@ def _publish_inline_code_comments(self) -> None: if comment: comments.append(comment) else: - self.git_provider.publish_inline_comment(content, relevant_file, relevant_line_in_file) + self.git_provider.publish_inline_comment(content, relevant_file, relevant_line_in_file, suggestion) if comments: self.git_provider.publish_inline_comments(comments) From 4a71ec90c6b9b00b08002bd53275b267f9289cb3 Mon Sep 17 00:00:00 2001 From: mrT23 Date: Fri, 16 Aug 2024 09:04:45 +0300 Subject: [PATCH 2/2] Add null check for 'suggestion' in publish_code_suggestions method in gitlab_provider.py --- pr_agent/git_providers/gitlab_provider.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pr_agent/git_providers/gitlab_provider.py b/pr_agent/git_providers/gitlab_provider.py index 5e6fe52fc..9e9944400 100644 --- a/pr_agent/git_providers/gitlab_provider.py +++ b/pr_agent/git_providers/gitlab_provider.py @@ -327,7 +327,7 @@ def get_relevant_diff(self, relevant_file: str, relevant_line_in_file: str) -> O def publish_code_suggestions(self, code_suggestions: list) -> bool: for suggestion in code_suggestions: try: - if 'original_suggestion' in suggestion: + if suggestion and 'original_suggestion' in suggestion: original_suggestion = suggestion['original_suggestion'] else: original_suggestion = suggestion