Skip to content

Commit d187925

Browse files
committed
feat: add global order_by option to ease pagination
1 parent 0b71ba4 commit d187925

File tree

4 files changed

+15
-2
lines changed

4 files changed

+15
-2
lines changed

docs/api-usage.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,8 @@ order options. At the time of writing, only ``order_by="id"`` works.
225225

226226
.. code-block:: python
227227
228-
gl = gitlab.Gitlab(url, token, pagination="keyset", per_page=100)
229-
gl.projects.list(order_by="id")
228+
gl = gitlab.Gitlab(url, token, pagination="keyset", order_by="id", per_page=100)
229+
gl.projects.list()
230230
231231
Reference:
232232
https://docs.gitlab.com/ce/api/README.html#keyset-based-pagination

gitlab/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class Gitlab(object):
7070
http_password (str): Password for HTTP authentication
7171
api_version (str): Gitlab API version to use (support for 4 only)
7272
pagination (str): Can be set to 'keyset' to use keyset pagination
73+
order_by (str): Set order_by globally
7374
"""
7475

7576
def __init__(
@@ -86,6 +87,7 @@ def __init__(
8687
session=None,
8788
per_page=None,
8889
pagination=None,
90+
order_by=None,
8991
):
9092

9193
self._api_version = str(api_version)
@@ -112,6 +114,7 @@ def __init__(
112114

113115
self.per_page = per_page
114116
self.pagination = pagination
117+
self.order_by = order_by
115118

116119
objects = importlib.import_module("gitlab.v%s.objects" % self._api_version)
117120
self._objects = objects
@@ -204,6 +207,7 @@ def from_config(cls, gitlab_id=None, config_files=None):
204207
api_version=config.api_version,
205208
per_page=config.per_page,
206209
pagination=config.pagination,
210+
order_by=config.order_by,
207211
)
208212

209213
def auth(self):

gitlab/config.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,9 @@ def __init__(self, gitlab_id=None, config_files=None):
169169
self.pagination = self._config.get(self.gitlab_id, "pagination")
170170
except Exception:
171171
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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ def list(self, **kwargs):
124124
if self.gitlab.pagination:
125125
data.setdefault("pagination", self.gitlab.pagination)
126126

127+
if self.gitlab.order_by:
128+
data.setdefault("order_by", self.gitlab.order_by)
129+
127130
# We get the attributes that need some special transformation
128131
types = getattr(self, "_types", {})
129132
if types:

0 commit comments

Comments
 (0)