diff --git a/pr_agent/git_providers/bitbucket_server_provider.py b/pr_agent/git_providers/bitbucket_server_provider.py index 33b840c7e..7588075e5 100644 --- a/pr_agent/git_providers/bitbucket_server_provider.py +++ b/pr_agent/git_providers/bitbucket_server_provider.py @@ -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 = [] @@ -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"