2018-12-13 14:17:19 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Suggestions
|
|
|
|
class ApplyService < ::BaseService
|
2020-06-08 20:08:47 -04:00
|
|
|
def initialize(current_user, *suggestions)
|
2018-12-13 14:17:19 -05:00
|
|
|
@current_user = current_user
|
2020-06-08 20:08:47 -04:00
|
|
|
@suggestion_set = Gitlab::Suggestions::SuggestionSet.new(suggestions)
|
2018-12-13 14:17:19 -05:00
|
|
|
end
|
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
def execute
|
|
|
|
if suggestion_set.valid?
|
|
|
|
result
|
|
|
|
else
|
|
|
|
error(suggestion_set.error_message)
|
2019-01-10 07:38:15 -05:00
|
|
|
end
|
2020-06-08 20:08:47 -04:00
|
|
|
end
|
2019-01-10 07:38:15 -05:00
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
private
|
2019-02-20 16:20:08 -05:00
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
attr_reader :current_user, :suggestion_set
|
2018-12-13 14:17:19 -05:00
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
def result
|
|
|
|
multi_service.execute.tap do |result|
|
|
|
|
update_suggestions(result)
|
2018-12-13 14:17:19 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
def update_suggestions(result)
|
|
|
|
return unless result[:status] == :success
|
2018-12-13 14:17:19 -05:00
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
Suggestion.id_in(suggestion_set.suggestions)
|
|
|
|
.update_all(commit_id: result[:result], applied: true)
|
2019-01-10 07:38:15 -05:00
|
|
|
end
|
2018-12-13 14:17:19 -05:00
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
def multi_service
|
|
|
|
params = {
|
2018-12-13 14:17:19 -05:00
|
|
|
commit_message: commit_message,
|
2020-06-08 20:08:47 -04:00
|
|
|
branch_name: suggestion_set.branch,
|
|
|
|
start_branch: suggestion_set.branch,
|
|
|
|
actions: suggestion_set.actions
|
2018-12-13 14:17:19 -05:00
|
|
|
}
|
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
::Files::MultiService.new(suggestion_set.project, current_user, params)
|
2020-01-13 07:08:04 -05:00
|
|
|
end
|
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
def commit_message
|
|
|
|
Gitlab::Suggestions::CommitMessage.new(current_user, suggestion_set).message
|
2020-01-13 07:08:04 -05:00
|
|
|
end
|
2018-12-13 14:17:19 -05:00
|
|
|
end
|
|
|
|
end
|