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-05-24 23:13:21 +05:30
|
|
|
def perform(project_id, alert_payload, am_alert_id = nil)
|
2020-03-13 15:44:24 +05:30
|
|
|
project = find_project(project_id)
|
|
|
|
return unless project
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
new_issue = create_issue(project, alert_payload)
|
|
|
|
return unless am_alert_id && new_issue.persisted?
|
|
|
|
|
|
|
|
link_issue_with_alert(am_alert_id, new_issue.id)
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def find_project(project_id)
|
|
|
|
Project.find_by_id(project_id)
|
|
|
|
end
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
def create_issue(project, alert_payload)
|
2020-03-13 15:44:24 +05:30
|
|
|
IncidentManagement::CreateIssueService
|
2020-05-24 23:13:21 +05:30
|
|
|
.new(project, alert_payload)
|
2020-03-13 15:44:24 +05:30
|
|
|
.execute
|
|
|
|
end
|
2020-05-24 23:13:21 +05:30
|
|
|
|
|
|
|
def link_issue_with_alert(alert_id, issue_id)
|
|
|
|
alert = AlertManagement::Alert.find_by_id(alert_id)
|
|
|
|
return unless alert
|
|
|
|
|
|
|
|
return if alert.update(issue_id: issue_id)
|
|
|
|
|
|
|
|
Gitlab::AppLogger.warn(
|
|
|
|
message: 'Cannot link an Issue with Alert',
|
|
|
|
issue_id: issue_id,
|
|
|
|
alert_id: alert_id,
|
|
|
|
alert_errors: alert.errors.messages
|
|
|
|
)
|
|
|
|
end
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
end
|