mirror of
https://github.com/qodo-ai/pr-agent.git
synced 2025-07-05 05:10:38 +08:00
refactor: optimize file content loading and improve rate limit handling
This commit is contained in:
@ -174,6 +174,24 @@ class GithubProvider(GitProvider):
|
|||||||
|
|
||||||
diff_files = []
|
diff_files = []
|
||||||
invalid_files_names = []
|
invalid_files_names = []
|
||||||
|
is_close_to_rate_limit = False
|
||||||
|
|
||||||
|
# The base.sha will point to the current state of the base branch (including parallel merges), not the original base commit when the PR was created
|
||||||
|
# We can fix this by finding the merge base commit between the PR head and base branches
|
||||||
|
# Note that The pr.head.sha is actually correct as is - it points to the latest commit in your PR branch.
|
||||||
|
# This SHA isn't affected by parallel merges to the base branch since it's specific to your PR's branch.
|
||||||
|
repo = self.repo_obj
|
||||||
|
pr = self.pr
|
||||||
|
try:
|
||||||
|
compare = repo.compare(pr.base.sha, pr.head.sha) # communication with GitHub
|
||||||
|
merge_base_commit = compare.merge_base_commit
|
||||||
|
except Exception as e:
|
||||||
|
get_logger().error(f"Failed to get merge base commit: {e}")
|
||||||
|
merge_base_commit = pr.base
|
||||||
|
if merge_base_commit.sha != pr.base.sha:
|
||||||
|
get_logger().info(
|
||||||
|
f"Using merge base commit {merge_base_commit.sha} instead of base commit ")
|
||||||
|
|
||||||
counter_valid = 0
|
counter_valid = 0
|
||||||
for file in files:
|
for file in files:
|
||||||
if not is_valid_file(file.filename):
|
if not is_valid_file(file.filename):
|
||||||
@ -181,7 +199,10 @@ class GithubProvider(GitProvider):
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
patch = file.patch
|
patch = file.patch
|
||||||
|
if is_close_to_rate_limit:
|
||||||
|
new_file_content_str = ""
|
||||||
|
original_file_content_str = ""
|
||||||
|
else:
|
||||||
# allow only a limited number of files to be fully loaded. We can manage the rest with diffs only
|
# allow only a limited number of files to be fully loaded. We can manage the rest with diffs only
|
||||||
counter_valid += 1
|
counter_valid += 1
|
||||||
avoid_load = False
|
avoid_load = False
|
||||||
@ -203,27 +224,12 @@ class GithubProvider(GitProvider):
|
|||||||
if avoid_load:
|
if avoid_load:
|
||||||
original_file_content_str = ""
|
original_file_content_str = ""
|
||||||
else:
|
else:
|
||||||
# The base.sha will point to the current state of the base branch (including parallel merges), not the original base commit when the PR was created
|
|
||||||
# We can fix this by finding the merge base commit between the PR head and base branches
|
|
||||||
# Note that The pr.head.sha is actually correct as is - it points to the latest commit in your PR branch.
|
|
||||||
# This SHA isn't affected by parallel merges to the base branch since it's specific to your PR's branch.
|
|
||||||
repo = self.repo_obj
|
|
||||||
pr = self.pr
|
|
||||||
try:
|
|
||||||
compare = repo.compare(pr.base.sha, pr.head.sha)
|
|
||||||
merge_base_commit = compare.merge_base_commit
|
|
||||||
except Exception as e:
|
|
||||||
get_logger().error(f"Failed to get merge base commit: {e}")
|
|
||||||
merge_base_commit = pr.base
|
|
||||||
if merge_base_commit.sha != pr.base.sha:
|
|
||||||
get_logger().info(
|
|
||||||
f"Using merge base commit {merge_base_commit.sha} instead of base commit "
|
|
||||||
f"{pr.base.sha} for {file.filename}")
|
|
||||||
original_file_content_str = self._get_pr_file_content(file, merge_base_commit.sha)
|
original_file_content_str = self._get_pr_file_content(file, merge_base_commit.sha)
|
||||||
|
# original_file_content_str = self._get_pr_file_content(file, self.pr.base.sha)
|
||||||
if not patch:
|
if not patch:
|
||||||
patch = load_large_diff(file.filename, new_file_content_str, original_file_content_str)
|
patch = load_large_diff(file.filename, new_file_content_str, original_file_content_str)
|
||||||
|
|
||||||
|
|
||||||
if file.status == 'added':
|
if file.status == 'added':
|
||||||
edit_type = EDIT_TYPE.ADDED
|
edit_type = EDIT_TYPE.ADDED
|
||||||
elif file.status == 'removed':
|
elif file.status == 'removed':
|
||||||
@ -237,9 +243,14 @@ class GithubProvider(GitProvider):
|
|||||||
edit_type = EDIT_TYPE.UNKNOWN
|
edit_type = EDIT_TYPE.UNKNOWN
|
||||||
|
|
||||||
# count number of lines added and removed
|
# count number of lines added and removed
|
||||||
|
if hasattr(file, 'additions') and hasattr(file, 'deletions'):
|
||||||
|
num_plus_lines = file.additions
|
||||||
|
num_minus_lines = file.deletions
|
||||||
|
else:
|
||||||
patch_lines = patch.splitlines(keepends=True)
|
patch_lines = patch.splitlines(keepends=True)
|
||||||
num_plus_lines = len([line for line in patch_lines if line.startswith('+')])
|
num_plus_lines = len([line for line in patch_lines if line.startswith('+')])
|
||||||
num_minus_lines = len([line for line in patch_lines if line.startswith('-')])
|
num_minus_lines = len([line for line in patch_lines if line.startswith('-')])
|
||||||
|
|
||||||
file_patch_canonical_structure = FilePatchInfo(original_file_content_str, new_file_content_str, patch,
|
file_patch_canonical_structure = FilePatchInfo(original_file_content_str, new_file_content_str, patch,
|
||||||
file.filename, edit_type=edit_type,
|
file.filename, edit_type=edit_type,
|
||||||
num_plus_lines=num_plus_lines,
|
num_plus_lines=num_plus_lines,
|
||||||
|
Reference in New Issue
Block a user