8000 fix(cli): generate UserWarning if `list` does not return all entries · python-gitlab/python-gitlab@05c0c70 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 05c0c70

Browse files
fix(cli): generate UserWarning if list does not return all entries
Previously in the CLI, calls to `list()` would have `get_all=False` by default. Therefore hiding the fact that not all items are being returned if there were more than 20 items. Added `--no-get-all` option to `list` actions. Along with the already existing `--get-all`. Closes: #2900
1 parent d4455e5 commit 05c0c70

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

gitlab/client.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,7 @@ def http_list(
869869
query_data: Optional[Dict[str, Any]] = None,
870870
*,
871871
iterator: Optional[bool] = None,
872+
cli: bool = False,
872873
**kwargs: Any,
873874
) -> Union["GitlabList", List[Dict[str, Any]]]:
874875
"""Make a GET request to the Gitlab server for list-oriented queries.
@@ -952,16 +953,26 @@ def should_emit_warning() -> bool:
952953
# Warn the user that they are only going to retrieve `per_page`
953954
# maximum items. This is a common cause of issues filed.
954955
total_items = "many" if gl_list.total is None else gl_list.total
955-
utils.warn(
956-
message=(
956+
if cli:
957+
message = (
958+
f"Your query returned {len(items)} of {total_items}. To return all "
959+
f"items use `--get-all`. To silence this warning use `--no-get-all`."
960+
)
961+
show_caller = False
962+
else:
963+
message = (
957964
f"Calling a `list()` method without specifying `get_all=True` or "
958965
f"`iterator=True` will return a maximum of {gl_list.per_page} items. "
959966
f"Your query returned {len(items)} of {total_items} items. See "
960967
f"{_PAGINATION_URL} for more details. If this was done intentionally, "
961968
f"then this warning can be supressed by adding the argument "
962969
f"`get_all=False` to the `list()` call."
963-
),
970+
)
971+
show_caller = True
972+
utils.warn(
973+
message=message,
964974
category=UserWarning,
975+
show_caller=show_caller,
965976
)
966977
return items
967978

gitlab/v4/cli.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import argparse
2+
import json
23
import operator
34
import sys
45
from typing import Any, Dict, List, Optional, Type, TYPE_CHECKING, Union
@@ -141,7 +142,7 @@ def do_list(
141142
if TYPE_CHECKING:
142143
assert isinstance(self.mgr, gitlab.mixins.ListMixin)
143144
try:
144-
result = self.mgr.list(**self.args)
145+
result = self.mgr.list(**self.args, cli=True)
145146
except Exception as e: # pragma: no cover, cli.die is unit-tested
146147
cli.die("Impossible to list objects", e)
147148
return result
@@ -238,12 +239,25 @@ def _populate_sub_parser_by_class(
238239

239240
sub_parser_action.add_argument("--page", required=False, type=int)
240241
sub_parser_action.add_argument("--per-page", required=False, type=int)
241-
sub_parser_action.add_argument(
242+
get_all_group = sub_parser_action.add_mutually_exclusive_group()
243+
get_all_group.add_argument(
242244
"--get-all",
243245
required=False,
244-
action="store_true",
246+
action="store_const",
247+
const=True,
248+
default=None,
249+
dest="get_all",
245250
help="Return all items from the server, without pagination.",
246251
)
252+
get_all_group.add_argument(
253+
"--no-get-all",
254+
required=False,
255+
action="store_const",
256+
const=False,
257+
default=None,
258+
dest="get_all",
259+
help="Don't return all items from the server.",
260+
)
247261

248262
if action_name == "delete":
249263
if cls._id_attr is not None:
@@ -416,8 +430,6 @@ def get_dict(
416430
class JSONPrinter:
417431
@staticmethod
418432
def display(d: Union[str, Dict[str, Any]], **_kwargs: Any) -> None:
419-
import json # noqa
420-
421433
print(json.dumps(d))
422434

423435
@staticmethod
@@ -426,8 +438,6 @@ def display_list(
426438
fields: List[str],
427439
**_kwargs: Any,
428440
) -> None:
429-
import json # noqa
430-
431441
print(json.dumps([get_dict(obj, fields) for obj in data]))
432442

433443

0 commit comments

Comments
 (0)
0