2020-08-20 20:10:44 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module IssuableLinks
|
|
|
|
class CreateService < BaseService
|
|
|
|
attr_reader :issuable, :current_user, :params
|
|
|
|
|
|
|
|
def initialize(issuable, user, params)
|
2021-04-19 17:09:27 -04:00
|
|
|
@issuable = issuable
|
|
|
|
@current_user = user
|
|
|
|
@params = params.dup
|
2022-07-07 11:08:37 -04:00
|
|
|
@errors = []
|
2020-08-20 20:10:44 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
# If ALL referenced issues are already assigned to the given epic it renders a conflict status,
|
|
|
|
# otherwise create issue links for the issues which
|
|
|
|
# are still not assigned and return success message.
|
|
|
|
if render_conflict_error?
|
2022-03-17 20:07:43 -04:00
|
|
|
return error(issuables_already_assigned_message, 409)
|
2020-08-20 20:10:44 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
if render_not_found_error?
|
|
|
|
return error(issuables_not_found_message, 404)
|
|
|
|
end
|
|
|
|
|
2022-03-22 08:07:28 -04:00
|
|
|
references = create_links
|
2020-08-20 20:10:44 -04:00
|
|
|
|
|
|
|
if @errors.present?
|
|
|
|
return error(@errors.join('. '), 422)
|
|
|
|
end
|
|
|
|
|
2020-09-10 17:08:28 -04:00
|
|
|
track_event
|
|
|
|
|
2022-03-22 08:07:28 -04:00
|
|
|
success(created_references: references)
|
2020-08-20 20:10:44 -04:00
|
|
|
end
|
|
|
|
|
2022-03-15 08:07:44 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
def relate_issuables(referenced_issuable)
|
|
|
|
link = link_class.find_or_initialize_by(source: issuable, target: referenced_issuable)
|
|
|
|
|
|
|
|
set_link_type(link)
|
|
|
|
|
|
|
|
if link.changed? && link.save
|
|
|
|
create_notes(referenced_issuable)
|
|
|
|
end
|
|
|
|
|
|
|
|
link
|
|
|
|
end
|
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
|
|
|
|
2020-08-20 20:10:44 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def render_conflict_error?
|
|
|
|
referenced_issuables.present? && (referenced_issuables - previous_related_issuables).empty?
|
|
|
|
end
|
|
|
|
|
|
|
|
def render_not_found_error?
|
|
|
|
linkable_issuables(referenced_issuables).empty?
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_links
|
|
|
|
objects = linkable_issuables(referenced_issuables)
|
|
|
|
link_issuables(objects)
|
|
|
|
end
|
|
|
|
|
|
|
|
def link_issuables(target_issuables)
|
2022-03-22 08:07:28 -04:00
|
|
|
target_issuables.map do |referenced_object|
|
2020-08-20 20:10:44 -04:00
|
|
|
link = relate_issuables(referenced_object)
|
|
|
|
|
2022-03-24 14:07:52 -04:00
|
|
|
if link.valid?
|
|
|
|
after_create_for(link)
|
|
|
|
else
|
2020-08-20 20:10:44 -04:00
|
|
|
@errors << _("%{ref} cannot be added: %{error}") % {
|
|
|
|
ref: referenced_object.to_reference,
|
|
|
|
error: link.errors.messages.values.flatten.to_sentence
|
|
|
|
}
|
|
|
|
end
|
2022-03-22 08:07:28 -04:00
|
|
|
|
|
|
|
link
|
2020-08-20 20:10:44 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def referenced_issuables
|
|
|
|
@referenced_issuables ||= begin
|
|
|
|
target_issuable = params[:target_issuable]
|
|
|
|
|
|
|
|
if params[:issuable_references].present?
|
|
|
|
extract_references
|
|
|
|
elsif target_issuable
|
|
|
|
[target_issuable]
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def extract_references
|
|
|
|
issuable_references = params[:issuable_references]
|
|
|
|
text = issuable_references.join(' ')
|
|
|
|
|
|
|
|
extractor = Gitlab::ReferenceExtractor.new(issuable.project, current_user)
|
|
|
|
extractor.analyze(text, extractor_context)
|
|
|
|
|
|
|
|
references(extractor)
|
|
|
|
end
|
|
|
|
|
|
|
|
def references(extractor)
|
|
|
|
extractor.issues
|
|
|
|
end
|
|
|
|
|
|
|
|
def extractor_context
|
|
|
|
{}
|
|
|
|
end
|
|
|
|
|
2022-03-17 20:07:43 -04:00
|
|
|
def issuables_already_assigned_message
|
2022-03-15 08:07:44 -04:00
|
|
|
_('%{issuable}(s) already assigned' % { issuable: target_issuable_type.capitalize })
|
|
|
|
end
|
|
|
|
|
|
|
|
def issuables_not_found_message
|
|
|
|
_('No matching %{issuable} found. Make sure that you are adding a valid %{issuable} URL.' % { issuable: target_issuable_type })
|
|
|
|
end
|
|
|
|
|
|
|
|
def target_issuable_type
|
|
|
|
:issue
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_notes(referenced_issuable)
|
|
|
|
SystemNoteService.relate_issuable(issuable, referenced_issuable, current_user)
|
|
|
|
SystemNoteService.relate_issuable(referenced_issuable, issuable, current_user)
|
|
|
|
end
|
|
|
|
|
2020-08-20 20:10:44 -04:00
|
|
|
def linkable_issuables(objects)
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
def previous_related_issuables
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
2022-03-15 08:07:44 -04:00
|
|
|
def link_class
|
2020-08-20 20:10:44 -04:00
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
2022-03-15 08:07:44 -04:00
|
|
|
def set_link_type(_link)
|
|
|
|
# no-op
|
2020-08-20 20:10:44 -04:00
|
|
|
end
|
2022-03-22 08:07:28 -04:00
|
|
|
|
2022-03-24 14:07:52 -04:00
|
|
|
# Override on child classes to perform
|
|
|
|
# actions when the service is executed.
|
2022-03-22 08:07:28 -04:00
|
|
|
def track_event
|
|
|
|
# no-op
|
|
|
|
end
|
2022-03-24 14:07:52 -04:00
|
|
|
|
|
|
|
# Override on child classes to
|
|
|
|
# perform actions for each object created.
|
|
|
|
def after_create_for(_link)
|
|
|
|
# no-op
|
|
|
|
end
|
2020-08-20 20:10:44 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
IssuableLinks::CreateService.prepend_mod_with('IssuableLinks::CreateService')
|