From d93a24bbf77be3500d25b1a64732464dbac48f6a Mon Sep 17 00:00:00 2001 From: "Hussam.lawen" Date: Sun, 11 Feb 2024 12:14:25 +0200 Subject: [PATCH 1/5] add loading comment to /improve --- pr_agent/tools/pr_code_suggestions.py | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/pr_agent/tools/pr_code_suggestions.py b/pr_agent/tools/pr_code_suggestions.py index b944047ea..83139abca 100644 --- a/pr_agent/tools/pr_code_suggestions.py +++ b/pr_agent/tools/pr_code_suggestions.py @@ -65,11 +65,16 @@ def __init__(self, pr_url: str, cli_mode=False, args: list = None, get_settings().pr_code_suggestions_prompt.system, get_settings().pr_code_suggestions_prompt.user) + self.progress = f"## Generating PR code suggestions\n\n" + self.progress += f"""\nWork in progress ...
\n""" + self.progress_response = None + async def run(self): try: get_logger().info('Generating code suggestions for PR...') + if get_settings().config.publish_output: - self.git_provider.publish_comment("Preparing suggestions...", is_temporary=True) + self.progress_response = self.git_provider.publish_comment(self.progress) get_logger().info('Preparing PR code suggestions...') if not self.is_extended: @@ -103,12 +108,20 @@ async def run(self): pr_body += HelpMessage.get_improve_usage_guide() pr_body += "\n\n" - self.git_provider.publish_comment(pr_body) + if self.progress_response: + self.git_provider.edit_comment(self.progress_response, body=pr_body) + else: + self.git_provider.publish_comment(pr_body) + else: get_logger().info('Pushing inline code suggestions...') self.push_inline_code_suggestions(data) + if self.progress_response: + self.progress_response.delete() except Exception as e: get_logger().error(f"Failed to generate code suggestions for PR, error: {e}") + if self.progress_response: + self.progress_response.delete() async def _prepare_prediction(self, model: str): get_logger().info('Getting PR diff...') @@ -162,7 +175,10 @@ def push_inline_code_suggestions(self, data): if not data['code_suggestions']: get_logger().info('No suggestions found to improve this PR.') - return self.git_provider.publish_comment('No suggestions found to improve this PR.') + if self.progress_response: + return self.git_provider.edit_comment(self.progress_response, body='No suggestions found to improve this PR.') + else: + return self.git_provider.publish_comment('No suggestions found to improve this PR.') for d in data['code_suggestions']: try: From a40643bbba4ba096e654604ff5234d3ab06a9f6b Mon Sep 17 00:00:00 2001 From: "Hussam.lawen" Date: Sun, 11 Feb 2024 12:20:06 +0200 Subject: [PATCH 2/5] add return response --- pr_agent/git_providers/github_provider.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pr_agent/git_providers/github_provider.py b/pr_agent/git_providers/github_provider.py index f5ec71299..7153f4030 100644 --- a/pr_agent/git_providers/github_provider.py +++ b/pr_agent/git_providers/github_provider.py @@ -202,6 +202,7 @@ def publish_comment(self, pr_comment: str, is_temporary: bool = False): if not hasattr(self.pr, 'comments_list'): self.pr.comments_list = [] self.pr.comments_list.append(response) + return response def publish_inline_comment(self, body: str, relevant_file: str, relevant_line_in_file: str): self.publish_inline_comments([self.create_inline_comment(body, relevant_file, relevant_line_in_file)]) From 73d2b1565dcf0d3a7855f789cef7fd89e9e73444 Mon Sep 17 00:00:00 2001 From: "Hussam.lawen" Date: Sun, 11 Feb 2024 12:31:30 +0200 Subject: [PATCH 3/5] Implement edit comment --- pr_agent/git_providers/bitbucket_provider.py | 7 +++++++ pr_agent/git_providers/git_provider.py | 3 +++ pr_agent/git_providers/github_provider.py | 3 +++ pr_agent/git_providers/gitlab_provider.py | 8 ++++++++ 4 files changed, 21 insertions(+) diff --git a/pr_agent/git_providers/bitbucket_provider.py b/pr_agent/git_providers/bitbucket_provider.py index a5b9d801e..751c413d5 100644 --- a/pr_agent/git_providers/bitbucket_provider.py +++ b/pr_agent/git_providers/bitbucket_provider.py @@ -187,6 +187,13 @@ def publish_comment(self, pr_comment: str, is_temporary: bool = False): comment = self.pr.comment(pr_comment) if is_temporary: self.temp_comments.append(comment["id"]) + return comment + + def edit_comment(self, comment, body: str): + try: + comment.update(body) + except Exception as e: + get_logger().exception(f"Failed to update comment, error: {e}") def remove_initial_comment(self): try: diff --git a/pr_agent/git_providers/git_provider.py b/pr_agent/git_providers/git_provider.py index 98efcf235..58d2adf73 100644 --- a/pr_agent/git_providers/git_provider.py +++ b/pr_agent/git_providers/git_provider.py @@ -41,6 +41,9 @@ def get_user_id(self): def get_pr_description_full(self) -> str: pass + def edit_comment(self, comment, body: str): + pass + def get_pr_description(self, *, full: bool = True) -> str: from pr_agent.config_loader import get_settings from pr_agent.algo.utils import clip_tokens diff --git a/pr_agent/git_providers/github_provider.py b/pr_agent/git_providers/github_provider.py index 7153f4030..07f808dbd 100644 --- a/pr_agent/git_providers/github_provider.py +++ b/pr_agent/git_providers/github_provider.py @@ -395,6 +395,9 @@ def remove_comment(self, comment): except Exception as e: get_logger().exception(f"Failed to remove comment, error: {e}") + def edit_comment(self, comment, body: str): + comment.edit(body=body) + def get_title(self): return self.pr.title diff --git a/pr_agent/git_providers/gitlab_provider.py b/pr_agent/git_providers/gitlab_provider.py index f5d9f8e6d..edb7d0958 100644 --- a/pr_agent/git_providers/gitlab_provider.py +++ b/pr_agent/git_providers/gitlab_provider.py @@ -176,6 +176,14 @@ def publish_comment(self, mr_comment: str, is_temporary: bool = False): comment = self.mr.notes.create({'body': mr_comment}) if is_temporary: self.temp_comments.append(comment) + return comment + + def edit_comment(self, comment, body: str): + try: + comment.body = body + comment.save() + except Exception as e: + get_logger().exception(f"Failed to edit comment, error: {e}") def publish_inline_comment(self, body: str, relevant_file: str, relevant_line_in_file: str): edit_type, found, source_line_no, target_file, target_line_no = self.search_line(relevant_file, From 6c500413f1fda62db0750820ad13e268fdc7a1b0 Mon Sep 17 00:00:00 2001 From: "Hussam.lawen" Date: Sun, 11 Feb 2024 13:14:47 +0200 Subject: [PATCH 4/5] default behavior for bitbucket --- pr_agent/tools/pr_code_suggestions.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pr_agent/tools/pr_code_suggestions.py b/pr_agent/tools/pr_code_suggestions.py index 83139abca..2d4036f61 100644 --- a/pr_agent/tools/pr_code_suggestions.py +++ b/pr_agent/tools/pr_code_suggestions.py @@ -74,7 +74,10 @@ async def run(self): get_logger().info('Generating code suggestions for PR...') if get_settings().config.publish_output: - self.progress_response = self.git_provider.publish_comment(self.progress) + if self.git_provider.is_supported("gfm_markdown"): + self.progress_response = self.git_provider.publish_comment(self.progress) + else: + self.git_provider.publish_comment("Preparing suggestions...", is_temporary=True) get_logger().info('Preparing PR code suggestions...') if not self.is_extended: From 0e08520c0c2f13f79920b68ea9174b0a77564f95 Mon Sep 17 00:00:00 2001 From: "Hussam.lawen" Date: Sun, 11 Feb 2024 13:21:59 +0200 Subject: [PATCH 5/5] match pr-pro --- pr_agent/git_providers/github_provider.py | 6 +++--- pr_agent/git_providers/gitlab_provider.py | 6 +----- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/pr_agent/git_providers/github_provider.py b/pr_agent/git_providers/github_provider.py index 07f808dbd..375e46ec3 100644 --- a/pr_agent/git_providers/github_provider.py +++ b/pr_agent/git_providers/github_provider.py @@ -381,6 +381,9 @@ def publish_code_suggestions(self, code_suggestions: list) -> bool: get_logger().error(f"Failed to publish code suggestion, error: {e}") return False + def edit_comment(self, comment, body: str): + comment.edit(body=body) + def remove_initial_comment(self): try: for comment in getattr(self.pr, 'comments_list', []): @@ -395,9 +398,6 @@ def remove_comment(self, comment): except Exception as e: get_logger().exception(f"Failed to remove comment, error: {e}") - def edit_comment(self, comment, body: str): - comment.edit(body=body) - def get_title(self): return self.pr.title diff --git a/pr_agent/git_providers/gitlab_provider.py b/pr_agent/git_providers/gitlab_provider.py index edb7d0958..5c1cf8e9b 100644 --- a/pr_agent/git_providers/gitlab_provider.py +++ b/pr_agent/git_providers/gitlab_provider.py @@ -179,11 +179,7 @@ def publish_comment(self, mr_comment: str, is_temporary: bool = False): return comment def edit_comment(self, comment, body: str): - try: - comment.body = body - comment.save() - except Exception as e: - get_logger().exception(f"Failed to edit comment, error: {e}") + self.mr.notes.update(comment.id,{'body': body} ) def publish_inline_comment(self, body: str, relevant_file: str, relevant_line_in_file: str): edit_type, found, source_line_no, target_file, target_line_no = self.search_line(relevant_file,