Skip to content

Commit cf14e45

Browse files
committed
further cleaned up code based on feedback
1 parent 29c5075 commit cf14e45

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

pr_agent/git_providers/bitbucket_server_provider.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -156,33 +156,33 @@ def get_diff_files(self) -> list[FilePatchInfo]:
156156
if self.diff_files:
157157
return self.diff_files
158158

159-
source_commits_list = list(self.bitbucket_client.get_pull_requests_commits(
160-
self.workspace_slug,
161-
self.repo_slug,
162-
self.pr_num
163-
))
164-
165-
# defaults to basic diff functionality with a guaranteed common ancestor
166-
base_sha, head_sha = source_commits_list[-1]['parents'][0]['id'], source_commits_list[0]['id']
167-
168-
# if Bitbucket api version is greater than or equal to 7.0 then use 2-way diff functionality for the base_sha
169-
if self.bitbucket_api_version is not None and self.bitbucket_api_version >= LooseVersion("7.0"):
170-
# Bitbucket endpoint for getting merge-base is available as of 8.16
171-
if self.bitbucket_api_version >= LooseVersion("8.16"):
172-
try:
173-
base_sha = self.bitbucket_client.get(self._get_best_common_ancestor())['id']
174-
except Exception as e:
175-
get_logger().error(f"Failed to get the best common ancestor for PR: {self.pr_url}, \nerror: {e}")
176-
raise e
177-
# for versions 7.0-8.15 try to calculate the merge-base on our own
178-
else:
159+
head_sha = self.pr.fromRef['latestCommit']
160+
161+
# if Bitbucket api version is >= 8.16 then use the merge-base api for 2-way diff calculation
162+
if self.bitbucket_api_version is not None and self.bitbucket_api_version >= LooseVersion("8.16"):
163+
try:
164+
base_sha = self.bitbucket_client.get(self._get_merge_base())['id']
165+
except Exception as e:
166+
get_logger().error(f"Failed to get the best common ancestor for PR: {self.pr_url}, \nerror: {e}")
167+
raise e
168+
else:
169+
source_commits_list = list(self.bitbucket_client.get_pull_requests_commits(
170+
self.workspace_slug,
171+
self.repo_slug,
172+
self.pr_num
173+
))
174+
# if Bitbucket api version is None or < 7.0 then do a simple diff with a guaranteed common ancestor
175+
base_sha = source_commits_list[-1]['parents'][0]['id']
176+
# if Bitbucket api version is 7.0-8.15 then use 2-way diff functionality for the base_sha
177+
if self.bitbucket_api_version is not None and self.bitbucket_api_version >= LooseVersion("7.0"):
179178
try:
180179
destination_commits = list(
181180
self.bitbucket_client.get_commits(self.workspace_slug, self.repo_slug, base_sha,
182181
self.pr.toRef['latestCommit']))
183182
base_sha = self.get_best_common_ancestor(source_commits_list, destination_commits, base_sha)
184183
except Exception as e:
185-
get_logger().error(f"Failed to get the commit list for calculating best common ancestor for PR: {self.pr_url}, \nerror: {e}")
184+
get_logger().error(
185+
f"Failed to get the commit list for calculating best common ancestor for PR: {self.pr_url}, \nerror: {e}")
186186
raise e
187187

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

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

0 commit comments

Comments
 (0)