Commit c3af42c3 authored by Sean Arnold's avatar Sean Arnold

Only return PrometheusAlert if singular found

parent af0b0094
...@@ -122,10 +122,13 @@ module Gitlab ...@@ -122,10 +122,13 @@ module Gitlab
def parse_gitlab_alert_from_payload def parse_gitlab_alert_from_payload
return unless metric_id || gitlab_prometheus_alert_id return unless metric_id || gitlab_prometheus_alert_id
Projects::Prometheus::AlertsFinder alerts_found = Projects::Prometheus::AlertsFinder
.new(project: project, metric: metric_id, id: gitlab_prometheus_alert_id) .new(project: project, metric: metric_id, id: gitlab_prometheus_alert_id)
.execute .execute
.first
return if alerts_found.empty? || alerts_found.size > 1
alerts_found.first
end end
def parse_title_from_payload def parse_title_from_payload
......
...@@ -79,11 +79,26 @@ describe Gitlab::Alerting::Alert do ...@@ -79,11 +79,26 @@ describe Gitlab::Alerting::Alert do
) )
end end
before do context 'alert id given in params' do
payload['labels'] = { 'gitlab_prometheus_alert_id' => second_gitlab_alert.id } before do
payload['labels'] = { 'gitlab_prometheus_alert_id' => second_gitlab_alert.id }
end
it { is_expected.to eq(second_gitlab_alert) }
end end
it { is_expected.to eq(second_gitlab_alert) } context 'metric id given in params' do
# This tests the case when two alerts are found, as metric id
# is not unique.
# Note the metric id was incorrectly named as 'gitlab_alert_id'
# in PrometheusAlert#to_param.
before do
payload['labels'] = { 'gitlab_alert_id' => gitlab_alert.prometheus_metric_id }
end
it { is_expected.to be_nil }
end
end end
end end
......
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