2022-07-07 15:08:37 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module WorkItems
|
|
|
|
module ParentLinks
|
|
|
|
class CreateService < IssuableLinks::CreateService
|
|
|
|
private
|
|
|
|
|
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
def relate_issuables(work_item)
|
|
|
|
link = WorkItems::ParentLink.find_or_initialize_by(work_item: work_item)
|
|
|
|
link.work_item_parent = issuable
|
|
|
|
|
|
|
|
if link.changed? && link.save
|
|
|
|
create_notes(work_item)
|
|
|
|
end
|
|
|
|
|
|
|
|
link
|
|
|
|
end
|
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
|
|
|
|
|
|
|
def linkable_issuables(work_items)
|
|
|
|
@linkable_issuables ||= begin
|
2022-07-15 15:15:01 +00:00
|
|
|
return [] unless can?(current_user, :admin_parent_link, issuable)
|
2022-07-07 15:08:37 +00:00
|
|
|
|
|
|
|
work_items.select do |work_item|
|
|
|
|
linkable?(work_item)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def linkable?(work_item)
|
2022-07-15 15:15:01 +00:00
|
|
|
can?(current_user, :admin_parent_link, work_item) &&
|
2022-07-07 15:08:37 +00:00
|
|
|
!previous_related_issuables.include?(work_item)
|
|
|
|
end
|
|
|
|
|
|
|
|
def previous_related_issuables
|
|
|
|
@related_issues ||= issuable.work_item_children.to_a
|
|
|
|
end
|
|
|
|
|
|
|
|
def extract_references
|
2022-07-19 09:08:45 +00:00
|
|
|
params[:issuable_references]
|
2022-07-07 15:08:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_notes(work_item)
|
2022-07-20 15:08:38 +00:00
|
|
|
SystemNoteService.relate_work_item(issuable, work_item, current_user)
|
2022-07-07 15:08:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def target_issuable_type
|
|
|
|
issuable.issue_type == 'issue' ? 'task' : issuable.issue_type
|
|
|
|
end
|
|
|
|
|
|
|
|
def issuables_not_found_message
|
|
|
|
_('No matching %{issuable} found. Make sure that you are adding a valid %{issuable} ID.' %
|
|
|
|
{ issuable: target_issuable_type })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|