Merge pull request #1119 from Codium-ai/hl/limit_long_comments

Hl/limit long comments
This commit is contained in:
Tal
2024-08-12 16:25:42 +03:00
committed by GitHub
5 changed files with 25 additions and 3 deletions

View File

@ -147,13 +147,13 @@ class LiteLLMAIHandler(BaseAiHandler):
response = await acompletion(**kwargs)
except (openai.APIError, openai.APITimeoutError) as e:
get_logger().error("Error during OpenAI inference: ", e)
get_logger().warning("Error during OpenAI inference: ", e)
raise
except (openai.RateLimitError) as e:
get_logger().error("Rate limit error during OpenAI inference: ", e)
raise
except (Exception) as e:
get_logger().error("Unknown error during OpenAI inference: ", e)
get_logger().warning("Unknown error during OpenAI inference: ", e)
raise openai.APIError from e
if response is None or len(response["choices"]) == 0:
raise openai.APIError

View File

@ -30,6 +30,7 @@ class BitbucketProvider(GitProvider):
s.headers["Content-Type"] = "application/json"
self.headers = s.headers
self.bitbucket_client = Cloud(session=s)
self.max_comment_length = 31000
self.workspace_slug = None
self.repo_slug = None
self.repo = None
@ -237,6 +238,7 @@ class BitbucketProvider(GitProvider):
self.publish_comment(pr_comment)
def publish_comment(self, pr_comment: str, is_temporary: bool = False):
pr_comment = self.limit_output_characters(pr_comment, self.max_comment_length)
comment = self.pr.comment(pr_comment)
if is_temporary:
self.temp_comments.append(comment["id"])
@ -244,6 +246,7 @@ class BitbucketProvider(GitProvider):
def edit_comment(self, comment, body: str):
try:
body = self.limit_output_characters(body, self.max_comment_length)
comment.update(body)
except Exception as e:
get_logger().exception(f"Failed to update comment, error: {e}")
@ -263,6 +266,7 @@ class BitbucketProvider(GitProvider):
# function to create_inline_comment
def create_inline_comment(self, body: str, relevant_file: str, relevant_line_in_file: str, absolute_position: int = None):
body = self.limit_output_characters(body, self.max_comment_length)
position, absolute_position = find_line_number_of_relevant_line_in_file(self.get_diff_files(),
relevant_file.strip('`'),
relevant_line_in_file, absolute_position)
@ -277,6 +281,7 @@ class BitbucketProvider(GitProvider):
def publish_inline_comment(self, comment: str, from_line: int, file: str):
comment = self.limit_output_characters(comment, self.max_comment_length)
payload = json.dumps( {
"content": {
"raw": comment,

View File

@ -256,6 +256,9 @@ class GitProvider(ABC):
except Exception as e:
return -1
def limit_output_characters(self, output: str, max_chars: int):
return output[:max_chars] + '...' if len(output) > max_chars else output
def get_main_pr_language(languages, files) -> str:
"""
@ -326,6 +329,8 @@ def get_main_pr_language(languages, files) -> str:
return main_language_str
class IncrementalPR:
def __init__(self, is_incremental: bool = False):
self.is_incremental = is_incremental

View File

@ -26,6 +26,7 @@ class GithubProvider(GitProvider):
self.installation_id = context.get("installation_id", None)
except Exception:
self.installation_id = None
self.max_comment_chars = 65000
self.base_url = get_settings().get("GITHUB.BASE_URL", "https://api.github.com").rstrip("/")
self.base_url_html = self.base_url.split("api/")[0].rstrip("/") if "api/" in self.base_url else "https://github.com"
self.github_client = self._get_github_client()
@ -254,7 +255,7 @@ class GithubProvider(GitProvider):
if is_temporary and not get_settings().config.publish_output_progress:
get_logger().debug(f"Skipping publish_comment for temporary comment: {pr_comment}")
return
pr_comment = self.limit_output_characters(pr_comment, self.max_comment_chars)
response = self.pr.create_issue_comment(pr_comment)
if hasattr(response, "user") and hasattr(response.user, "login"):
self.github_user_id = response.user.login
@ -265,11 +266,13 @@ class GithubProvider(GitProvider):
return response
def publish_inline_comment(self, body: str, relevant_file: str, relevant_line_in_file: str):
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)])
def create_inline_comment(self, body: str, relevant_file: str, relevant_line_in_file: str,
absolute_position: int = None):
body = self.limit_output_characters(body, self.max_comment_chars)
position, absolute_position = find_line_number_of_relevant_line_in_file(self.diff_files,
relevant_file.strip('`'),
relevant_line_in_file,
@ -442,10 +445,12 @@ class GithubProvider(GitProvider):
return False
def edit_comment(self, comment, body: str):
body = self.limit_output_characters(body, self.max_comment_chars)
comment.edit(body=body)
def edit_comment_from_comment_id(self, comment_id: int, body: str):
try:
body = self.limit_output_characters(body, self.max_comment_chars)
# self.pr.get_issue_comment(comment_id).edit(body)
headers, data_patch = self.pr._requester.requestJsonAndCheck(
"PATCH", f"{self.base_url}/repos/{self.repo}/issues/comments/{comment_id}",
@ -456,6 +461,7 @@ class GithubProvider(GitProvider):
def reply_to_comment_from_comment_id(self, comment_id: int, body: str):
try:
body = self.limit_output_characters(body, self.max_comment_chars)
# self.pr.get_issue_comment(comment_id).edit(body)
headers, data_patch = self.pr._requester.requestJsonAndCheck(
"POST", f"{self.base_url}/repos/{self.repo}/pulls/{self.pr_num}/comments/{comment_id}/replies",

View File

@ -33,6 +33,7 @@ class GitLabProvider(GitProvider):
url=gitlab_url,
oauth_token=gitlab_access_token
)
self.max_comment_chars = 65000
self.id_project = None
self.id_mr = None
self.mr = None
@ -188,24 +189,29 @@ class GitLabProvider(GitProvider):
self.publish_persistent_comment_full(pr_comment, initial_header, update_header, name, final_update_message)
def publish_comment(self, mr_comment: str, is_temporary: bool = False):
mr_comment = self.limit_output_characters(mr_comment, self.max_comment_chars)
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):
body = self.limit_output_characters(body, self.max_comment_chars)
self.mr.notes.update(comment.id,{'body': body} )
def edit_comment_from_comment_id(self, comment_id: int, body: str):
body = self.limit_output_characters(body, self.max_comment_chars)
comment = self.mr.notes.get(comment_id)
comment.body = body
comment.save()
def reply_to_comment_from_comment_id(self, comment_id: int, body: str):
body = self.limit_output_characters(body, self.max_comment_chars)
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):
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,