2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
module Issues
|
|
|
|
class CreateService < Issues::BaseService
|
2017-08-17 22:00:37 +05:30
|
|
|
include ResolveDiscussions
|
2021-11-18 22:05:49 +05:30
|
|
|
prepend RateLimitedService
|
2022-11-25 23:54:43 +05:30
|
|
|
include ::Services::ReturnServiceResponses
|
2021-11-18 22:05:49 +05:30
|
|
|
|
|
|
|
rate_limit key: :issues_create,
|
2021-12-11 22:18:48 +05:30
|
|
|
opts: { scope: [:project, :current_user, :external_author] }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
# NOTE: For Issues::CreateService, we require the spam_params and do not default it to nil, because
|
|
|
|
# spam_checking is likely to be necessary. However, if there is not a request available in scope
|
|
|
|
# in the caller (for example, an issue created via email) and the required arguments to the
|
|
|
|
# SpamParams constructor are not otherwise available, spam_params: must be explicitly passed as nil.
|
2023-04-23 21:23:45 +05:30
|
|
|
def initialize(container:, spam_params:, current_user: nil, params: {}, build_service: nil)
|
2022-08-13 15:12:31 +05:30
|
|
|
@extra_params = params.delete(:extra_params) || {}
|
2023-05-27 22:25:52 +05:30
|
|
|
super(container: container, current_user: current_user, params: params)
|
2021-09-30 23:02:18 +05:30
|
|
|
@spam_params = spam_params
|
2023-05-27 22:25:52 +05:30
|
|
|
@build_service = build_service ||
|
|
|
|
BuildService.new(container: project, current_user: current_user, params: params)
|
2021-09-30 23:02:18 +05:30
|
|
|
end
|
2021-03-11 19:13:27 +05:30
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
def execute(skip_system_notes: false)
|
2022-11-25 23:54:43 +05:30
|
|
|
return error(_('Operation not allowed'), 403) unless @current_user.can?(authorization_action, @project)
|
|
|
|
|
2022-03-02 08:16:31 +05:30
|
|
|
@issue = @build_service.execute
|
2023-03-17 16:20:25 +05:30
|
|
|
# issue_type is set in BuildService, so we can delete it from params, in later phase
|
|
|
|
# it can be set also from quick actions - in that case work_item_id is synced later again
|
|
|
|
params.delete(:issue_type)
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
handle_move_between_ids(@issue)
|
|
|
|
|
2022-05-07 20:08:51 +05:30
|
|
|
@add_related_issue ||= params.delete(:add_related_issue)
|
2017-08-17 22:00:37 +05:30
|
|
|
filter_resolve_discussion_params
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
issue = create(@issue, skip_system_notes: skip_system_notes)
|
|
|
|
|
|
|
|
if issue.persisted?
|
|
|
|
success(issue: issue)
|
|
|
|
else
|
|
|
|
error(issue.errors.full_messages, 422, pass_back: { issue: issue })
|
|
|
|
end
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
def external_author
|
|
|
|
params[:external_author] # present when creating an issue using service desk (email: from)
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def before_create(issue)
|
2021-03-11 19:13:27 +05:30
|
|
|
Spam::SpamActionService.new(
|
|
|
|
spammable: issue,
|
2021-09-30 23:02:18 +05:30
|
|
|
spam_params: spam_params,
|
2021-03-11 19:13:27 +05:30
|
|
|
user: current_user,
|
|
|
|
action: :create
|
2021-09-30 23:02:18 +05:30
|
|
|
).execute
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
# current_user (defined in BaseService) is not available within run_after_commit block
|
2017-09-10 17:25:29 +05:30
|
|
|
user = current_user
|
|
|
|
issue.run_after_commit do
|
2022-08-27 11:52:29 +05:30
|
|
|
NewIssueWorker.perform_async(issue.id, user.id, issue.class.to_s)
|
2022-01-26 12:08:38 +05:30
|
|
|
Issues::PlacementWorker.perform_async(nil, issue.project_id)
|
2022-11-25 23:54:43 +05:30
|
|
|
Onboarding::IssueCreatedWorker.perform_async(issue.project.namespace_id)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
# Add new items to Issues::AfterCreateService if they can be performed in Sidekiq
|
2020-11-24 15:15:51 +05:30
|
|
|
def after_create(issue)
|
2016-09-13 17:45:13 +05:30
|
|
|
user_agent_detail_service.create
|
2022-05-07 20:08:51 +05:30
|
|
|
handle_add_related_issue(issue)
|
2020-11-24 15:15:51 +05:30
|
|
|
resolve_discussions_with_issue(issue)
|
2022-11-25 23:54:43 +05:30
|
|
|
handle_escalation_status_change(issue)
|
2022-08-13 15:12:31 +05:30
|
|
|
create_timeline_event(issue)
|
|
|
|
try_to_associate_contacts(issue)
|
2023-03-17 16:20:25 +05:30
|
|
|
change_additional_attributes(issue)
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
super
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
def handle_changes(issue, options)
|
|
|
|
super
|
|
|
|
old_associations = options.fetch(:old_associations, {})
|
|
|
|
old_assignees = old_associations.fetch(:assignees, [])
|
|
|
|
|
|
|
|
handle_assignee_changes(issue, old_assignees)
|
|
|
|
end
|
|
|
|
|
|
|
|
def handle_assignee_changes(issue, old_assignees)
|
|
|
|
return if issue.assignees == old_assignees
|
|
|
|
|
|
|
|
create_assignee_note(issue, old_assignees)
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def resolve_discussions_with_issue(issue)
|
|
|
|
return if discussions_to_resolve.empty?
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
Discussions::ResolveService.new(project, current_user,
|
2020-06-23 00:09:42 +05:30
|
|
|
one_or_more_discussions: discussions_to_resolve,
|
|
|
|
follow_up_issue: issue).execute
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
private
|
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
def handle_quick_actions(issue)
|
|
|
|
# Do not handle quick actions unless the work item is the default Issue.
|
|
|
|
# The available quick actions for a work item depend on its type and widgets.
|
|
|
|
return if @params[:work_item_type].present? && @params[:work_item_type] != WorkItems::Type.default_by_type(:issue)
|
|
|
|
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
def authorization_action
|
|
|
|
:create_issue
|
2022-01-26 12:08:38 +05:30
|
|
|
end
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
attr_reader :spam_params, :extra_params
|
|
|
|
|
2022-08-13 15:12:31 +05:30
|
|
|
def create_timeline_event(issue)
|
|
|
|
return unless issue.incident?
|
|
|
|
|
|
|
|
IncidentManagement::TimelineEvents::CreateService.create_incident(issue, current_user)
|
|
|
|
end
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
def user_agent_detail_service
|
2021-09-30 23:02:18 +05:30
|
|
|
UserAgentDetailService.new(spammable: @issue, spam_params: spam_params)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
2022-05-07 20:08:51 +05:30
|
|
|
|
|
|
|
def handle_add_related_issue(issue)
|
|
|
|
return unless @add_related_issue
|
|
|
|
|
|
|
|
IssueLinks::CreateService.new(issue, issue.author, { target_issuable: @add_related_issue }).execute
|
|
|
|
end
|
2022-07-23 23:45:48 +05:30
|
|
|
|
2022-08-13 15:12:31 +05:30
|
|
|
def try_to_associate_contacts(issue)
|
2022-07-23 23:45:48 +05:30
|
|
|
return unless issue.external_author
|
|
|
|
return unless current_user.can?(:set_issue_crm_contacts, issue)
|
|
|
|
|
2022-08-13 15:12:31 +05:30
|
|
|
contacts = [issue.external_author]
|
|
|
|
contacts.concat extra_params[:cc] unless extra_params[:cc].nil?
|
|
|
|
|
|
|
|
set_crm_contacts(issue, contacts)
|
2022-07-23 23:45:48 +05:30
|
|
|
end
|
2023-03-17 16:20:25 +05:30
|
|
|
|
|
|
|
override :change_additional_attributes
|
|
|
|
def change_additional_attributes(issue)
|
|
|
|
super
|
|
|
|
|
|
|
|
# issue_type can be still set through quick actions, in that case
|
|
|
|
# we have to make sure to re-sync work_item_type with it
|
|
|
|
issue.work_item_type_id = find_work_item_type_id(params[:issue_type]) if params[:issue_type]
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
2019-12-04 20:38:33 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
Issues::CreateService.prepend_mod
|