Skip to content

Commit 9b88132

Browse files
JohnVillalovosnejch
authored andcommitted
fix(cli): handle list response for json/yaml output
Handle the case with the CLI where a list response is returned from GitLab and json/yaml output is requested. Add a functional CLI test to validate it works. Closes: #2287
1 parent c15bd33 commit 9b88132

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

gitlab/v4/cli.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -387,9 +387,9 @@ def extend_parser(parser: argparse.ArgumentParser) -> argparse.ArgumentParser:
387387

388388

389389
def get_dict(
390-
obj: Union[str, gitlab.base.RESTObject], fields: List[str]
390+
obj: Union[str, Dict[str, Any], gitlab.base.RESTObject], fields: List[str]
391391
) -> Union[str, Dict[str, Any]]:
392-
if isinstance(obj, str):
392+
if not isinstance(obj, gitlab.base.RESTObject):
393393
return obj
394394

395395
if fields:
@@ -406,7 +406,7 @@ def display(d: Union[str, Dict[str, Any]], **_kwargs: Any) -> None:
406406

407407
@staticmethod
408408
def display_list(
409-
data: List[Union[str, gitlab.base.RESTObject]],
409+
data: List[Union[str, Dict[str, Any], gitlab.base.RESTObject]],
410410
fields: List[str],
411411
**_kwargs: Any,
412412
) -> None:
@@ -431,7 +431,7 @@ def display(d: Union[str, Dict[str, Any]], **_kwargs: Any) -> None:
431431

432432
@staticmethod
433433
def display_list(
434-
data: List[Union[str, gitlab.base.RESTObject]],
434+
data: List[Union[str, Dict[str, Any], gitlab.base.RESTObject]],
435435
fields: List[str],
436436
**_kwargs: Any,
437437
) -> None:

tests/functional/cli/test_cli_repository.py

+44
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import json
2+
import time
3+
4+
15
def test_project_create_file(gitlab_cli, project):
26
file_path = "README"
37
branch = "main"
@@ -45,6 +49,46 @@ def test_list_all_commits(gitlab_cli, project):
4549
assert len(ret_all.stdout) > len(ret.stdout)
4650

4751

52+
def test_commit_merge_requests(gitlab_cli, project, merge_request, wait_for_sidekiq):
53+
"""This tests the `project-commit merge-requests` command and also tests
54+
that we can print the result using the `json` formatter"""
55+
# create and then merge a merge-request
56+
mr = merge_request(source_branch="test_commit_merge_requests")
57+
merge_result = mr.merge(should_remove_source_branch=True)
58+
wait_for_sidekiq(timeout=60)
59+
# Wait until it is merged
60+
mr_iid = mr.iid
61+
for _ in range(60):
62+
mr = project.mergerequests.get(mr_iid)
63+
if mr.merged_at is not None:
64+
break
65+
time.sleep(0.5)
66+
assert mr.merged_at is not None
67+
time.sleep(0.5)
68+
wait_for_sidekiq(timeout=60)
69+
70+
commit_sha = merge_result["sha"]
71+
cmd = [
72+
"-o",
73+
"json",
74+
"project-commit",
75+
"merge-requests",
76+
"--project-id",
77+
project.id,
78+
"--id",
79+
commit_sha,
80+
]
81+
ret = gitlab_cli(cmd)
82+
assert ret.success
83+
84+
json_list = json.loads(ret.stdout)
85+
assert isinstance(json_list, list)
86+
assert len(json_list) == 1
87+
mr_dict = json_list[0]
88+
assert mr_dict["id"] == mr.id
89+
assert mr_dict["iid"] == mr.iid
90+
91+
4892
def test_revert_commit(gitlab_cli, project):
4993
commit = project.commits.list()[0]
5094

0 commit comments

Comments
 (0)