From e5438c6440a2477c796427bc598b2b31b10dc762 Mon Sep 17 00:00:00 2001 From: Gauvain Pocentek Date: Sun, 24 Jan 2016 21:15:22 +0100 Subject: [PATCH] Implement project variables support --- gitlab/__init__.py | 3 ++- gitlab/cli.py | 4 ++-- gitlab/objects.py | 14 ++++++++++++++ tools/functional_tests.sh | 2 +- tools/py_functional_tests.sh | 2 +- tools/python_test.py | 9 +++++++++ 6 files changed, 29 insertions(+), 5 deletions(-) diff --git a/gitlab/__init__.py b/gitlab/__init__.py index d8ee5bff2..28ebfe349 100644 --- a/gitlab/__init__.py +++ b/gitlab/__init__.py @@ -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 diff --git a/gitlab/cli.py b/gitlab/cli.py index c2b2fa57f..8ac8e45f2 100644 --- a/gitlab/cli.py +++ b/gitlab/cli.py @@ -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('_', '-'), @@ -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 diff --git a/gitlab/objects.py b/gitlab/objects.py index ddcbae72b..0330807e2 100644 --- a/gitlab/objects.py +++ b/gitlab/objects.py @@ -178,6 +178,7 @@ class GitlabObject(object): # plural _urlPlural = None _id_in_delete_url = True + _id_in_update_url = True _returnClass = None _constructorTypes = None @@ -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' @@ -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'} @@ -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): diff --git a/tools/functional_tests.sh b/tools/functional_tests.sh index 18770e9f0..6cb868dcc 100755 --- a/tools/functional_tests.sh +++ b/tools/functional_tests.sh @@ -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 diff --git a/tools/py_functional_tests.sh b/tools/py_functional_tests.sh index a30230ba2..f37aaead3 100755 --- a/tools/py_functional_tests.sh +++ b/tools/py_functional_tests.sh @@ -34,6 +34,6 @@ $VENV_CMD $VENV pip install -rrequirements.txt pip install -e . -sleep 10 +sleep 20 python $(dirname $0)/python_test.py diff --git a/tools/python_test.py b/tools/python_test.py index 820dca1c7..ff4aa2a15 100644 --- a/tools/python_test.py +++ b/tools/python_test.py @@ -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()