Skip to content

Commit 2f4545d

Browse files
committed
Refactor byte decoding in Bitbucket server provider using decode_if_bytes function
1 parent cbd490b commit 2f4545d

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

pr_agent/git_providers/bitbucket_server_provider.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from atlassian.bitbucket import Bitbucket
66
from requests.exceptions import HTTPError
77

8+
from ..algo.git_patch_processing import decode_if_bytes
89
from ..algo.language_handler import is_valid_file
910
from ..algo.types import EDIT_TYPE, FilePatchInfo
1011
from ..algo.utils import (find_line_number_of_relevant_line_in_file,
@@ -201,25 +202,21 @@ def get_diff_files(self) -> list[FilePatchInfo]:
201202
case 'ADD':
202203
edit_type = EDIT_TYPE.ADDED
203204
new_file_content_str = self.get_file(file_path, head_sha)
204-
if isinstance(new_file_content_str, (bytes, bytearray)):
205-
new_file_content_str = new_file_content_str.decode("utf-8")
205+
new_file_content_str = decode_if_bytes(new_file_content_str)
206206
original_file_content_str = ""
207207
case 'DELETE':
208208
edit_type = EDIT_TYPE.DELETED
209209
new_file_content_str = ""
210210
original_file_content_str = self.get_file(file_path, base_sha)
211-
if isinstance(original_file_content_str, (bytes, bytearray)):
212-
original_file_content_str = original_file_content_str.decode("utf-8")
211+
original_file_content_str = decode_if_bytes(original_file_content_str)
213212
case 'RENAME':
214213
edit_type = EDIT_TYPE.RENAMED
215214
case _:
216215
edit_type = EDIT_TYPE.MODIFIED
217216
original_file_content_str = self.get_file(file_path, base_sha)
218-
if isinstance(original_file_content_str, (bytes, bytearray)):
219-
original_file_content_str = original_file_content_str.decode("utf-8")
217+
original_file_content_str = decode_if_bytes(original_file_content_str)
220218
new_file_content_str = self.get_file(file_path, head_sha)
221-
if isinstance(new_file_content_str, (bytes, bytearray)):
222-
new_file_content_str = new_file_content_str.decode("utf-8")
219+
new_file_content_str = decode_if_bytes(new_file_content_str)
223220

224221
patch = load_large_diff(file_path, new_file_content_str, original_file_content_str)
225222

@@ -330,10 +327,10 @@ def publish_inline_comments(self, comments: list[dict]):
330327
for comment in comments:
331328
if 'position' in comment:
332329
self.publish_inline_comment(comment['body'], comment['position'], comment['path'])
333-
elif 'start_line' in comment: # multi-line comment
330+
elif 'start_line' in comment: # multi-line comment
334331
# note that bitbucket does not seem to support range - only a comment on a single line - https://community.developer.atlassian.com/t/api-post-endpoint-for-inline-pull-request-comments/60452
335332
self.publish_inline_comment(comment['body'], comment['start_line'], comment['path'])
336-
elif 'line' in comment: # single-line comment
333+
elif 'line' in comment: # single-line comment
337334
self.publish_inline_comment(comment['body'], comment['line'], comment['path'])
338335
else:
339336
get_logger().error(f"Could not publish inline comment: {comment}")

0 commit comments

Comments
 (0)