-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
204 additions
and
12 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
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
from .run import run | ||
from .task import create, update |
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 |
---|---|---|
@@ -0,0 +1,101 @@ | ||
from typing import Tuple | ||
|
||
import typer | ||
from rich import print | ||
|
||
from taskbadger import StatusEnum, create_task, update_task | ||
from taskbadger.cli.utils import configure_api, err_console, get_actions, get_metadata | ||
|
||
|
||
def create( | ||
ctx: typer.Context, | ||
name: str = typer.Argument(..., show_default=False, help="The task name."), | ||
monitor_id: str = typer.Option(None, help="Associate this task with a monitor."), | ||
action_def: Tuple[str, str, str] = typer.Option( | ||
(None, None, None), | ||
"--action", | ||
"-a", | ||
metavar="<trigger integration config>", | ||
show_default=False, | ||
help="Action definition e.g. 'success,error email to:[email protected]'", | ||
), | ||
status: StatusEnum = typer.Option(StatusEnum.PROCESSING, help="The initial status of the task."), | ||
value_max: int = typer.Option(100, help="The maximum value for the task."), | ||
metadata: list[str] = typer.Option( | ||
None, | ||
show_default=False, | ||
help="Metadata 'key=value' pair to associate with the task. Can be specified multiple times.", | ||
), | ||
metadata_json: str = typer.Option( | ||
None, show_default=False, help="Metadata to associate with the task. Must be valid JSON." | ||
), | ||
quiet: bool = typer.Option(False, "--quiet", "-q", help="Minimal output. Only the Task ID."), | ||
): | ||
"""Create a task.""" | ||
configure_api(ctx) | ||
actions = get_actions(action_def) | ||
metadata = get_metadata(metadata, metadata_json) | ||
|
||
try: | ||
task = create_task( | ||
name, | ||
status=status, | ||
value_max=value_max, | ||
data=metadata, | ||
actions=actions, | ||
monitor_id=monitor_id, | ||
) | ||
except Exception as e: | ||
err_console.print(f"Error creating task: {e}") | ||
else: | ||
if quiet: | ||
print(task.id) | ||
else: | ||
print(f"Task created: {task.public_url}") | ||
|
||
|
||
def update( | ||
ctx: typer.Context, | ||
task_id: str = typer.Argument(..., show_default=False, help="The ID of the task to update."), | ||
name: str = typer.Option(None, show_default=False, help="Update the name of the task."), | ||
action_def: Tuple[str, str, str] = typer.Option( | ||
(None, None, None), | ||
"--action", | ||
"-a", | ||
metavar="<trigger integration config>", | ||
show_default=False, | ||
help="Action definition e.g. 'success,error email to:[email protected]'", | ||
), | ||
status: StatusEnum = typer.Option(StatusEnum.PROCESSING, help="The status of the task."), | ||
value: int = typer.Option(None, show_default=False, help="The current task value (progress)."), | ||
value_max: int = typer.Option(None, show_default=False, help="The maximum value for the task."), | ||
metadata: list[str] = typer.Option( | ||
None, | ||
show_default=False, | ||
help="Metadata 'key=value' pair to associate with the task. Can be specified multiple times.", | ||
), | ||
metadata_json: str = typer.Option( | ||
None, show_default=False, help="Metadata to associate with the task. Must be valid JSON." | ||
), | ||
quiet: bool = typer.Option(False, "--quiet", "-q", help="No output."), | ||
): | ||
"""Update a task.""" | ||
configure_api(ctx) | ||
actions = get_actions(action_def) | ||
metadata = get_metadata(metadata, metadata_json) | ||
|
||
try: | ||
task = update_task( | ||
task_id, | ||
name=name, | ||
status=status, | ||
value=value, | ||
value_max=value_max, | ||
data=metadata, | ||
actions=actions, | ||
) | ||
except Exception as e: | ||
err_console.print(f"Error creating task: {e}") | ||
else: | ||
if not quiet: | ||
print(f"Task updated: {task.public_url}") |
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
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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import os | ||
from http import HTTPStatus | ||
from unittest import mock | ||
|
||
import pytest | ||
from typer.testing import CliRunner | ||
|
||
from taskbadger.cli_main import app | ||
from taskbadger.internal.models import ( | ||
PatchedTaskRequest, | ||
PatchedTaskRequestData, | ||
StatusEnum, | ||
TaskRequest, | ||
TaskRequestData, | ||
) | ||
from taskbadger.internal.types import UNSET, Response | ||
from taskbadger.mug import Badger | ||
from taskbadger.sdk import Task | ||
from tests.utils import task_for_test | ||
|
||
runner = CliRunner() | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def mock_env(): | ||
with mock.patch.dict( | ||
os.environ, | ||
{ | ||
"TASKBADGER_ORG": "org", | ||
"TASKBADGER_PROJECT": "project", | ||
"TASKBADGER_API_KEY": "token", | ||
}, | ||
clear=True, | ||
): | ||
yield | ||
|
||
|
||
def test_cli_create(): | ||
with (mock.patch("taskbadger.sdk.task_create.sync_detailed") as create,): | ||
task = task_for_test() | ||
create.return_value = Response(HTTPStatus.OK, b"", {}, task) | ||
|
||
args = ["create", "my-task", "--metadata-json", '{"a": 1, "c": 1}', "--metadata", "b=2", "--metadata", "a=3"] | ||
result = runner.invoke(app, args, catch_exceptions=False) | ||
assert result.exit_code == 0, result.output | ||
|
||
request = TaskRequest( | ||
name="my-task", | ||
status=StatusEnum.PROCESSING, | ||
value_max=100, | ||
data=TaskRequestData.from_dict({"b": "2", "a": 1, "c": 1}), | ||
) | ||
create.assert_called_with(client=mock.ANY, organization_slug="org", project_slug="project", json_body=request) | ||
|
||
|
||
def test_cli_update(): | ||
with mock.patch("taskbadger.sdk.task_partial_update.sync_detailed") as update: | ||
task = task_for_test() | ||
update.return_value = Response(HTTPStatus.OK, b"", {}, task) | ||
|
||
result = runner.invoke(app, ["update", "task123", "--status=success", "--value", "100"], catch_exceptions=False) | ||
assert result.exit_code == 0, result.output | ||
|
||
body = PatchedTaskRequest(status=StatusEnum.SUCCESS, value=100) | ||
|
||
update.assert_called_with( | ||
client=mock.ANY, organization_slug="org", project_slug="project", id="task123", json_body=body | ||
) |