Skip to content

Commit ee4591d

Browse files
author
Gauvain Pocentek
authored
Merge pull request #426 from tardyp/readmixin
introduce RefreshMixin
2 parents 6bcc92a + 3424333 commit ee4591d

File tree

5 files changed

+63
-3
lines changed

5 files changed

+63
-3
lines changed

docs/gl_objects/builds.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,22 @@
4444
trigger.delete()
4545
# end trigger delete
4646

47+
# pipeline trigger
48+
def get_or_create_trigger(project):
49+
trigger_decription = 'my_trigger_id'
50+
for t in project.triggers.list():
51+
if t.description == trigger_decription:
52+
return t
53+
return project.triggers.create({'description': trigger_decription})
54+
55+
trigger = get_or_create_trigger(project)
56+
pipeline = project.trigger_pipeline('master', trigger.token, variables={"DEPLOY_ZONE": "us-west1"})
57+
while pipeline.finished_at is None:
58+
pipeline.refresh()
59+
os.sleep(1)
60+
61+
# end pipeline trigger
62+
4763
# list
4864
builds = project.builds.list() # v3
4965
jobs = project.jobs.list() # v4

docs/gl_objects/builds.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ Remove a trigger:
102102
:start-after: # trigger delete
103103
:end-before: # end trigger delete
104104

105+
Full example with wait for finish:
106+
107+
.. literalinclude:: builds.py
108+
:start-after: # pipeline trigger
109+
:end-before: # end pipeline trigger
110+
105111
Pipeline schedule
106112
=================
107113

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 RefreshMixin(object):
72+
@exc.on_http_error(exc.GitlabGetError)
73+
def refresh(self, **kwargs):
74+
"""Refresh 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_refresh_mixin(self):
157+
class O(RefreshMixin, 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.refresh()
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: 3 additions & 3 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, RefreshMixin):
913913
@cli.register_custom_action('ProjectJob')
914914
@exc.on_http_error(exc.GitlabJobCancelError)
915915
def cancel(self, **kwargs):
@@ -1045,7 +1045,7 @@ class ProjectJobManager(RetrieveMixin, RESTManager):
10451045
_from_parent_attrs = {'project_id': 'id'}
10461046

10471047

1048-
class ProjectCommitStatus(RESTObject):
1048+
class ProjectCommitStatus(RESTObject, RefreshMixin):
10491049
pass
10501050

10511051

@@ -1964,7 +1964,7 @@ class ProjectPipelineJobsManager(ListMixin, RESTManager):
19641964
_list_filters = ('scope',)
19651965

19661966

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

19701970
@cli.register_custom_action('ProjectPipeline')

0 commit comments

Comments
 (0)