Skip to content

Commit

Permalink
Refactor Azure DevOps provider to use PR iterations for change detect…
Browse files Browse the repository at this point in the history
…ion, improving accuracy of diff file identification
  • Loading branch information
mrT23 committed May 14, 2024
1 parent 05876af commit e4565f7
Showing 1 changed file with 52 additions and 28 deletions.
80 changes: 52 additions & 28 deletions pr_agent/git_providers/azuredevops_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
CommentThread,
GitVersionDescriptor,
GitPullRequest,
GitPullRequestIterationChanges,
)
except ImportError:
AZURE_DEVOPS_AVAILABLE = False
Expand Down Expand Up @@ -230,29 +231,56 @@ def get_diff_files(self) -> list[FilePatchInfo]:
base_sha = self.pr.last_merge_target_commit
head_sha = self.pr.last_merge_source_commit

commits = self.azure_devops_client.get_pull_request_commits(
project=self.workspace_slug,
# Get PR iterations
iterations = self.azure_devops_client.get_pull_request_iterations(
repository_id=self.repo_slug,
pull_request_id=self.pr_num,
project=self.workspace_slug
)
changes = []
if iterations:
iteration_id = iterations[-1].id # Get the last iteration (most recent changes)

# Get changes for the iteration
changes: GitPullRequestIterationChanges = 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 = []
diffs = []
diff_types = {}

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))
if changes:
for change in changes.change_entries:
c = change.additional_properties['item']
diffs.append(c['path'])
diff_types[c['path']] = change.additional_properties['changeType']

# wrong implementation - gets all the files that were changed in any commit in the PR
# commits = self.azure_devops_client.get_pull_request_commits(
# project=self.workspace_slug,
# repository_id=self.repo_slug,
# pull_request_id=self.pr_num,
# )
#
# diff_files = []
# diffs = []
# diff_types = {}

# 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:
if not is_valid_file(file):
Expand All @@ -273,12 +301,13 @@ def get_diff_files(self) -> list[FilePatchInfo]:

new_file_content_str = new_file_content_str.content
except Exception as error:
get_logger().error(
"Failed to retrieve new file content of %s at version %s. Error: %s",
file,
version,
str(error),
)
get_logger().error(f"Failed to retrieve new file content of {file} at version {version}. Error: {str(error)}")
# get_logger().error(
# "Failed to retrieve new file content of %s at version %s. Error: %s",
# file,
# version,
# str(error),
# )
new_file_content_str = ""

edit_type = EDIT_TYPE.MODIFIED
Expand All @@ -303,12 +332,7 @@ def get_diff_files(self) -> list[FilePatchInfo]:
)
original_file_content_str = original_file_content_str.content
except Exception as error:
get_logger().error(
"Failed to retrieve original file content of %s at version %s. Error: %s",
file,
version,
str(error),
)
get_logger().error(f"Failed to retrieve original file content of {file} at version {version}. Error: {str(error)}")
original_file_content_str = ""

patch = load_large_diff(
Expand Down

0 comments on commit e4565f7

Please sign in to comment.