Skip to content

Commit

Permalink
Enable remote git repos for check-ref, history, and stages (#273)
Browse files Browse the repository at this point in the history
* enable check-ref to use remote git repos

* change help message for repo to don't include remote repos, as not all commands support it

* enable history to use remote repos

* enable stages to use remote repos

* fix expected history to UTC time zone

* fix tz test

* fixing mypy complaint?

* hmmmmm

* hmmmmmmmmm just ignoring missing stubs

Co-authored-by: Francesco Calcavecchia <f18771@eon.com>
Co-authored-by: Alexander Guschin <1aguschin@gmail.com>
  • Loading branch information
3 people authored Sep 21, 2022
1 parent 0ee826d commit 24c165a
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 3 deletions.
3 changes: 3 additions & 0 deletions gto/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def _get_state(repo: Union[str, Repo]):
return GitRegistry.from_repo(repo).get_state()


@git_clone_remote_repo
def get_stages(repo: Union[str, Repo], allowed: bool = False, used: bool = False):
return GitRegistry.from_repo(repo).get_stages(allowed=allowed, used=used)

Expand Down Expand Up @@ -262,6 +263,7 @@ def find_versions_in_stage(
)


@git_clone_remote_repo
def check_ref(repo: Union[str, Repo], ref: str):
"""Find out what have been registered/assigned in the provided ref"""
reg = GitRegistry.from_repo(repo)
Expand Down Expand Up @@ -476,6 +478,7 @@ def describe(
raise NotImplementedError


@git_clone_remote_repo
def history(
repo: Union[str, Repo],
artifact: str = None,
Expand Down
2 changes: 1 addition & 1 deletion gto/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ def GTOGroupSection(section):
".",
"-r",
"--repo",
help="Repository to use (remote repos accepted)",
help="Repository to use",
show_default=True,
)
option_all_branches = Option(
Expand Down
3 changes: 3 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,6 @@ check_untyped_defs = False
warn_no_return = True
warn_redundant_casts = True
warn_unreachable = True

[mypy-freezegun.*]
ignore_missing_imports = True
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
# we use this to suppress some messages in tests, eg: foo/bar naming,
# and, protected method calls in our tests
"pylint-plugin-utils",
"freezegun",
"types-freezegun",
]


Expand Down
9 changes: 9 additions & 0 deletions tests/resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,12 @@ def get_sample_remote_repo_expected_registry() -> dict:
encoding="utf-8",
) as f:
return json.load(f)


def get_sample_remote_repo_expected_history_churn() -> dict:
with open(
Path(__file__).parent / "sample_remote_repo_expected_history_churn.json",
"r",
encoding="utf-8",
) as f:
return json.load(f)
93 changes: 93 additions & 0 deletions tests/resources/sample_remote_repo_expected_history_churn.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
[
{
"timestamp": "2022-08-26 04:55:09",
"artifact": "churn",
"event": "commit",
"priority": 0,
"version": "e1cc719a8486a083c033844ef8fe29f81f7c5b48",
"stage": null,
"commit": "e1cc719a8486a083c033844ef8fe29f81f7c5b48",
"author": "Amrit Ghimire",
"author_email": "mail@amritghimire.com",
"message": "Add new model to test deprecation\n",
"ref": "e1cc719a8486a083c033844ef8fe29f81f7c5b48"
},
{
"timestamp": "2022-05-26 23:49:52",
"artifact": "churn",
"event": "assignment",
"priority": 5,
"version": "v3.0.0",
"stage": "prod",
"commit": "e19353d623660452e97a351ab0ba56a92126bd70",
"author": "Alexander Guschin",
"author_email": "1aguschin@gmail.com",
"message": "Promoting churn version v3.0.0 to stage prod",
"ref": "churn#prod#2"
},
{
"timestamp": "2022-05-25 20:03:12",
"artifact": "churn",
"event": "assignment",
"priority": 5,
"version": "v3.1.0",
"stage": "staging",
"commit": "299e82a7b3244899a84e715a39b786a4451c25cd",
"author": "Alexander Guschin",
"author_email": "1aguschin@gmail.com",
"message": "Promoting churn version None to stage staging",
"ref": "churn#staging#1"
},
{
"timestamp": "2022-05-24 16:16:32",
"artifact": "churn",
"event": "registration",
"priority": 3,
"version": "v3.1.0",
"stage": null,
"commit": "299e82a7b3244899a84e715a39b786a4451c25cd",
"author": "Alexander Guschin",
"author_email": "1aguschin@gmail.com",
"message": "Registering artifact churn version v3.1.0",
"ref": "churn@v3.1.0"
},
{
"timestamp": "2022-05-23 12:29:52",
"artifact": "churn",
"event": "commit",
"priority": 0,
"version": "v3.1.0",
"stage": null,
"commit": "299e82a7b3244899a84e715a39b786a4451c25cd",
"author": "Alexander Guschin",
"author_email": "1aguschin@gmail.com",
"message": "Update model\n",
"ref": "299e82a7b3244899a84e715a39b786a4451c25cd"
},
{
"timestamp": "2022-05-20 01:09:52",
"artifact": "churn",
"event": "registration",
"priority": 3,
"version": "v3.0.0",
"stage": null,
"commit": "e19353d623660452e97a351ab0ba56a92126bd70",
"author": "Alexander Guschin",
"author_email": "1aguschin@gmail.com",
"message": "Registering artifact churn version v3.0.0",
"ref": "churn@v3.0.0"
},
{
"timestamp": "2022-05-18 21:23:12",
"artifact": "churn",
"event": "commit",
"priority": 0,
"version": "v3.0.0",
"stage": null,
"commit": "e19353d623660452e97a351ab0ba56a92126bd70",
"author": "Alexander Guschin",
"author_email": "1aguschin@gmail.com",
"message": "Annotate models with GTO\n",
"ref": "e19353d623660452e97a351ab0ba56a92126bd70"
}
]
50 changes: 48 additions & 2 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
"""TODO: add more tests for API"""
import os
from contextlib import contextmanager
from typing import Callable, Tuple
from typing import Callable, Optional, Tuple

import git
import pytest
from freezegun import freeze_time

import gto
import tests.resources
Expand All @@ -14,7 +15,10 @@
from gto.tag import find
from gto.versions import SemVer
from tests.skip_presets import skip_for_windows_py_lt_3_9
from tests.utils import check_obj
from tests.utils import (
check_obj,
convert_objects_to_str_in_json_serializable_object,
)


def test_empty_index(empty_git_repo: Tuple[git.Repo, Callable]):
Expand Down Expand Up @@ -298,3 +302,45 @@ def test_is_gto_repo_because_of_artifacts_yaml(empty_git_repo):
def test_if_show_on_remote_git_repo_then_return_expected_registry():
result = show(repo=tests.resources.SAMPLE_REMOTE_REPO_URL)
assert result == tests.resources.get_sample_remote_repo_expected_registry()


@skip_for_windows_py_lt_3_9
@pytest.mark.parametrize(
"ref,expected_stage,expected_version,expected_artifact",
(
("churn#prod#2", "prod", "v3.0.0", "churn"),
("segment@v0.4.1", None, "v0.4.1", "segment"),
),
)
def test_if_check_ref_on_remote_git_repo_then_return_expected_reference(
ref: str,
expected_stage: Optional[str],
expected_version: str,
expected_artifact: str,
):
result = gto.api.check_ref(repo=tests.resources.SAMPLE_REMOTE_REPO_URL, ref=ref)
assert len(result) == 1
if expected_stage is not None:
assert result[0].stage == expected_stage
else:
assert hasattr(result[0], "stage") is False
assert result[0].version == expected_version
assert result[0].artifact == expected_artifact


@freeze_time("1996-06-09 00:00:00", tz_offset=0)
@skip_for_windows_py_lt_3_9
def test_if_history_on_remote_git_repo_then_return_expected_history():
result = gto.api.history(
repo=tests.resources.SAMPLE_REMOTE_REPO_URL, artifact="churn"
)
assert (
convert_objects_to_str_in_json_serializable_object(result)
== tests.resources.get_sample_remote_repo_expected_history_churn()
)


@skip_for_windows_py_lt_3_9
def test_if_stages_on_remote_git_repo_then_return_expected_stages():
result = gto.api.get_stages(repo=tests.resources.SAMPLE_REMOTE_REPO_URL)
assert result == ["dev", "prod", "staging"]
7 changes: 7 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import os
from copy import deepcopy
from typing import Any, Dict, Sequence, Set, Union
Expand Down Expand Up @@ -35,6 +36,12 @@ def check_obj(
assert_equals(obj_values, values)


def convert_objects_to_str_in_json_serializable_object(
o: Union[list, dict]
) -> Union[list, dict]:
return json.loads(json.dumps(o, default=str))


def is_os_windows_and_py_lt_3_9() -> bool:
return (
os.environ.get("GITHUB_MATRIX_OS") == "windows-latest"
Expand Down

0 comments on commit 24c165a

Please sign in to comment.