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)
|
2018-12-13 14:17:19 -05:00
|
|
|
suggestions = find_suggestions(text)
|
2016-11-16 06:09:09 -05:00
|
|
|
|
|
|
|
success(
|
|
|
|
text: text,
|
|
|
|
users: users,
|
2018-12-13 14:17:19 -05:00
|
|
|
suggestions: suggestions,
|
2019-01-31 03:58:23 -05:00
|
|
|
commands: commands.join(' ')
|
2016-11-16 06:09:09 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-05-31 01:50:53 -04:00
|
|
|
def explain_quick_actions(text)
|
2019-04-04 09:08:34 -04:00
|
|
|
return text, [] unless %w(Issue MergeRequest Commit).include?(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
|
|
|
|
|
2018-12-13 14:17:19 -05:00
|
|
|
def find_suggestions(text)
|
2019-04-04 09:08:34 -04:00
|
|
|
return [] unless preview_sugestions?
|
2018-12-13 14:17:19 -05:00
|
|
|
|
2019-04-04 09:08:34 -04:00
|
|
|
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])
|
|
|
|
|
|
|
|
Gitlab::Diff::SuggestionsParser.parse(text, position: position, project: project)
|
|
|
|
end
|
|
|
|
|
|
|
|
def preview_sugestions?
|
|
|
|
params[:preview_suggestions] &&
|
|
|
|
target_type == 'MergeRequest' &&
|
|
|
|
Ability.allowed?(current_user, :download_code, project)
|
2018-12-13 14:17:19 -05:00
|
|
|
end
|
|
|
|
|
2016-11-16 06:09:09 -05:00
|
|
|
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)
|
2019-04-04 09:08:34 -04:00
|
|
|
.execute(target_type, target_id)
|
2016-11-16 06:09:09 -05:00
|
|
|
end
|
|
|
|
|
2019-04-04 09:08:34 -04:00
|
|
|
def target_type
|
|
|
|
params[:target_type]
|
2016-11-16 06:09:09 -05:00
|
|
|
end
|
|
|
|
|
2019-04-04 09:08:34 -04:00
|
|
|
def target_id
|
|
|
|
params[:target_id]
|
2016-11-16 06:09:09 -05:00
|
|
|
end
|
|
|
|
end
|