debian-mirror-gitlab/app/services/issuable_links/create_service.rb

180 lines
4.5 KiB
Ruby
Raw Normal View History

2020-11-24 15:15:51 +05:30
# frozen_string_literal: true
module IssuableLinks
class CreateService < BaseService
attr_reader :issuable, :current_user, :params
def initialize(issuable, user, params)
2021-04-29 21:17:54 +05:30
@issuable = issuable
@current_user = user
@params = params.dup
2022-08-13 15:12:31 +05:30
@errors = []
2020-11-24 15:15:51 +05:30
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-05-07 20:08:51 +05:30
return error(issuables_already_assigned_message, 409)
2020-11-24 15:15:51 +05:30
end
2023-07-09 08:55:56 +05:30
if render_no_permission_error?
return error(issuables_no_permission_error_message, 403)
end
2020-11-24 15:15:51 +05:30
if render_not_found_error?
return error(issuables_not_found_message, 404)
end
2022-06-21 17:19:12 +05:30
references = create_links
2020-11-24 15:15:51 +05:30
if @errors.present?
return error(@errors.join('. '), 422)
end
track_event
2022-06-21 17:19:12 +05:30
success(created_references: references)
2020-11-24 15:15:51 +05:30
end
2022-05-07 20:08:51 +05:30
# 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
2022-10-11 01:57:18 +05:30
create_notes(link)
2022-05-07 20:08:51 +05:30
end
link
end
2023-07-09 08:55:56 +05:30
2022-05-07 20:08:51 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2020-11-24 15:15:51 +05:30
private
def render_conflict_error?
referenced_issuables.present? && (referenced_issuables - previous_related_issuables).empty?
end
2023-07-09 08:55:56 +05:30
def render_no_permission_error?
readonly_issuables(referenced_issuables).present? && linkable_issuables(referenced_issuables).empty?
end
2020-11-24 15:15:51 +05:30
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-06-21 17:19:12 +05:30
target_issuables.map do |referenced_object|
2020-11-24 15:15:51 +05:30
link = relate_issuables(referenced_object)
2022-06-21 17:19:12 +05:30
if link.valid?
after_create_for(link)
else
2020-11-24 15:15:51 +05:30
@errors << _("%{ref} cannot be added: %{error}") % {
ref: referenced_object.to_reference,
error: link.errors.messages.values.flatten.to_sentence
}
end
2022-06-21 17:19:12 +05:30
link
2020-11-24 15:15:51 +05:30
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-05-07 20:08:51 +05:30
def issuables_already_assigned_message
_('%{issuable}(s) already assigned' % { issuable: target_issuable_type.capitalize })
end
2023-07-09 08:55:56 +05:30
def issuables_no_permission_error_message
_("Couldn't link %{issuable}. You must have at least the Reporter role in both projects." % { issuable: target_issuable_type })
end
2022-05-07 20:08:51 +05:30
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
2023-03-17 16:20:25 +05:30
'issue'
2022-05-07 20:08:51 +05:30
end
2022-10-11 01:57:18 +05:30
def create_notes(issuable_link)
SystemNoteService.relate_issuable(issuable_link.source, issuable_link.target, current_user)
SystemNoteService.relate_issuable(issuable_link.target, issuable_link.source, current_user)
2022-05-07 20:08:51 +05:30
end
2020-11-24 15:15:51 +05:30
def linkable_issuables(objects)
raise NotImplementedError
end
2023-07-09 08:55:56 +05:30
def readonly_issuables(_issuables)
[] # default to empty for non-issues
end
2020-11-24 15:15:51 +05:30
def previous_related_issuables
raise NotImplementedError
end
2022-05-07 20:08:51 +05:30
def link_class
2020-11-24 15:15:51 +05:30
raise NotImplementedError
end
2022-05-07 20:08:51 +05:30
def set_link_type(_link)
# no-op
2020-11-24 15:15:51 +05:30
end
2022-06-21 17:19:12 +05:30
# Override on child classes to perform
# actions when the service is executed.
def track_event
# no-op
end
# Override on child classes to
# perform actions for each object created.
def after_create_for(_link)
# no-op
end
2020-11-24 15:15:51 +05:30
end
end
2021-06-08 01:23:25 +05:30
IssuableLinks::CreateService.prepend_mod_with('IssuableLinks::CreateService')