Skip to content

Commit e5438c6

Browse files
author
Gauvain Pocentek
committed
Implement project variables support
1 parent c11bebd commit e5438c6

File tree

6 files changed

+29
-5
lines changed

6 files changed

+29
-5
lines changed

gitlab/__init__.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,8 @@ def update(self, obj, **kwargs):
551551
if missing:
552552
raise GitlabUpdateError('Missing attribute(s): %s' %
553553
", ".join(missing))
554-
url = self._construct_url(id_=obj.id, obj=obj, parameters=params)
554+
obj_id = params[obj.idAttr] if obj._id_in_update_url else None
555+
url = self._construct_url(id_=obj_id, obj=obj, parameters=params)
555556
headers = self._create_headers(content_type="application/json")
556557

557558
# build data that can really be sent to server

gitlab/cli.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def _populate_sub_parser_by_class(cls, sub_parser):
8989
required=True)
9090
[sub_parser_action.add_argument("--%s" % x.replace('_', '-'),
9191
required=True)
92-
for x in cls.requiredGetAttrs]
92+
for x in cls.requiredGetAttrs if x != cls.idAttr]
9393

9494
elif action_name == CREATE:
9595
[sub_parser_action.add_argument("--%s" % x.replace('_', '-'),
@@ -109,7 +109,7 @@ def _populate_sub_parser_by_class(cls, sub_parser):
109109
else cls.requiredCreateAttrs)
110110
[sub_parser_action.add_argument("--%s" % x.replace('_', '-'),
111111
required=True)
112-
for x in attrs]
112+
for x in attrs if x != cls.idAttr]
113113

114114
attrs = (cls.optionalUpdateAttrs
115115
if cls.optionalUpdateAttrs is not None

gitlab/objects.py

+14
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ class GitlabObject(object):
178178
# plural
179179
_urlPlural = None
180180
_id_in_delete_url = True
181+
_id_in_update_url = True
181182
_returnClass = None
182183
_constructorTypes = None
183184

@@ -936,6 +937,7 @@ class ProjectMilestoneManager(BaseManager):
936937
class ProjectLabel(GitlabObject):
937938
_url = '/projects/%(project_id)s/labels'
938939
_id_in_delete_url = False
940+
_id_in_update_url = False
939941
canGet = 'from_list'
940942
requiredUrlAttrs = ['project_id']
941943
idAttr = 'name'
@@ -1031,6 +1033,17 @@ class ProjectTriggerManager(BaseManager):
10311033
obj_cls = ProjectTrigger
10321034

10331035

1036+
class ProjectVariable(GitlabObject):
1037+
_url = '/projects/%(project_id)s/variables'
1038+
idAttr = 'key'
1039+
requiredUrlAttrs = ['project_id']
1040+
requiredCreateAttrs = ['key', 'value']
1041+
1042+
1043+
class ProjectVariableManager(BaseManager):
1044+
obj_cls = ProjectVariable
1045+
1046+
10341047
class Project(GitlabObject):
10351048
_url = '/projects'
10361049
_constructorTypes = {'owner': 'User', 'namespace': 'Group'}
@@ -1059,6 +1072,7 @@ class Project(GitlabObject):
10591072
('snippets', ProjectSnippetManager, [('project_id', 'id')]),
10601073
('tags', ProjectTagManager, [('project_id', 'id')]),
10611074
('triggers', ProjectTriggerManager, [('project_id', 'id')]),
1075+
('variables', ProjectVariableManager, [('project_id', 'id')]),
10621076
]
10631077

10641078
def Branch(self, id=None, **kwargs):

tools/functional_tests.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pip install -rrequirements.txt
4141
pip install -e .
4242

4343
# NOTE(gpocentek): the first call might fail without a little delay
44-
sleep 5
44+
sleep 20
4545

4646
set -e
4747

tools/py_functional_tests.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ $VENV_CMD $VENV
3434
pip install -rrequirements.txt
3535
pip install -e .
3636

37-
sleep 10
37+
sleep 20
3838

3939
python $(dirname $0)/python_test.py

tools/python_test.py

+9
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,12 @@
158158
assert(len(admin_project.triggers.list()) == 1)
159159
tr1 = admin_project.triggers.get(tr1.token)
160160
tr1.delete()
161+
162+
# variables
163+
v1 = admin_project.variables.create({'key': 'key1', 'value': 'value1'})
164+
assert(len(admin_project.variables.list()) == 1)
165+
v1.value = 'new_value1'
166+
v1.save()
167+
v1 = admin_project.variables.get(v1.key)
168+
assert(v1.value == 'new_value1')
169+
v1.delete()

0 commit comments

Comments
 (0)