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

Skip to content

Commit e5a4379

Browse files
JohnVillalovosnejch
authored andcommitted
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 7d04315 commit e5a4379

File tree

3 files changed

+49
-10
lines changed

3 files changed

+49
-10
lines changed

gitlab/client.py

Lines changed: 17 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+
message_details: Optional[utils.WarnMessageData] = None,
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,29 @@ 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 message_details is not None:
957+
message = message_details.message.format_map(
958+
{
959+
"len_items": len(items),
960+
"per_page": gl_list.per_page,
961+
"total_items": total_items,
962+
}
963+
)
964+
show_caller = message_details.show_caller
965+
else:
966+
message = (
957967
f"Calling a `list()` method without specifying `get_all=True` or "
958968
f"`iterator=True` will return a maximum of {gl_list.per_page} items. "
959969
f"Your query returned {len(items)} of {total_items} items. See "
960970
f"{_PAGINATION_URL} for more details. If this was done intentionally, "
961971
f"then this warning can be supressed by adding the argument "
962972
f"`get_all=False` to the `list()` call."
963-
),
973+
)
974+
show_caller = True
975+
utils.warn(
976+
message=message,
964977
category=UserWarning,
978+
show_caller=show_caller,
965979
)
966980
return items
967981

gitlab/utils.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import dataclasses
12
import email.message
23
import logging
34
import pathlib
@@ -205,3 +206,9 @@ def warn(
205206
stacklevel=stacklevel,
206207
source=source,
207208
)
209+
210+
211+
@dataclasses.dataclass
212+
class WarnMessageData:
213+
message: str
214+
show_caller: bool

gitlab/v4/cli.py

Lines changed: 25 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
@@ -140,8 +141,16 @@ def do_list(
140141
) -> Union[gitlab.base.RESTObjectList, List[gitlab.base.RESTObject]]:
141142
if TYPE_CHECKING:
142143
assert isinstance(self.mgr, gitlab.mixins.ListMixin)
144+
message_details = gitlab.utils.WarnMessageData(
145+
message=(
146+
"Your query returned {len_items} of {total_items} items. To return all "
147+
"items use `--get-all`. To silence this warning use `--no-get-all`."
148+
),
149+
show_caller=False,
150+
)
151+
143152
try:
144-
result = self.mgr.list(**self.args)
153+
result = self.mgr.list(**self.args, message_details=message_details)
145154
except Exception as e: # pragma: no cover, cli.die is unit-tested
146155
cli.die("Impossible to list objects", e)
147156
return result
@@ -238,12 +247,25 @@ def _populate_sub_parser_by_class(
238247

239248
sub_parser_action.add_argument("--page", required=False, type=int)
240249
sub_parser_action.add_argument("--per-page", required=False, type=int)
241-
sub_parser_action.add_argument(
250+
get_all_group = sub_parser_action.add_mutually_exclusive_group()
251+
get_all_group.add_argument(
242252
"--get-all",
243253
required=False,
244-
action="store_true",
254+
action="store_const",
255+
const=True,
256+
default=None,
257+
dest="get_all",
245258
help="Return all items from the server, without pagination.",
246259
)
260+
get_all_group.add_argument(
261+
"--no-get-all",
262+
required=False,
263+
action="store_const",
264+
const=False,
265+
default=None,
266+
dest="get_all",
267+
help="Don't return all items from the server.",
268+
)
247269

248270
if action_name == "delete":
249271
if cls._id_attr is not None:
@@ -416,8 +438,6 @@ def get_dict(
416438
class JSONPrinter:
417439
@staticmethod
418440
def display(d: Union[str, Dict[str, Any]], **_kwargs: Any) -> None:
419-
import json # noqa
420-
421441
print(json.dumps(d))
422442

423443
@staticmethod
@@ -426,8 +446,6 @@ def display_list(
426446
fields: List[str],
427447
**_kwargs: Any,
428448
) -> None:
429-
import json # noqa
430-
431449
print(json.dumps([get_dict(obj, fields) for obj in data]))
432450

433451

0 commit comments

Comments
 (0)
0