debian-mirror-gitlab/app/services/preview_markdown_service.rb

53 lines
1.4 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
class PreviewMarkdownService < BaseService
def execute
2017-09-10 17:25:29 +05:30
text, commands = explain_quick_actions(params[:text])
2017-08-17 22:00:37 +05:30
users = find_user_references(text)
success(
text: text,
users: users,
2018-11-08 19:23:39 +05:30
commands: commands.join(' '),
markdown_engine: markdown_engine
2017-08-17 22:00:37 +05:30
)
end
private
2017-09-10 17:25:29 +05:30
def explain_quick_actions(text)
2017-08-17 22:00:37 +05:30
return text, [] unless %w(Issue MergeRequest).include?(commands_target_type)
2017-09-10 17:25:29 +05:30
quick_actions_service = QuickActions::InterpretService.new(project, current_user)
quick_actions_service.explain(text, find_commands_target)
2017-08-17 22:00:37 +05:30
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
if commands_target_id.present?
finder = commands_target_type == 'Issue' ? IssuesFinder : MergeRequestsFinder
finder.new(current_user, project_id: project.id).find(commands_target_id)
else
collection = commands_target_type == 'Issue' ? project.issues : project.merge_requests
collection.build
end
end
def commands_target_type
2017-09-10 17:25:29 +05:30
params[:quick_actions_target_type]
2017-08-17 22:00:37 +05:30
end
def commands_target_id
2017-09-10 17:25:29 +05:30
params[:quick_actions_target_id]
2017-08-17 22:00:37 +05:30
end
2018-11-08 19:23:39 +05:30
def markdown_engine
CacheMarkdownField::MarkdownEngine.from_version(params[:markdown_version].to_i)
end
2017-08-17 22:00:37 +05:30
end