Skip to content

Commit

Permalink
fix: parse queries in url to get branch and ensure checkout
Browse files Browse the repository at this point in the history
  • Loading branch information
natthan-pigoux committed Jan 14, 2025
1 parent 9298e09 commit d89f51f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
21 changes: 18 additions & 3 deletions diracx-core/src/diracx/core/config/sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from pathlib import Path
from tempfile import TemporaryDirectory
from typing import Annotated
from urllib.parse import parse_qs, urlparse, urlunparse

import sh
import yaml
Expand Down Expand Up @@ -136,13 +137,15 @@ def __init__(self, *, backend_url: ConfigSourceUrl) -> None:
MAX_CS_CACHED_VERSIONS, DEFAULT_CS_CACHE_TTL
)
self._read_raw_cache: Cache = LRUCache(MAX_CS_CACHED_VERSIONS)
self.remote_url = self.exctract_remote_url(backend_url)
self.git_branch = self.get_git_branch_from_url(backend_url)

@cachedmethod(lambda self: self._latest_revision_cache)
def latest_revision(self) -> tuple[str, datetime]:
try:
rev = sh.git(
"rev-parse",
DEFAULT_GIT_BRANCH,
self.git_branch,
_cwd=self.repo_location,
_tty_out=False,
_async=is_running_in_async_context(),
Expand Down Expand Up @@ -192,6 +195,18 @@ def clear_caches(self):
self._latest_revision_cache.clear()
self._read_raw_cache.clear()

def exctract_remote_url(self, backend_url: ConfigSourceUrl) -> str:
"""Extract the base URL without the 'git+' prefix and query parameters."""
parsed_url = urlparse(str(backend_url).replace("git+", ""))
remote_url = urlunparse(parsed_url._replace(query=""))
return remote_url

def get_git_branch_from_url(self, backend_url: ConfigSourceUrl) -> str:
"""Extract the branch from the query parameters."""
parsed_url = urlparse(str(backend_url))
branch = parse_qs(parsed_url.query).get("branch", [DEFAULT_GIT_BRANCH])[0]
return branch


class LocalGitConfigSource(BaseGitConfigSource):
"""The configuration is stored on a local git repository
Expand Down Expand Up @@ -219,6 +234,7 @@ def __init__(self, *, backend_url: ConfigSourceUrl) -> None:
raise ValueError(
f"{self.repo_location} is not a valid git repository"
) from e
sh.git.checkout(self.git_branch, _cwd=self.repo_location, _async=False)

def __hash__(self):
return hash(self.repo_location)
Expand All @@ -234,14 +250,13 @@ def __init__(self, *, backend_url: ConfigSourceUrl) -> None:
if not backend_url:
raise ValueError("No remote url for RemoteGitConfigSource")

# git does not understand `git+https`, so we remove the `git+` part
self.remote_url = str(backend_url).replace("git+", "")
self._temp_dir = TemporaryDirectory()
self.repo_location = Path(self._temp_dir.name)
sh.git.clone(self.remote_url, self.repo_location, _async=False)
self._pull_cache: Cache = TTLCache(
MAX_PULL_CACHED_VERSIONS, DEFAULT_PULL_CACHE_TTL
)
sh.git.checkout(self.git_branch, _cwd=self.repo_location, _async=False)

def clear_caches(self):
super().clear_caches()
Expand Down
8 changes: 5 additions & 3 deletions diracx-core/tests/test_config_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
from diracx.core.config.schema import Config

# The diracx-chart contains a CS example
TEST_REPO = "git+https://github.com/DIRACGrid/diracx-charts/"
TEST_REPO = "git+https://github.com/DIRACGrid/diracx-charts.git"
TEST_REPO_SPECIFIC_BRANCH = TEST_REPO + "?branch=master"


def github_is_down():
Expand All @@ -22,13 +23,14 @@ def github_is_down():


@pytest.mark.skipif(github_is_down(), reason="Github unavailble")
def test_remote_git_config_source(monkeypatch):
@pytest.mark.parametrize("repo_url", [TEST_REPO, TEST_REPO_SPECIFIC_BRANCH])
def test_remote_git_config_source(monkeypatch, repo_url):

monkeypatch.setattr(
"diracx.core.config.sources.DEFAULT_CONFIG_FILE",
"k3s/examples/cs.yaml",
)
remote_conf = ConfigSource.create_from_url(backend_url=TEST_REPO)
remote_conf = ConfigSource.create_from_url(backend_url=repo_url)
assert isinstance(remote_conf, RemoteGitConfigSource)

hexsha, modified = remote_conf.latest_revision()
Expand Down

0 comments on commit d89f51f

Please sign in to comment.