2018-07-16 12:31:01 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-10-26 17:21:50 -04:00
|
|
|
module Issues
|
|
|
|
class BuildService < Issues::BaseService
|
2017-02-27 12:12:52 -05:00
|
|
|
include ResolveDiscussions
|
|
|
|
|
2016-10-26 17:21:50 -04:00
|
|
|
def execute
|
2017-02-27 12:12:52 -05:00
|
|
|
filter_resolve_discussion_params
|
2021-09-10 11:11:12 -04:00
|
|
|
|
2021-12-24 13:13:33 -05:00
|
|
|
@issue = model_klass.new(issue_params.merge(project: project)).tap do |issue|
|
2019-01-14 05:46:39 -05:00
|
|
|
ensure_milestone_available(issue)
|
|
|
|
end
|
2016-10-26 17:21:50 -04:00
|
|
|
end
|
|
|
|
|
2016-12-22 07:31:12 -05:00
|
|
|
def issue_params_with_info_from_discussions
|
2017-03-10 03:19:12 -05:00
|
|
|
return {} unless merge_request_to_resolve_discussions_of
|
2016-10-26 17:21:50 -04:00
|
|
|
|
2017-03-07 18:00:21 -05:00
|
|
|
{ title: title_from_merge_request, description: description_for_discussions }
|
2016-10-26 17:21:50 -04:00
|
|
|
end
|
|
|
|
|
2017-03-07 18:00:21 -05:00
|
|
|
def title_from_merge_request
|
2017-03-10 03:19:12 -05:00
|
|
|
"Follow-up from \"#{merge_request_to_resolve_discussions_of.title}\""
|
2016-10-26 17:21:50 -04:00
|
|
|
end
|
|
|
|
|
2016-12-22 07:31:12 -05:00
|
|
|
def description_for_discussions
|
|
|
|
if discussions_to_resolve.empty?
|
2016-10-26 17:21:50 -04:00
|
|
|
return "There are no unresolved discussions. "\
|
2017-03-10 03:19:12 -05:00
|
|
|
"Review the conversation in #{merge_request_to_resolve_discussions_of.to_reference}"
|
2016-10-26 17:21:50 -04:00
|
|
|
end
|
|
|
|
|
2016-12-22 07:31:12 -05:00
|
|
|
description = "The following #{'discussion'.pluralize(discussions_to_resolve.size)} "\
|
2017-03-10 03:19:12 -05:00
|
|
|
"from #{merge_request_to_resolve_discussions_of.to_reference} "\
|
2016-12-22 07:31:12 -05:00
|
|
|
"should be addressed:"
|
|
|
|
|
2016-10-26 17:21:50 -04:00
|
|
|
[description, *items_for_discussions].join("\n\n")
|
|
|
|
end
|
|
|
|
|
|
|
|
def items_for_discussions
|
2016-12-22 07:31:12 -05:00
|
|
|
discussions_to_resolve.map { |discussion| item_for_discussion(discussion) }
|
2016-10-26 17:21:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def item_for_discussion(discussion)
|
2017-04-05 21:48:26 -04:00
|
|
|
first_note_to_resolve = discussion.first_note_to_resolve || discussion.first_note
|
2016-10-26 17:21:50 -04:00
|
|
|
|
2017-04-05 21:48:26 -04:00
|
|
|
is_very_first_note = first_note_to_resolve == discussion.first_note
|
|
|
|
action = is_very_first_note ? "started" : "commented on"
|
2017-04-06 11:05:57 -04:00
|
|
|
|
|
|
|
note_url = Gitlab::UrlBuilder.build(first_note_to_resolve)
|
|
|
|
|
|
|
|
other_note_count = discussion.notes.size - 1
|
|
|
|
|
2018-07-16 12:31:01 -04:00
|
|
|
discussion_info = ["- [ ] #{first_note_to_resolve.author.to_reference} #{action} a [discussion](#{note_url}): "]
|
|
|
|
discussion_info << "(+#{other_note_count} #{'comment'.pluralize(other_note_count)})" if other_note_count > 0
|
2016-10-26 17:21:50 -04:00
|
|
|
|
2017-04-05 21:48:26 -04:00
|
|
|
note_without_block_quotes = Banzai::Filter::BlockquoteFenceFilter.new(first_note_to_resolve.note).call
|
2017-03-07 18:55:50 -05:00
|
|
|
spaces = ' ' * 4
|
|
|
|
quote = note_without_block_quotes.lines.map { |line| "#{spaces}> #{line}" }.join
|
2016-10-26 17:21:50 -04:00
|
|
|
|
2018-07-16 12:31:01 -04:00
|
|
|
[discussion_info.join(' '), quote].join("\n\n")
|
2016-10-26 17:21:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def issue_params
|
2019-02-06 07:41:17 -05:00
|
|
|
@issue_params ||= build_issue_params
|
2021-09-10 11:11:12 -04:00
|
|
|
|
2021-12-24 13:13:33 -05:00
|
|
|
if @issue_params[:work_item_type].present?
|
|
|
|
@issue_params[:issue_type] = @issue_params[:work_item_type].base_type
|
|
|
|
else
|
|
|
|
# If :issue_type is nil then params[:issue_type] was either nil
|
|
|
|
# or not permitted. Either way, the :issue_type will default
|
|
|
|
# to the column default of `issue`. And that means we need to
|
|
|
|
# ensure the work_item_type_id is set
|
|
|
|
@issue_params[:work_item_type_id] = get_work_item_type_id(@issue_params[:issue_type])
|
|
|
|
end
|
|
|
|
|
2021-09-10 11:11:12 -04:00
|
|
|
@issue_params
|
2017-01-28 19:14:56 -05:00
|
|
|
end
|
|
|
|
|
2019-02-06 07:41:17 -05:00
|
|
|
private
|
|
|
|
|
2021-12-24 13:13:33 -05:00
|
|
|
def model_klass
|
|
|
|
::Issue
|
|
|
|
end
|
|
|
|
|
2022-07-04 11:10:24 -04:00
|
|
|
def public_params
|
|
|
|
# Additional params may be assigned later (in a CreateService for example)
|
|
|
|
public_issue_params = [
|
2021-05-14 08:10:58 -04:00
|
|
|
:title,
|
|
|
|
:description,
|
|
|
|
:confidential
|
|
|
|
]
|
2020-10-20 05:08:43 -04:00
|
|
|
|
2021-12-24 13:13:33 -05:00
|
|
|
params[:work_item_type] = WorkItems::Type.find_by(id: params[:work_item_type_id]) if params[:work_item_type_id].present? # rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
|
2022-07-04 11:10:24 -04:00
|
|
|
public_issue_params << :milestone_id if can?(current_user, :admin_issue, project)
|
|
|
|
public_issue_params << :issue_type if create_issue_type_allowed?(project, params[:issue_type])
|
|
|
|
public_issue_params << :work_item_type if create_issue_type_allowed?(project, params[:work_item_type]&.base_type)
|
2020-05-08 05:09:39 -04:00
|
|
|
|
2022-07-04 11:10:24 -04:00
|
|
|
params.slice(*public_issue_params)
|
2016-10-26 17:21:50 -04:00
|
|
|
end
|
2019-02-06 07:41:17 -05:00
|
|
|
|
|
|
|
def build_issue_params
|
2020-10-20 05:08:43 -04:00
|
|
|
{ author: current_user }
|
|
|
|
.merge(issue_params_with_info_from_discussions)
|
2022-07-04 11:10:24 -04:00
|
|
|
.merge(public_params)
|
2021-09-10 11:11:12 -04:00
|
|
|
.with_indifferent_access
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_work_item_type_id(issue_type = :issue)
|
|
|
|
find_work_item_type_id(issue_type)
|
2019-02-06 07:41:17 -05:00
|
|
|
end
|
2016-10-26 17:21:50 -04:00
|
|
|
end
|
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
Issues::BuildService.prepend_mod_with('Issues::BuildService')
|