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
|
|
|
|
|
2023-07-09 08:55:56 +05:30
|
|
|
SUPPORTED_NOTEABLES = %w[WorkItem Issue MergeRequest Commit].freeze
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2023-07-09 08:55:56 +05:30
|
|
|
private_constant :SUPPORTED_NOTEABLES
|
2023-05-27 22:25:52 +05:30
|
|
|
|
2023-07-09 08:55:56 +05:30
|
|
|
def self.supported_noteables
|
|
|
|
SUPPORTED_NOTEABLES
|
2016-09-29 09:46:39 +05:30
|
|
|
end
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
def self.supported?(note)
|
2023-07-09 08:55:56 +05:30
|
|
|
return true if note.for_work_item?
|
|
|
|
|
|
|
|
supported_noteables.include? note.noteable_type
|
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
|
|
|
|
|
2023-07-09 08:55:56 +05:30
|
|
|
noteable_update_service(note, update_params).execute(note.noteable)
|
|
|
|
end
|
|
|
|
|
|
|
|
def noteable_update_service(note, update_params)
|
|
|
|
if note.for_work_item?
|
|
|
|
parsed_params = note.noteable.transform_quick_action_params(update_params)
|
|
|
|
|
|
|
|
WorkItems::UpdateService.new(
|
|
|
|
container: note.resource_parent,
|
|
|
|
current_user: current_user,
|
|
|
|
params: parsed_params[:common],
|
|
|
|
widget_params: parsed_params[:widgets]
|
|
|
|
)
|
|
|
|
elsif note.for_issue?
|
|
|
|
Issues::UpdateService.new(container: note.resource_parent, current_user: current_user, params: update_params)
|
|
|
|
elsif note.for_merge_request?
|
|
|
|
MergeRequests::UpdateService.new(
|
|
|
|
project: note.resource_parent, current_user: current_user, params: update_params
|
|
|
|
)
|
|
|
|
elsif note.for_commit?
|
|
|
|
Commits::TagService.new(note.resource_parent, current_user, update_params)
|
|
|
|
end
|
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')
|