Skip to content

add per_page config option #505

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 1 commit into from
May 26, 2018
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
7 changes: 5 additions & 2 deletions gitlab/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class Gitlab(object):
def __init__(self, url, private_token=None, oauth_token=None, email=None,
password=None, ssl_verify=True, http_username=None,
http_password=None, timeout=None, api_version='4',
session=None):
session=None, per_page=None):

self._api_version = str(api_version)
self._server_version = self._server_revision = None
Expand All @@ -97,6 +97,8 @@ def __init__(self, url, private_token=None, oauth_token=None, email=None,
#: Create a session object for requests
self.session = session or requests.Session()

self.per_page = per_page

objects = importlib.import_module('gitlab.v%s.objects' %
self._api_version)
self._objects = objects
Expand Down Expand Up @@ -177,7 +179,8 @@ def from_config(gitlab_id=None, config_files=None):
ssl_verify=config.ssl_verify, timeout=config.timeout,
http_username=config.http_username,
http_password=config.http_password,
api_version=config.api_version)
api_version=config.api_version,
per_page=config.per_page)

def auth(self):
"""Performs an authentication.
Expand Down
10 changes: 10 additions & 0 deletions gitlab/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,13 @@ def __init__(self, gitlab_id=None, config_files=None):
if self.api_version not in ('4',):
raise GitlabDataError("Unsupported API version: %s" %
self.api_version)

self.per_page = None
for section in ['global', self.gitlab_id]:
try:
self.per_page = self._config.getint(section, 'per_page')
except Exception:
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)
2 changes: 2 additions & 0 deletions gitlab/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ def list(self, **kwargs):

# Duplicate data to avoid messing with what the user sent us
data = kwargs.copy()
if self.gitlab.per_page:
data.setdefault('per_page', self.gitlab.per_page)

# We get the attributes that need some special transformation
types = getattr(self, '_types', {})
Expand Down
16 changes: 15 additions & 1 deletion gitlab/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
url = https://three.url
private_token = MNOPQR
ssl_verify = /path/to/CA/bundle.crt
per_page = 50

[four]
url = https://four.url
Expand All @@ -66,6 +67,11 @@

[three]
meh = hem

[four]
url = http://four.url
private_token = ABCDEF
per_page = 200
"""


Expand All @@ -87,13 +93,19 @@ def test_invalid_id(self, m_open):
@mock.patch('six.moves.builtins.open')
def test_invalid_data(self, m_open):
fd = six.StringIO(missing_attr_config)
fd.close = mock.Mock(return_value=None)
fd.close = mock.Mock(return_value=None,
side_effect=lambda: fd.seek(0))
m_open.return_value = fd
config.GitlabConfigParser('one')
config.GitlabConfigParser('one')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this duplicated line is useful :) Could you remove it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this to test the change line#97.

Without the fd.seek(0), all calls to config.GitlabConfigParser fail except the 1st one:

# fd.tell() = 0
config.GitlabConfigParser('one')  # reads fd
# fd.tell() = end of file
config.GitlabConfigParser('one')  # reads nothing because fd is at end of file, so it fail

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, thanks for the explanation.

self.assertRaises(config.GitlabDataError, config.GitlabConfigParser,
gitlab_id='two')
self.assertRaises(config.GitlabDataError, config.GitlabConfigParser,
gitlab_id='three')
with self.assertRaises(config.GitlabDataError) as emgr:
config.GitlabConfigParser('four')
self.assertEqual('Unsupported per_page number: 200',
emgr.exception.args[0])

@mock.patch('six.moves.builtins.open')
def test_valid_data(self, m_open):
Expand All @@ -108,6 +120,7 @@ def test_valid_data(self, m_open):
self.assertEqual(None, cp.oauth_token)
self.assertEqual(2, cp.timeout)
self.assertEqual(True, cp.ssl_verify)
self.assertIsNone(cp.per_page)

fd = six.StringIO(valid_config)
fd.close = mock.Mock(return_value=None)
Expand All @@ -130,6 +143,7 @@ def test_valid_data(self, m_open):
self.assertEqual(None, cp.oauth_token)
self.assertEqual(2, cp.timeout)
self.assertEqual("/path/to/CA/bundle.crt", cp.ssl_verify)
self.assertEqual(50, cp.per_page)

fd = six.StringIO(valid_config)
fd.close = mock.Mock(return_value=None)
Expand Down