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

InfrahubCTL: Repository command changes #244

Open
wants to merge 2 commits into
base: stable
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
16 changes: 11 additions & 5 deletions infrahub_sdk/ctl/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ async def add(
description: str = "",
username: str | None = None,
password: str = "",
commit: str = "",
ref: str = "",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do we need to keep commit but make it mutually exclusive with ref?

read_only: bool = False,
debug: bool = False,
branch: str = typer.Option("main", help="Branch on which to add the repository."),
Expand All @@ -85,15 +85,21 @@ async def add(
"name": {"value": name},
"location": {"value": location},
"description": {"value": description},
"commit": {"value": commit},
"ref": {"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",
Expand Down
70 changes: 59 additions & 11 deletions tests/unit/ctl/test_repository_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,54 @@ def mock_client() -> mock.Mock:
class TestInfrahubctlRepository:
"""Groups the 'infrahubctl repository' test cases."""

@requires_python_310
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: ""
}
ref: {
value: ""
}
}
){
ok
}
}
""",
branch_name="main",
tracker="mutation-repository-create",
)

@requires_python_310
def test_repo_no_username(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."""
Expand Down Expand Up @@ -72,7 +120,7 @@ def test_repo_no_username(self, mock_init_client, mock_client) -> None:
description: {
value: ""
}
commit: {
ref: {
value: ""
}
credential: {
Expand Down Expand Up @@ -134,7 +182,7 @@ def test_repo_username(self, mock_init_client, mock_client) -> None:
description: {
value: ""
}
commit: {
ref: {
value: ""
}
credential: {
Expand Down Expand Up @@ -164,7 +212,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",
Expand All @@ -190,12 +238,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: {
Expand Down Expand Up @@ -225,15 +273,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",
],
Expand All @@ -258,13 +306,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"
ref: {
value: "my-custom-branch"
}
credential: {
id: "1234"
Expand Down
Loading