Skip to content

Commit 1b6a2e2

Browse files
committed
introduce ReadMixin
ReadMixin allows to update a REST object so that you can poll on it. This is mostly useful for pipelines and jobs, but could be set on most of other objects, with unknown usecases.
1 parent e7546de commit 1b6a2e2

File tree

5 files changed

+62
-2
lines changed

5 files changed

+62
-2
lines changed

docs/gl_objects/projects.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,22 @@
333333
pipeline = project.pipelines.create({'ref': 'master'})
334334
# end pipeline create
335335

336+
# pipeline trigger
337+
def get_or_create_trigger(project):
338+
trigger_decription = 'my_trigger_id'
339+
for t in project.triggers.list():
340+
if t.description == trigger_decription:
341+
return t
342+
return project.triggers.create({'description': trigger_decription})
343+
344+
trigger = get_or_create_trigger(project)
345+
pipeline = project.trigger_pipeline('master', trigger.token, variables={"DEPLOY_ZONE": "us-west1"})
346+
while pipeline.finished_at is None:
347+
pipeline.read()
348+
os.sleep(1)
349+
350+
# end pipeline trigger
351+
336352
# pipeline retry
337353
pipeline.retry()
338354
# end pipeline retry

docs/gl_objects/projects.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,12 @@ Create a pipeline for a particular reference:
658658
:start-after: # pipeline create
659659
:end-before: # end pipeline create
660660

661+
Trigger a pipeline for a particular reference with variables and wait for completion:
662+
663+
.. literalinclude:: projects.py
664+
:start-after: # pipeline trigger
665+
:end-before: # end pipeline trigger
666+
661667
Project Services
662668
================
663669

gitlab/mixins.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,25 @@ def get(self, id=None, **kwargs):
6868
return self._obj_cls(self, server_data)
6969

7070

71+
class ReadMixin(object):
72+
@exc.on_http_error(exc.GitlabGetError)
73+
def read(self, **kwargs):
74+
"""Read and update a single object from server.
75+
76+
Args:
77+
**kwargs: Extra options to send to the Gitlab server (e.g. sudo)
78+
79+
Returns None (updates the object)
80+
81+
Raises:
82+
GitlabAuthenticationError: If authentication is not correct
83+
GitlabGetError: If the server cannot perform the request
84+
"""
85+
path = '%s/%s' % (self.manager.path, self.id)
86+
server_data = self.manager.gitlab.http_get(path, **kwargs)
87+
self._update_attrs(server_data)
88+
89+
7190
class ListMixin(object):
7291
@exc.on_http_error(exc.GitlabListError)
7392
def list(self, **kwargs):

gitlab/tests/test_mixins.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,25 @@ def resp_cont(url, request):
153153
self.assertEqual(obj.foo, 'bar')
154154
self.assertEqual(obj.id, 42)
155155

156+
def test_read_mixin(self):
157+
class O(ReadMixin, FakeObject):
158+
pass
159+
160+
@urlmatch(scheme="http", netloc="localhost", path='/api/v4/tests/42',
161+
method="get")
162+
def resp_cont(url, request):
163+
headers = {'Content-Type': 'application/json'}
164+
content = '{"id": 42, "foo": "bar"}'
165+
return response(200, content, headers, None, 5, request)
166+
167+
with HTTMock(resp_cont):
168+
mgr = FakeManager(self.gl)
169+
obj = O(mgr, {'id': 42})
170+
res = obj.read()
171+
self.assertIsNone(res)
172+
self.assertEqual(obj.foo, 'bar')
173+
self.assertEqual(obj.id, 42)
174+
156175
def test_get_without_id_mixin(self):
157176
class M(GetWithoutIdMixin, FakeManager):
158177
pass

gitlab/v4/objects.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,7 @@ class ProjectCustomAttributeManager(RetrieveMixin, SetMixin, DeleteMixin,
909909
_from_parent_attrs = {'project_id': 'id'}
910910

911911

912-
class ProjectJob(RESTObject):
912+
class ProjectJob(RESTObject, ReadMixin):
913913
@cli.register_custom_action('ProjectJob')
914914
@exc.on_http_error(exc.GitlabJobCancelError)
915915
def cancel(self, **kwargs):
@@ -1964,7 +1964,7 @@ class ProjectPipelineJobsManager(ListMixin, RESTManager):
19641964
_list_filters = ('scope',)
19651965

19661966

1967-
class ProjectPipeline(RESTObject):
1967+
class ProjectPipeline(RESTObject, ReadMixin):
19681968
_managers = (('jobs', 'ProjectPipelineJobManager'), )
19691969

19701970
@cli.register_custom_action('ProjectPipeline')

0 commit comments

Comments
 (0)