fix long comments

This commit is contained in:
Hussam.lawen
2024-08-12 12:27:09 +03:00
parent ef37271ce9
commit 9c1ab06491
4 changed files with 23 additions and 1 deletions

View File

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

View File

@ -256,6 +256,9 @@ class GitProvider(ABC):
except Exception as e: except Exception as e:
return -1 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: def get_main_pr_language(languages, files) -> str:
""" """
@ -326,6 +329,8 @@ def get_main_pr_language(languages, files) -> str:
return main_language_str return main_language_str
class IncrementalPR: class IncrementalPR:
def __init__(self, is_incremental: bool = False): def __init__(self, is_incremental: bool = False):
self.is_incremental = is_incremental self.is_incremental = is_incremental

View File

@ -26,6 +26,7 @@ class GithubProvider(GitProvider):
self.installation_id = context.get("installation_id", None) self.installation_id = context.get("installation_id", None)
except Exception: except Exception:
self.installation_id = None 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 = 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.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() 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: if is_temporary and not get_settings().config.publish_output_progress:
get_logger().debug(f"Skipping publish_comment for temporary comment: {pr_comment}") get_logger().debug(f"Skipping publish_comment for temporary comment: {pr_comment}")
return return
pr_comment = self.limit_output_characters(pr_comment, self.max_comment_chars)
response = self.pr.create_issue_comment(pr_comment) response = self.pr.create_issue_comment(pr_comment)
if hasattr(response, "user") and hasattr(response.user, "login"): if hasattr(response, "user") and hasattr(response.user, "login"):
self.github_user_id = response.user.login self.github_user_id = response.user.login
@ -265,11 +266,13 @@ class GithubProvider(GitProvider):
return 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):
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)]) 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, def create_inline_comment(self, body: str, relevant_file: str, relevant_line_in_file: str,
absolute_position: int = None): 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, position, absolute_position = find_line_number_of_relevant_line_in_file(self.diff_files,
relevant_file.strip('`'), relevant_file.strip('`'),
relevant_line_in_file, relevant_line_in_file,
@ -442,10 +445,12 @@ class GithubProvider(GitProvider):
return False return False
def edit_comment(self, comment, body: str): def edit_comment(self, comment, body: str):
body = self.limit_output_characters(body, self.max_comment_chars)
comment.edit(body=body) comment.edit(body=body)
def edit_comment_from_comment_id(self, comment_id: int, body: str): def edit_comment_from_comment_id(self, comment_id: int, body: str):
try: try:
body = self.limit_output_characters(body, self.max_comment_chars)
# self.pr.get_issue_comment(comment_id).edit(body) # self.pr.get_issue_comment(comment_id).edit(body)
headers, data_patch = self.pr._requester.requestJsonAndCheck( headers, data_patch = self.pr._requester.requestJsonAndCheck(
"PATCH", f"{self.base_url}/repos/{self.repo}/issues/comments/{comment_id}", "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): def reply_to_comment_from_comment_id(self, comment_id: int, body: str):
try: try:
body = self.limit_output_characters(body, self.max_comment_chars)
# self.pr.get_issue_comment(comment_id).edit(body) # self.pr.get_issue_comment(comment_id).edit(body)
headers, data_patch = self.pr._requester.requestJsonAndCheck( headers, data_patch = self.pr._requester.requestJsonAndCheck(
"POST", f"{self.base_url}/repos/{self.repo}/pulls/{self.pr_num}/comments/{comment_id}/replies", "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, url=gitlab_url,
oauth_token=gitlab_access_token oauth_token=gitlab_access_token
) )
self.max_comment_chars = 65000
self.id_project = None self.id_project = None
self.id_mr = None self.id_mr = None
self.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) 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): 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}) comment = self.mr.notes.create({'body': mr_comment})
if is_temporary: if is_temporary:
self.temp_comments.append(comment) self.temp_comments.append(comment)
return comment return comment
def edit_comment(self, comment, body: str): 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} ) self.mr.notes.update(comment.id,{'body': body} )
def edit_comment_from_comment_id(self, comment_id: int, body: str): 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 = self.mr.notes.get(comment_id)
comment.body = body comment.body = body
comment.save() comment.save()
def reply_to_comment_from_comment_id(self, comment_id: int, body: str): 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 = self.mr.discussions.get(comment_id)
discussion.notes.create({'body': body}) 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):
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, edit_type, found, source_line_no, target_file, target_line_no = self.search_line(relevant_file,
relevant_line_in_file) relevant_line_in_file)
self.send_inline_comment(body, edit_type, found, relevant_file, relevant_line_in_file, source_line_no, self.send_inline_comment(body, edit_type, found, relevant_file, relevant_line_in_file, source_line_no,