From 8d46775b9b73eb4100895ecba6507451e75945e2 Mon Sep 17 00:00:00 2001 From: Philip Claesson Date: Fri, 17 Nov 2023 16:15:08 +0100 Subject: [PATCH] formatting --- .../opal_common/git/branch_tracker.py | 4 ++-- .../opal_common/git/tag_tracker.py | 24 ++++++++++++------- .../opal_common/git/tests/tag_tracker_test.py | 7 ++++-- .../opal_common/sources/git_policy_source.py | 8 +++++-- .../opal_server/policy/watcher/factory.py | 4 +--- 5 files changed, 29 insertions(+), 18 deletions(-) diff --git a/packages/opal-common/opal_common/git/branch_tracker.py b/packages/opal-common/opal_common/git/branch_tracker.py index 72581a3b..3c8b374d 100644 --- a/packages/opal-common/opal_common/git/branch_tracker.py +++ b/packages/opal-common/opal_common/git/branch_tracker.py @@ -1,7 +1,7 @@ from functools import partial from typing import Optional, Tuple -from git import GitCommandError, Head, Remote, Repo, Reference +from git import GitCommandError, Head, Reference, Remote, Repo from git.objects.commit import Commit from opal_common.git.env import provide_git_ssh_environment from opal_common.git.exceptions import GitFailed @@ -134,7 +134,7 @@ def tracked_branch(self) -> Head: branches_found=branches, ) raise GitFailed(e) - + @property def tracked_reference(self) -> Reference: return self.tracked_branch diff --git a/packages/opal-common/opal_common/git/tag_tracker.py b/packages/opal-common/opal_common/git/tag_tracker.py index 57a22f32..5fb436fe 100644 --- a/packages/opal-common/opal_common/git/tag_tracker.py +++ b/packages/opal-common/opal_common/git/tag_tracker.py @@ -1,13 +1,14 @@ from functools import partial from typing import Optional, Tuple -from git import GitCommandError, Tag, Repo, Reference +from git import GitCommandError, Reference, Repo from git.objects.commit import Commit +from opal_common.git.branch_tracker import BranchTracker from opal_common.git.env import provide_git_ssh_environment from opal_common.git.exceptions import GitFailed from opal_common.logger import logger from tenacity import retry, stop_after_attempt, wait_fixed -from opal_common.git.branch_tracker import BranchTracker + class TagTracker(BranchTracker): """Tracks the state of a git tag (hash the tag is pointing at). @@ -33,7 +34,13 @@ def __init__( ssh_key (Optional[str]): SSH key for private repositories """ self._tag_name = tag_name - super().__init__(repo, branch_name=None, remote_name=remote_name, retry_config=retry_config, ssh_key=ssh_key) + super().__init__( + repo, + branch_name=None, + remote_name=remote_name, + retry_config=retry_config, + ssh_key=ssh_key, + ) def checkout(self): """Checkouts the repository at the current tag.""" @@ -53,10 +60,11 @@ def checkout(self): def _fetch(self): """Fetch updates including tags with force option.""" + def _inner_fetch(*args, **kwargs): env = provide_git_ssh_environment(self.tracked_remote.url, self._ssh_key) with self.tracked_remote.repo.git.custom_environment(**env): - self.tracked_remote.repo.git.fetch('--tags', '--force', *args, **kwargs) + self.tracked_remote.repo.git.fetch("--tags", "--force", *args, **kwargs) attempt_fetch = retry(**self._retry_config)(_inner_fetch) return attempt_fetch() @@ -68,14 +76,12 @@ def latest_commit(self) -> Commit: @property def tracked_tag(self) -> Tag: - """returns the tracked tag reference (of type git.Reference) or throws if - such tag does not exist on the repo.""" + """returns the tracked tag reference (of type git.Reference) or throws + if such tag does not exist on the repo.""" try: return getattr(self._repo.tags, self._tag_name) except AttributeError as e: - tags = [ - {"path": tag.path} for tag in self._repo.tags - ] + tags = [{"path": tag.path} for tag in self._repo.tags] logger.exception( "did not find main branch: {error}, instead found: {tags_found}", error=e, diff --git a/packages/opal-common/opal_common/git/tests/tag_tracker_test.py b/packages/opal-common/opal_common/git/tests/tag_tracker_test.py index fe7d007f..87347197 100644 --- a/packages/opal-common/opal_common/git/tests/tag_tracker_test.py +++ b/packages/opal-common/opal_common/git/tests/tag_tracker_test.py @@ -18,8 +18,8 @@ from git import Repo from git.objects.commit import Commit -from opal_common.git.tag_tracker import TagTracker from opal_common.git.exceptions import GitFailed +from opal_common.git.tag_tracker import TagTracker def test_pull_with_no_changes(local_repo_clone: Repo): @@ -68,7 +68,10 @@ def test_pull_with_new_commits( assert prev != latest assert most_recent_commit_before_pull == prev assert ( - remote_repo.tags.__getattr__("test_tag").commit == repo.tags.__getattr__("test_tag").commit == latest == tracker.latest_commit + remote_repo.tags.__getattr__("test_tag").commit + == repo.tags.__getattr__("test_tag").commit + == latest + == tracker.latest_commit ) diff --git a/packages/opal-common/opal_common/sources/git_policy_source.py b/packages/opal-common/opal_common/sources/git_policy_source.py index f9a30818..88e0f132 100644 --- a/packages/opal-common/opal_common/sources/git_policy_source.py +++ b/packages/opal-common/opal_common/sources/git_policy_source.py @@ -2,9 +2,9 @@ from git import Repo from opal_common.git.branch_tracker import BranchTracker -from opal_common.git.tag_tracker import TagTracker from opal_common.git.exceptions import GitFailed from opal_common.git.repo_cloner import RepoCloner +from opal_common.git.tag_tracker import TagTracker from opal_common.logger import logger from opal_common.sources.base_policy_source import BasePolicySource @@ -114,7 +114,11 @@ async def check_for_changes(self): ) has_changes, prev, latest = self._tracker.pull() if not has_changes: - logger.info("No new commits: {ref} is at '{head}'", ref=self._tracker.tracked_reference.name, head=latest.hexsha) + logger.info( + "No new commits: {ref} is at '{head}'", + ref=self._tracker.tracked_reference.name, + head=latest.hexsha, + ) else: logger.info( "Found new commits: old HEAD was '{prev_head}', new HEAD is '{new_head}'", diff --git a/packages/opal-server/opal_server/policy/watcher/factory.py b/packages/opal-server/opal_server/policy/watcher/factory.py index 6d4388b0..0f738d1c 100644 --- a/packages/opal-server/opal_server/policy/watcher/factory.py +++ b/packages/opal-server/opal_server/policy/watcher/factory.py @@ -73,9 +73,7 @@ def setup_watcher_task( branch_name = load_conf_if_none( branch_name, opal_server_config.POLICY_REPO_MAIN_BRANCH ) - tag_name = load_conf_if_none( - tag_name, opal_server_config.POLICY_REPO_TAG - ) + tag_name = load_conf_if_none(tag_name, opal_server_config.POLICY_REPO_TAG) if branch_name is None and tag_name is None: logger.info("No branch or tag specified, falling back to using branch 'master'") branch_name = "master"