Skip to content

Commit 6500602

Browse files
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 239db3d commit 6500602

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-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

+47
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,49 @@ 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+
source_branch = "test_commit_merge_requests"
57+
mr = merge_request(source_branch=source_branch)
58+
merge_result = mr.merge(should_remove_source_branch=True)
59+
result = wait_for_sidekiq(timeout=60)
60+
assert result is True, "sidekiq process should have terminated but did not"
61+
# Wait until it is merged
62+
mr_iid = mr.iid
63+
for _ in range(60):
64+
mr = project.mergerequests.get(mr_iid)
65+
if mr.merged_at is not None:
66+
break
67+
time.sleep(0.5)
68+
assert mr.merged_at is not None
69+
time.sleep(0.5)
70+
result = wait_for_sidekiq(timeout=60)
71+
assert result is True, "sidekiq process should have terminated but did not"
72+
73+
commit_sha = merge_result["sha"]
74+
cmd = [
75+
"-o",
76+
"json",
77+
"project-commit",
78+
"merge-requests",
79+
"--project-id",
80+
project.id,
81+
"--id",
82+
commit_sha,
83+
]
84+
ret = gitlab_cli(cmd)
85+
assert ret.success
86+
87+
json_list = json.loads(ret.stdout)
88+
assert isinstance(json_list, list)
89+
assert len(json_list) == 1
90+
mr_dict = json_list[0]
91+
assert mr_dict["id"] == mr.id
92+
assert mr_dict["iid"] == mr.iid
93+
94+
4895
def test_revert_commit(gitlab_cli, project):
4996
commit = project.commits.list()[0]
5097

0 commit comments

Comments
 (0)