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

53 lines
1.1 KiB
Ruby
Raw Normal View History

2020-03-13 15:44:24 +05:30
# frozen_string_literal: true
module Projects
module Alerting
2021-02-22 17:27:13 +05:30
class NotifyService
2021-03-11 19:13:27 +05:30
extend ::Gitlab::Utils::Override
include ::AlertManagement::AlertProcessing
2021-12-11 22:18:48 +05:30
include ::AlertManagement::Responses
2020-03-13 15:44:24 +05:30
2021-02-22 17:27:13 +05:30
def initialize(project, payload)
@project = project
@payload = payload
end
2021-01-29 00:20:46 +05:30
def execute(token, integration = nil)
@integration = integration
2021-01-03 14:25:43 +05:30
return bad_request unless valid_payload_size?
2021-01-29 00:20:46 +05:30
return forbidden unless active_integration?
2020-03-13 15:44:24 +05:30
return unauthorized unless valid_token?(token)
2021-01-03 14:25:43 +05:30
process_alert
2020-05-24 23:13:21 +05:30
return bad_request unless alert.persisted?
2021-03-11 19:13:27 +05:30
complete_post_processing_tasks
2020-03-13 15:44:24 +05:30
2021-12-11 22:18:48 +05:30
success(alert)
2020-03-13 15:44:24 +05:30
end
private
2021-02-22 17:27:13 +05:30
attr_reader :project, :payload, :integration
2020-03-13 15:44:24 +05:30
2021-03-11 19:13:27 +05:30
def valid_payload_size?
2022-08-27 11:52:29 +05:30
Gitlab::Utils::DeepSize.new(payload.to_h).valid?
2020-04-08 14:13:33 +05:30
end
2021-03-11 19:13:27 +05:30
override :alert_source
def alert_source
2021-04-29 21:17:54 +05:30
super || integration&.name || 'Generic Alert Endpoint'
2021-01-29 00:20:46 +05:30
end
def active_integration?
integration&.active?
end
2020-03-13 15:44:24 +05:30
def valid_token?(token)
2021-01-29 00:20:46 +05:30
token == integration.token
2020-03-13 15:44:24 +05:30
end
end
end
end