diff options
| author | Gauvain Pocentek <gauvain@pocentek.net> | 2018-03-05 09:39:12 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-03-05 09:39:12 +0100 |
| commit | ee4591d44fa3c998694eded7f57aada2f6ea90c2 (patch) | |
| tree | 978ad66f1e18027d6e86ceeffa83e6a4ed08c474 /gitlab | |
| parent | 6bcc92a39a9a9dd97fa7387f754474c1cc5d78dc (diff) | |
| parent | 3424333bc98fcfc4733f2c5f1bf9a93b9a02135b (diff) | |
| download | gitlab-ee4591d44fa3c998694eded7f57aada2f6ea90c2.tar.gz | |
Merge pull request #426 from tardyp/readmixin
introduce RefreshMixin
Diffstat (limited to 'gitlab')
| -rw-r--r-- | gitlab/mixins.py | 19 | ||||
| -rw-r--r-- | gitlab/tests/test_mixins.py | 19 | ||||
| -rw-r--r-- | gitlab/v4/objects.py | 6 |
3 files changed, 41 insertions, 3 deletions
diff --git a/gitlab/mixins.py b/gitlab/mixins.py index cb35efc..ea21e10 100644 --- a/gitlab/mixins.py +++ b/gitlab/mixins.py @@ -68,6 +68,25 @@ class GetWithoutIdMixin(object): return self._obj_cls(self, server_data) +class RefreshMixin(object): + @exc.on_http_error(exc.GitlabGetError) + def refresh(self, **kwargs): + """Refresh a single object from server. + + Args: + **kwargs: Extra options to send to the Gitlab server (e.g. sudo) + + Returns None (updates the object) + + Raises: + GitlabAuthenticationError: If authentication is not correct + GitlabGetError: If the server cannot perform the request + """ + path = '%s/%s' % (self.manager.path, self.id) + server_data = self.manager.gitlab.http_get(path, **kwargs) + self._update_attrs(server_data) + + class ListMixin(object): @exc.on_http_error(exc.GitlabListError) def list(self, **kwargs): diff --git a/gitlab/tests/test_mixins.py b/gitlab/tests/test_mixins.py index c51322a..5c10597 100644 --- a/gitlab/tests/test_mixins.py +++ b/gitlab/tests/test_mixins.py @@ -153,6 +153,25 @@ class TestMixinMethods(unittest.TestCase): self.assertEqual(obj.foo, 'bar') self.assertEqual(obj.id, 42) + def test_refresh_mixin(self): + class O(RefreshMixin, FakeObject): + pass + + @urlmatch(scheme="http", netloc="localhost", path='/api/v4/tests/42', + method="get") + def resp_cont(url, request): + headers = {'Content-Type': 'application/json'} + content = '{"id": 42, "foo": "bar"}' + return response(200, content, headers, None, 5, request) + + with HTTMock(resp_cont): + mgr = FakeManager(self.gl) + obj = O(mgr, {'id': 42}) + res = obj.refresh() + self.assertIsNone(res) + self.assertEqual(obj.foo, 'bar') + self.assertEqual(obj.id, 42) + def test_get_without_id_mixin(self): class M(GetWithoutIdMixin, FakeManager): pass diff --git a/gitlab/v4/objects.py b/gitlab/v4/objects.py index 16564e4..80a6ca5 100644 --- a/gitlab/v4/objects.py +++ b/gitlab/v4/objects.py @@ -909,7 +909,7 @@ class ProjectCustomAttributeManager(RetrieveMixin, SetMixin, DeleteMixin, _from_parent_attrs = {'project_id': 'id'} -class ProjectJob(RESTObject): +class ProjectJob(RESTObject, RefreshMixin): @cli.register_custom_action('ProjectJob') @exc.on_http_error(exc.GitlabJobCancelError) def cancel(self, **kwargs): @@ -1045,7 +1045,7 @@ class ProjectJobManager(RetrieveMixin, RESTManager): _from_parent_attrs = {'project_id': 'id'} -class ProjectCommitStatus(RESTObject): +class ProjectCommitStatus(RESTObject, RefreshMixin): pass @@ -1964,7 +1964,7 @@ class ProjectPipelineJobsManager(ListMixin, RESTManager): _list_filters = ('scope',) -class ProjectPipeline(RESTObject): +class ProjectPipeline(RESTObject, RefreshMixin): _managers = (('jobs', 'ProjectPipelineJobManager'), ) @cli.register_custom_action('ProjectPipeline') |
