Skip to content

Feat/keyset pagination #1001

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/api-usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,18 @@ You can define the ``per_page`` value globally to avoid passing it to every

gl = gitlab.Gitlab(url, token, per_page=50)

Gitlab allows to also use keyset pagination. You can supply it to your project listing,
but you can also do so globally. Be aware that GitLab then also requires you to only use supported
order options. At the time of writing, only ``order_by="id"`` works.

.. code-block:: python

gl = gitlab.Gitlab(url, token, pagination="keyset", order_by="id", per_page=100)
gl.projects.list()

Reference:
https://docs.gitlab.com/ce/api/README.html#keyset-based-pagination

``list()`` methods can also return a generator object which will handle the
next calls to the API when required. This is the recommended way to iterate
through a large number of items:
Expand Down
8 changes: 8 additions & 0 deletions gitlab/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ class Gitlab(object):
http_username (str): Username for HTTP authentication
http_password (str): Password for HTTP authentication
api_version (str): Gitlab API version to use (support for 4 only)
pagination (str): Can be set to 'keyset' to use keyset pagination
order_by (str): Set order_by globally
"""

def __init__(
Expand All @@ -84,6 +86,8 @@ def __init__(
api_version="4",
session=None,
per_page=None,
pagination=None,
order_by=None,
):

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

self.per_page = per_page
self.pagination = pagination
self.order_by = order_by

objects = importlib.import_module("gitlab.v%s.objects" % self._api_version)
self._objects = objects
Expand Down Expand Up @@ -200,6 +206,8 @@ def from_config(cls, gitlab_id=None, config_files=None):
http_password=config.http_password,
api_version=config.api_version,
per_page=config.per_page,
pagination=config.pagination,
order_by=config.order_by,
)

def auth(self):
Expand Down
12 changes: 12 additions & 0 deletions gitlab/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,15 @@ def __init__(self, gitlab_id=None, config_files=None):
pass
if self.per_page is not None and not 0 <= self.per_page <= 100:
raise GitlabDataError("Unsupported per_page number: %s" % self.per_page)

self.pagination = None
try:
self.pagination = self._config.get(self.gitlab_id, "pagination")
except Exception:
pass

self.order_by = None
try:
self.order_by = self._config.get(self.gitlab_id, "order_by")
except Exception:
pass
7 changes: 7 additions & 0 deletions gitlab/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@ def list(self, **kwargs):
if self.gitlab.per_page:
data.setdefault("per_page", self.gitlab.per_page)

# global keyset pagination
if self.gitlab.pagination:
data.setdefault("pagination", self.gitlab.pagination)

if self.gitlab.order_by:
data.setdefault("order_by", self.gitlab.order_by)

# We get the attributes that need some special transformation
types = getattr(self, "_types", {})
if types:
Expand Down
13 changes: 12 additions & 1 deletion gitlab/v4/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,13 @@ class UserProjectManager(ListMixin, CreateMixin, RESTManager):
"statistics",
"with_issues_enabled",
"with_merge_requests_enabled",
"with_custom_attributes",
"with_programming_language",
"wiki_checksum_failed",
"repository_checksum_failed",
"min_access_level",
"id_after",
"id_before",
)

def list(self, **kwargs):
Expand Down Expand Up @@ -1192,12 +1199,16 @@ class GroupProjectManager(ListMixin, RESTManager):
"order_by",
"sort",
"search",
"ci_enabled_first",
"simple",
"owned",
"starred",
"with_custom_attributes",
"include_subgroups",
"with_issues_enabled",
"with_merge_requests_enabled",
"with_shared",
"min_access_level",
"with_security_reports",
)


Expand Down