debian-mirror-gitlab/app/services/projects/alerting/notify_service.rb

100 lines
2.5 KiB
Ruby
Raw Normal View History

2020-03-13 15:44:24 +05:30
# frozen_string_literal: true
module Projects
module Alerting
class NotifyService < BaseService
include Gitlab::Utils::StrongMemoize
2020-04-08 14:13:33 +05:30
include IncidentManagement::Settings
2020-03-13 15:44:24 +05:30
def execute(token)
return forbidden unless alerts_service_activated?
return unauthorized unless valid_token?(token)
2020-06-23 00:09:42 +05:30
alert = process_alert
2020-05-24 23:13:21 +05:30
return bad_request unless alert.persisted?
process_incident_issues(alert) if process_issues?
2020-04-08 14:13:33 +05:30
send_alert_email if send_email?
2020-03-13 15:44:24 +05:30
ServiceResponse.success
rescue Gitlab::Alerting::NotificationPayloadParser::BadPayloadError
bad_request
end
private
delegate :alerts_service, :alerts_service_activated?, to: :project
2020-05-24 23:13:21 +05:30
def am_alert_params
2020-06-23 00:09:42 +05:30
strong_memoize(:am_alert_params) do
Gitlab::AlertManagement::AlertParams.from_generic_alert(project: project, payload: params.to_h)
end
end
def process_alert
existing_alert = find_alert_by_fingerprint(am_alert_params[:fingerprint])
if existing_alert
process_existing_alert(existing_alert)
else
create_alert
end
end
def process_existing_alert(alert)
alert.register_new_event!
2020-05-24 23:13:21 +05:30
end
def create_alert
2020-06-23 00:09:42 +05:30
alert = AlertManagement::Alert.create(am_alert_params)
alert.execute_services if alert.persisted?
alert
end
def find_alert_by_fingerprint(fingerprint)
return unless fingerprint
AlertManagement::Alert.for_fingerprint(project, fingerprint).first
2020-05-24 23:13:21 +05:30
end
2020-04-08 14:13:33 +05:30
def send_email?
incident_management_setting.send_email?
end
2020-05-24 23:13:21 +05:30
def process_incident_issues(alert)
2020-06-23 00:09:42 +05:30
return if alert.issue
2020-03-13 15:44:24 +05:30
IncidentManagement::ProcessAlertWorker
2020-05-24 23:13:21 +05:30
.perform_async(project.id, parsed_payload, alert.id)
2020-03-13 15:44:24 +05:30
end
2020-04-08 14:13:33 +05:30
def send_alert_email
notification_service
.async
.prometheus_alerts_fired(project, [parsed_payload])
end
2020-03-13 15:44:24 +05:30
def parsed_payload
Gitlab::Alerting::NotificationPayloadParser.call(params.to_h)
end
def valid_token?(token)
token == alerts_service.token
end
def bad_request
2020-04-22 19:07:51 +05:30
ServiceResponse.error(message: 'Bad Request', http_status: :bad_request)
2020-03-13 15:44:24 +05:30
end
def unauthorized
2020-04-22 19:07:51 +05:30
ServiceResponse.error(message: 'Unauthorized', http_status: :unauthorized)
2020-03-13 15:44:24 +05:30
end
def forbidden
2020-04-22 19:07:51 +05:30
ServiceResponse.error(message: 'Forbidden', http_status: :forbidden)
2020-03-13 15:44:24 +05:30
end
end
end
end