From 8ae3c60670c46a24d1c83eaacf613af482537b99 Mon Sep 17 00:00:00 2001 From: Patryk Kowalski Date: Mon, 17 Jul 2023 14:53:23 +0200 Subject: [PATCH] In case of new file creation by the MR there is a 404 error on file retrieval by gitlab provider. It was handled by catching the error and replacing the file string with an empty string. Type checking was added before byte decoding - necessary in case of the empty string. --- pr_agent/git_providers/gitlab_provider.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/pr_agent/git_providers/gitlab_provider.py b/pr_agent/git_providers/gitlab_provider.py index 07a25e2f..0da50e51 100644 --- a/pr_agent/git_providers/gitlab_provider.py +++ b/pr_agent/git_providers/gitlab_provider.py @@ -4,6 +4,7 @@ from typing import Optional, Tuple from urllib.parse import urlparse import gitlab +from gitlab import GitlabGetError from pr_agent.config_loader import settings @@ -42,7 +43,11 @@ class GitLabProvider(GitProvider): self.last_diff = self.mr.diffs.list()[-1] def _get_pr_file_content(self, file_path: str, branch: str) -> str: - return self.gl.projects.get(self.id_project).files.get(file_path, branch).decode() + try: + return self.gl.projects.get(self.id_project).files.get(file_path, branch).decode() + except GitlabGetError: + # In case of file creation the method returns GitlabGetError (404 file not found). In this case we return an empty string for the diff. + return '' def get_diff_files(self) -> list[FilePatchInfo]: diffs = self.mr.changes()['changes'] @@ -58,8 +63,10 @@ class GitLabProvider(GitProvider): elif diff['renamed_file']: edit_type = EDIT_TYPE.RENAMED try: - original_file_content_str = bytes.decode(original_file_content_str, 'utf-8') - new_file_content_str = bytes.decode(new_file_content_str, 'utf-8') + if isinstance(original_file_content_str, bytes): + original_file_content_str = bytes.decode(original_file_content_str, 'utf-8') + if isinstance(new_file_content_str, bytes): + new_file_content_str = bytes.decode(new_file_content_str, 'utf-8') except UnicodeDecodeError: logging.warning( f"Cannot decode file {diff['old_path']} or {diff['new_path']} in merge request {self.id_mr}")