Skip to content

fix(api): avoid parameter conflicts with python and gitlab #681

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
Jan 19, 2019
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
19 changes: 19 additions & 0 deletions RELEASE_NOTES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,25 @@ Release notes

This page describes important changes between python-gitlab releases.

Changes from 1.7 to 1.8
=======================

* You can now use the ``query_parameters`` argument in method calls to define
arguments to send to the GitLab server. This allows to avoid conflicts
between python-gitlab and GitLab server variables, and allows to use the
python reserved keywords as GitLab arguments.

The following examples make the same GitLab request with the 2 syntaxes::

projects = gl.projects.list(owned=True, starred=True)
projects = gl.projects.list(query_parameters={'owned': True, 'starred': True})

The following example only works with the new parameter::

activities = gl.user_activities.list(
query_parameters={'from': '2019-01-01'},
all=True)

Changes from 1.5 to 1.6
=======================

Expand Down
19 changes: 19 additions & 0 deletions docs/api-usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,25 @@ Some objects also provide managers to access related GitLab resources:
project = gl.projects.get(1)
issues = project.issues.list()

python-gitlab allows to send any data to the GitLab server when making queries.
In case of invalid or missing arguments python-gitlab will raise an exception
with the GitLab server error message:

.. code-block:: python

>>> gl.projects.list(sort='invalid value')
...
GitlabListError: 400: sort does not have a valid value

You can use the ``query_parameters`` argument to send arguments that would
conflict with python or python-gitlab when using them as kwargs:

.. code-block:: python

gl.user_activities.list(from='2019-01-01') ## invalid

gl.user_activities.list(query_parameters={'from': '2019-01-01'}) # OK

Gitlab Objects
==============

Expand Down
8 changes: 8 additions & 0 deletions docs/gl_objects/commits.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ results::
commits = project.commits.list(ref_name='my_branch')
commits = project.commits.list(since='2016-01-01T00:00:00Z')

.. note::

The available ``all`` listing argument conflicts with the python-gitlab
argument. Use ``query_parameters`` to avoid the conflict::

commits = project.commits.list(all=True,
query_parameters={'ref_name': 'my_branch'})

Create a commit::

# See https://docs.gitlab.com/ce/api/commits.html#create-a-commit-with-multiple-files-and-actions
Expand Down
4 changes: 3 additions & 1 deletion docs/gl_objects/users.rst
Original file line number Diff line number Diff line change
Expand Up @@ -312,4 +312,6 @@ Examples

Get the users activities::

activities = gl.user_activities.list(all=True, as_list=False)
activities = gl.user_activities.list(
query_parameters={'from': '2018-07-01'},
all=True, as_list=False)
15 changes: 14 additions & 1 deletion gitlab/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,20 @@ def http_request(self, verb, path, query_data={}, post_data=None,

params = {}
utils.copy_dict(params, query_data)
utils.copy_dict(params, kwargs)

# Deal with kwargs: by default a user uses kwargs to send data to the
# gitlab server, but this generates problems (python keyword conflicts
# and python-gitlab/gitlab conflicts).
# So we provide a `query_parameters` key: if it's there we use its dict
# value as arguments for the gitlab server, and ignore the other
# arguments, except pagination ones (per_page and page)
if 'query_parameters' in kwargs:
utils.copy_dict(params, kwargs['query_parameters'])
for arg in ('per_page', 'page'):
if arg in kwargs:
params[arg] = kwargs[arg]
else:
utils.copy_dict(params, kwargs)

opts = self._get_session_opts(content_type='application/json')

Expand Down
2 changes: 1 addition & 1 deletion tools/python_test_v4.py
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@
assert(len(snippets) == 0)

# user activities
gl.user_activities.list()
gl.user_activities.list(query_parameters={'from': '2019-01-01'})

# events
gl.events.list()
Expand Down