mirror of
https://github.com/qodo-ai/pr-agent.git
synced 2025-07-04 12:50:38 +08:00
Merge pull request #902 from Codium-ai/tr/self_reflect
Refactor Azure DevOps provider to use PR iterations for change detect…
This commit is contained in:
@ -356,7 +356,7 @@ def convert_str_to_datetime(date_str):
|
|||||||
return datetime.strptime(date_str, datetime_format)
|
return datetime.strptime(date_str, datetime_format)
|
||||||
|
|
||||||
|
|
||||||
def load_large_diff(filename, new_file_content_str: str, original_file_content_str: str) -> str:
|
def load_large_diff(filename, new_file_content_str: str, original_file_content_str: str, show_warning: bool = True) -> str:
|
||||||
"""
|
"""
|
||||||
Generate a patch for a modified file by comparing the original content of the file with the new content provided as
|
Generate a patch for a modified file by comparing the original content of the file with the new content provided as
|
||||||
input.
|
input.
|
||||||
@ -375,7 +375,7 @@ def load_large_diff(filename, new_file_content_str: str, original_file_content_s
|
|||||||
try:
|
try:
|
||||||
diff = difflib.unified_diff(original_file_content_str.splitlines(keepends=True),
|
diff = difflib.unified_diff(original_file_content_str.splitlines(keepends=True),
|
||||||
new_file_content_str.splitlines(keepends=True))
|
new_file_content_str.splitlines(keepends=True))
|
||||||
if get_settings().config.verbosity_level >= 2:
|
if get_settings().config.verbosity_level >= 2 and show_warning:
|
||||||
get_logger().warning(f"File was modified, but no patch was found. Manually creating patch: {filename}.")
|
get_logger().warning(f"File was modified, but no patch was found. Manually creating patch: {filename}.")
|
||||||
patch = ''.join(diff)
|
patch = ''.join(diff)
|
||||||
except Exception:
|
except Exception:
|
||||||
|
@ -26,6 +26,7 @@ try:
|
|||||||
CommentThread,
|
CommentThread,
|
||||||
GitVersionDescriptor,
|
GitVersionDescriptor,
|
||||||
GitPullRequest,
|
GitPullRequest,
|
||||||
|
GitPullRequestIterationChanges,
|
||||||
)
|
)
|
||||||
except ImportError:
|
except ImportError:
|
||||||
AZURE_DEVOPS_AVAILABLE = False
|
AZURE_DEVOPS_AVAILABLE = False
|
||||||
@ -230,29 +231,58 @@ class AzureDevopsProvider(GitProvider):
|
|||||||
base_sha = self.pr.last_merge_target_commit
|
base_sha = self.pr.last_merge_target_commit
|
||||||
head_sha = self.pr.last_merge_source_commit
|
head_sha = self.pr.last_merge_source_commit
|
||||||
|
|
||||||
commits = self.azure_devops_client.get_pull_request_commits(
|
# Get PR iterations
|
||||||
project=self.workspace_slug,
|
iterations = self.azure_devops_client.get_pull_request_iterations(
|
||||||
repository_id=self.repo_slug,
|
repository_id=self.repo_slug,
|
||||||
pull_request_id=self.pr_num,
|
pull_request_id=self.pr_num,
|
||||||
|
project=self.workspace_slug
|
||||||
)
|
)
|
||||||
|
changes = None
|
||||||
|
if iterations:
|
||||||
|
iteration_id = iterations[-1].id # Get the last iteration (most recent changes)
|
||||||
|
|
||||||
|
# Get changes for the iteration
|
||||||
|
changes = self.azure_devops_client.get_pull_request_iteration_changes(
|
||||||
|
repository_id=self.repo_slug,
|
||||||
|
pull_request_id=self.pr_num,
|
||||||
|
iteration_id=iteration_id,
|
||||||
|
project=self.workspace_slug
|
||||||
|
)
|
||||||
diff_files = []
|
diff_files = []
|
||||||
diffs = []
|
diffs = []
|
||||||
diff_types = {}
|
diff_types = {}
|
||||||
|
if changes:
|
||||||
|
for change in changes.change_entries:
|
||||||
|
item = change.additional_properties.get('item', {})
|
||||||
|
path = item.get('path', None)
|
||||||
|
if path:
|
||||||
|
diffs.append(path)
|
||||||
|
diff_types[path] = change.additional_properties.get('changeType', 'Unknown')
|
||||||
|
|
||||||
for c in commits:
|
# wrong implementation - gets all the files that were changed in any commit in the PR
|
||||||
changes_obj = self.azure_devops_client.get_changes(
|
# commits = self.azure_devops_client.get_pull_request_commits(
|
||||||
project=self.workspace_slug,
|
# project=self.workspace_slug,
|
||||||
repository_id=self.repo_slug,
|
# repository_id=self.repo_slug,
|
||||||
commit_id=c.commit_id,
|
# pull_request_id=self.pr_num,
|
||||||
)
|
# )
|
||||||
for i in changes_obj.changes:
|
#
|
||||||
if i["item"]["gitObjectType"] == "tree":
|
# diff_files = []
|
||||||
continue
|
# diffs = []
|
||||||
diffs.append(i["item"]["path"])
|
# diff_types = {}
|
||||||
diff_types[i["item"]["path"]] = i["changeType"]
|
|
||||||
|
|
||||||
diffs = list(set(diffs))
|
# for c in commits:
|
||||||
|
# changes_obj = self.azure_devops_client.get_changes(
|
||||||
|
# project=self.workspace_slug,
|
||||||
|
# repository_id=self.repo_slug,
|
||||||
|
# commit_id=c.commit_id,
|
||||||
|
# )
|
||||||
|
# for i in changes_obj.changes:
|
||||||
|
# if i["item"]["gitObjectType"] == "tree":
|
||||||
|
# continue
|
||||||
|
# diffs.append(i["item"]["path"])
|
||||||
|
# diff_types[i["item"]["path"]] = i["changeType"]
|
||||||
|
#
|
||||||
|
# diffs = list(set(diffs))
|
||||||
|
|
||||||
for file in diffs:
|
for file in diffs:
|
||||||
if not is_valid_file(file):
|
if not is_valid_file(file):
|
||||||
@ -273,12 +303,13 @@ class AzureDevopsProvider(GitProvider):
|
|||||||
|
|
||||||
new_file_content_str = new_file_content_str.content
|
new_file_content_str = new_file_content_str.content
|
||||||
except Exception as error:
|
except Exception as error:
|
||||||
get_logger().error(
|
get_logger().error(f"Failed to retrieve new file content of {file} at version {version}. Error: {str(error)}")
|
||||||
"Failed to retrieve new file content of %s at version %s. Error: %s",
|
# get_logger().error(
|
||||||
file,
|
# "Failed to retrieve new file content of %s at version %s. Error: %s",
|
||||||
version,
|
# file,
|
||||||
str(error),
|
# version,
|
||||||
)
|
# str(error),
|
||||||
|
# )
|
||||||
new_file_content_str = ""
|
new_file_content_str = ""
|
||||||
|
|
||||||
edit_type = EDIT_TYPE.MODIFIED
|
edit_type = EDIT_TYPE.MODIFIED
|
||||||
@ -303,17 +334,12 @@ class AzureDevopsProvider(GitProvider):
|
|||||||
)
|
)
|
||||||
original_file_content_str = original_file_content_str.content
|
original_file_content_str = original_file_content_str.content
|
||||||
except Exception as error:
|
except Exception as error:
|
||||||
get_logger().error(
|
get_logger().error(f"Failed to retrieve original file content of {file} at version {version}. Error: {str(error)}")
|
||||||
"Failed to retrieve original file content of %s at version %s. Error: %s",
|
|
||||||
file,
|
|
||||||
version,
|
|
||||||
str(error),
|
|
||||||
)
|
|
||||||
original_file_content_str = ""
|
original_file_content_str = ""
|
||||||
|
|
||||||
patch = load_large_diff(
|
patch = load_large_diff(
|
||||||
file, new_file_content_str, original_file_content_str
|
file, new_file_content_str, original_file_content_str, show_warning=False
|
||||||
)
|
).rstrip()
|
||||||
|
|
||||||
diff_files.append(
|
diff_files.append(
|
||||||
FilePatchInfo(
|
FilePatchInfo(
|
||||||
|
Reference in New Issue
Block a user