debian-mirror-gitlab/app/services/notes/quick_actions_service.rb

78 lines
2.5 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2019-07-07 11:18:12 +05:30
# QuickActionsService class
#
# Executes quick actions commands extracted from note text
#
# Most commands returns parameters to be applied later
# using QuickActionService#apply_updates
#
2016-09-13 17:45:13 +05:30
module Notes
2017-09-10 17:25:29 +05:30
class QuickActionsService < BaseService
2019-07-07 11:18:12 +05:30
attr_reader :interpret_service
delegate :commands_executed_count, to: :interpret_service, allow_nil: true
2016-09-13 17:45:13 +05:30
UPDATE_SERVICES = {
'Issue' => Issues::UpdateService,
2018-11-20 20:47:30 +05:30
'MergeRequest' => MergeRequests::UpdateService,
'Commit' => Commits::TagService
2017-08-17 22:00:37 +05:30
}.freeze
2019-07-07 11:18:12 +05:30
private_constant :UPDATE_SERVICES
def self.update_services
UPDATE_SERVICES
end
2016-09-13 17:45:13 +05:30
2021-06-08 01:23:25 +05:30
def self.noteable_update_service_class(note)
2019-07-07 11:18:12 +05:30
update_services[note.noteable_type]
2016-09-29 09:46:39 +05:30
end
2018-03-27 19:54:05 +05:30
def self.supported?(note)
2021-06-08 01:23:25 +05:30
!!noteable_update_service_class(note)
2016-09-29 09:46:39 +05:30
end
def supported?(note)
2018-03-27 19:54:05 +05:30
self.class.supported?(note)
2016-09-13 17:45:13 +05:30
end
2019-07-07 11:18:12 +05:30
def execute(note, options = {})
2016-09-13 17:45:13 +05:30
return [note.note, {}] unless supported?(note)
2019-07-07 11:18:12 +05:30
@interpret_service = QuickActions::InterpretService.new(project, current_user, options)
2020-07-28 23:09:34 +05:30
interpret_service.execute(note.note, note.noteable)
2016-09-13 17:45:13 +05:30
end
2019-07-07 11:18:12 +05:30
# Applies updates extracted to note#noteable
# The update parameters are extracted on self#execute
def apply_updates(update_params, note)
return if update_params.empty?
2016-09-13 17:45:13 +05:30
return unless supported?(note)
2020-10-24 23:57:45 +05:30
# We need the `id` after the note is persisted
if update_params[:spend_time]
update_params[:spend_time][:note_id] = note.id
end
2021-06-08 01:23:25 +05:30
noteable_update_service_class = self.class.noteable_update_service_class(note)
# TODO: This conditional is necessary because we have not fully converted all possible
# noteable_update_service_class classes to use named arguments. See more details
# on the partial conversion at https://gitlab.com/gitlab-org/gitlab/-/merge_requests/59182
# Follow-on issue to address this is here:
# https://gitlab.com/gitlab-org/gitlab/-/issues/328734
service =
if noteable_update_service_class.respond_to?(:constructor_container_arg)
noteable_update_service_class.new(**noteable_update_service_class.constructor_container_arg(note.resource_parent), current_user: current_user, params: update_params)
else
noteable_update_service_class.new(note.resource_parent, current_user, update_params)
end
service.execute(note.noteable)
2016-09-13 17:45:13 +05:30
end
end
end
2019-12-04 20:38:33 +05:30
2021-06-08 01:23:25 +05:30
Notes::QuickActionsService.prepend_mod_with('Notes::QuickActionsService')