diff options
Diffstat (limited to 'lib/api/suggestions.rb')
-rw-r--r-- | lib/api/suggestions.rb | 43 |
1 files changed, 38 insertions, 5 deletions
diff --git a/lib/api/suggestions.rb b/lib/api/suggestions.rb index d008d1b9e97..05aaa8a6f41 100644 --- a/lib/api/suggestions.rb +++ b/lib/api/suggestions.rb @@ -14,18 +14,51 @@ module API put ':id/apply' do suggestion = Suggestion.find_by_id(params[:id]) - not_found! unless suggestion - authorize! :apply_suggestion, suggestion + 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 + requires :ids, type: Array[String], desc: "An array of suggestion ID's" + end + put 'batch_apply' do + ids = params[:ids] + + suggestions = Suggestion.id_in(ids) - result = ::Suggestions::ApplyService.new(current_user).execute(suggestion) + 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 if result[:status] == :success - present suggestion, with: Entities::Suggestion, current_user: current_user + present suggestions, with: Entities::Suggestion, current_user: current_user else - http_status = result[:http_status] || 400 + http_status = result[:http_status] || :bad_request render_api_error!(result[:message], http_status) end end + + def authorize_suggestions(*suggestions) + suggestions.each do |suggestion| + authorize! :apply_suggestion, suggestion + end + end end end end |