Skip to content

Commit

Permalink
fix: handle cloning issues when repo is in an unexpected state
Browse files Browse the repository at this point in the history
Signed-off-by: behnazh-w <[email protected]>
  • Loading branch information
behnazh-w committed Aug 2, 2023
1 parent e9b2f72 commit 5e2c348
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions src/macaron/slsa_analyzer/git_service/gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from abc import abstractmethod
from urllib.parse import ParseResult, urlunparse

from git import GitError
from pydriller.git import Git

from macaron.errors import CloneError, ConfigurationError, RepoCheckOutError
Expand Down Expand Up @@ -129,7 +130,7 @@ def clone_repo(self, clone_dir: str, url: str) -> None:
# Therefore, we don't need to catch and handle the CloneError exceptions here.
repo = git_url.clone_remote_repo(clone_dir, clone_url)

# If ``git_url.clone_remote_repo`` returns an Repo instance, this means that the repository is freshly cloned
# If ``git_url.clone_remote_repo`` returns a Repo instance, this means that the repository is freshly cloned
# with the token embedded URL. We will set its value back to the original non-token URL.
# If ``git_url.clone_remote_repo`` returns None, it means that the repository already exists so we don't need
# to do anything.
Expand All @@ -139,7 +140,15 @@ def clone_repo(self, clone_dir: str, url: str) -> None:
except ValueError as error:
raise CloneError("Cannot find the remote origin for this repository.") from error

origin_remote.set_url(url)
try:
# Even though the documentation of ``set_url`` function does not explicitly mention
# ``ValueError`` or ``GitError`` as raised errors, these errors might be
# raised based on their implementation.
origin_remote.set_url(url)
except (ValueError, GitError) as error:
raise CloneError(
"Failed to set the remote origin URL because this repository is in an unexpected state."
) from error

def check_out_repo(self, git_obj: Git, branch: str, digest: str, offline_mode: bool) -> Git:
"""Checkout the branch and commit specified by the user of a repository.
Expand Down Expand Up @@ -168,7 +177,7 @@ def check_out_repo(self, git_obj: Git, branch: str, digest: str, offline_mode: b
Raises
------
RepoError
RepoCheckOutError
If there is error while checkout the specific branch and digest.
"""
remote_origin_url = git_url.get_remote_origin_of_local_repo(git_obj)
Expand All @@ -183,11 +192,24 @@ def check_out_repo(self, git_obj: Git, branch: str, digest: str, offline_mode: b
except CloneError as error:
raise RepoCheckOutError("Cannot parse the remote origin URL of this repository.") from error

origin_remote.set_url(reconstructed_url, remote_origin_url)
try:
# Even though the documentation of ``set_url`` function does not explicitly mention
# ``ValueError`` or ``GitError`` as raised errors, these errors might be
# raised based on their implementation.
origin_remote.set_url(reconstructed_url, remote_origin_url)
except (ValueError, GitError) as error:
raise RepoCheckOutError(
"Failed to set the remote origin URL because this repository is in an unexpected state."
) from error

check_out_status = git_url.check_out_repo_target(git_obj, branch, digest, offline_mode)

origin_remote.set_url(remote_origin_url, reconstructed_url)
try:
origin_remote.set_url(remote_origin_url, reconstructed_url)
except (ValueError, GitError) as error:
raise RepoCheckOutError(
"Failed to set the remote origin URL because this repository is in an unexpected state."
) from error

if not check_out_status:
raise RepoCheckOutError(
Expand Down

0 comments on commit 5e2c348

Please sign in to comment.