Skip to content

Commit

Permalink
further cleaned up code based on feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
mhoecke1 committed Aug 30, 2024
1 parent 29c5075 commit cf14e45
Showing 1 changed file with 22 additions and 22 deletions.
44 changes: 22 additions & 22 deletions pr_agent/git_providers/bitbucket_server_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,33 +156,33 @@ def get_diff_files(self) -> list[FilePatchInfo]:
if self.diff_files:
return self.diff_files

source_commits_list = list(self.bitbucket_client.get_pull_requests_commits(
self.workspace_slug,
self.repo_slug,
self.pr_num
))

# defaults to basic diff functionality with a guaranteed common ancestor
base_sha, head_sha = source_commits_list[-1]['parents'][0]['id'], source_commits_list[0]['id']

# if Bitbucket api version is greater than or equal to 7.0 then use 2-way diff functionality for the base_sha
if self.bitbucket_api_version is not None and self.bitbucket_api_version >= LooseVersion("7.0"):
# Bitbucket endpoint for getting merge-base is available as of 8.16
if self.bitbucket_api_version >= LooseVersion("8.16"):
try:
base_sha = self.bitbucket_client.get(self._get_best_common_ancestor())['id']
except Exception as e:
get_logger().error(f"Failed to get the best common ancestor for PR: {self.pr_url}, \nerror: {e}")
raise e
# for versions 7.0-8.15 try to calculate the merge-base on our own
else:
head_sha = self.pr.fromRef['latestCommit']

# if Bitbucket api version is >= 8.16 then use the merge-base api for 2-way diff calculation
if self.bitbucket_api_version is not None and self.bitbucket_api_version >= LooseVersion("8.16"):
try:
base_sha = self.bitbucket_client.get(self._get_merge_base())['id']
except Exception as e:
get_logger().error(f"Failed to get the best common ancestor for PR: {self.pr_url}, \nerror: {e}")
raise e
else:
source_commits_list = list(self.bitbucket_client.get_pull_requests_commits(
self.workspace_slug,
self.repo_slug,
self.pr_num
))
# if Bitbucket api version is None or < 7.0 then do a simple diff with a guaranteed common ancestor
base_sha = source_commits_list[-1]['parents'][0]['id']
# if Bitbucket api version is 7.0-8.15 then use 2-way diff functionality for the base_sha
if self.bitbucket_api_version is not None and self.bitbucket_api_version >= LooseVersion("7.0"):
try:
destination_commits = list(
self.bitbucket_client.get_commits(self.workspace_slug, self.repo_slug, base_sha,
self.pr.toRef['latestCommit']))
base_sha = self.get_best_common_ancestor(source_commits_list, destination_commits, base_sha)
except Exception as e:
get_logger().error(f"Failed to get the commit list for calculating best common ancestor for PR: {self.pr_url}, \nerror: {e}")
get_logger().error(
f"Failed to get the commit list for calculating best common ancestor for PR: {self.pr_url}, \nerror: {e}")
raise e

diff_files = []
Expand Down Expand Up @@ -452,5 +452,5 @@ def get_pr_labels(self, update=False):
def _get_pr_comments_path(self):
return f"rest/api/latest/projects/{self.workspace_slug}/repos/{self.repo_slug}/pull-requests/{self.pr_num}/comments"

def _get_best_common_ancestor(self):
def _get_merge_base(self):
return f"rest/api/latest/projects/{self.workspace_slug}/repos/{self.repo_slug}/pull-requests/{self.pr_num}/merge-base"

0 comments on commit cf14e45

Please sign in to comment.