Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
philipclaesson committed Nov 17, 2023
1 parent bdc3c96 commit 2794154
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 18 deletions.
4 changes: 2 additions & 2 deletions packages/opal-common/opal_common/git/branch_tracker.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
24 changes: 15 additions & 9 deletions packages/opal-common/opal_common/git/tag_tracker.py
Original file line number Diff line number Diff line change
@@ -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, Tag
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).
Expand All @@ -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."""
Expand All @@ -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()
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
)


Expand Down
8 changes: 6 additions & 2 deletions packages/opal-common/opal_common/sources/git_policy_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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}'",
Expand Down
4 changes: 1 addition & 3 deletions packages/opal-server/opal_server/policy/watcher/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 2794154

Please sign in to comment.