Skip to content

Commit 572029c

Browse files
authored
Merge pull request #681 from python-gitlab/no-param-conflicts
fix(api): avoid parameter conflicts with python and gitlab
2 parents 89679ce + 4bd027a commit 572029c

File tree

6 files changed

+64
-3
lines changed

6 files changed

+64
-3
lines changed

RELEASE_NOTES.rst

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,25 @@ Release notes
44

55
This page describes important changes between python-gitlab releases.
66

7+
Changes from 1.7 to 1.8
8+
=======================
9+
10+
* You can now use the ``query_parameters`` argument in method calls to define
11+
arguments to send to the GitLab server. This allows to avoid conflicts
12+
between python-gitlab and GitLab server variables, and allows to use the
13+
python reserved keywords as GitLab arguments.
14+
15+
The following examples make the same GitLab request with the 2 syntaxes::
16+
17+
projects = gl.projects.list(owned=True, starred=True)
18+
projects = gl.projects.list(query_parameters={'owned': True, 'starred': True})
19+
20+
The following example only works with the new parameter::
21+
22+
activities = gl.user_activities.list(
23+
query_parameters={'from': '2019-01-01'},
24+
all=True)
25+
726
Changes from 1.5 to 1.6
827
=======================
928

docs/api-usage.rst

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,25 @@ Some objects also provide managers to access related GitLab resources:
118118
project = gl.projects.get(1)
119119
issues = project.issues.list()
120120
121+
python-gitlab allows to send any data to the GitLab server when making queries.
122+
In case of invalid or missing arguments python-gitlab will raise an exception
123+
with the GitLab server error message:
124+
125+
.. code-block:: python
126+
127+
>>> gl.projects.list(sort='invalid value')
128+
...
129+
GitlabListError: 400: sort does not have a valid value
130+
131+
You can use the ``query_parameters`` argument to send arguments that would
132+
conflict with python or python-gitlab when using them as kwargs:
133+
134+
.. code-block:: python
135+
136+
gl.user_activities.list(from='2019-01-01') ## invalid
137+
138+
gl.user_activities.list(query_parameters={'from': '2019-01-01'}) # OK
139+
121140
Gitlab Objects
122141
==============
123142

docs/gl_objects/commits.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ results::
2727
commits = project.commits.list(ref_name='my_branch')
2828
commits = project.commits.list(since='2016-01-01T00:00:00Z')
2929

30+
.. note::
31+
32+
The available ``all`` listing argument conflicts with the python-gitlab
33+
argument. Use ``query_parameters`` to avoid the conflict::
34+
35+
commits = project.commits.list(all=True,
36+
query_parameters={'ref_name': 'my_branch'})
37+
3038
Create a commit::
3139

3240
# See https://docs.gitlab.com/ce/api/commits.html#create-a-commit-with-multiple-files-and-actions

docs/gl_objects/users.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,4 +312,6 @@ Examples
312312

313313
Get the users activities::
314314

315-
activities = gl.user_activities.list(all=True, as_list=False)
315+
activities = gl.user_activities.list(
316+
query_parameters={'from': '2018-07-01'},
317+
all=True, as_list=False)

gitlab/__init__.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,20 @@ def http_request(self, verb, path, query_data={}, post_data=None,
445445

446446
params = {}
447447
utils.copy_dict(params, query_data)
448-
utils.copy_dict(params, kwargs)
448+
449+
# Deal with kwargs: by default a user uses kwargs to send data to the
450+
# gitlab server, but this generates problems (python keyword conflicts
451+
# and python-gitlab/gitlab conflicts).
452+
# So we provide a `query_parameters` key: if it's there we use its dict
453+
# value as arguments for the gitlab server, and ignore the other
454+
# arguments, except pagination ones (per_page and page)
455+
if 'query_parameters' in kwargs:
456+
utils.copy_dict(params, kwargs['query_parameters'])
457+
for arg in ('per_page', 'page'):
458+
if arg in kwargs:
459+
params[arg] = kwargs[arg]
460+
else:
461+
utils.copy_dict(params, kwargs)
449462

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

tools/python_test_v4.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,7 @@
773773
assert(len(snippets) == 0)
774774

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

778778
# events
779779
gl.events.list()

0 commit comments

Comments
 (0)