Skip to content

Commit

Permalink
Improve check-ref (#205)
Browse files Browse the repository at this point in the history
* enhance check-ref

* add examples

* remove commented code
  • Loading branch information
aguschin authored Jul 12, 2022
1 parent c27e993 commit c64cbdf
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 15 deletions.
59 changes: 46 additions & 13 deletions gto/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from typer.core import TyperCommand, TyperGroup

import gto
from gto.exceptions import GTOException, WrongArgs
from gto.exceptions import GTOException, NotImplementedInGTO, WrongArgs
from gto.ui import EMOJI_FAIL, EMOJI_GTO, EMOJI_OK, bold, cli_echo, color, echo
from gto.utils import format_echo, make_ready_to_serialize

Expand Down Expand Up @@ -588,31 +588,64 @@ def parse_tag(
def check_ref(
repo: str = option_repo,
ref: str = Argument(..., help="Git reference to analyze"),
json: bool = Option(False, "--json", is_flag=True, help="Output in JSON format"),
json: bool = option_json,
registration: bool = Option(
False, "--registration", is_flag=True, help="Check if ref registers a version"
),
promotion: bool = Option(
False, "--promotion", is_flag=True, help="Check if ref promotes a version"
),
name: bool = Option(False, "--name", is_flag=True, help="Output artifact name"),
version: bool = Option(
False, "--version", is_flag=True, help="Output artifact version"
),
stage: bool = Option(False, "--stage", is_flag=True, help="Output artifact stage"),
):
"""Find out the artifact version registered/promoted with ref
Examples:
$ gto check-ref [email protected]
$ gto check-ref rf#prod
$ gto check-ref rf#prod --name
$ gto check-ref rf#prod --version
"""
assert (
sum(bool(i) for i in (json, registration, promotion, name, version, stage)) <= 1
), "Only one output formatting flags is allowed"
result = {
action: {name: version.dict() for name, version in found.items()}
for action, found in gto.api.check_ref(repo, ref).items()
}
version_dict = list(result["version"].values())[0] if result["version"] else {}
stage_dict = list(result["stage"].values())[0] if result["stage"] else {}
if json:
format_echo(result, "json")
else:
if result["version"]:
v = list(result["version"].values())[0]
echo(
f"""{EMOJI_OK} Version "{v["name"]}" of artifact "{v["artifact"]}" was registered"""
)
if result["stage"]:
s = list(result["stage"].values())[0]
echo(
f"""{EMOJI_OK} Version "{s["version"]}" of artifact "{s["artifact"]}" was promoted to "{s["stage"]}" stage"""
elif name:
if version_dict:
echo(version_dict["artifact"])
if stage_dict:
echo(stage_dict["artifact"])
elif version:
if version_dict:
echo(version_dict["name"])
if stage_dict:
echo(stage_dict["version"])
elif stage:
if version_dict:
raise NotImplementedInGTO(
"--stage is not implemented for version promotion git tag"
)
if stage_dict:
echo(stage_dict["stage"])
elif (registration or not promotion) and version_dict:
echo(
f"""{EMOJI_OK} Version "{version_dict["name"]}" of artifact """
f""""{version_dict["artifact"]}" was registered"""
)
elif (promotion or not registration) and stage_dict:
echo(
f"""{EMOJI_OK} Version "{stage_dict["version"]}" of artifact """
f""""{stage_dict["artifact"]}" was promoted to "{stage_dict["stage"]}" stage"""
)


@gto_command(section=CommandGroups.querying)
Expand Down
4 changes: 4 additions & 0 deletions gto/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,7 @@ def __init__(self, name) -> None:

class ValidationError(GTOException):
pass


class NotImplementedInGTO(GTOException):
pass
45 changes: 43 additions & 2 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def _check_successful_cmd(cmd: str, args: list, expected_stdout: Optional[str]):
runner = CliRunner()
result = runner.invoke(app, [cmd] + args)
assert result.exit_code == 0, (result.output, result.exception)
if expected_stdout:
if expected_stdout is not None:
if len(expected_stdout):
assert len(result.output) > 0, "Output is empty, but should not be"
assert result.output == expected_stdout
Expand All @@ -26,7 +26,7 @@ def _check_failing_cmd(cmd: str, args: list, expected_stderr: str):
runner = CliRunner()
result = runner.invoke(app, [cmd] + args)
assert result.exit_code != 0, (result.output, result.exception)
if expected_stderr:
if expected_stderr is not None:
assert len(result.output) > 0, "Output is empty, but should not be"
assert result.output == expected_stderr

Expand Down Expand Up @@ -131,6 +131,47 @@ def test_commands(showcase):
["-r", path],
None,
)
# check-ref
_check_successful_cmd(
"check-ref",
["-r", path, "rf#production#3", "--name"],
"rf\n",
)
_check_successful_cmd(
"check-ref",
["-r", path, "rf#production#3", "--stage"],
"production\n",
)
_check_successful_cmd(
"check-ref",
["-r", path, "rf#production#3", "--version"],
"v1.2.4\n", # since this version was promoted
)
_check_successful_cmd(
"check-ref",
["-r", path, "[email protected]", "--version"],
"v1.2.4\n",
)
_check_successful_cmd(
"check-ref",
["-r", path, "[email protected]", "--promotion"],
"", # since this tag doesn't promote to any stage
)
_check_successful_cmd(
"check-ref",
["-r", path, "[email protected]", "--registration"],
'✅ Version "v1.2.4" of artifact "rf" was registered\n',
)
_check_successful_cmd(
"check-ref",
["-r", path, "rf#production#3", "--registration"],
"",
)
_check_successful_cmd(
"check-ref",
["-r", path, "rf#production#3", "--promotion"],
'✅ Version "v1.2.4" of artifact "rf" was promoted to "production" stage\n',
)


EXPECTED_DESCRIBE_OUTPUT_2 = """{
Expand Down

0 comments on commit c64cbdf

Please sign in to comment.