Skip to content

Commit 4d55068

Browse files
FragmentedPacketdgarros
authored andcommitted
InfrahubCTL: Repository command changes (#244)
* Refactor repository to not create blank credentials. Remove commit option in favor of ref option to match API and allow setting the ref on CTL. * Format/Lint. * Fixed payload between CoreRepositoryCreate and CoreReadOnlyRepositoryCreate mutations.
1 parent c0634af commit 4d55068

File tree

2 files changed

+74
-16
lines changed

2 files changed

+74
-16
lines changed

infrahub_sdk/ctl/repository.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ async def add(
7171
description: str = "",
7272
username: str | None = None,
7373
password: str = "",
74-
commit: str = "",
74+
ref: str = "",
7575
read_only: bool = False,
7676
debug: bool = False,
7777
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(
8686
"name": {"value": name},
8787
"location": {"value": location},
8888
"description": {"value": description},
89-
"commit": {"value": commit},
9089
},
9190
}
91+
if read_only:
92+
input_data["data"]["ref"] = {"value": ref}
93+
else:
94+
input_data["data"]["default_branch"] = {"value": ref}
9295

9396
client = initialize_client()
9497

95-
credential = await client.create(kind="CorePasswordCredential", name=name, username=username, password=password)
96-
await credential.save(allow_upsert=True)
97-
input_data["data"]["credential"] = {"id": credential.id}
98+
if username or password:
99+
credential = await client.create(
100+
kind="CorePasswordCredential",
101+
name=name,
102+
username=username,
103+
password=password,
104+
)
105+
await credential.save(allow_upsert=True)
106+
input_data["data"]["credential"] = {"id": credential.id}
98107

99108
query = Mutation(
100109
mutation="CoreReadOnlyRepositoryCreate" if read_only else "CoreRepositoryCreate",

tests/unit/ctl/test_repository_app.py

Lines changed: 60 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,55 @@ def mock_client() -> mock.Mock:
2929
class TestInfrahubctlRepository:
3030
"""Groups the 'infrahubctl repository' test cases."""
3131

32+
@requires_python_310
33+
@mock.patch("infrahub_sdk.ctl.repository.initialize_client")
34+
def test_repo_no_username_or_password(self, mock_init_client, mock_client) -> None:
35+
"""Case allow no username to be passed in and set it as None rather than blank string that fails."""
36+
mock_cred = mock.AsyncMock()
37+
mock_cred.id = "1234"
38+
mock_client.create.return_value = mock_cred
39+
40+
mock_init_client.return_value = mock_client
41+
output = runner.invoke(
42+
app,
43+
[
44+
"repository",
45+
"add",
46+
"Gitlab",
47+
"https://gitlab.com/opsmill/example-repo.git",
48+
],
49+
)
50+
assert output.exit_code == 0
51+
mock_client.create.assert_not_called()
52+
mock_cred.save.assert_not_called()
53+
mock_client.execute_graphql.assert_called_once()
54+
mock_client.execute_graphql.assert_called_with(
55+
query="""
56+
mutation {
57+
CoreRepositoryCreate(
58+
data: {
59+
name: {
60+
value: "Gitlab"
61+
}
62+
location: {
63+
value: "https://gitlab.com/opsmill/example-repo.git"
64+
}
65+
description: {
66+
value: ""
67+
}
68+
default_branch: {
69+
value: ""
70+
}
71+
}
72+
){
73+
ok
74+
}
75+
}
76+
""",
77+
branch_name="main",
78+
tracker="mutation-repository-create",
79+
)
80+
3281
@requires_python_310
3382
@mock.patch("infrahub_sdk.ctl.repository.initialize_client")
3483
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:
74123
description: {
75124
value: ""
76125
}
77-
commit: {
126+
default_branch: {
78127
value: ""
79128
}
80129
credential: {
@@ -137,7 +186,7 @@ def test_repo_username(self, mock_init_client, mock_client) -> None:
137186
description: {
138187
value: ""
139188
}
140-
commit: {
189+
default_branch: {
141190
value: ""
142191
}
143192
credential: {
@@ -168,7 +217,7 @@ def test_repo_readonly_true(self, mock_init_client, mock_client) -> None:
168217
"repository",
169218
"add",
170219
"Gitlab",
171-
"https://gitlab.com/FragmentedPacket/nautobot-plugin-ansible-filters.git",
220+
"https://gitlab.com/opsmill/example-repo.git",
172221
"--password",
173222
"mySup3rSecureP@ssw0rd",
174223
"--read-only",
@@ -194,12 +243,12 @@ def test_repo_readonly_true(self, mock_init_client, mock_client) -> None:
194243
value: "Gitlab"
195244
}
196245
location: {
197-
value: "https://gitlab.com/FragmentedPacket/nautobot-plugin-ansible-filters.git"
246+
value: "https://gitlab.com/opsmill/example-repo.git"
198247
}
199248
description: {
200249
value: ""
201250
}
202-
commit: {
251+
ref: {
203252
value: ""
204253
}
205254
credential: {
@@ -230,15 +279,15 @@ def test_repo_description_commit_branch(self, mock_init_client, mock_client) ->
230279
"repository",
231280
"add",
232281
"Gitlab",
233-
"https://gitlab.com/FragmentedPacket/nautobot-plugin-ansible-filters.git",
282+
"https://gitlab.com/opsmill/example-repo.git",
234283
"--password",
235284
"mySup3rSecureP@ssw0rd",
236285
"--username",
237286
"opsmill",
238287
"--description",
239288
"This is a test description",
240-
"--commit",
241-
"myHashCommit",
289+
"--ref",
290+
"my-custom-branch",
242291
"--branch",
243292
"develop",
244293
],
@@ -263,13 +312,13 @@ def test_repo_description_commit_branch(self, mock_init_client, mock_client) ->
263312
value: "Gitlab"
264313
}
265314
location: {
266-
value: "https://gitlab.com/FragmentedPacket/nautobot-plugin-ansible-filters.git"
315+
value: "https://gitlab.com/opsmill/example-repo.git"
267316
}
268317
description: {
269318
value: "This is a test description"
270319
}
271-
commit: {
272-
value: "myHashCommit"
320+
default_branch: {
321+
value: "my-custom-branch"
273322
}
274323
credential: {
275324
id: "1234"

0 commit comments

Comments
 (0)