Skip to content

Commit 9012b62

Browse files
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.
1 parent dbbe385 commit 9012b62

File tree

2 files changed

+85
-21
lines changed

2 files changed

+85
-21
lines changed

infrahub_sdk/ctl/repository.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ def get_repository_config(repo_config_file: Path) -> InfrahubRepositoryConfig:
3434
try:
3535
data = InfrahubRepositoryConfig(**config_file_data)
3636
except ValidationError as exc:
37-
console.print(f"[red]Repository config file not valid, found {len(exc.errors())} error(s)")
37+
console.print(
38+
f"[red]Repository config file not valid, found {len(exc.errors())} error(s)"
39+
)
3840
for error in exc.errors():
3941
loc_str = [str(item) for item in error["loc"]]
4042
console.print(f" {'/'.join(loc_str)} | {error['msg']} ({error['type']})")
@@ -70,7 +72,7 @@ async def add(
7072
description: str = "",
7173
username: str | None = None,
7274
password: str = "",
73-
commit: str = "",
75+
ref: str = "",
7476
read_only: bool = False,
7577
debug: bool = False,
7678
branch: str = typer.Option("main", help="Branch on which to add the repository."),
@@ -85,20 +87,30 @@ async def add(
8587
"name": {"value": name},
8688
"location": {"value": location},
8789
"description": {"value": description},
88-
"commit": {"value": commit},
90+
"ref": {"value": ref},
8991
},
9092
}
9193

9294
client = initialize_client()
9395

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

98106
query = Mutation(
99-
mutation="CoreReadOnlyRepositoryCreate" if read_only else "CoreRepositoryCreate",
107+
mutation="CoreReadOnlyRepositoryCreate"
108+
if read_only
109+
else "CoreRepositoryCreate",
100110
input_data=input_data,
101111
query={"ok": None},
102112
)
103113

104-
await client.execute_graphql(query=query.render(), branch_name=branch, tracker="mutation-repository-create")
114+
await client.execute_graphql(
115+
query=query.render(), branch_name=branch, tracker="mutation-repository-create"
116+
)

tests/unit/ctl/test_repository_app.py

Lines changed: 65 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111

1212
runner = CliRunner()
1313

14-
requires_python_310 = pytest.mark.skipif(sys.version_info < (3, 10), reason="Requires Python 3.10 or higher")
14+
requires_python_310 = pytest.mark.skipif(
15+
sys.version_info < (3, 10), reason="Requires Python 3.10 or higher"
16+
)
1517

1618

1719
@pytest.fixture
@@ -28,6 +30,54 @@ def mock_client() -> mock.Mock:
2830
class TestInfrahubctlRepository:
2931
"""Groups the 'infrahubctl repository' test cases."""
3032

33+
@requires_python_310
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+
ref: {
69+
value: ""
70+
}
71+
}
72+
){
73+
ok
74+
}
75+
}
76+
""",
77+
branch_name="main",
78+
tracker="mutation-repository-create",
79+
)
80+
3181
@requires_python_310
3282
def test_repo_no_username(self, mock_init_client, mock_client) -> None:
3383
"""Case allow no username to be passed in and set it as None rather than blank string that fails."""
@@ -72,7 +122,7 @@ def test_repo_no_username(self, mock_init_client, mock_client) -> None:
72122
description: {
73123
value: ""
74124
}
75-
commit: {
125+
ref: {
76126
value: ""
77127
}
78128
credential: {
@@ -134,7 +184,7 @@ def test_repo_username(self, mock_init_client, mock_client) -> None:
134184
description: {
135185
value: ""
136186
}
137-
commit: {
187+
ref: {
138188
value: ""
139189
}
140190
credential: {
@@ -164,7 +214,7 @@ def test_repo_readonly_true(self, mock_init_client, mock_client) -> None:
164214
"repository",
165215
"add",
166216
"Gitlab",
167-
"https://gitlab.com/FragmentedPacket/nautobot-plugin-ansible-filters.git",
217+
"https://gitlab.com/opsmill/example-repo.git",
168218
"--password",
169219
"mySup3rSecureP@ssw0rd",
170220
"--read-only",
@@ -190,12 +240,12 @@ def test_repo_readonly_true(self, mock_init_client, mock_client) -> None:
190240
value: "Gitlab"
191241
}
192242
location: {
193-
value: "https://gitlab.com/FragmentedPacket/nautobot-plugin-ansible-filters.git"
243+
value: "https://gitlab.com/opsmill/example-repo.git"
194244
}
195245
description: {
196246
value: ""
197247
}
198-
commit: {
248+
ref: {
199249
value: ""
200250
}
201251
credential: {
@@ -212,7 +262,9 @@ def test_repo_readonly_true(self, mock_init_client, mock_client) -> None:
212262
)
213263

214264
@requires_python_310
215-
def test_repo_description_commit_branch(self, mock_init_client, mock_client) -> None:
265+
def test_repo_description_commit_branch(
266+
self, mock_init_client, mock_client
267+
) -> None:
216268
"""Case allow no username to be passed in and set it as None rather than blank string that fails."""
217269
mock_cred = mock.AsyncMock()
218270
mock_cred.id = "1234"
@@ -225,15 +277,15 @@ def test_repo_description_commit_branch(self, mock_init_client, mock_client) ->
225277
"repository",
226278
"add",
227279
"Gitlab",
228-
"https://gitlab.com/FragmentedPacket/nautobot-plugin-ansible-filters.git",
280+
"https://gitlab.com/opsmill/example-repo.git",
229281
"--password",
230282
"mySup3rSecureP@ssw0rd",
231283
"--username",
232284
"opsmill",
233285
"--description",
234286
"This is a test description",
235-
"--commit",
236-
"myHashCommit",
287+
"--ref",
288+
"my-custom-branch",
237289
"--branch",
238290
"develop",
239291
],
@@ -258,13 +310,13 @@ def test_repo_description_commit_branch(self, mock_init_client, mock_client) ->
258310
value: "Gitlab"
259311
}
260312
location: {
261-
value: "https://gitlab.com/FragmentedPacket/nautobot-plugin-ansible-filters.git"
313+
value: "https://gitlab.com/opsmill/example-repo.git"
262314
}
263315
description: {
264316
value: "This is a test description"
265317
}
266-
commit: {
267-
value: "myHashCommit"
318+
ref: {
319+
value: "my-custom-branch"
268320
}
269321
credential: {
270322
id: "1234"

0 commit comments

Comments
 (0)