Skip to content

Commit 9d1e6e8

Browse files
feat: emit a warning when using a list() method returns max
A common cause of issues filed and questions raised is that a user will call a `list()` method and only get 20 items. As this is the default maximum of items that will be returned from a `list()` method. To help with this we now emit a warning when the result from a `list()` method is greater-than or equal to 20 (or the specified `per_page` value) and the user is not using either `all=True`, `as_list=False`, or `page=X`.
1 parent 64d01ef commit 9d1e6e8

31 files changed

+145
-6
lines changed

gitlab/client.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@
1818

1919
import os
2020
import time
21+
import warnings
2122
from typing import Any, cast, Dict, List, Optional, Tuple, TYPE_CHECKING, Union
2223

2324
import requests
2425
import requests.utils
2526
from requests_toolbelt.multipart.encoder import MultipartEncoder # type: ignore
2627

28+
import gitlab
2729
import gitlab.config
2830
import gitlab.const
2931
import gitlab.exceptions
@@ -35,6 +37,12 @@
3537
"{source!r} to {target!r}"
3638
)
3739

40+
# https://docs.gitlab.com/ee/api/#offset-based-pagination
41+
_PAGINATION_URL = (
42+
f"https://python-gitlab.readthedocs.io/en/v{gitlab.__version__}/"
43+
f"api-usage.html#pagination"
44+
)
45+
3846

3947
class Gitlab:
4048
"""Represents a GitLab server connection.
@@ -818,7 +826,26 @@ def http_list(
818826

819827
if page or as_list is True:
820828
# pagination requested, we return a list
821-
return list(GitlabList(self, url, query_data, get_next=False, **kwargs))
829+
gl_list = GitlabList(self, url, query_data, get_next=False, **kwargs)
830+
items = list(gl_list)
831+
if (
832+
page is None
833+
and len(items) >= gl_list.per_page
834+
and (gl_list.total is None or len(items) < gl_list.total)
835+
):
836+
total_items = "10,000+" if gl_list.total is None else gl_list.total
837+
# Warn the user that they are only going to retrieve `per_page` maximum
838+
# items. This is a common cause of issues filed.
839+
warnings.warn(
840+
(
841+
f"Calling a `list()` method without specifying `all=True` or "
842+
f"`as_list=False` will return a maximum of {gl_list.per_page} "
843+
f"items. Your query returned {len(items)} of {total_items} "
844+
f"items. See {_PAGINATION_URL} for more details"
845+
),
846+
UserWarning,
847+
)
848+
return items
822849

823850
# No pagination, generator requested
824851
return GitlabList(self, url, query_data, **kwargs)

tests/functional/api/test_gitlab.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,13 @@ def test_template_dockerfile(gl):
8181

8282

8383
def test_template_gitignore(gl):
84-
assert gl.gitignores.list()
84+
assert gl.gitignores.list(all=True)
8585
gitignore = gl.gitignores.get("Node")
8686
assert gitignore.content is not None
8787

8888

8989
def test_template_gitlabciyml(gl):
90-
assert gl.gitlabciymls.list()
90+
assert gl.gitlabciymls.list(all=True)
9191
gitlabciyml = gl.gitlabciymls.get("Nodejs")
9292
assert gitlabciyml.content is not None
9393

tests/unit/objects/test_audit_events.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ def resp_list_audit_events():
5252
url=audit_events_url,
5353
json=[audit_events_content],
5454
content_type="application/json",
55+
headers={"X-Total": "1", "x-per-page": "20"},
5556
status=200,
5657
)
5758
yield rsps

tests/unit/objects/test_badges.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ def resp_list_badges():
6363
json=[badge_content],
6464
content_type="application/json",
6565
status=200,
66+
headers={"X-Total": "1", "x-per-page": "20"},
6667
)
6768
yield rsps
6869

tests/unit/objects/test_bridges.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,15 @@ def resp_list_bridges():
8484
url="http://localhost/api/v4/projects/1/pipelines/6/bridges",
8585
json=[export_bridges_content],
8686
content_type="application/json",
87+
headers={"X-Total": "1", "x-per-page": "20"},
8788
status=200,
8889
)
8990
rsps.add(
9091
method=responses.GET,
9192
url="http://localhost/api/v4/projects/1/pipelines",
9293
json=export_pipelines_content,
9394
content_type="application/json",
95+
headers={"X-Total": "1", "x-per-page": "20"},
9496
status=200,
9597
)
9698
yield rsps

tests/unit/objects/test_group_access_tokens.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def resp_list_group_access_token():
2727
url="http://localhost/api/v4/groups/1/access_tokens",
2828
json=content,
2929
content_type="application/json",
30+
headers={"X-Total": "1", "x-per-page": "20"},
3031
status=200,
3132
)
3233
yield rsps
@@ -84,6 +85,7 @@ def resp_revoke_group_access_token():
8485
url="http://localhost/api/v4/groups/1/access_tokens",
8586
json=content,
8687
content_type="application/json",
88+
headers={"X-Total": "1", "x-per-page": "20"},
8789
status=200,
8890
)
8991
yield rsps

tests/unit/objects/test_groups.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ def resp_list_subgroups_descendant_groups():
7777
),
7878
json=subgroup_descgroup_content,
7979
content_type="application/json",
80+
headers={"X-Total": "1", "x-per-page": "20"},
8081
status=200,
8182
)
8283
yield rsps

tests/unit/objects/test_hooks.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def resp_hooks_list():
3737
url=re.compile(r"http://localhost/api/v4/((groups|projects)/1/|)hooks"),
3838
json=hooks_content,
3939
content_type="application/json",
40+
headers={"X-Total": "1", "x-per-page": "20"},
4041
status=200,
4142
)
4243
yield rsps

tests/unit/objects/test_issues.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def resp_list_issues():
2323
url="http://localhost/api/v4/issues",
2424
json=content,
2525
content_type="application/json",
26+
headers={"X-Total": "2", "x-per-page": "20"},
2627
status=200,
2728
)
2829
yield rsps

tests/unit/objects/test_members.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ def resp_list_billable_group_members():
2929
url="http://localhost/api/v4/groups/1/billable_members",
3030
json=billable_members_content,
3131
content_type="application/json",
32+
headers={"X-Total": "1", "x-per-page": "20"},
3233
status=200,
3334
)
3435
yield rsps

0 commit comments

Comments
 (0)