Commit 0f8c7e9e authored by Peter Leitzen's avatar Peter Leitzen Committed by Douglas Barbosa Alexandre

Send notification mails for external alerts

parent c51917fe
......@@ -4,7 +4,7 @@ module Projects
module Prometheus
class AlertPresenter < Gitlab::View::Presenter::Delegated
def email_subject
[environment_name, query_title].compact.join(' ')
[environment_name, alert_title].compact.join(' ')
end
def project_full_path
......@@ -29,6 +29,10 @@ module Projects
private
def alert_title
query_title || title
end
def query_title
return unless gitlab_alert
......
%p
An alert has been triggered in #{@alert.project_full_path}.
- if description = @alert.description
%p
Description: #{description}
- if env_name = @alert.environment_name
%p
Environment: #{env_name}
......
An alert has been triggered in <%= @alert.project_full_path %>.
<% if description = @alert.description %>
Description: <%= description %>
<% end %>
<% if env_name = @alert.environment_name %>
Environment: <%= env_name %>
<% end %>
......
---
title: Enable mails for external alerts
merge_request: 9457
author:
type: added
......@@ -15,12 +15,24 @@ module Gitlab
end
end
def title
strong_memoize(:title) do
parse_title_from_payload
end
end
def description
strong_memoize(:description) do
parse_description_from_payload
end
end
def environment
gitlab_alert&.environment
end
def valid?
project && gitlab_alert
project && title
end
def present
......@@ -35,6 +47,16 @@ module Gitlab
project.prometheus_alerts.for_metric(metric_id).first
end
def parse_title_from_payload
gitlab_alert&.title ||
payload&.dig('annotations', 'title') ||
payload&.dig('annotations', 'summary')
end
def parse_description_from_payload
payload&.dig('annotations', 'description')
end
end
end
end
......@@ -28,6 +28,12 @@ describe Gitlab::Alerting::Alert do
expect(alert.environment).to eq(gitlab_alert.environment)
end
it 'prefers gitlab_alert\'s title over annotated title' do
payload['annontations'] = { 'title' => 'other title' }
expect(alert.title).to eq(gitlab_alert.title)
end
it 'is valid' do
expect(alert).to be_valid
end
......
......@@ -46,7 +46,7 @@ describe EE::Emails::Projects do
it_behaves_like 'a user cannot unsubscribe through footer link'
it 'has expected subject' do
is_expected.to have_subject(/Alert: #{environment.name} #{title}/)
is_expected.to have_subject("#{project.name} | Alert: #{environment.name} #{title} for 5 minutes")
end
it 'has expected content' do
......@@ -77,5 +77,51 @@ describe EE::Emails::Projects do
it_behaves_like 'no email'
end
context 'with an external alert' do
let(:title) { 'alert title' }
let(:metrics_url) do
metrics_project_environments_url(project)
end
let(:alert_params) do
{
'annotations' => {
'title' => title
}
}
end
it_behaves_like 'an email sent from GitLab'
it_behaves_like 'it should not have Gmail Actions links'
it_behaves_like 'a user cannot unsubscribe through footer link'
it 'has expected subject' do
is_expected.to have_subject("#{project.name} | Alert: #{title}")
end
it 'has expected content' do
is_expected.to have_body_text('An alert has been triggered')
is_expected.to have_body_text(project.full_path)
is_expected.not_to have_body_text('Description:')
is_expected.not_to have_body_text('Environment:')
is_expected.not_to have_body_text('Metric:')
is_expected.to have_body_text(metrics_url)
end
context 'with annotated description' do
let(:description) { 'description' }
before do
alert_params['annotations']['description'] = description
end
it 'shows the description' do
is_expected.to have_body_text('Description:')
is_expected.to have_body_text(description)
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