Commit 1f2e7ec1 authored by rpereira2's avatar rpereira2

Check setting before sending alert email

Check the incident_management_settings before sending alert emails to
developers.
parent 93689357
...@@ -95,6 +95,7 @@ class License < ActiveRecord::Base ...@@ -95,6 +95,7 @@ class License < ActiveRecord::Base
tracing tracing
insights insights
web_ide_terminal web_ide_terminal
incident_management
] ]
EEU_FEATURES.freeze EEU_FEATURES.freeze
......
...@@ -8,7 +8,7 @@ module Projects ...@@ -8,7 +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)
send_alert_email(project, firings) if firings.any? send_alert_email if send_email? && firings.any?
persist_events(project, params) persist_events(project, params)
true true
...@@ -16,6 +16,20 @@ module Projects ...@@ -16,6 +16,20 @@ module Projects
private private
def has_incident_management_license?
project.feature_available?(:incident_management)
end
def send_email?
return true unless has_incident_management_license?
setting = project.incident_management_setting
return true if setting.nil?
setting.send_email
end
def firings def firings
@firings ||= alerts_by_status('firing') @firings ||= alerts_by_status('firing')
end end
...@@ -89,7 +103,7 @@ module Projects ...@@ -89,7 +103,7 @@ 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) def send_alert_email
notification_service notification_service
.async .async
.prometheus_alerts_fired(project, firings) .prometheus_alerts_fired(project, firings)
......
...@@ -9,9 +9,8 @@ describe Projects::Prometheus::Alerts::NotifyService do ...@@ -9,9 +9,8 @@ describe Projects::Prometheus::Alerts::NotifyService do
let(:token_input) { 'token' } let(:token_input) { 'token' }
let(:subject) { service.execute(token_input) } let(:subject) { service.execute(token_input) }
shared_examples 'notifies alerts' do shared_examples 'sends notification email' do
let(:notification_service) { spy } let(:notification_service) { spy }
let(:create_events_service) { spy }
it 'sends a notification for firing alerts only' do it 'sends a notification for firing alerts only' do
expect(NotificationService) expect(NotificationService)
...@@ -23,6 +22,10 @@ describe Projects::Prometheus::Alerts::NotifyService do ...@@ -23,6 +22,10 @@ describe Projects::Prometheus::Alerts::NotifyService do
expect(subject).to eq(true) expect(subject).to eq(true)
end end
end
shared_examples 'persists events' do
let(:create_events_service) { spy }
it 'persists events' do it 'persists events' do
expect(Projects::Prometheus::Alerts::CreateEventsService) expect(Projects::Prometheus::Alerts::CreateEventsService)
...@@ -36,6 +39,11 @@ describe Projects::Prometheus::Alerts::NotifyService do ...@@ -36,6 +39,11 @@ describe Projects::Prometheus::Alerts::NotifyService do
end end
end end
shared_examples 'notifies alerts' do
it_behaves_like 'sends notification email'
it_behaves_like 'persists events'
end
shared_examples 'no notifications' do shared_examples 'no notifications' do
let(:notification_service) { spy } let(:notification_service) { spy }
let(:create_events_service) { spy } let(:create_events_service) { spy }
...@@ -171,6 +179,62 @@ describe Projects::Prometheus::Alerts::NotifyService do ...@@ -171,6 +179,62 @@ describe Projects::Prometheus::Alerts::NotifyService do
end end
end end
end end
context 'no incident_management license' do
before do
create(:prometheus_service, project: project)
create(:project_alerting_setting,
project: project,
token: token)
create(:project_incident_management_setting, send_email: false, project: project)
stub_licensed_features(incident_management: false)
end
include_examples 'notifies alerts'
end
context 'with incident_management license' do
before do
create(:prometheus_service, project: project)
create(:project_alerting_setting,
project: project,
token: token)
stub_licensed_features(incident_management: true)
end
context 'when incident_management_setting does not exist' do
include_examples 'notifies alerts'
end
context 'when incident_management_setting.send_email is true' do
before do
create(:project_incident_management_setting, send_email: true, project: project)
end
include_examples 'notifies alerts'
end
context 'incident_management_setting.send_email is false' do
before do
create(:project_incident_management_setting, send_email: false, project: project)
end
it_behaves_like 'persists events'
it 'does not send notification' do
expect(project.feature_available?(:incident_management)).to eq(true)
expect(project).to receive(:incident_management_setting).and_call_original
expect(NotificationService).not_to receive(:new)
expect(subject).to eq(true)
end
end
end
end end
context 'with invalid payload' do context 'with invalid payload' 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