diff --git a/infrahub_sdk/ctl/repository.py b/infrahub_sdk/ctl/repository.py index 09003f6e..8f793946 100644 --- a/infrahub_sdk/ctl/repository.py +++ b/infrahub_sdk/ctl/repository.py @@ -71,7 +71,7 @@ async def add( description: str = "", username: str | None = None, password: str = "", - commit: str = "", + ref: str = "", read_only: bool = False, debug: bool = False, branch: str = typer.Option("main", help="Branch on which to add the repository."), # TODO: Replace main by None @@ -86,15 +86,24 @@ async def add( "name": {"value": name}, "location": {"value": location}, "description": {"value": description}, - "commit": {"value": commit}, }, } + if read_only: + input_data["data"]["ref"] = {"value": ref} + else: + input_data["data"]["default_branch"] = {"value": ref} client = initialize_client() - credential = await client.create(kind="CorePasswordCredential", name=name, username=username, password=password) - await credential.save(allow_upsert=True) - input_data["data"]["credential"] = {"id": credential.id} + if username or password: + credential = await client.create( + kind="CorePasswordCredential", + name=name, + username=username, + password=password, + ) + await credential.save(allow_upsert=True) + input_data["data"]["credential"] = {"id": credential.id} query = Mutation( mutation="CoreReadOnlyRepositoryCreate" if read_only else "CoreRepositoryCreate", diff --git a/tests/unit/ctl/test_repository_app.py b/tests/unit/ctl/test_repository_app.py index a7cd70f0..04d05ed4 100644 --- a/tests/unit/ctl/test_repository_app.py +++ b/tests/unit/ctl/test_repository_app.py @@ -29,6 +29,55 @@ def mock_client() -> mock.Mock: class TestInfrahubctlRepository: """Groups the 'infrahubctl repository' test cases.""" + @requires_python_310 + @mock.patch("infrahub_sdk.ctl.repository.initialize_client") + def test_repo_no_username_or_password(self, mock_init_client, mock_client) -> None: + """Case allow no username to be passed in and set it as None rather than blank string that fails.""" + mock_cred = mock.AsyncMock() + mock_cred.id = "1234" + mock_client.create.return_value = mock_cred + + mock_init_client.return_value = mock_client + output = runner.invoke( + app, + [ + "repository", + "add", + "Gitlab", + "https://gitlab.com/opsmill/example-repo.git", + ], + ) + assert output.exit_code == 0 + mock_client.create.assert_not_called() + mock_cred.save.assert_not_called() + mock_client.execute_graphql.assert_called_once() + mock_client.execute_graphql.assert_called_with( + query=""" +mutation { + CoreRepositoryCreate( + data: { + name: { + value: "Gitlab" + } + location: { + value: "https://gitlab.com/opsmill/example-repo.git" + } + description: { + value: "" + } + default_branch: { + value: "" + } + } + ){ + ok + } +} +""", + branch_name="main", + tracker="mutation-repository-create", + ) + @requires_python_310 @mock.patch("infrahub_sdk.ctl.repository.initialize_client") def test_repo_no_username(self, mock_init_client, mock_client) -> None: @@ -74,7 +123,7 @@ def test_repo_no_username(self, mock_init_client, mock_client) -> None: description: { value: "" } - commit: { + default_branch: { value: "" } credential: { @@ -137,7 +186,7 @@ def test_repo_username(self, mock_init_client, mock_client) -> None: description: { value: "" } - commit: { + default_branch: { value: "" } credential: { @@ -168,7 +217,7 @@ def test_repo_readonly_true(self, mock_init_client, mock_client) -> None: "repository", "add", "Gitlab", - "https://gitlab.com/FragmentedPacket/nautobot-plugin-ansible-filters.git", + "https://gitlab.com/opsmill/example-repo.git", "--password", "mySup3rSecureP@ssw0rd", "--read-only", @@ -194,12 +243,12 @@ def test_repo_readonly_true(self, mock_init_client, mock_client) -> None: value: "Gitlab" } location: { - value: "https://gitlab.com/FragmentedPacket/nautobot-plugin-ansible-filters.git" + value: "https://gitlab.com/opsmill/example-repo.git" } description: { value: "" } - commit: { + ref: { value: "" } credential: { @@ -230,15 +279,15 @@ def test_repo_description_commit_branch(self, mock_init_client, mock_client) -> "repository", "add", "Gitlab", - "https://gitlab.com/FragmentedPacket/nautobot-plugin-ansible-filters.git", + "https://gitlab.com/opsmill/example-repo.git", "--password", "mySup3rSecureP@ssw0rd", "--username", "opsmill", "--description", "This is a test description", - "--commit", - "myHashCommit", + "--ref", + "my-custom-branch", "--branch", "develop", ], @@ -263,13 +312,13 @@ def test_repo_description_commit_branch(self, mock_init_client, mock_client) -> value: "Gitlab" } location: { - value: "https://gitlab.com/FragmentedPacket/nautobot-plugin-ansible-filters.git" + value: "https://gitlab.com/opsmill/example-repo.git" } description: { value: "This is a test description" } - commit: { - value: "myHashCommit" + default_branch: { + value: "my-custom-branch" } credential: { id: "1234"