Skip to content

Commit

Permalink
Implement project variables support
Browse files Browse the repository at this point in the history
  • Loading branch information
Gauvain Pocentek committed Jan 24, 2016
1 parent c11bebd commit e5438c6
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 5 deletions.
3 changes: 2 additions & 1 deletion gitlab/__init__.py
Expand Up @@ -551,7 +551,8 @@ def update(self, obj, **kwargs):
if missing:
raise GitlabUpdateError('Missing attribute(s): %s' %
", ".join(missing))
url = self._construct_url(id_=obj.id, obj=obj, parameters=params)
obj_id = params[obj.idAttr] if obj._id_in_update_url else None
url = self._construct_url(id_=obj_id, obj=obj, parameters=params)
headers = self._create_headers(content_type="application/json")

# build data that can really be sent to server
Expand Down
4 changes: 2 additions & 2 deletions gitlab/cli.py
Expand Up @@ -89,7 +89,7 @@ def _populate_sub_parser_by_class(cls, sub_parser):
required=True)
[sub_parser_action.add_argument("--%s" % x.replace('_', '-'),
required=True)
for x in cls.requiredGetAttrs]
for x in cls.requiredGetAttrs if x != cls.idAttr]

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

attrs = (cls.optionalUpdateAttrs
if cls.optionalUpdateAttrs is not None
Expand Down
14 changes: 14 additions & 0 deletions gitlab/objects.py
Expand Up @@ -178,6 +178,7 @@ class GitlabObject(object):
# plural
_urlPlural = None
_id_in_delete_url = True
_id_in_update_url = True
_returnClass = None
_constructorTypes = None

Expand Down Expand Up @@ -936,6 +937,7 @@ class ProjectMilestoneManager(BaseManager):
class ProjectLabel(GitlabObject):
_url = '/projects/%(project_id)s/labels'
_id_in_delete_url = False
_id_in_update_url = False
canGet = 'from_list'
requiredUrlAttrs = ['project_id']
idAttr = 'name'
Expand Down Expand Up @@ -1031,6 +1033,17 @@ class ProjectTriggerManager(BaseManager):
obj_cls = ProjectTrigger


class ProjectVariable(GitlabObject):
_url = '/projects/%(project_id)s/variables'
idAttr = 'key'
requiredUrlAttrs = ['project_id']
requiredCreateAttrs = ['key', 'value']


class ProjectVariableManager(BaseManager):
obj_cls = ProjectVariable


class Project(GitlabObject):
_url = '/projects'
_constructorTypes = {'owner': 'User', 'namespace': 'Group'}
Expand Down Expand Up @@ -1059,6 +1072,7 @@ class Project(GitlabObject):
('snippets', ProjectSnippetManager, [('project_id', 'id')]),
('tags', ProjectTagManager, [('project_id', 'id')]),
('triggers', ProjectTriggerManager, [('project_id', 'id')]),
('variables', ProjectVariableManager, [('project_id', 'id')]),
]

def Branch(self, id=None, **kwargs):
Expand Down
2 changes: 1 addition & 1 deletion tools/functional_tests.sh
Expand Up @@ -41,7 +41,7 @@ pip install -rrequirements.txt
pip install -e .

# NOTE(gpocentek): the first call might fail without a little delay
sleep 5
sleep 20

set -e

Expand Down
2 changes: 1 addition & 1 deletion tools/py_functional_tests.sh
Expand Up @@ -34,6 +34,6 @@ $VENV_CMD $VENV
pip install -rrequirements.txt
pip install -e .

sleep 10
sleep 20

python $(dirname $0)/python_test.py
9 changes: 9 additions & 0 deletions tools/python_test.py
Expand Up @@ -158,3 +158,12 @@
assert(len(admin_project.triggers.list()) == 1)
tr1 = admin_project.triggers.get(tr1.token)
tr1.delete()

# variables
v1 = admin_project.variables.create({'key': 'key1', 'value': 'value1'})
assert(len(admin_project.variables.list()) == 1)
v1.value = 'new_value1'
v1.save()
v1 = admin_project.variables.get(v1.key)
assert(v1.value == 'new_value1')
v1.delete()

0 comments on commit e5438c6

Please sign in to comment.