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

64 lines
1.7 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
2016-09-29 09:46:39 +05:30
def self.noteable_update_service(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)
!!noteable_update_service(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
2019-12-21 20:55:43 +05:30
self.class.noteable_update_service(note).new(note.resource_parent, current_user, update_params).execute(note.noteable)
2016-09-13 17:45:13 +05:30
end
end
end
2019-12-04 20:38:33 +05:30
Notes::QuickActionsService.prepend_if_ee('EE::Notes::QuickActionsService')