debian-mirror-gitlab/lib/api/suggestions.rb

65 lines
2 KiB
Ruby
Raw Normal View History

2019-02-15 15:39:39 +05:30
# frozen_string_literal: true
module API
2020-07-28 23:09:34 +05:30
class Suggestions < Grape::API::Instance
2019-02-15 15:39:39 +05:30
before { authenticate! }
resource :suggestions do
desc 'Apply suggestion patch in the Merge Request it was created' do
success Entities::Suggestion
end
params do
requires :id, type: String, desc: 'The suggestion ID'
end
put ':id/apply' do
suggestion = Suggestion.find_by_id(params[:id])
2020-06-23 00:09:42 +05:30
if suggestion
apply_suggestions(suggestion, current_user)
else
render_api_error!(_('Suggestion is not applicable as the suggestion was not found.'), :not_found)
end
end
desc 'Apply multiple suggestion patches in the Merge Request where they were created' do
success Entities::Suggestion
end
params do
2020-07-28 23:09:34 +05:30
requires :ids, type: Array[Integer], coerce_with: ::API::Validations::Types::CommaSeparatedToIntegerArray.coerce, desc: "An array of suggestion ID's"
2020-06-23 00:09:42 +05:30
end
put 'batch_apply' do
ids = params[:ids]
suggestions = Suggestion.id_in(ids)
2019-02-15 15:39:39 +05:30
2020-06-23 00:09:42 +05:30
if suggestions.size == ids.length
apply_suggestions(suggestions, current_user)
else
render_api_error!(_('Suggestions are not applicable as one or more suggestions were not found.'), :not_found)
end
end
end
helpers do
def apply_suggestions(suggestions, current_user)
authorize_suggestions(*suggestions)
result = ::Suggestions::ApplyService.new(current_user, *suggestions).execute
2019-02-15 15:39:39 +05:30
if result[:status] == :success
2020-06-23 00:09:42 +05:30
present suggestions, with: Entities::Suggestion, current_user: current_user
2019-02-15 15:39:39 +05:30
else
2020-06-23 00:09:42 +05:30
http_status = result[:http_status] || :bad_request
2019-02-15 15:39:39 +05:30
render_api_error!(result[:message], http_status)
end
end
2020-06-23 00:09:42 +05:30
def authorize_suggestions(*suggestions)
suggestions.each do |suggestion|
authorize! :apply_suggestion, suggestion
end
end
2019-02-15 15:39:39 +05:30
end
end
end