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

64 lines
1.5 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
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
ServiceResponse.success
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?
Gitlab::Utils::DeepSize.new(payload).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-01-29 00:20:46 +05:30
alert.monitoring_tool || integration&.name || 'Generic Alert Endpoint'
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
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