Commit 91b332f5 authored by Mayra Cabrera's avatar Mayra Cabrera

Merge branch '35739-add-mr-to-deployment-api' into 'master'

Add API support for retrieving deployed merge requests

See merge request gitlab-org/gitlab!21837
parents cfb3ac6f bb65dcf9
...@@ -39,6 +39,7 @@ class MergeRequestsFinder < IssuableFinder ...@@ -39,6 +39,7 @@ class MergeRequestsFinder < IssuableFinder
def filter_items(_items) def filter_items(_items)
items = by_commit(super) items = by_commit(super)
items = by_deployment(items)
items = by_source_branch(items) items = by_source_branch(items)
items = by_wip(items) items = by_wip(items)
items = by_target_branch(items) items = by_target_branch(items)
...@@ -101,6 +102,17 @@ class MergeRequestsFinder < IssuableFinder ...@@ -101,6 +102,17 @@ class MergeRequestsFinder < IssuableFinder
.or(table[:title].matches('WIP %')) .or(table[:title].matches('WIP %'))
.or(table[:title].matches('[WIP]%')) .or(table[:title].matches('[WIP]%'))
end end
def by_deployment(items)
return items unless deployment_id
items.includes(:deployment_merge_requests)
.where(deployment_merge_requests: { deployment_id: deployment_id })
end
def deployment_id
@deployment_id ||= params[:deployment_id].presence
end
end end
MergeRequestsFinder.prepend_if_ee('EE::MergeRequestsFinder') MergeRequestsFinder.prepend_if_ee('EE::MergeRequestsFinder')
---
title: Add API support for retrieving merge requests deployed in a deployment
merge_request: 21837
author:
type: added
...@@ -349,3 +349,19 @@ Example of a response: ...@@ -349,3 +349,19 @@ Example of a response:
"deployable": null "deployable": null
} }
``` ```
## List of merge requests associated with a deployment
> [Introduced](https://gitlab.com/gitlab-org/gitlab/issues/35739) in GitLab 12.7.
This API retrieves the list of merge requests shipped with a given deployment:
```
GET /projects/:id/deployments/:deployment_id/merge_requests
```
It supports the same parameters as the [Merge Requests API](./merge_requests.md#list-merge-requests) and will return a response using the same format:
```bash
curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/1/deployments/42"
```
...@@ -127,6 +127,26 @@ module API ...@@ -127,6 +127,26 @@ module API
render_validation_error!(deployment) render_validation_error!(deployment)
end end
end end
helpers Helpers::MergeRequestsHelpers
desc 'Get all merge requests of a deployment' do
detail 'This feature was introduced in GitLab 12.7.'
success Entities::MergeRequestBasic
end
params do
requires :deployment_id, type: Integer, desc: 'The deployment ID'
use :merge_requests_base_params
end
get ':id/deployments/:deployment_id/merge_requests' do
authorize! :read_deployment, user_project
mr_params = declared_params.merge(deployment_id: params[:deployment_id])
merge_requests = MergeRequestsFinder.new(current_user, mr_params).execute
present merge_requests, { with: Entities::MergeRequestBasic, current_user: current_user }
end
end end
end end
end end
# frozen_string_literal: true
module API
module Helpers
module MergeRequestsHelpers
extend Grape::API::Helpers
include ::API::Helpers::CustomValidators
params :merge_requests_base_params do
optional :state,
type: String,
values: %w[opened closed locked merged all],
default: 'all',
desc: 'Return opened, closed, locked, merged, or all merge requests'
optional :order_by,
type: String,
values: %w[created_at updated_at],
default: 'created_at',
desc: 'Return merge requests ordered by `created_at` or `updated_at` fields.'
optional :sort,
type: String,
values: %w[asc desc],
default: 'desc',
desc: 'Return merge requests sorted in `asc` or `desc` order.'
optional :milestone, type: String, desc: 'Return merge requests for a specific milestone'
optional :labels,
type: Array[String],
coerce_with: Validations::Types::LabelsList.coerce,
desc: 'Comma-separated list of label names'
optional :with_labels_details, type: Boolean, desc: 'Return titles of labels and other details', default: false
optional :created_after, type: DateTime, desc: 'Return merge requests created after the specified time'
optional :created_before, type: DateTime, desc: 'Return merge requests created before the specified time'
optional :updated_after, type: DateTime, desc: 'Return merge requests updated after the specified time'
optional :updated_before, type: DateTime, desc: 'Return merge requests updated before the specified time'
optional :view,
type: String,
values: %w[simple],
desc: 'If simple, returns the `iid`, URL, title, description, and basic state of merge request'
optional :author_id, type: Integer, desc: 'Return merge requests which are authored by the user with the given ID'
optional :assignee_id,
types: [Integer, String],
integer_none_any: true,
desc: 'Return merge requests which are assigned to the user with the given ID'
optional :scope,
type: String,
values: %w[created-by-me assigned-to-me created_by_me assigned_to_me all],
desc: 'Return merge requests for the given scope: `created_by_me`, `assigned_to_me` or `all`'
optional :my_reaction_emoji, type: String, desc: 'Return issues reacted by the authenticated user by the given emoji'
optional :source_branch, type: String, desc: 'Return merge requests with the given source branch'
optional :source_project_id, type: Integer, desc: 'Return merge requests with the given source project id'
optional :target_branch, type: String, desc: 'Return merge requests with the given target branch'
optional :search,
type: String,
desc: 'Search merge requests for text present in the title, description, or any combination of these'
optional :in, type: String, desc: '`title`, `description`, or a string joining them with comma'
optional :wip, type: String, values: %w[yes no], desc: 'Search merge requests for WIP in the title'
end
params :optional_scope_param do
optional :scope,
type: String,
values: %w[created-by-me assigned-to-me created_by_me assigned_to_me all],
default: 'created_by_me',
desc: 'Return merge requests for the given scope: `created_by_me`, `assigned_to_me` or `all`'
end
end
end
end
...@@ -7,6 +7,7 @@ module API ...@@ -7,6 +7,7 @@ module API
before { authenticate_non_get! } before { authenticate_non_get! }
helpers ::Gitlab::IssuableMetadata helpers ::Gitlab::IssuableMetadata
helpers Helpers::MergeRequestsHelpers
# EE::API::MergeRequests would override the following helpers # EE::API::MergeRequests would override the following helpers
helpers do helpers do
...@@ -107,33 +108,7 @@ module API ...@@ -107,33 +108,7 @@ module API
end end
params :merge_requests_params do params :merge_requests_params do
optional :state, type: String, values: %w[opened closed locked merged all], default: 'all', use :merge_requests_base_params
desc: 'Return opened, closed, locked, merged, or all merge requests'
optional :order_by, type: String, values: %w[created_at updated_at], default: 'created_at',
desc: 'Return merge requests ordered by `created_at` or `updated_at` fields.'
optional :sort, type: String, values: %w[asc desc], default: 'desc',
desc: 'Return merge requests sorted in `asc` or `desc` order.'
optional :milestone, type: String, desc: 'Return merge requests for a specific milestone'
optional :labels, type: Array[String], coerce_with: Validations::Types::LabelsList.coerce, desc: 'Comma-separated list of label names'
optional :with_labels_details, type: Boolean, desc: 'Return titles of labels and other details', default: false
optional :created_after, type: DateTime, desc: 'Return merge requests created after the specified time'
optional :created_before, type: DateTime, desc: 'Return merge requests created before the specified time'
optional :updated_after, type: DateTime, desc: 'Return merge requests updated after the specified time'
optional :updated_before, type: DateTime, desc: 'Return merge requests updated before the specified time'
optional :view, type: String, values: %w[simple], desc: 'If simple, returns the `iid`, URL, title, description, and basic state of merge request'
optional :author_id, type: Integer, desc: 'Return merge requests which are authored by the user with the given ID'
optional :assignee_id, types: [Integer, String], integer_none_any: true,
desc: 'Return merge requests which are assigned to the user with the given ID'
optional :scope, type: String, values: %w[created-by-me assigned-to-me created_by_me assigned_to_me all],
desc: 'Return merge requests for the given scope: `created_by_me`, `assigned_to_me` or `all`'
optional :my_reaction_emoji, type: String, desc: 'Return issues reacted by the authenticated user by the given emoji'
optional :source_branch, type: String, desc: 'Return merge requests with the given source branch'
optional :source_project_id, type: Integer, desc: 'Return merge requests with the given source project id'
optional :target_branch, type: String, desc: 'Return merge requests with the given target branch'
optional :search, type: String, desc: 'Search merge requests for text present in the title, description, or any combination of these'
optional :in, type: String, desc: '`title`, `description`, or a string joining them with comma'
optional :wip, type: String, values: %w[yes no], desc: 'Search merge requests for WIP in the title'
use :optional_merge_requests_search_params use :optional_merge_requests_search_params
use :pagination use :pagination
end end
...@@ -145,8 +120,7 @@ module API ...@@ -145,8 +120,7 @@ module API
end end
params do params do
use :merge_requests_params use :merge_requests_params
optional :scope, type: String, values: %w[created-by-me assigned-to-me created_by_me assigned_to_me all], default: 'created_by_me', use :optional_scope_param
desc: 'Return merge requests for the given scope: `created_by_me`, `assigned_to_me` or `all`'
end end
get do get do
authenticate! unless params[:scope] == 'all' authenticate! unless params[:scope] == 'all'
......
...@@ -166,6 +166,38 @@ describe MergeRequestsFinder do ...@@ -166,6 +166,38 @@ describe MergeRequestsFinder do
expect(scalar_params).to include(:wip, :assignee_id) expect(scalar_params).to include(:wip, :assignee_id)
end end
context 'filter by deployment' do
let_it_be(:project_with_repo) { create(:project, :repository) }
it 'returns the relevant merge requests' do
deployment1 = create(
:deployment,
project: project_with_repo,
sha: project_with_repo.commit.id,
merge_requests: [merge_request1, merge_request2]
)
create(
:deployment,
project: project_with_repo,
sha: project_with_repo.commit.id,
merge_requests: [merge_request3]
)
params = { deployment_id: deployment1.id }
merge_requests = described_class.new(user, params).execute
expect(merge_requests).to contain_exactly(merge_request1, merge_request2)
end
context 'when a deployment does not contain any merge requests' do
it 'returns an empty result' do
params = { deployment_id: create(:deployment, project: project_with_repo, sha: project_with_repo.commit.sha).id }
merge_requests = described_class.new(user, params).execute
expect(merge_requests).to be_empty
end
end
end
end end
context 'assignee filtering' do context 'assignee filtering' do
......
...@@ -343,6 +343,48 @@ describe API::Deployments do ...@@ -343,6 +343,48 @@ describe API::Deployments do
end end
end end
describe 'GET /projects/:id/deployments/:deployment_id/merge_requests' do
let(:project) { create(:project, :repository) }
let!(:deployment) { create(:deployment, :success, project: project) }
subject { get api("/projects/#{project.id}/deployments/#{deployment.id}/merge_requests", user) }
context 'when a user is not a member of the deployment project' do
let(:user) { build(:user) }
it 'returns a 404 status code' do
subject
expect(response).to have_gitlab_http_status(404)
end
end
context 'when a user member of the deployment project' do
let_it_be(:project2) { create(:project) }
let!(:merge_request1) { create(:merge_request, source_project: project, target_project: project) }
let!(:merge_request2) { create(:merge_request, source_project: project, target_project: project, state: 'closed') }
let!(:merge_request3) { create(:merge_request, source_project: project2, target_project: project2) }
it 'returns the relevant merge requests linked to a deployment for a project' do
deployment.merge_requests << [merge_request1, merge_request2]
subject
expect(response).to have_gitlab_http_status(200)
expect(json_response.map { |d| d['id'] }).to contain_exactly(merge_request1.id, merge_request2.id)
end
context 'when a deployment is not associated to any existing merge requests' do
it 'returns an empty array' do
subject
expect(response).to have_gitlab_http_status(200)
expect(json_response).to eq([])
end
end
end
end
context 'prevent N + 1 queries' do context 'prevent N + 1 queries' do
context 'when the endpoint returns multiple records' do context 'when the endpoint returns multiple records' do
let(:project) { create(:project, :repository) } let(:project) { create(:project, :repository) }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment