Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: delete pipeline #91

Merged
merged 3 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions openhexa/cli/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,26 @@ def create_pipeline(config, pipeline_code: str, pipeline_name: str):
return data["createPipeline"]["pipeline"]


def delete_pipeline(config, id: str):
data = graphql(
config,
"""
mutation deletePipeline($input: DeletePipelineInput!) {
deletePipeline(input: $input) {
success
errors
}
}
""",
{"input": {"id": id}},
)

if not data["deletePipeline"]["success"]:
raise Exception(data["deletePipeline"]["errors"])

return data["deletePipeline"]["success"]


def ensure_is_pipeline_dir(pipeline_path: str):
# Ensure that there is a pipeline.py file in the directory
if not os.path.isdir(pipeline_path):
Expand Down
51 changes: 51 additions & 0 deletions openhexa/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
open_config,
save_config,
upload_pipeline,
delete_pipeline,
)
from openhexa.cli.utils import terminate
from openhexa.sdk.pipelines import get_local_workspace_config, import_pipeline
Expand Down Expand Up @@ -308,6 +309,56 @@ def pipelines_push(path: str):
)


@pipelines.command("delete")
@click.argument("code", type=str)
def pipelines_delete(code: str):
"""
Delete a pipeline and all his versions.
"""

user_config = open_config()
try:
workspace = user_config["openhexa"]["current_workspace"]
except KeyError:
click.echo(
"No workspace activated. Use openhexa workspaces add or openhexa workspaces activate to "
"activate a workspace.",
err=True,
)
sys.exit(1)
else:
pipeline = get_pipeline(user_config, code)
if get_pipeline(user_config, code) is None:
click.echo(
f"Pipeline {click.style(code, bold=True)} does not exist in workspace {click.style(workspace, bold=True)}"
)
sys.exit(1)

confirmation_code = click.prompt(
f"Removing {code} from workspace {workspace}. Please type {code} to confirm",
Copy link
Contributor

@pvanliefland pvanliefland Nov 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor change:

f'This will remove the pipeline "{code}" from the "{workspace} workspace. This operation cannot be undone.\nPlease enter "{code}" to confirm:'

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and let's use click.style like a couple of lines above

type=str,
)

if confirmation_code != code:
click.echo(
"Pipeline code and confirmation are different, aborted.",
err=True,
)
sys.exit(1)

try:
if delete_pipeline(user_config, pipeline["id"]):
click.echo(f"Pipeline {code} deleted.")

except Exception as e:
terminate(
f'Error while deleting pipeline: "{e}"',
err=True,
exception=e,
debug=is_debug(user_config),
)


@pipelines.command("run")
@click.argument("path", default=".", type=click.Path(exists=True, file_okay=False, dir_okay=True))
@click.option("-c", "config_str", type=str, help="Configuration JSON as a string")
Expand Down
61 changes: 60 additions & 1 deletion tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
import pytest
import shutil
import yaml
import uuid

from click.testing import CliRunner
from openhexa.cli.api import upload_pipeline
from openhexa.cli.cli import pipelines_init
from openhexa.cli.cli import pipelines_init, pipelines_delete
from pathlib import Path

from unittest import mock
Expand Down Expand Up @@ -70,3 +71,61 @@ def test_upload_pipeline_custom_files_path():

shutil.rmtree(pipeline_dir)
os.remove(pipeline_zip_file_dir)


def test_delete_pipeline_not_in_workspace():
config = configparser.ConfigParser()
config["openhexa"] = {"debug": True, "current_workspace": "test_workspace"}

with mock.patch("openhexa.cli.api.graphql") as mocked_graphql_client, mock.patch(
"openhexa.cli.cli.open_config"
) as mocked_config:
runner = CliRunner()
mocked_config.return_value = config
mocked_graphql_client.return_value = {"pipelineByCode": None}
r = runner.invoke(pipelines_delete, ["test_pipelines"], input="test_pipelines")

assert r.output == "Pipeline test_pipelines does not exist in workspace test_workspace\n"


def test_delete_pipeline_confirm_code_invalid():
config = configparser.ConfigParser()
config["openhexa"] = {"debug": True, "current_workspace": "test_workspace"}

with mock.patch("openhexa.cli.api.graphql") as mocked_graphql_client, mock.patch(
"openhexa.cli.cli.open_config"
) as mocked_config:
runner = CliRunner()
mocked_config.return_value = config

mocked_graphql_client.return_value = {
"pipelineByCode": {
"id": uuid.uuid4(),
"code": "test_pipelines",
"currentVersion": {"number": 1},
}
}
r = runner.invoke(pipelines_delete, ["test_pipelines"], input="test_pipeline")
# "Pipeline code and confirmation are different
assert r.exit_code == 1


def test_delete_pipeline():
config = configparser.ConfigParser()
config["openhexa"] = {"debug": True, "current_workspace": "test_workspace"}

with mock.patch("openhexa.cli.cli.get_pipeline") as mocked_get_pipeline, mock.patch(
"openhexa.cli.cli.delete_pipeline"
) as mocked_delete_pipeline, mock.patch("openhexa.cli.cli.open_config") as mocked_config:
runner = CliRunner()
mocked_config.return_value = config

mocked_get_pipeline.return_value = {
"id": uuid.uuid4(),
"code": "test_pipelines",
"currentVersion": {"number": 1},
}
mocked_delete_pipeline.return_value = True
r = runner.invoke(pipelines_delete, ["test_pipelines"], input="test_pipelines")

assert r.exit_code == 0
Loading