Skip to content

Commit

Permalink
Merge pull request #1133 from Codium-ai/tr/err_protections
Browse files Browse the repository at this point in the history
Add error handling for empty diff files in utils.py and optimize file…
  • Loading branch information
mrT23 authored Aug 13, 2024
2 parents 7ac9f27 + 86a9cfe commit 672cdc0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
3 changes: 3 additions & 0 deletions pr_agent/algo/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,9 @@ def find_line_number_of_relevant_line_in_file(diff_files: List[FilePatchInfo],
re_hunk_header = re.compile(
r"^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@[ ]?(.*)")

if not diff_files:
return position, absolute_position

for file in diff_files:
if file.filename and (file.filename.strip() == relevant_file):
patch = file.patch
Expand Down
22 changes: 16 additions & 6 deletions pr_agent/git_providers/bitbucket_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from ..algo.utils import find_line_number_of_relevant_line_in_file
from ..config_loader import get_settings
from ..log import get_logger
from .git_provider import GitProvider
from .git_provider import GitProvider, MAX_FILES_ALLOWED_FULL


def _gef_filename(diff):
Expand Down Expand Up @@ -198,20 +198,30 @@ def get_diff_files(self) -> list[FilePatchInfo]:

invalid_files_names = []
diff_files = []
counter_valid = 0
for index, diff in enumerate(diffs):
file_path = _gef_filename(diff)
if not is_valid_file(file_path):
invalid_files_names.append(file_path)
continue

try:
if diff.old.get_data("links"):
original_file_content_str = self._get_pr_file_content(diff.old.get_data("links")['self']['href'])
counter_valid += 1
if counter_valid < MAX_FILES_ALLOWED_FULL // 2: # factor 2 because bitbucket has limited API calls
if diff.old.get_data("links"):
original_file_content_str = self._get_pr_file_content(
diff.old.get_data("links")['self']['href'])
else:
original_file_content_str = ""
if diff.new.get_data("links"):
new_file_content_str = self._get_pr_file_content(diff.new.get_data("links")['self']['href'])
else:
new_file_content_str = ""
else:
if counter_valid == MAX_FILES_ALLOWED_FULL // 2:
get_logger().info(
f"Bitbucket too many files in PR, will avoid loading full content for rest of files")
original_file_content_str = ""
if diff.new.get_data("links"):
new_file_content_str = self._get_pr_file_content(diff.new.get_data("links")['self']['href'])
else:
new_file_content_str = ""
except Exception as e:
get_logger().exception(f"Error - bitbucket failed to get file content, error: {e}")
Expand Down

0 comments on commit 672cdc0

Please sign in to comment.