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)
|
2019-02-15 15:39:39 +05:30
|
|
|
suggestions = find_suggestions(text)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
success(
|
|
|
|
text: text,
|
|
|
|
users: users,
|
2019-02-15 15:39:39 +05:30
|
|
|
suggestions: suggestions,
|
2022-08-13 15:12:31 +05:30
|
|
|
commands: commands.join('<br>')
|
2017-08-17 22:00:37 +05:30
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-12-26 22:10:19 +05:30
|
|
|
def quick_action_types
|
2023-04-23 21:23:45 +05:30
|
|
|
%w(Issue MergeRequest Commit WorkItem)
|
2019-12-26 22:10:19 +05:30
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
def explain_quick_actions(text)
|
2019-12-26 22:10:19 +05:30
|
|
|
return text, [] unless quick_action_types.include?(target_type)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
quick_actions_service = QuickActions::InterpretService.new(project, current_user)
|
2023-07-09 08:55:56 +05:30
|
|
|
quick_actions_service.explain(text, find_commands_target, keep_actions: params[:render_quick_actions])
|
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
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
def find_suggestions(text)
|
2019-07-07 11:18:12 +05:30
|
|
|
return [] unless preview_sugestions?
|
2019-02-15 15:39:39 +05:30
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
position = Gitlab::Diff::Position.new(new_path: params[:file_path],
|
|
|
|
new_line: params[:line].to_i,
|
|
|
|
base_sha: params[:base_sha],
|
|
|
|
head_sha: params[:head_sha],
|
|
|
|
start_sha: params[:start_sha])
|
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
Gitlab::Diff::SuggestionsParser.parse(text, position: position,
|
|
|
|
project: project,
|
|
|
|
supports_suggestion: params[:preview_suggestions])
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def preview_sugestions?
|
|
|
|
params[:preview_suggestions] &&
|
|
|
|
target_type == 'MergeRequest' &&
|
|
|
|
Ability.allowed?(current_user, :download_code, project)
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def find_commands_target
|
2018-11-20 20:47:30 +05:30
|
|
|
QuickActions::TargetService
|
2019-12-26 22:10:19 +05:30
|
|
|
.new(project, current_user, group: params[:group])
|
2019-07-07 11:18:12 +05:30
|
|
|
.execute(target_type, target_id)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
def target_type
|
|
|
|
params[:target_type]
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
def target_id
|
|
|
|
params[:target_id]
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
2019-12-26 22:10:19 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
PreviewMarkdownService.prepend_mod_with('PreviewMarkdownService')
|