Skip to content

Commit

Permalink
Merge pull request #1128 from Codium-ai/tr/err_protections
Browse files Browse the repository at this point in the history
Tr/err_protections
  • Loading branch information
mrT23 authored Aug 13, 2024
2 parents a18a0bf + cb65b05 commit d8572f8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 12 deletions.
4 changes: 3 additions & 1 deletion pr_agent/algo/language_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ def filter_bad_extensions(files):
return [f for f in files if f.filename is not None and is_valid_file(f.filename, bad_extensions)]


def is_valid_file(filename, bad_extensions=None):
def is_valid_file(filename:str, bad_extensions=None) -> bool:
if not filename:
return False
if not bad_extensions:
bad_extensions = get_settings().bad_extensions.default
if get_settings().config.use_extra_bad_extensions:
Expand Down
33 changes: 27 additions & 6 deletions pr_agent/git_providers/bitbucket_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
from .git_provider import GitProvider


def _gef_filename(diff):
if diff.new.path:
return diff.new.path
return diff.old.path


class BitbucketProvider(GitProvider):
def __init__(
self, pr_url: Optional[str] = None, incremental: Optional[bool] = False
Expand Down Expand Up @@ -123,7 +129,18 @@ def set_pr(self, pr_url: str):
self.pr = self._get_pr()

def get_files(self):
return [diff.new.path for diff in self.pr.diffstat()]
try:
git_files = context.get("git_files", None)
if git_files:
return git_files
self.git_files = [_gef_filename(diff) for diff in self.pr.diffstat()]
context["git_files"] = self.git_files
return self.git_files
except Exception:
if not self.git_files:
self.git_files = [_gef_filename(diff) for diff in self.pr.diffstat()]
return self.git_files


def get_diff_files(self) -> list[FilePatchInfo]:
if self.diff_files:
Expand Down Expand Up @@ -172,14 +189,18 @@ def get_diff_files(self) -> list[FilePatchInfo]:
diff_split_lines[5].startswith("@@"))):
diff_split[i] = "\n".join(diff_split_lines[4:])
else:
get_logger().error(f"Error - failed to remove the bitbucket header from diff {i}")
break
if diffs[i].data.get('lines_added', 0) == 0 and diffs[i].data.get('lines_removed', 0) == 0:
diff_split[i] = ""
else:
get_logger().error(f"Error - failed to remove the bitbucket header from diff {i}")
break

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

try:
Expand All @@ -200,7 +221,7 @@ def get_diff_files(self) -> list[FilePatchInfo]:
original_file_content_str,
new_file_content_str,
diff_split[index],
diff.new.path,
file_path,
)

if diff.data['status'] == 'added':
Expand Down
15 changes: 10 additions & 5 deletions pr_agent/servers/bitbucket_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,18 @@ async def inner():
return "OK"
except KeyError:
get_logger().error("Failed to get actor type, check previous logs, this shouldn't happen.")

# Get the username of the sender
try:
owner = data["data"]["repository"]["owner"]["username"]
except Exception as e:
get_logger().error(f"Failed to get owner, will continue: {e}")
owner = "unknown"
username = data["data"]["actor"]["username"]
except KeyError:
try:
username = data["data"]["actor"]["display_name"]
except KeyError:
username = data["data"]["actor"]["nickname"]
log_context["sender"] = username

sender_id = data["data"]["actor"]["account_id"]
log_context["sender"] = owner
log_context["sender_id"] = sender_id
jwt_parts = input_jwt.split(".")
claim_part = jwt_parts[1]
Expand Down

0 comments on commit d8572f8

Please sign in to comment.