Skip to content

Commit

Permalink
feat: delete pipeline (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
cheikhgwane authored Nov 30, 2023
1 parent b3b6740 commit 549f73d
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 1 deletion.
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'This will remove the pipeline "{click.style(code, bold=True)}" from the "{click.style(workspace, bold=True)} workspace. This operation cannot be undone.\nPlease enter "{click.style(code, bold=True)}" to confirm',
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 {click.style(code, bold=True)} 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

0 comments on commit 549f73d

Please sign in to comment.