2018-12-13 14:17:19 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module API
|
2020-10-14 20:08:42 -04:00
|
|
|
class Suggestions < ::API::Base
|
2018-12-13 14:17:19 -05:00
|
|
|
before { authenticate! }
|
|
|
|
|
2020-10-30 14:08:56 -04:00
|
|
|
feature_category :code_review
|
|
|
|
|
2018-12-13 14:17:19 -05:00
|
|
|
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'
|
2021-01-26 10:08:58 -05:00
|
|
|
optional :commit_message, type: String, desc: "A custom commit message to use instead of the default generated message or the project's default message"
|
2018-12-13 14:17:19 -05:00
|
|
|
end
|
2021-12-08 07:13:04 -05:00
|
|
|
put ':id/apply', urgency: :low do
|
2018-12-13 14:17:19 -05:00
|
|
|
suggestion = Suggestion.find_by_id(params[:id])
|
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
if suggestion
|
2021-01-26 10:08:58 -05:00
|
|
|
apply_suggestions(suggestion, current_user, params[:commit_message])
|
2020-06-08 20:08:47 -04:00
|
|
|
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-06-29 17:09:07 -04:00
|
|
|
requires :ids, type: Array[Integer], coerce_with: ::API::Validations::Types::CommaSeparatedToIntegerArray.coerce, desc: "An array of suggestion ID's"
|
2021-01-26 10:08:58 -05:00
|
|
|
optional :commit_message, type: String, desc: "A custom commit message to use instead of the default generated message or the project's default message"
|
2020-06-08 20:08:47 -04:00
|
|
|
end
|
2021-12-08 07:13:04 -05:00
|
|
|
put 'batch_apply', urgency: :low do
|
2020-06-08 20:08:47 -04:00
|
|
|
ids = params[:ids]
|
|
|
|
|
|
|
|
suggestions = Suggestion.id_in(ids)
|
2018-12-13 14:17:19 -05:00
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
if suggestions.size == ids.length
|
2021-01-26 10:08:58 -05:00
|
|
|
apply_suggestions(suggestions, current_user, params[:commit_message])
|
2020-06-08 20:08:47 -04:00
|
|
|
else
|
|
|
|
render_api_error!(_('Suggestions are not applicable as one or more suggestions were not found.'), :not_found)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
helpers do
|
2021-01-26 10:08:58 -05:00
|
|
|
def apply_suggestions(suggestions, current_user, message)
|
2020-06-08 20:08:47 -04:00
|
|
|
authorize_suggestions(*suggestions)
|
|
|
|
|
2021-01-26 10:08:58 -05:00
|
|
|
result = ::Suggestions::ApplyService.new(current_user, *suggestions, message: message).execute
|
2018-12-13 14:17:19 -05:00
|
|
|
|
|
|
|
if result[:status] == :success
|
2020-06-08 20:08:47 -04:00
|
|
|
present suggestions, with: Entities::Suggestion, current_user: current_user
|
2018-12-13 14:17:19 -05:00
|
|
|
else
|
2020-06-08 20:08:47 -04:00
|
|
|
http_status = result[:http_status] || :bad_request
|
2018-12-13 14:17:19 -05:00
|
|
|
render_api_error!(result[:message], http_status)
|
|
|
|
end
|
|
|
|
end
|
2020-06-08 20:08:47 -04:00
|
|
|
|
|
|
|
def authorize_suggestions(*suggestions)
|
|
|
|
suggestions.each do |suggestion|
|
|
|
|
authorize! :apply_suggestion, suggestion
|
|
|
|
end
|
|
|
|
end
|
2018-12-13 14:17:19 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|