8000 feat(api): add support for latest pipeline · python-gitlab/python-gitlab@635f5a7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 635f5a7

Browse files
oliver.blasiusJohnVillalovos
authored andcommitted
feat(api): add support for latest pipeline
1 parent 88de2f0 commit 635f5a7

File tree

3 files changed

+123
-0
lines changed

3 files changed

+123
-0
lines changed

docs/gl_objects/pipelines_and_jobs.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ Delete a pipeline::
4949

5050
pipeline.delete()
5151

52+
Get latest pipeline::
53+
54+
projet.pipelines.latest(ref="main")
55+
56+
5257
Triggers
5358
========
5459

gitlab/v4/objects/pipelines.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,27 @@ def create(
139139
ProjectPipeline, CreateMixin.create(self, data, path=path, **kwargs)
140140
)
141141

142+
def latest(self, ref: Optional[str] = None, lazy: bool = False) -> ProjectPipeline:
143+
"""Get the latest pipeline for the most recent commit
144+
on a specific ref in a project
145+
146+
Args:
147+
ref: The branch or tag to check for the latest pipeline.
148+
Defaults to the default branch when not specified.
149+
Returns:
150+
A Pipeline instance
151+
"""
152+
data = {}
153+
if ref:
154+
data = {"ref": ref}
155+
if TYPE_CHECKING:
156+
assert self._obj_cls is not None
157+
assert self.path is not None
158+
server_data = self.gitlab.http_get(self.path + "/latest", query_data=data)
159+
if TYPE_CHECKING:
160+
assert not isinstance(server_data, requests.Response)
161+
return self._obj_cls(self, server_data, lazy=lazy)
162+
142163

143164
class ProjectPipelineJob(RESTObject):
144165
pass

tests/unit/objects/test_pipelines.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,62 @@
3939
"web_url": "https://example.com/foo/bar/pipelines/46",
4040
}
4141

42+
pipeline_latest = {
43+
"id": 47,
44+
"project_id": 1,
45+
"status": "pending",
46+
"ref": "main",
47+
"sha": "a91957a858320c0e17f3a0eca7cfacbff50ea29a",
48+
"before_sha": "a91957a858320c0e17f3a0eca7cfacbff50ea29a",
49+
"tag": False,
50+
"yaml_errors": None,
51+
"user": {
52+
"name": "Administrator",
53+
"username": "root",
54+
"id": 1,
55+
"state": "active",
56+
"avatar_url": "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon",
57+
"web_url": "http://localhost:3000/root",
58+
},
59+
"created_at": "2016-08-11T11:28:34.085Z",
60+
"updated_at": "2016-08-11T11:32:35.169Z",
61+
"started_at": None,
62+
"finished_at": "2016-08-11T11:32:35.145Z",
63+
"committed_at": None,
64+
"duration": None,
65+
"queued_duration": 0.010,
66+
"coverage": None,
67+
"web_url": "https://example.com/foo/bar/pipelines/46",
68+
}
69+
70+
pipeline_latest_other_ref = {
71+
"id": 48,
72+
"project_id": 1,
73+
"status": "pending",
74+
"ref": "feature-ref",
75+
"sha": "a91957a858320c0e17f3a0eca7cfacbff50ea29a",
76+
"before_sha": "a91957a858320c0e17f3a0eca7cfacbff50ea29a",
77+
"tag": False,
78+
"yaml_errors": None,
79+
"user": {
80+
"name": "Administrator",
81+
"username": "root",
82+
"id": 1,
83+
"state": "active",
84+
"avatar_url": "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon",
85+
"web_url": "http://localhost:3000/root",
86+
},
87+
"created_at": "2016-08-11T11:28:34.085Z",
88+
"updated_at": "2016-08-11T11:32:35.169Z",
89+
"started_at": None,
90+
"finished_at": "2016-08-11T11:32:35.145Z",
91+
"committed_at": None,
92+
"duration": None,
93+
"queued_duration": 0.010,
94+
"coverage": None,
95+
"web_url": "https://example.com/foo/bar/pipelines/46",
96+
}
97+
4298

4399
test_report_content = {
44100
"total_time": 5,
@@ -162,10 +218,37 @@ def resp_get_pipeline_test_report_summary():
162218
yield rsps
163219

164220

221+
@pytest.fixture
222+
def resp_get_latest():
223+
with responses.RequestsMock() as rsps:
224+
rsps.add(
225+
method=responses.GET,
226+
url="http://localhost/api/v4/projects/1/pipelines/latest",
2 F438 27+
json=pipeline_latest,
228+
content_type="application/json",
229+
status=200,
230+
)
231+
yield rsps
232+
233+
234+
@pytest.fixture
235+
def resp_get_latest_other_ref():
236+
with responses.RequestsMock() as rsps:
237+
rsps.add(
238+
method=responses.GET,
239+
url="http://localhost/api/v4/projects/1/pipelines/latest",
240+
json=pipeline_latest_other_ref,
241+
content_type="application/json",
242+
status=200,
243+
)
244+
yield rsps
245+
246+
165247
def test_get_project_pipeline(project, resp_get_pipeline):
166248
pipeline = project.pipelines.get(1)
167249
assert isinstance(pipeline, ProjectPipeline)
168250
assert pipeline.ref == "main"
251+
assert pipeline.id == 46
169252

170253

171254
def test_cancel_project_pipeline(project, resp_cancel_pipeline):
@@ -198,3 +281,17 @@ def test_get_project_pipeline_test_report_summary(
198281
assert isinstance(test_report_summary, ProjectPipelineTestReportSummary)
199282
assert test_report_summary.total["count"] == 3363
200283
assert test_report_summary.test_suites[0]["name"] == "test"
284+
285+
286+
def test_latest_pipeline(project, resp_get_latest):
287+
pipeline = project.pipelines.latest()
288+
assert isinstance(pipeline, ProjectPipeline)
289+
assert pipeline.ref == "main"
290+
assert pipeline.id == 47
291+
292+
293+
def test_latest_pipeline_other_ref(project, resp_get_latest_other_ref):
294+
pipeline = project.pipelines.latest(ref="feature-ref")
295+
assert isinstance(pipeline, ProjectPipeline)
296+
assert pipeline.ref == "feature-ref"
297+
assert pipeline.id == 48

0 commit comments

Comments
 (0)
0