Commit fc018a62 authored by Sean McGivern's avatar Sean McGivern

Merge branch '9552-alert-notification-emails-are-not-sent' into 'master'

Resolve "Alert notification emails are not sent"

See merge request gitlab-org/gitlab-ee!9393
parents 476af7a9 f5f82401
...@@ -25,7 +25,8 @@ module Projects ...@@ -25,7 +25,8 @@ module Projects
def notify def notify
token = extract_alert_manager_token(request) token = extract_alert_manager_token(request)
notify = Projects::Prometheus::Alerts::NotifyService.new(project, current_user, params) notify = Projects::Prometheus::Alerts::NotifyService
.new(project, current_user, params.permit!)
if notify.execute(token) if notify.execute(token)
head :ok head :ok
......
...@@ -8,8 +8,7 @@ module Projects ...@@ -8,8 +8,7 @@ module Projects
return false unless valid_version? return false unless valid_version?
return false unless valid_alert_manager_token?(token) return false unless valid_alert_manager_token?(token)
notification_service.async.prometheus_alerts_fired(project, firings) if firings.any? send_alert_email(project, firings) if firings.any?
persist_events(project, current_user, params) persist_events(project, current_user, params)
true true
...@@ -83,6 +82,12 @@ module Projects ...@@ -83,6 +82,12 @@ module Projects
ActiveSupport::SecurityUtils.variable_size_secure_compare(expected, actual) ActiveSupport::SecurityUtils.variable_size_secure_compare(expected, actual)
end end
def send_alert_email(projects, firing_alerts)
notification_service
.async
.prometheus_alerts_fired(project, firings)
end
def persist_events(project, current_user, params) def persist_events(project, current_user, params)
CreateEventsService.new(project, current_user, params).execute CreateEventsService.new(project, current_user, params).execute
end end
......
---
title: Fix alert notification emails are not being sent
merge_request: 9393
author:
type: fixed
...@@ -86,16 +86,18 @@ describe Projects::Prometheus::AlertsController do ...@@ -86,16 +86,18 @@ describe Projects::Prometheus::AlertsController do
describe 'POST #notify' do describe 'POST #notify' do
let(:notify_service) { spy } let(:notify_service) { spy }
let(:payload) { {} }
before do before do
allow(Projects::Prometheus::Alerts::NotifyService).to receive(:new).and_return(notify_service) expect(Projects::Prometheus::Alerts::NotifyService)
.to receive(:new)
.and_return(notify_service)
.with(project, user, duck_type(:permitted?))
end end
it 'sends a notification for firing alerts only' do it 'renders ok if notification succeeds' do
expect(notify_service).to receive(:execute).and_return(true) expect(notify_service).to receive(:execute).and_return(true)
post :notify, params: project_params(payload), session: { as: :json } post :notify, params: project_params, session: { as: :json }
expect(response).to have_gitlab_http_status(200) expect(response).to have_gitlab_http_status(200)
end end
......
...@@ -14,22 +14,24 @@ describe Projects::Prometheus::Alerts::NotifyService do ...@@ -14,22 +14,24 @@ describe Projects::Prometheus::Alerts::NotifyService do
let(:notification_service) { spy } let(:notification_service) { spy }
let(:create_events_service) { spy } let(:create_events_service) { spy }
before do
allow(NotificationService).to receive(:new).and_return(notification_service)
allow(Projects::Prometheus::Alerts::CreateEventsService)
.to receive(:new).and_return(create_events_service)
end
it 'sends a notification for firing alerts only' do it 'sends a notification for firing alerts only' do
expect(NotificationService)
.to receive(:new)
.and_return(notification_service)
expect(notification_service) expect(notification_service)
.to receive_message_chain(:async, :prometheus_alerts_fired) .to receive_message_chain(:async, :prometheus_alerts_fired)
.with(project, [payload_alert_firing])
expect(subject).to eq(true) expect(subject).to eq(true)
end end
it 'persists events' do it 'persists events' do
expect(create_events_service).to receive(:execute) expect(Projects::Prometheus::Alerts::CreateEventsService)
.to receive(:new)
.and_return(create_events_service)
expect(create_events_service)
.to receive(:execute)
expect(subject).to eq(true) expect(subject).to eq(true)
end end
...@@ -50,8 +52,9 @@ describe Projects::Prometheus::Alerts::NotifyService do ...@@ -50,8 +52,9 @@ describe Projects::Prometheus::Alerts::NotifyService do
context 'with valid payload' do context 'with valid payload' do
let(:alert_firing) { create(:prometheus_alert, project: project) } let(:alert_firing) { create(:prometheus_alert, project: project) }
let(:alert_resolved) { create(:prometheus_alert, project: project) } let(:alert_resolved) { create(:prometheus_alert, project: project) }
let(:payload) { payload_for(firing: [alert_firing], resolved: [alert_resolved]) } let(:payload_raw) { payload_for(firing: [alert_firing], resolved: [alert_resolved]) }
let(:payload_alert_firing) { payload['alerts'].first } let(:payload) { ActionController::Parameters.new(payload_raw).permit! }
let(:payload_alert_firing) { payload_raw['alerts'].first }
let(:token) { 'token' } let(:token) { 'token' }
context 'with project specific cluster' do context 'with project specific cluster' do
......
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