Commit 9dbb0417 authored by Robert Schilling's avatar Robert Schilling

Grapify the merge request API

parent 36fa5d66
module API module API
# MergeRequest API
class MergeRequests < Grape::API class MergeRequests < Grape::API
DEPRECATION_MESSAGE = 'This endpoint is deprecated and will be removed in GitLab 9.0.'.freeze
before { authenticate! } before { authenticate! }
params do
requires :id, type: String, desc: 'The ID of a project'
end
resource :projects do resource :projects do
helpers do helpers do
def handle_merge_request_errors!(errors) def handle_merge_request_errors!(errors)
...@@ -18,27 +22,27 @@ module API ...@@ -18,27 +22,27 @@ module API
render_api_error!(errors, 400) render_api_error!(errors, 400)
end end
params :optional_params do
optional :description, type: String, desc: 'The description of the merge request'
optional :assignee_id, type: Integer, desc: 'The ID of a user to assign the merge request'
optional :milestone_id, type: Integer, desc: 'The ID of a milestone to assign the merge request'
optional :labels, type: String, desc: 'Comma-separated list of label names'
end
end end
# List merge requests desc 'List merge requests' do
# success Entities::MergeRequest
# Parameters: end
# id (required) - The ID of a project params do
# iid (optional) - Return the project MR having the given `iid` optional :state, type: String, values: %w[opened closed merged all], default: 'all',
# state (optional) - Return requests "merged", "opened" or "closed" desc: 'Return opened, closed, merged, or all merge requests'
# order_by (optional) - Return requests ordered by `created_at` or `updated_at` fields. Default is `created_at` optional :order_by, type: String, values: %w[created_at updated_at], default: 'created_at',
# sort (optional) - Return requests sorted in `asc` or `desc` order. Default is `desc` desc: 'Return merge requests ordered by `created_at` or `updated_at` fields.'
# optional :sort, type: String, values: %w[asc desc], default: 'desc',
# Example: desc: 'Return merge requests sorted in `asc` or `desc` order.'
# GET /projects/:id/merge_requests optional :iid, type: Integer, desc: 'The IID of the merge requests'
# GET /projects/:id/merge_requests?state=opened end
# GET /projects/:id/merge_requests?state=closed
# GET /projects/:id/merge_requests?order_by=created_at
# GET /projects/:id/merge_requests?order_by=updated_at
# GET /projects/:id/merge_requests?sort=desc
# GET /projects/:id/merge_requests?sort=asc
# GET /projects/:id/merge_requests?iid=42
#
get ":id/merge_requests" do get ":id/merge_requests" do
authorize! :read_merge_request, user_project authorize! :read_merge_request, user_project
merge_requests = user_project.merge_requests.inc_notes_with_associations merge_requests = user_project.merge_requests.inc_notes_with_associations
...@@ -48,10 +52,10 @@ module API ...@@ -48,10 +52,10 @@ module API
end end
merge_requests = merge_requests =
case params["state"] case params[:state]
when "opened" then merge_requests.opened when 'opened' then merge_requests.opened
when "closed" then merge_requests.closed when 'closed' then merge_requests.closed
when "merged" then merge_requests.merged when 'merged' then merge_requests.merged
else merge_requests else merge_requests
end end
...@@ -59,36 +63,28 @@ module API ...@@ -59,36 +63,28 @@ module API
present paginate(merge_requests), with: Entities::MergeRequest, current_user: current_user present paginate(merge_requests), with: Entities::MergeRequest, current_user: current_user
end end
# Create MR desc 'Create a merge request' do
# success Entities::MergeRequest
# Parameters: end
# params do
# id (required) - The ID of a project - this will be the source of the merge request requires :title, type: String, desc: 'The title of the merge request'
# source_branch (required) - The source branch requires :source_branch, type: String, desc: 'The source branch'
# target_branch (required) - The target branch requires :target_branch, type: String, desc: 'The target branch'
# target_project_id - The target project of the merge request defaults to the :id of the project optional :target_project_id, type: Integer,
# assignee_id - Assignee user ID desc: 'The target project of the merge request defaults to the :id of the project'
# title (required) - Title of MR use :optional_params
# description - Description of MR end
# labels (optional) - Labels for MR as a comma-separated list
# milestone_id (optional) - Milestone ID
#
# Example:
# POST /projects/:id/merge_requests
#
post ":id/merge_requests" do post ":id/merge_requests" do
authorize! :create_merge_request, user_project authorize! :create_merge_request, user_project
required_attributes! [:source_branch, :target_branch, :title]
attrs = attributes_for_keys [:source_branch, :target_branch, :assignee_id, :title, :target_project_id, :description, :milestone_id] mr_params = declared_params
# Validate label names in advance # Validate label names in advance
if (errors = validate_label_params(params)).any? if (errors = validate_label_params(mr_params)).any?
render_api_error!({ labels: errors }, 400) render_api_error!({ labels: errors }, 400)
end end
attrs[:labels] = params[:labels] if params[:labels] merge_request = ::MergeRequests::CreateService.new(user_project, current_user, mr_params).execute
merge_request = ::MergeRequests::CreateService.new(user_project, current_user, attrs).execute
if merge_request.valid? if merge_request.valid?
present merge_request, with: Entities::MergeRequest, current_user: current_user present merge_request, with: Entities::MergeRequest, current_user: current_user
...@@ -97,11 +93,10 @@ module API ...@@ -97,11 +93,10 @@ module API
end end
end end
# Delete a MR desc 'Delete a merge request'
# params do
# Parameters: requires :merge_request_id, type: Integer, desc: 'The ID of a merge request'
# id (required) - The ID of the project end
# merge_request_id (required) - The MR id
delete ":id/merge_requests/:merge_request_id" do delete ":id/merge_requests/:merge_request_id" do
merge_request = user_project.merge_requests.find_by(id: params[:merge_request_id]) merge_request = user_project.merge_requests.find_by(id: params[:merge_request_id])
...@@ -112,89 +107,64 @@ module API ...@@ -112,89 +107,64 @@ module API
# Routing "merge_request/:merge_request_id/..." is DEPRECATED and WILL BE REMOVED in version 9.0 # Routing "merge_request/:merge_request_id/..." is DEPRECATED and WILL BE REMOVED in version 9.0
# Use "merge_requests/:merge_request_id/..." instead. # Use "merge_requests/:merge_request_id/..." instead.
# #
[":id/merge_request/:merge_request_id", ":id/merge_requests/:merge_request_id"].each do |path| params do
# Show MR requires :merge_request_id, type: Integer, desc: 'The ID of a merge request'
# end
# Parameters: { ":id/merge_request/:merge_request_id" => :deprecated, ":id/merge_requests/:merge_request_id" => :ok }.each do |path, status|
# id (required) - The ID of a project desc 'Get a single merge request' do
# merge_request_id (required) - The ID of MR if status == :deprecated
# detail DEPRECATION_MESSAGE
# Example: end
# GET /projects/:id/merge_requests/:merge_request_id success Entities::MergeRequest
# end
get path do get path do
merge_request = user_project.merge_requests.find(params[:merge_request_id]) merge_request = user_project.merge_requests.find(params[:merge_request_id])
authorize! :read_merge_request, merge_request authorize! :read_merge_request, merge_request
present merge_request, with: Entities::MergeRequest, current_user: current_user present merge_request, with: Entities::MergeRequest, current_user: current_user
end end
# Show MR commits desc 'Get the commits of a merge request' do
# success Entities::RepoCommit
# Parameters: end
# id (required) - The ID of a project
# merge_request_id (required) - The ID of MR
#
# Example:
# GET /projects/:id/merge_requests/:merge_request_id/commits
#
get "#{path}/commits" do get "#{path}/commits" do
merge_request = user_project.merge_requests. merge_request = user_project.merge_requests.find(params[:merge_request_id])
find(params[:merge_request_id])
authorize! :read_merge_request, merge_request authorize! :read_merge_request, merge_request
present merge_request.commits, with: Entities::RepoCommit present merge_request.commits, with: Entities::RepoCommit
end end
# Show MR changes desc 'Show the merge request changes' do
# success Entities::MergeRequestChanges
# Parameters: end
# id (required) - The ID of a project
# merge_request_id (required) - The ID of MR
#
# Example:
# GET /projects/:id/merge_requests/:merge_request_id/changes
#
get "#{path}/changes" do get "#{path}/changes" do
merge_request = user_project.merge_requests. merge_request = user_project.merge_requests.find(params[:merge_request_id])
find(params[:merge_request_id])
authorize! :read_merge_request, merge_request authorize! :read_merge_request, merge_request
present merge_request, with: Entities::MergeRequestChanges, current_user: current_user present merge_request, with: Entities::MergeRequestChanges, current_user: current_user
end end
# Update MR desc 'Update a merge request' do
# success Entities::MergeRequest
# Parameters: end
# id (required) - The ID of a project params do
# merge_request_id (required) - ID of MR optional :title, type: String, desc: 'The title of the merge request'
# target_branch - The target branch optional :target_branch, type: String, desc: 'The target branch'
# assignee_id - Assignee user ID optional :state_event, type: String, values: %w[close reopen merge],
# title - Title of MR desc: 'Status of the merge request'
# state_event - Status of MR. (close|reopen|merge) use :optional_params
# description - Description of MR at_least_one_of :title, :target_branch, :description, :assignee_id,
# labels (optional) - Labels for a MR as a comma-separated list :milestone_id, :labels, :state_event
# milestone_id (optional) - Milestone ID end
# Example:
# PUT /projects/:id/merge_requests/:merge_request_id
#
put path do put path do
attrs = attributes_for_keys [:target_branch, :assignee_id, :title, :state_event, :description, :milestone_id] merge_request = user_project.merge_requests.find(params.delete(:merge_request_id))
merge_request = user_project.merge_requests.find(params[:merge_request_id])
authorize! :update_merge_request, merge_request authorize! :update_merge_request, merge_request
# Ensure source_branch is not specified mr_params = declared_params(include_missing: false)
if params[:source_branch].present?
render_api_error!('Source branch cannot be changed', 400)
end
# Validate label names in advance # Validate label names in advance
if (errors = validate_label_params(params)).any? if (errors = validate_label_params(mr_params)).any?
render_api_error!({ labels: errors }, 400) render_api_error!({ labels: errors }, 400)
end end
attrs[:labels] = params[:labels] if params[:labels] merge_request = ::MergeRequests::UpdateService.new(user_project, current_user, mr_params).execute(merge_request)
merge_request = ::MergeRequests::UpdateService.new(user_project, current_user, attrs).execute(merge_request)
if merge_request.valid? if merge_request.valid?
present merge_request, with: Entities::MergeRequest, current_user: current_user present merge_request, with: Entities::MergeRequest, current_user: current_user
...@@ -203,18 +173,17 @@ module API ...@@ -203,18 +173,17 @@ module API
end end
end end
# Merge MR desc 'Merge a merge request' do
# success Entities::MergeRequest
# Parameters: end
# id (required) - The ID of a project params do
# merge_request_id (required) - ID of MR optional :merge_commit_message, type: String, desc: 'Custom merge commit message'
# merge_commit_message (optional) - Custom merge commit message optional :should_remove_source_branch, type: Boolean,
# should_remove_source_branch (optional) - When true, the source branch will be deleted if possible desc: 'When true, the source branch will be deleted if possible'
# merge_when_build_succeeds (optional) - When true, this MR will be merged when the build succeeds optional :merge_when_build_succeeds, type: Boolean,
# sha (optional) - When present, must have the HEAD SHA of the source branch desc: 'When true, this merge request will be merged when the build succeeds'
# Example: optional :sha, type: String, desc: 'When present, must have the HEAD SHA of the source branch'
# PUT /projects/:id/merge_requests/:merge_request_id/merge end
#
put "#{path}/merge" do put "#{path}/merge" do
merge_request = user_project.merge_requests.find(params[:merge_request_id]) merge_request = user_project.merge_requests.find(params[:merge_request_id])
...@@ -235,7 +204,7 @@ module API ...@@ -235,7 +204,7 @@ module API
should_remove_source_branch: params[:should_remove_source_branch] should_remove_source_branch: params[:should_remove_source_branch]
} }
if to_boolean(params[:merge_when_build_succeeds]) && merge_request.pipeline && merge_request.pipeline.active? if params[:merge_when_build_succeeds] && merge_request.pipeline && merge_request.pipeline.active?
::MergeRequests::MergeWhenBuildSucceedsService.new(merge_request.target_project, current_user, merge_params). ::MergeRequests::MergeWhenBuildSucceedsService.new(merge_request.target_project, current_user, merge_params).
execute(merge_request) execute(merge_request)
else else
...@@ -246,11 +215,9 @@ module API ...@@ -246,11 +215,9 @@ module API
present merge_request, with: Entities::MergeRequest, current_user: current_user present merge_request, with: Entities::MergeRequest, current_user: current_user
end end
# Cancel Merge if Merge When build succeeds is enabled desc 'Cancel merge if "Merge when build succeeds" is enabled' do
# Parameters: success Entities::MergeRequest
# id (required) - The ID of a project end
# merge_request_id (required) - ID of MR
#
post "#{path}/cancel_merge_when_build_succeeds" do post "#{path}/cancel_merge_when_build_succeeds" do
merge_request = user_project.merge_requests.find(params[:merge_request_id]) merge_request = user_project.merge_requests.find(params[:merge_request_id])
...@@ -259,17 +226,10 @@ module API ...@@ -259,17 +226,10 @@ module API
::MergeRequest::MergeWhenBuildSucceedsService.new(merge_request.target_project, current_user).cancel(merge_request) ::MergeRequest::MergeWhenBuildSucceedsService.new(merge_request.target_project, current_user).cancel(merge_request)
end end
# Duplicate. DEPRECATED and WILL BE REMOVED in 9.0. desc 'Get the comments of a merge request' do
# Use GET "/projects/:id/merge_requests/:merge_request_id/notes" instead detail 'Duplicate. DEPRECATED and WILL BE REMOVED in 9.0'
# success Entities::MRNote
# Get a merge request's comments end
#
# Parameters:
# id (required) - The ID of a project
# merge_request_id (required) - ID of MR
# Examples:
# GET /projects/:id/merge_requests/:merge_request_id/comments
#
get "#{path}/comments" do get "#{path}/comments" do
merge_request = user_project.merge_requests.find(params[:merge_request_id]) merge_request = user_project.merge_requests.find(params[:merge_request_id])
...@@ -278,23 +238,15 @@ module API ...@@ -278,23 +238,15 @@ module API
present paginate(merge_request.notes.fresh), with: Entities::MRNote present paginate(merge_request.notes.fresh), with: Entities::MRNote
end end
# Duplicate. DEPRECATED and WILL BE REMOVED in 9.0. desc 'Post a comment to a merge request' do
# Use POST "/projects/:id/merge_requests/:merge_request_id/notes" instead detail 'Duplicate. DEPRECATED and WILL BE REMOVED in 9.0'
# success Entities::MRNote
# Post comment to merge request end
# params do
# Parameters: requires :note, type: String, desc: 'The text of the comment'
# id (required) - The ID of a project end
# merge_request_id (required) - ID of MR
# note (required) - Text of comment
# Examples:
# POST /projects/:id/merge_requests/:merge_request_id/comments
#
post "#{path}/comments" do post "#{path}/comments" do
required_attributes! [:note]
merge_request = user_project.merge_requests.find(params[:merge_request_id]) merge_request = user_project.merge_requests.find(params[:merge_request_id])
authorize! :create_note, merge_request authorize! :create_note, merge_request
opts = { opts = {
...@@ -312,13 +264,9 @@ module API ...@@ -312,13 +264,9 @@ module API
end end
end end
# List issues that will close on merge desc 'List issues that will be closed on merge' do
# success Entities::MRNote
# Parameters: end
# id (required) - The ID of a project
# merge_request_id (required) - ID of MR
# Examples:
# GET /projects/:id/merge_requests/:merge_request_id/closes_issues
get "#{path}/closes_issues" do get "#{path}/closes_issues" do
merge_request = user_project.merge_requests.find(params[:merge_request_id]) merge_request = user_project.merge_requests.find(params[:merge_request_id])
issues = ::Kaminari.paginate_array(merge_request.closes_issues(current_user)) issues = ::Kaminari.paginate_array(merge_request.closes_issues(current_user))
......
...@@ -494,12 +494,6 @@ describe API::API, api: true do ...@@ -494,12 +494,6 @@ describe API::API, api: true do
expect(json_response['milestone']['id']).to eq(milestone.id) expect(json_response['milestone']['id']).to eq(milestone.id)
end end
it "returns 400 when source_branch is specified" do
put api("/projects/#{project.id}/merge_requests/#{merge_request.id}", user),
source_branch: "master", target_branch: "master"
expect(response).to have_http_status(400)
end
it "returns merge_request with renamed target_branch" do it "returns merge_request with renamed target_branch" do
put api("/projects/#{project.id}/merge_requests/#{merge_request.id}", user), target_branch: "wiki" put api("/projects/#{project.id}/merge_requests/#{merge_request.id}", user), target_branch: "wiki"
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
......
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