2020-05-24 23:13:21 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module AlertManagement
|
|
|
|
class CreateAlertIssueService
|
2020-07-28 23:09:34 +05:30
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
DEFAULT_ALERT_TITLE = ::Gitlab::AlertManagement::Payload::Generic::DEFAULT_TITLE
|
|
|
|
DEFAULT_INCIDENT_TITLE = 'New: Incident'
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
# @param alert [AlertManagement::Alert]
|
|
|
|
# @param user [User]
|
|
|
|
def initialize(alert, user)
|
|
|
|
@alert = alert
|
|
|
|
@user = user
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
return error_no_permissions unless allowed?
|
|
|
|
return error_issue_already_exists if alert.issue
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
result = create_incident
|
|
|
|
return result unless result.success?
|
2020-07-28 23:09:34 +05:30
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
issue = result.payload[:issue]
|
2020-07-28 23:09:34 +05:30
|
|
|
return error(object_errors(alert), issue) unless associate_alert_with_issue(issue)
|
2020-05-24 23:13:21 +05:30
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
update_title_for(issue)
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
SystemNoteService.new_alert_issue(alert, issue, user)
|
2020-05-24 23:13:21 +05:30
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
result
|
2020-05-24 23:13:21 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
attr_reader :alert, :user
|
2020-05-24 23:13:21 +05:30
|
|
|
|
|
|
|
delegate :project, to: :alert
|
|
|
|
|
|
|
|
def allowed?
|
2020-06-23 00:09:42 +05:30
|
|
|
user.can?(:create_issue, project)
|
2020-05-24 23:13:21 +05:30
|
|
|
end
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
def create_incident
|
|
|
|
::IncidentManagement::Incidents::CreateService.new(
|
2020-07-28 23:09:34 +05:30
|
|
|
project,
|
|
|
|
user,
|
|
|
|
title: alert_presenter.title,
|
2020-11-24 15:15:51 +05:30
|
|
|
description: alert_presenter.issue_description,
|
|
|
|
severity: alert.severity
|
2020-07-28 23:09:34 +05:30
|
|
|
).execute
|
2020-05-24 23:13:21 +05:30
|
|
|
end
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
def associate_alert_with_issue(issue)
|
2020-05-24 23:13:21 +05:30
|
|
|
alert.update(issue_id: issue.id)
|
|
|
|
end
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
def update_title_for(issue)
|
|
|
|
return unless issue.title == DEFAULT_ALERT_TITLE
|
|
|
|
|
|
|
|
issue.update!(title: "#{DEFAULT_INCIDENT_TITLE} #{issue.iid}")
|
|
|
|
end
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
def error(message, issue = nil)
|
2020-05-24 23:13:21 +05:30
|
|
|
ServiceResponse.error(payload: { issue: issue }, message: message)
|
|
|
|
end
|
|
|
|
|
|
|
|
def error_issue_already_exists
|
|
|
|
error(_('An issue already exists'))
|
|
|
|
end
|
|
|
|
|
|
|
|
def error_no_permissions
|
|
|
|
error(_('You have no permissions'))
|
|
|
|
end
|
2020-07-28 23:09:34 +05:30
|
|
|
|
|
|
|
def alert_presenter
|
|
|
|
strong_memoize(:alert_presenter) do
|
|
|
|
alert.present
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def object_errors(object)
|
|
|
|
object.errors.full_messages.to_sentence
|
|
|
|
end
|
2020-05-24 23:13:21 +05:30
|
|
|
end
|
|
|
|
end
|