Skip to content

Commit df485a9

Browse files
Merge pull request #1001 from python-gitlab/feat/keyset-pagination
Feat/keyset pagination
2 parents 7fd3226 + d187925 commit df485a9

File tree

5 files changed

+51
-1
lines changed

5 files changed

+51
-1
lines changed

docs/api-usage.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,18 @@ You can define the ``per_page`` value globally to avoid passing it to every
219219
220220
gl = gitlab.Gitlab(url, token, per_page=50)
221221
222+
Gitlab allows to also use keyset pagination. You can supply it to your project listing,
223+
but you can also do so globally. Be aware that GitLab then also requires you to only use supported
224+
order options. At the time of writing, only ``order_by="id"`` works.
225+
226+
.. code-block:: python
227+
228+
gl = gitlab.Gitlab(url, token, pagination="keyset", order_by="id", per_page=100)
229+
gl.projects.list()
230+
231+
Reference:
232+
https://docs.gitlab.com/ce/api/README.html#keyset-based-pagination
233+
222234
``list()`` methods can also return a generator object which will handle the
223235
next calls to the API when required. This is the recommended way to iterate
224236
through a large number of items:

gitlab/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ class Gitlab(object):
6969
http_username (str): Username for HTTP authentication
7070
http_password (str): Password for HTTP authentication
7171
api_version (str): Gitlab API version to use (support for 4 only)
72+
pagination (str): Can be set to 'keyset' to use keyset pagination
73+
order_by (str): Set order_by globally
7274
"""
7375

7476
def __init__(
@@ -84,6 +86,8 @@ def __init__(
8486
api_version="4",
8587
session=None,
8688
per_page=None,
89+
pagination=None,
90+
order_by=None,
8791
):
8892

8993
self._api_version = str(api_version)
@@ -109,6 +113,8 @@ def __init__(
109113
self.session = session or requests.Session()
110114

111115
self.per_page = per_page
116+
self.pagination = pagination
117+
self.order_by = order_by
112118

113119
objects = importlib.import_module("gitlab.v%s.objects" % self._api_version)
114120
self._objects = objects
@@ -200,6 +206,8 @@ def from_config(cls, gitlab_id=None, config_files=None):
200206
http_password=config.http_password,
201207
api_version=config.api_version,
202208
per_page=config.per_page,
209+
pagination=config.pagination,
210+
order_by=config.order_by,
203211
)
204212

205213
def auth(self):

gitlab/config.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,15 @@ def __init__(self, gitlab_id=None, config_files=None):
163163
pass
164164
if self.per_page is not None and not 0 <= self.per_page <= 100:
165165
raise GitlabDataError("Unsupported per_page number: %s" % self.per_page)
166+
167+
self.pagination = None
168+
try:
169+
self.pagination = self._config.get(self.gitlab_id, "pagination")
170+
except Exception:
171+
pass
172+
173+
self.order_by = None
174+
try:
175+
self.order_by = self._config.get(self.gitlab_id, "order_by")
176+
except Exception:
177+
pass

gitlab/mixins.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,13 @@ def list(self, **kwargs):
120120
if self.gitlab.per_page:
121121
data.setdefault("per_page", self.gitlab.per_page)
122122

123+
# global keyset pagination
124+
if self.gitlab.pagination:
125+
data.setdefault("pagination", self.gitlab.pagination)
126+
127+
if self.gitlab.order_by:
128+
data.setdefault("order_by", self.gitlab.order_by)
129+
123130
# We get the attributes that need some special transformation
124131
types = getattr(self, "_types", {})
125132
if types:

gitlab/v4/objects.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,13 @@ class UserProjectManager(ListMixin, CreateMixin, RESTManager):
268268
"statistics",
269269
"with_issues_enabled",
270270
"with_merge_requests_enabled",
271+
"with_custom_attributes",
272+
"with_programming_language",
273+
"wiki_checksum_failed",
274+
"repository_checksum_failed",
275+
"min_access_level",
276+
"id_after",
277+
"id_before",
271278
)
272279

273280
def list(self, **kwargs):
@@ -1192,12 +1199,16 @@ class GroupProjectManager(ListMixin, RESTManager):
11921199
"order_by",
11931200
"sort",
11941201
"search",
1195-
"ci_enabled_first",
11961202
"simple",
11971203
"owned",
11981204
"starred",
11991205
"with_custom_attributes",
12001206
"include_subgroups",
1207+
"with_issues_enabled",
1208+
"with_merge_requests_enabled",
1209+
"with_shared",
1210+
"min_access_level",
1211+
"with_security_reports",
12011212
)
12021213

12031214

0 commit comments

Comments
 (0)