-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* enhance check-ref * add examples * remove commented code
- Loading branch information
Showing
3 changed files
with
93 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
||
|
@@ -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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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 | ||
|
||
|
@@ -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 = """{ | ||
|