2020-03-13 15:44:24 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module IncidentManagement
|
2020-04-08 14:13:33 +05:30
|
|
|
class ProcessAlertWorker # rubocop:disable Scalability/IdempotentWorker
|
2020-03-13 15:44:24 +05:30
|
|
|
include ApplicationWorker
|
|
|
|
|
|
|
|
queue_namespace :incident_management
|
|
|
|
feature_category :incident_management
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
# `project_id` and `alert_payload` are deprecated and can be removed
|
|
|
|
# starting from 14.0 release
|
|
|
|
# https://gitlab.com/gitlab-org/gitlab/-/issues/224500
|
|
|
|
def perform(_project_id = nil, _alert_payload = nil, alert_id = nil)
|
|
|
|
return unless alert_id
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
alert = find_alert(alert_id)
|
|
|
|
return unless alert
|
|
|
|
|
|
|
|
new_issue = create_issue_for(alert)
|
|
|
|
return unless new_issue&.persisted?
|
2020-05-24 23:13:21 +05:30
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
link_issue_with_alert(alert, new_issue.id)
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
def find_alert(alert_id)
|
|
|
|
AlertManagement::Alert.find_by_id(alert_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def parsed_payload(alert)
|
|
|
|
Gitlab::Alerting::NotificationPayloadParser.call(alert.payload.to_h, alert.project)
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
def create_issue_for(alert)
|
2020-03-13 15:44:24 +05:30
|
|
|
IncidentManagement::CreateIssueService
|
2020-07-28 23:09:34 +05:30
|
|
|
.new(alert.project, parsed_payload(alert))
|
2020-03-13 15:44:24 +05:30
|
|
|
.execute
|
2020-06-11 16:45:22 +05:30
|
|
|
.dig(:issue)
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
2020-05-24 23:13:21 +05:30
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
def link_issue_with_alert(alert, issue_id)
|
2020-05-24 23:13:21 +05:30
|
|
|
return if alert.update(issue_id: issue_id)
|
|
|
|
|
|
|
|
Gitlab::AppLogger.warn(
|
|
|
|
message: 'Cannot link an Issue with Alert',
|
|
|
|
issue_id: issue_id,
|
2020-07-28 23:09:34 +05:30
|
|
|
alert_id: alert.id,
|
2020-05-24 23:13:21 +05:30
|
|
|
alert_errors: alert.errors.messages
|
|
|
|
)
|
|
|
|
end
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
end
|