Skip to content

Commit 1cc86d8

Browse files
committed
feat(api): add MR pipeline manager in favor of pipelines() method
1 parent f909cae commit 1cc86d8

File tree

4 files changed

+138
-23
lines changed

4 files changed

+138
-23
lines changed

docs/gl_objects/mrs.rst

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,6 @@ List the changes of a MR::
127127

128128
changes = mr.changes()
129129

130-
List the pipelines for a MR::
131-
132-
pipelines = mr.pipelines()
133-
134130
List issues that will close on merge::
135131

136132
mr.closes_issues()
@@ -185,3 +181,31 @@ Get user agent detail for the issue (admin only)::
185181
Attempt to rebase an MR::
186182

187183
mr.rebase()
184+
185+
Merge Request Pipelines
186+
=======================
187+
188+
Reference
189+
---------
190+
191+
* v4 API:
192+
193+
+ :class:`gitlab.v4.objects.MergeRequestPipeline`
194+
+ :class:`gitlab.v4.objects.MergeRequestPipelineManager`
195+
+ :attr:`gitlab.v4.objects.ProjectMergeRequest.pipelines`
196+
197+
* GitLab API: https://docs.gitlab.com/ee/api/merge_requests.html#list-mr-pipelines
198+
199+
Examples
200+
--------
201+
202+
List pipelines for a merge request::
203+
204+
pipelines = mr.pipelines.list()
205+
206+
# Deprecated, will be removed
207+
pipelines = mr.pipelines()
208+
209+
Create a pipeline for a merge request::
210+
211+
pipeline = mr.pipelines.create()
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
"""
2+
GitLab API: https://docs.gitlab.com/ee/api/merge_requests.html#list-mr-pipelines
3+
"""
4+
import re
5+
6+
import pytest
7+
import responses
8+
9+
from gitlab.v4.objects import MergeRequestPipeline
10+
11+
pipeline_content = {
12+
"id": 1,
13+
"sha": "959e04d7c7a30600c894bd3c0cd0e1ce7f42c11d",
14+
"ref": "master",
15+
"status": "success",
16+
}
17+
18+
19+
@pytest.fixture()
20+
def resp_list_merge_request_pipelines():
21+
with responses.RequestsMock() as rsps:
22+
rsps.add(
23+
method=responses.GET,
24+
url="http://localhost/api/v4/projects/1/merge_requests/1/pipelines",
25+
json=[pipeline_content],
26+
content_type="application/json",
27+
status=200,
28+
)
29+
yield rsps
30+
31+
32+
@pytest.fixture()
33+
def resp_create_merge_request_pipeline():
34+
with responses.RequestsMock() as rsps:
35+
rsps.add(
36+
method=responses.POST,
37+
url="http://localhost/api/v4/projects/1/merge_requests/1/pipelines",
38+
json=pipeline_content,
39+
content_type="application/json",
40+
status=201,
41+
)
42+
yield rsps
43+
44+
45+
def test_merge_requests_pipelines_deprecated_raises_warning(
46+
project, resp_list_merge_request_pipelines
47+
):
48+
with pytest.deprecated_call():
49+
pipelines = project.mergerequests.get(1, lazy=True).pipelines()
50+
51+
assert len(pipelines) == 1
52+
assert isinstance(pipelines[0], MergeRequestPipeline)
53+
assert pipelines[0].sha == pipeline_content["sha"]
54+
55+
56+
def test_list_merge_requests_pipelines(project, resp_list_merge_request_pipelines):
57+
pipelines = project.mergerequests.get(1, lazy=True).pipelines.list()
58+
assert len(pipelines) == 1
59+
assert isinstance(pipelines[0], MergeRequestPipeline)
60+
assert pipelines[0].sha == pipeline_content["sha"]
61+
62+
63+
def test_create_merge_requests_pipelines(project, resp_create_merge_request_pipeline):
64+
pipeline = project.mergerequests.get(1, lazy=True).pipelines.create()
65+
assert isinstance(pipeline, MergeRequestPipeline)
66+
assert pipeline.sha == pipeline_content["sha"]

