From 411b244670ce680d799c0dbdf77e719dc4f32a0d Mon Sep 17 00:00:00 2001 From: Michael Yochpaz <8832013+MichaelYochpaz@users.noreply.github.com> Date: Tue, 30 Jan 2024 15:58:19 +0200 Subject: [PATCH] [Pre-Commit] Continue Even if Fetch Failed (#3878) * Add `try` and `except` blocks * Bypass logger circular import issues * Add release notes * Import logger * Update log message * pre-commit * Update .changelog/3878.yml Co-authored-by: Guy Afik <53861351+GuyAfik@users.noreply.github.com> * Remove lru_cache * Address review notes * Minor change --------- Co-authored-by: Guy Afik <53861351+GuyAfik@users.noreply.github.com> --- .changelog/3878.yml | 4 +++ demisto_sdk/commands/common/git_util.py | 26 ++++++++++++++----- .../commands/pre_commit/pre_commit_command.py | 6 ++++- 3 files changed, 28 insertions(+), 8 deletions(-) create mode 100644 .changelog/3878.yml diff --git a/.changelog/3878.yml b/.changelog/3878.yml new file mode 100644 index 0000000000..a87d7e90bf --- /dev/null +++ b/.changelog/3878.yml @@ -0,0 +1,4 @@ +changes: +- description: Fixed an issue where fetching from a git remote would cause a failure in cases where fetching is not necessary. + type: fix +pr_number: 3878 diff --git a/demisto_sdk/commands/common/git_util.py b/demisto_sdk/commands/common/git_util.py index d797d4e0b7..d1e42f6dbd 100644 --- a/demisto_sdk/commands/common/git_util.py +++ b/demisto_sdk/commands/common/git_util.py @@ -20,6 +20,7 @@ DEMISTO_GIT_UPSTREAM, PACKS_FOLDER, ) +from demisto_sdk.commands.common.logger import logger class CommitOrBranchNotFoundError(GitError): @@ -618,13 +619,25 @@ def _get_staged_files(self) -> Set[Path]: } def _get_all_changed_files(self, prev_ver: str = "") -> Set[Path]: - """Get all the files changed in the current branch without status distinction. + """ + Get all the files changed in the current branch without status distinction. + Args: prev_ver (str): The base branch against which the comparison is made. + Returns: - Set: of Paths to files changed in the current branch. + Set[Path]: of Paths to files changed in the current branch. """ - self.fetch() + try: + self.fetch() + + except Exception as e: + logger.warning( + f"Failed to fetch branch '{self.get_current_working_branch()}' " + f"from remote '{self.repo.remote().name}' ({self.repo.remote().url}). Continuing without fetching." + ) + logger.debug(f"Error: {e}") + remote, branch = self.handle_prev_ver(prev_ver) current_hash = self.get_current_commit_hash() @@ -899,7 +912,8 @@ def get_all_changed_files( debug: bool = False, include_untracked: bool = False, ) -> Set[Path]: - """Get a set of all changed files in the branch (modified, added and renamed) + """ + Get a set of all changed files in the branch (modified, added and renamed) Args: prev_ver (str): The base branch against which the comparison is made. @@ -908,7 +922,7 @@ def get_all_changed_files( debug (bool): Whether to print the debug logs. include_untracked (bool): Whether to include untracked files. Returns: - Set. A set of all the changed files in the given branch when comparing to prev_ver + Set[Path]: A set of all the changed files in the given branch when comparing to prev_ver """ self.fetch() modified_files: Set[Path] = self.modified_files( @@ -944,11 +958,9 @@ def _is_file_git_ignored(self, file_path: str) -> bool: """ return bool(self.repo.ignored(file_path)) - @lru_cache def fetch(self): self.repo.remote().fetch() - @lru_cache def fetch_all(self): for remote in self.repo.remotes: remote.fetch() diff --git a/demisto_sdk/commands/pre_commit/pre_commit_command.py b/demisto_sdk/commands/pre_commit/pre_commit_command.py index c48a33840f..5d3494c0b8 100644 --- a/demisto_sdk/commands/pre_commit/pre_commit_command.py +++ b/demisto_sdk/commands/pre_commit/pre_commit_command.py @@ -439,7 +439,11 @@ def pre_commit_manager( git_diff = True files_to_run = preprocess_files( - input_files, staged_only, commited_only, git_diff, all_files + input_files=input_files, + staged_only=staged_only, + commited_only=commited_only, + use_git=git_diff, + all_files=all_files, ) if not files_to_run: logger.info("No files were changed, skipping pre-commit.")