Skip to content

Commit

Permalink
Merge pull request #571 from mfocko/no-ref-file-content
Browse files Browse the repository at this point in the history
Unify behaviour for get_file_content for GitHub

Catch GithubException in GithubProject.get_file_content as we do
for GitLab and Pagure to:
a) unify the behaviour
b) ignore unwanted 404 related to:
https://sentry.io/organizations/red-hat-0p/issues/1809076278
Signed-off-by: Matej Focko [email protected]

Reviewed-by: None <None>
  • Loading branch information
softwarefactory-project-zuul[bot] authored Apr 21, 2021
2 parents 2891ce1 + 39b5af7 commit 6ab343b
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 17 deletions.
4 changes: 0 additions & 4 deletions ogr/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,5 @@ def __init__(self, *args: Any, gitlab_error: Optional[str] = None) -> None:
self.gitlab_error = gitlab_error


class OurPagureRawRequest(OgrException):
""" Mocking Exceptions for pagure raw request """


class OperationNotSupported(OgrException):
""" Raise when the operation is not supported by the backend. """
6 changes: 4 additions & 2 deletions ogr/services/github/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,8 +441,10 @@ def get_file_content(self, path: str, ref=None) -> str:
return self.github_repo.get_contents(
path=path, ref=ref
).decoded_content.decode()
except UnknownObjectException as ex:
raise FileNotFoundError(f"File '{path}' on {ref} not found", ex)
except (UnknownObjectException, GithubException) as ex:
if ex.status == 404:
raise FileNotFoundError(f"File '{path}' on {ref} not found", ex)
raise GithubAPIException(ex)

def get_files(
self, ref: str = None, filter_regex: str = None, recursive: bool = False
Expand Down
4 changes: 3 additions & 1 deletion ogr/services/gitlab/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,9 @@ def get_file_content(self, path, ref=None) -> str:
file = self.gitlab_repo.files.get(file_path=path, ref=ref)
return file.decode().decode()
except gitlab.exceptions.GitlabGetError as ex:
raise FileNotFoundError(f"File '{path}' on {ref} not found", ex)
if ex.response_code == 404:
raise FileNotFoundError(f"File '{path}' on {ref} not found", ex)
raise GitlabAPIException(ex)

def get_files(
self, ref: str = None, filter_regex: str = None, recursive: bool = False
Expand Down
17 changes: 7 additions & 10 deletions ogr/services/pagure/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
AccessLevel,
)
from ogr.exceptions import (
OurPagureRawRequest,
PagureAPIException,
OgrException,
OperationNotSupported,
Expand Down Expand Up @@ -434,15 +433,13 @@ def change_token(self, new_token: str) -> None:

def get_file_content(self, path: str, ref=None) -> str:
ref = ref or self.default_branch
try:
result = self._call_project_api_raw(
"raw", ref, "f", path, add_api_endpoint_part=False
)
if not result or result.reason == "NOT FOUND":
raise FileNotFoundError(f"File '{path}' on {ref} not found")
return result.content.decode()
except OurPagureRawRequest as ex:
raise FileNotFoundError(f"Problem with getting file '{path}' on {ref}", ex)
result = self._call_project_api_raw(
"raw", ref, "f", path, add_api_endpoint_part=False
)

if not result or result.reason == "NOT FOUND":
raise FileNotFoundError(f"File '{path}' on {ref} not found")
return result.content.decode()

def get_sha_from_tag(self, tag_name: str) -> str:
tags_dict = self.get_tags_dict()
Expand Down

0 comments on commit 6ab343b

Please sign in to comment.