Skip to content

Commit 69d1cb9

Browse files
authored
feat: cli users should have a way to give a pipeline code in the command to upload a pipeline (HEXA-1216) (#242)
* Support code * Test * Minor * Last test * Fix test
1 parent 957ad2c commit 69d1cb9

2 files changed

Lines changed: 120 additions & 1 deletion

File tree

openhexa/cli/cli.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,14 @@ def _handle_enter_pipeline_code():
322322
dir_okay=True,
323323
),
324324
)
325+
@click.option(
326+
"--code",
327+
"-c",
328+
type=str,
329+
help="Code of the pipeline",
330+
prompt="Code of the pipeline",
331+
prompt_required=False,
332+
)
325333
@click.option(
326334
"--name",
327335
"-n",
@@ -352,6 +360,7 @@ def _handle_enter_pipeline_code():
352360
@click.option("--yes", is_flag=True, help="Skip confirmation")
353361
def pipelines_push(
354362
path: str,
363+
code: str = None,
355364
name: str = None,
356365
description: str = None,
357366
link: str = None,
@@ -368,6 +377,8 @@ def pipelines_push(
368377
"activate a workspace.",
369378
err=True,
370379
)
380+
if yes and not code:
381+
_terminate("❌ You must provide a pipeline code (using -c or --code) when using the --yes flag.", err=True)
371382

372383
ensure_is_pipeline_dir(path)
373384

@@ -387,7 +398,11 @@ def pipelines_push(
387398
if settings.debug:
388399
click.echo(workspace_pipelines)
389400

390-
if yes:
401+
if code:
402+
selected_pipeline = get_pipeline_from_code(code) or _terminate(
403+
f"❌ Pipeline with code '{code}' not found.", err=True
404+
)
405+
elif yes:
391406
selected_pipeline = workspace_pipelines[0] if workspace_pipelines else None
392407
else:
393408
selected_pipeline = select_pipeline(workspace_pipelines, number_of_pages, pipeline)

tests/test_cli.py

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,110 @@ def test_push_pipeline(
185185
result.output,
186186
)
187187

188+
@patch("openhexa.cli.api.graphql")
189+
@patch.dict(os.environ, {"HEXA_API_URL": "https://www.bluesquarehub.com/", "HEXA_WORKSPACE": "workspace"})
190+
def test_push_pipeline_with_yes_flag_without_code(self, mock_graphql):
191+
"""Test pushing a pipeline with the --yes flag without providing a --code."""
192+
with self.runner.isolated_filesystem() as tmp:
193+
with open(Path(tmp) / python_file_name, "w") as f:
194+
f.write(python_code)
195+
mock_graphql.return_value = setup_graphql_response()
196+
197+
result = self.runner.invoke(
198+
pipelines_push,
199+
[tmp, "--yes"],
200+
)
201+
self.assertEqual(result.exit_code, 1)
202+
self.assertIn(
203+
"❌ You must provide a pipeline code (using -c or --code) when using the --yes flag.", result.output
204+
)
205+
206+
@patch("openhexa.cli.api.graphql")
207+
@patch("openhexa.cli.cli.get_pipeline")
208+
@patch("openhexa.cli.cli.get_pipelines_pages")
209+
@patch("openhexa.cli.cli.get_pipeline_from_code")
210+
@patch.dict(os.environ, {"HEXA_API_URL": "https://www.bluesquarehub.com/", "HEXA_WORKSPACE": "workspace"})
211+
def test_push_pipeline_with_non_existing_code(
212+
self, mock_get_pipeline_from_code, mock_get_pipelines_pages, mock_get_pipeline, mock_graphql
213+
):
214+
"""Test pushing a pipeline with a non-existing --code flag."""
215+
with self.runner.isolated_filesystem() as tmp:
216+
with open(Path(tmp) / python_file_name, "w") as f:
217+
f.write(python_code)
218+
mock_graphql.return_value = setup_graphql_response()
219+
mock_pipeline = MagicMock(spec=Pipeline)
220+
mock_pipeline.name = pipeline_name
221+
mock_get_pipeline.return_value = mock_pipeline
222+
mock_get_pipelines_pages.return_value = {
223+
"items": [
224+
{"name": "Pipeline1", "code": "code1"},
225+
{"name": "Pipeline2", "code": "code2"},
226+
],
227+
"totalPages": 2,
228+
}
229+
mock_get_pipeline_from_code.return_value = None # Simulate non-existing pipeline code
230+
231+
result = self.runner.invoke(
232+
pipelines_push,
233+
[tmp, "--code", "non_existing_code"],
234+
)
235+
self.assertEqual(result.exit_code, 1)
236+
self.assertIn("❌ Pipeline with code 'non_existing_code' not found.", result.output)
237+
238+
@patch("openhexa.cli.api.graphql")
239+
@patch("openhexa.cli.cli.get_pipeline")
240+
@patch("openhexa.cli.cli.get_pipelines_pages")
241+
@patch("openhexa.cli.cli.upload_pipeline")
242+
@patch.dict(os.environ, {"HEXA_API_URL": "https://www.bluesquarehub.com/", "HEXA_WORKSPACE": "workspace"})
243+
def test_push_pipeline_with_code_flag(
244+
self, mock_upload_pipeline, mock_get_pipelines_pages, mock_get_pipeline, mock_graphql
245+
):
246+
"""Test pushing a pipeline using the --code flag."""
247+
code = "code1"
248+
with self.runner.isolated_filesystem() as tmp:
249+
with open(Path(tmp) / python_file_name, "w") as f:
250+
f.write(python_code)
251+
mock_graphql.return_value = {
252+
"pipelineByCode": {
253+
"code": code,
254+
"currentVersion": {"zipfile": ""},
255+
},
256+
"pipelines": {"items": []},
257+
}
258+
mock_pipeline = MagicMock(spec=Pipeline)
259+
mock_pipeline.name = pipeline_name
260+
mock_get_pipeline.return_value = mock_pipeline
261+
mock_get_pipelines_pages.return_value = {
262+
"items": [
263+
{"name": "Pipeline1", "code": "code1"},
264+
{"name": "Pipeline2", "code": "code2"},
265+
],
266+
"totalPages": 2,
267+
}
268+
mock_upload_pipeline.return_value = {
269+
"versionName": version,
270+
"pipeline": {
271+
"id": pipeline_id,
272+
"permissions": {"createTemplateVersion": True},
273+
"template": template,
274+
},
275+
"id": pipeline_version_id,
276+
}
277+
278+
result = self.runner.invoke(
279+
pipelines_push,
280+
[tmp, "--name", version, "--code", code],
281+
input="Y\nn\n",
282+
)
283+
self.assertEqual(result.exit_code, 0)
284+
self.assertNotIn("Which pipeline do you want to update?", result.output)
285+
self.assertTrue(mock_upload_pipeline.called)
286+
self.assertEqual(mock_upload_pipeline.call_args[0][0], code)
287+
self.assertIn(
288+
f"✅ New version '{version}' created! ",
289+
result.output,
290+
)
291+
188292
@patch("openhexa.cli.cli.click.prompt")
189293
def test_select_pipeline(self, mock_prompt):
190294
workspace_pipelines = [

0 commit comments

Comments
 (0)