summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTony Rom <thetonyrom@gmail.com>2017-11-16 21:10:15 +0300
committerTony Rom <thetonyrom@gmail.com>2017-12-15 19:53:57 +0300
commitfd88b0ca56b3a4230902f76a7b049228e53e6bb0 (patch)
treefdb0ac8c494817e946bc3b342c54e3b17e4e2e0d
parent627a96875ee68e37b45192af3121f09032ea4bbf (diff)
downloadgitlab-ce-fd88b0ca56b3a4230902f76a7b049228e53e6bb0.tar.gz
Add `pipelines` endpoint to merge requests API
-rw-r--r--changelogs/unreleased/39214__pipeline_api.yml5
-rw-r--r--doc/api/merge_requests.md26
-rw-r--r--lib/api/merge_requests.rb15
-rw-r--r--spec/fixtures/api/schemas/public_api/v4/pipelines.json4
-rw-r--r--spec/requests/api/merge_requests_spec.rb37
5 files changed, 87 insertions, 0 deletions
diff --git a/changelogs/unreleased/39214__pipeline_api.yml b/changelogs/unreleased/39214__pipeline_api.yml
new file mode 100644
index 00000000000..18ee2e43798
--- /dev/null
+++ b/changelogs/unreleased/39214__pipeline_api.yml
@@ -0,0 +1,5 @@
+---
+title: Add `pipelines` endpoint to merge requests API
+merge_request: 15454
+author: Tony Rom <thetonyrom@gmail.com>
+type: added
diff --git a/doc/api/merge_requests.md b/doc/api/merge_requests.md
index 880b0ed2c65..a3261cf7ee4 100644
--- a/doc/api/merge_requests.md
+++ b/doc/api/merge_requests.md
@@ -431,6 +431,32 @@ Parameters:
}
```
+## List MR pipelines
+
+Get a list of merge request pipelines.
+
+```
+GET /projects/:id/merge_requests/:merge_request_iid/pipelines
+```
+
+Parameters:
+
+- `id` (required) - The ID or [URL-encoded path of the project](README.md#namespaced-path-encoding) owned by the authenticated user
+- `merge_request_iid` (required) - The internal ID of the merge request
+
+Example of response
+
+```json
+[
+ {
+ "id": 77,
+ "sha": "959e04d7c7a30600c894bd3c0cd0e1ce7f42c11d",
+ "ref": "master",
+ "status": "success"
+ }
+]
+```
+
## Create MR
Creates a new merge request.
diff --git a/lib/api/merge_requests.rb b/lib/api/merge_requests.rb
index d34886fca2e..77c563ec0b4 100644
--- a/lib/api/merge_requests.rb
+++ b/lib/api/merge_requests.rb
@@ -24,6 +24,12 @@ module API
.preload(:notes, :author, :assignee, :milestone, :latest_merge_request_diff, :labels, :timelogs)
end
+ def merge_request_pipelines_with_access(access_level = :read_pipeline)
+ authorize! access_level, user_project
+ mr = find_merge_request_with_access(params[:merge_request_iid])
+ mr.all_pipelines
+ end
+
params :merge_requests_params do
optional :state, type: String, values: %w[opened closed merged all], default: 'all',
desc: 'Return opened, closed, merged, or all merge requests'
@@ -203,6 +209,15 @@ module API
present merge_request, with: Entities::MergeRequestChanges, current_user: current_user
end
+ desc 'Get the merge request pipelines' do
+ success Entities::PipelineBasic
+ end
+ get ':id/merge_requests/:merge_request_iid/pipelines' do
+ pipelines = merge_request_pipelines_with_access
+
+ present paginate(pipelines), with: Entities::PipelineBasic
+ end
+
desc 'Update a merge request' do
success Entities::MergeRequest
end
diff --git a/spec/fixtures/api/schemas/public_api/v4/pipelines.json b/spec/fixtures/api/schemas/public_api/v4/pipelines.json
new file mode 100644
index 00000000000..8b08a00f708
--- /dev/null
+++ b/spec/fixtures/api/schemas/public_api/v4/pipelines.json
@@ -0,0 +1,4 @@
+{
+ "type": "array",
+ "items": { "$ref": "pipeline/basic.json" }
+}
diff --git a/spec/requests/api/merge_requests_spec.rb b/spec/requests/api/merge_requests_spec.rb
index 91616da6d9a..4278e32dc78 100644
--- a/spec/requests/api/merge_requests_spec.rb
+++ b/spec/requests/api/merge_requests_spec.rb
@@ -525,6 +525,43 @@ describe API::MergeRequests do
end
end
+ describe 'GET /projects/:id/merge_requests/:merge_request_iid/pipelines' do
+ context 'when authorized' do
+ let!(:pipeline) { create(:ci_empty_pipeline, project: project, user: user, ref: merge_request.source_branch, sha: merge_request.diff_head_sha) }
+ let!(:pipeline2) { create(:ci_empty_pipeline, project: project) }
+
+ it 'returns a paginated array of corresponding pipelines' do
+ get api("/projects/#{project.id}/merge_requests/#{merge_request.iid}/pipelines")
+
+ expect(response).to have_gitlab_http_status(200)
+ expect(response).to include_pagination_headers
+ expect(json_response).to be_an Array
+ expect(json_response.count).to eq(1)
+ expect(json_response.first['id']).to eq(pipeline.id)
+ end
+
+ it 'exposes basic attributes' do
+ get api("/projects/#{project.id}/merge_requests/#{merge_request.iid}/pipelines")
+
+ expect(response).to have_gitlab_http_status(200)
+ expect(response).to match_response_schema('public_api/v4/pipelines')
+ end
+ end
+
+ context 'when unauthorized' do
+ it 'returns 403' do
+ project = create(:project, public_builds: false)
+ merge_request = create(:merge_request, :simple, source_project: project)
+ guest = create(:user)
+ project.team << [guest, :guest]
+
+ get api("/projects/#{project.id}/merge_requests/#{merge_request.iid}/pipelines", guest)
+
+ expect(response).to have_gitlab_http_status(403)
+ end
+ end
+ end
+
describe "POST /projects/:id/merge_requests" do
context 'between branches projects' do
it "returns merge_request" do