gitlab/v4/objects/merge_requests.py

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
ProjectMergeRequestResourceLabelEventManager,
2626
ProjectMergeRequestResourceMilestoneEventManager,
2727
)
28+
from .pipelines import MergeRequestPipelineManager
2829

2930

3031
__all__ = [
@@ -119,6 +120,7 @@ class ProjectMergeRequest(
119120
("diffs", "ProjectMergeRequestDiffManager"),
120121
("discussions", "ProjectMergeRequestDiscussionManager"),
121122
("notes", "ProjectMergeRequestNoteManager"),
123+
("pipelines", "MergeRequestPipelineManager"),
122124
("resourcelabelevents", "ProjectMergeRequestResourceLabelEventManager"),
123125
("resourcemilestoneevents", "ProjectMergeRequestResourceMilestoneEventManager"),
124126
)
@@ -213,25 +215,6 @@ def changes(self, **kwargs):
213215
path = "%s/%s/changes" % (self.manager.path, self.get_id())
214216
return self.manager.gitlab.http_get(path, **kwargs)
215217

216-
@cli.register_custom_action("ProjectMergeRequest")
217-
@exc.on_http_error(exc.GitlabListError)
218-
def pipelines(self, **kwargs):
219-
"""List the merge request pipelines.
220-
221-
Args:
222-
**kwargs: Extra options to send to the server (e.g. sudo)
223-
224-
Raises:
225-
GitlabAuthenticationError: If authentication is not correct
226-
GitlabListError: If the list could not be retrieved
227-
228-
Returns:
229-
RESTObjectList: List of changes
230-
"""
231-
232-
path = "%s/%s/pipelines" % (self.manager.path, self.get_id())
233-
return self.manager.gitlab.http_get(path, **kwargs)
234-
235218
@cli.register_custom_action("ProjectMergeRequest", tuple(), ("sha"))
236219
@exc.on_http_error(exc.GitlabMRApprovalError)
237220
def approve(self, sha=None, **kwargs):

gitlab/v4/objects/pipelines.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import warnings
2+
13
from gitlab import cli, types
24
from gitlab import exceptions as exc
35
from gitlab.base import RESTManager, RESTObject
@@ -15,6 +17,8 @@
1517

1618

1719
__all__ = [
20+
"MergeRequestPipeline",
21+
"MergeRequestPipelineManager",
1822
"ProjectPipeline",
1923
"ProjectPipelineManager",
2024
"ProjectPipelineJob",
@@ -30,6 +34,44 @@
3034
]
3135

3236

37+
class MergeRequestPipeline(RESTObject):
38+
pass
39+
40+
41+
class MergeRequestPipelineManager(CreateMixin, ListMixin, RESTManager):
42+
_path = "/projects/%(project_id)s/merge_requests/%(mr_iid)s/pipelines"
43+
_obj_cls = MergeRequestPipeline
44+
_from_parent_attrs = {"project_id": "project_id", "mr_iid": "iid"}
45+
_create_attrs = (tuple(), tuple())
46+
47+
# If the manager was called directly as a callable via
48+
# mr.pipelines(), execute the deprecated method for now.
49+
# TODO: in python-gitlab 3.0.0, remove this method entirely.
50+
51+
@cli.register_custom_action("ProjectMergeRequest")
52+
@exc.on_http_error(exc.GitlabListError)
53+
def __call__(self, **kwargs):
54+
"""List the merge request pipelines.
55+
56+
Args:
57+
**kwargs: Extra options to send to the server (e.g. sudo)
58+
59+
Raises:
60+
GitlabAuthenticationError: If authentication is not correct
61+
GitlabListError: If the list could not be retrieved
62+
63+
Returns:
64+
RESTObjectList: List of changes
65+
"""
66+
warnings.warn(
67+
"Calling the ProjectMergeRequest.pipelines() method on "
68+
"merge request objects directly is deprecated and will be replaced "
69+
"by ProjectMergeRequest.pipelines.list() in python-gitlab 3.0.0.\n",
70+
DeprecationWarning,
71+
)
72+
return self.list(**kwargs)
73+
74+
3375
class ProjectPipeline(RESTObject, RefreshMixin, ObjectDeleteMixin):
3476
_managers = (
3577
("jobs", "ProjectPipelineJobManager"),

0 commit comments

Comments
 (0)