From b46b779465ab87ea63be30d1a4ee76e44c033312 Mon Sep 17 00:00:00 2001 From: Lucas Gomide Date: Mon, 9 Jun 2025 11:29:11 -0300 Subject: [PATCH] feat: improve logging about current organization while publishing/install a Tool --- src/crewai/cli/tools/main.py | 11 ++++++++++- tests/cli/tools/test_main.py | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/crewai/cli/tools/main.py b/src/crewai/cli/tools/main.py index 92feec3a0..ecdf972d2 100644 --- a/src/crewai/cli/tools/main.py +++ b/src/crewai/cli/tools/main.py @@ -91,6 +91,7 @@ class ToolCommand(BaseCommand, PlusAPIMixin): console.print( f"[green]Found these tools to publish: {', '.join([e['name'] for e in available_exports])}[/green]" ) + self._print_current_organization() with tempfile.TemporaryDirectory() as temp_build_dir: subprocess.run( @@ -136,6 +137,7 @@ class ToolCommand(BaseCommand, PlusAPIMixin): ) def install(self, handle: str): + self._print_current_organization() get_response = self.plus_api_client.get_tool(handle) if get_response.status_code == 404: @@ -182,7 +184,7 @@ class ToolCommand(BaseCommand, PlusAPIMixin): settings.dump() console.print( - "Successfully authenticated to the tool repository.", style="bold green" + f"Successfully authenticated to the tool repository as {settings.org_name} ({settings.org_uuid}).", style="bold green" ) def _add_package(self, tool_details: dict[str, Any]): @@ -240,3 +242,10 @@ class ToolCommand(BaseCommand, PlusAPIMixin): ) return env + + def _print_current_organization(self): + settings = Settings() + if settings.org_uuid: + console.print(f"Current organization: {settings.org_name} ({settings.org_uuid})", style="bold blue") + else: + console.print("No organization currently set. We recommend setting one before using: `crewai org switch ` command.", style="yellow") diff --git a/tests/cli/tools/test_main.py b/tests/cli/tools/test_main.py index 005663b56..182c3eb4f 100644 --- a/tests/cli/tools/test_main.py +++ b/tests/cli/tools/test_main.py @@ -56,7 +56,8 @@ def test_create_success(mock_subprocess, capsys, tool_command): @patch("crewai.cli.tools.main.subprocess.run") @patch("crewai.cli.plus_api.PlusAPI.get_tool") -def test_install_success(mock_get, mock_subprocess_run, capsys, tool_command): +@patch("crewai.cli.tools.main.ToolCommand._print_current_organization") +def test_install_success(mock_print_org, mock_get, mock_subprocess_run, capsys, tool_command): mock_get_response = MagicMock() mock_get_response.status_code = 200 mock_get_response.json.return_value = { @@ -85,6 +86,9 @@ def test_install_success(mock_get, mock_subprocess_run, capsys, tool_command): env=unittest.mock.ANY, ) + # Verify _print_current_organization was called + mock_print_org.assert_called_once() + @patch("crewai.cli.tools.main.subprocess.run") @patch("crewai.cli.plus_api.PlusAPI.get_tool") def test_install_success_from_pypi(mock_get, mock_subprocess_run, capsys, tool_command): @@ -166,7 +170,9 @@ def test_publish_when_not_in_sync(mock_is_synced, capsys, tool_command): @patch("crewai.cli.plus_api.PlusAPI.publish_tool") @patch("crewai.cli.tools.main.git.Repository.is_synced", return_value=False) @patch("crewai.cli.tools.main.extract_available_exports", return_value=[{"name": "SampleTool"}]) +@patch("crewai.cli.tools.main.ToolCommand._print_current_organization") def test_publish_when_not_in_sync_and_force( + mock_print_org, mock_available_exports, mock_is_synced, mock_publish, @@ -202,6 +208,7 @@ def test_publish_when_not_in_sync_and_force( encoded_file=unittest.mock.ANY, available_exports=[{"name": "SampleTool"}], ) + mock_print_org.assert_called_once() @patch("crewai.cli.tools.main.get_project_name", return_value="sample-tool") @@ -329,3 +336,27 @@ def test_publish_api_error( assert "Request to Enterprise API failed" in output mock_publish.assert_called_once() + + + +@patch("crewai.cli.tools.main.Settings") +def test_print_current_organization_with_org(mock_settings, capsys, tool_command): + mock_settings_instance = MagicMock() + mock_settings_instance.org_uuid = "test-org-uuid" + mock_settings_instance.org_name = "Test Organization" + mock_settings.return_value = mock_settings_instance + tool_command._print_current_organization() + output = capsys.readouterr().out + assert "Current organization: Test Organization (test-org-uuid)" in output + + +@patch("crewai.cli.tools.main.Settings") +def test_print_current_organization_without_org(mock_settings, capsys, tool_command): + mock_settings_instance = MagicMock() + mock_settings_instance.org_uuid = None + mock_settings_instance.org_name = None + mock_settings.return_value = mock_settings_instance + tool_command._print_current_organization() + output = capsys.readouterr().out + assert "No organization currently set" in output + assert "org switch " in output