2018-07-05 06:18:17 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-11-16 06:09:09 -05:00
|
|
|
class PreviewMarkdownService < BaseService
|
|
|
|
def execute
|
2017-05-31 01:50:53 -04:00
|
|
|
text, commands = explain_quick_actions(params[:text])
|
2016-11-16 06:09:09 -05:00
|
|
|
users = find_user_references(text)
|
|
|
|
|
|
|
|
success(
|
|
|
|
text: text,
|
|
|
|
users: users,
|
2018-07-06 14:49:33 -04:00
|
|
|
commands: commands.join(' '),
|
|
|
|
markdown_engine: markdown_engine
|
2016-11-16 06:09:09 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-05-31 01:50:53 -04:00
|
|
|
def explain_quick_actions(text)
|
2018-07-28 17:43:21 -04:00
|
|
|
return text, [] unless %w(Issue MergeRequest Commit).include?(commands_target_type)
|
2016-11-16 06:09:09 -05:00
|
|
|
|
2017-05-31 01:50:53 -04:00
|
|
|
quick_actions_service = QuickActions::InterpretService.new(project, current_user)
|
|
|
|
quick_actions_service.explain(text, find_commands_target)
|
2016-11-16 06:09:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def find_user_references(text)
|
|
|
|
extractor = Gitlab::ReferenceExtractor.new(project, current_user)
|
|
|
|
extractor.analyze(text, author: current_user)
|
|
|
|
extractor.users.map(&:username)
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_commands_target
|
2018-08-09 05:04:12 -04:00
|
|
|
QuickActions::TargetService
|
2018-07-28 17:43:21 -04:00
|
|
|
.new(project, current_user)
|
2018-08-09 05:04:12 -04:00
|
|
|
.execute(commands_target_type, commands_target_id)
|
2016-11-16 06:09:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def commands_target_type
|
2017-05-31 01:50:53 -04:00
|
|
|
params[:quick_actions_target_type]
|
2016-11-16 06:09:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def commands_target_id
|
2017-05-31 01:50:53 -04:00
|
|
|
params[:quick_actions_target_id]
|
2016-11-16 06:09:09 -05:00
|
|
|
end
|
2018-07-06 14:49:33 -04:00
|
|
|
|
|
|
|
def markdown_engine
|
2018-09-02 11:24:27 -04:00
|
|
|
if params[:legacy_render]
|
|
|
|
:redcarpet
|
|
|
|
else
|
|
|
|
CacheMarkdownField::MarkdownEngine.from_version(params[:markdown_version].to_i)
|
|
|
|
end
|
2018-07-06 14:49:33 -04:00
|
|
|
end
|
2016-11-16 06:09:09 -05:00
|
|
|
end
|