Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add loading comment to /improve #653

Merged
merged 5 commits into from
Feb 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions pr_agent/git_providers/bitbucket_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 3 additions & 0 deletions pr_agent/git_providers/git_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions pr_agent/git_providers/github_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)])
Expand Down Expand Up @@ -380,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', []):
Expand Down
4 changes: 4 additions & 0 deletions pr_agent/git_providers/gitlab_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ 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):
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,
Expand Down
25 changes: 22 additions & 3 deletions pr_agent/tools/pr_code_suggestions.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,19 @@ 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 ...<br>\n<img src="https://codium.ai/images/pr_agent/dual_ball_loading-crop.gif" width=48>"""
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)
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:
Expand Down Expand Up @@ -103,12 +111,20 @@ async def run(self):
pr_body += HelpMessage.get_improve_usage_guide()
pr_body += "\n</details>\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...')
Expand Down Expand Up @@ -162,7 +178,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:
Expand Down
Loading