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
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
issue = result[:issue]
|
2022-05-07 20:08:51 +05:30
|
|
|
perform_after_create_tasks(issue)
|
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,
|
2022-04-04 11:22:00 +05:30
|
|
|
severity: alert.severity,
|
|
|
|
alert: alert
|
2020-07-28 23:09:34 +05:30
|
|
|
).execute
|
2020-05-24 23:13:21 +05:30
|
|
|
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
|
|
|
|
|
2022-05-07 20:08:51 +05:30
|
|
|
def perform_after_create_tasks(issue)
|
|
|
|
update_title_for(issue)
|
|
|
|
|
|
|
|
SystemNoteService.new_alert_issue(alert, issue, user)
|
|
|
|
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
|
2020-05-24 23:13:21 +05:30
|
|
|
end
|
|
|
|
end
|
2022-05-07 20:08:51 +05:30
|
|
|
|
|
|
|
AlertManagement::CreateAlertIssueService.prepend_mod
|