Commit f7fda715 authored by Sarah Yasonik's avatar Sarah Yasonik Committed by Dmytro Zaporozhets (DZ)

Process prometheus-formatted alerts via HTTP integrations

parent 77e068e2
......@@ -17,10 +17,10 @@ module Projects
SUPPORTED_VERSION = '4'
def execute(token, _integration = nil)
def execute(token, integration = nil)
return bad_request unless valid_payload_size?
return unprocessable_entity unless self.class.processable?(params)
return unauthorized unless valid_alert_manager_token?(token)
return unauthorized unless valid_alert_manager_token?(token, integration)
process_prometheus_alerts
......@@ -53,9 +53,9 @@ module Projects
params['alerts']
end
def valid_alert_manager_token?(token)
def valid_alert_manager_token?(token, integration)
valid_for_manual?(token) ||
valid_for_alerts_endpoint?(token) ||
valid_for_alerts_endpoint?(token, integration) ||
valid_for_managed?(token)
end
......@@ -70,11 +70,10 @@ module Projects
end
end
def valid_for_alerts_endpoint?(token)
return false unless project.alerts_service_activated?
def valid_for_alerts_endpoint?(token, integration)
return false unless integration&.active?
# Here we are enforcing the existence of the token
compare_token(token, project.alerts_service.token)
compare_token(token, integration.token)
end
def valid_for_managed?(token)
......
---
title: Handle prometheus-formatted alert notifications through HTTP integrations
merge_request: 49268
author:
type: fixed
......@@ -11,6 +11,10 @@ FactoryBot.define do
active { false }
end
trait :active do
active { true }
end
trait :legacy do
endpoint_identifier { 'legacy' }
end
......
......@@ -138,10 +138,10 @@ RSpec.describe Projects::Prometheus::Alerts::NotifyService do
end
end
context 'with generic alerts integration' do
context 'with HTTP integration' do
using RSpec::Parameterized::TableSyntax
where(:alerts_service, :token, :result) do
where(:active, :token, :result) do
:active | :valid | :success
:active | :invalid | :failure
:active | nil | :failure
......@@ -150,15 +150,12 @@ RSpec.describe Projects::Prometheus::Alerts::NotifyService do
end
with_them do
let(:valid) { project.alerts_service.token }
let(:valid) { integration.token }
let(:invalid) { 'invalid token' }
let(:token_input) { public_send(token) if token }
let(:integration) { create(:alert_management_http_integration, active, project: project) if active }
before do
if alerts_service
create(:alerts_service, alerts_service, project: project)
end
end
let(:subject) { service.execute(token_input, integration) }
case result = params[:result]
when :success
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment