Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
c069134f
Commit
c069134f
authored
Feb 04, 2019
by
Peter Leitzen
Committed by
Sean McGivern
Feb 04, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor sending Prometheus alert mails
parent
71e6a91a
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
85 additions
and
9 deletions
+85
-9
ee/app/mailers/ee/emails/projects.rb
ee/app/mailers/ee/emails/projects.rb
+4
-9
ee/spec/mailers/ee/emails/projects_spec.rb
ee/spec/mailers/ee/emails/projects_spec.rb
+81
-0
No files found.
ee/app/mailers/ee/emails/projects.rb
View file @
c069134f
...
...
@@ -20,26 +20,21 @@ module EE
subject:
subject
(
'Mirror user changed'
))
end
# rubocop: disable CodeReuse/ActiveRecord
def
prometheus_alert_fired_email
(
project_id
,
user_id
,
alert_params
)
alert_metric_id
=
alert_params
[
"labels"
][
"gitlab_alert_id"
]
alert_metric_id
=
alert_params
.
dig
(
'labels'
,
'gitlab_alert_id'
)
@project
=
::
Project
.
find
_by
(
id:
project_id
)
return
unless
@project
@project
=
::
Project
.
find
(
project_id
)
user
=
::
User
.
find
(
user_id
)
@alert
=
@project
.
prometheus_alerts
.
f
ind_by
(
prometheus_metric:
alert_metric_id
)
@alert
=
@project
.
prometheus_alerts
.
f
or_metric
(
alert_metric_id
).
first
return
unless
@alert
@environment
=
@alert
.
environment
user
=
::
User
.
find_by
(
id:
user_id
)
return
unless
user
subject_text
=
"Alert:
#{
@environment
.
name
}
-
#{
@alert
.
title
}
#{
@alert
.
computed_operator
}
#{
@alert
.
threshold
}
for 5 minutes"
mail
(
to:
user
.
notification_email
,
subject:
subject
(
subject_text
))
end
# rubocop: enable CodeReuse/ActiveRecord
end
end
end
ee/spec/mailers/ee/emails/projects_spec.rb
0 → 100644
View file @
c069134f
# frozen_string_literal: true
require
'spec_helper'
require
'email_spec'
describe
EE
::
Emails
::
Projects
do
include
EmailSpec
::
Matchers
include_context
'gitlab email notification'
shared_examples
'no email'
do
it
'does not send mail'
do
expect
(
subject
.
message
).
to
be_a_kind_of
(
ActionMailer
::
Base
::
NullMail
)
end
end
set
(
:user
)
{
create
(
:user
)
}
describe
'#prometheus_alert_fired_email'
do
subject
do
Notify
.
prometheus_alert_fired_email
(
project
.
id
,
user
.
id
,
alert_params
)
end
context
'with an alert'
do
let
(
:alert_params
)
do
{
'labels'
=>
{
'gitlab_alert_id'
=>
alert
.
prometheus_metric_id
.
to_s
}
}
end
let
(
:environment
)
{
alert
.
environment
}
let
(
:metrics_url
)
do
metrics_project_environment_url
(
project
,
environment
)
end
let!
(
:alert
)
{
create
(
:prometheus_alert
,
project:
project
)
}
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
aggregate_failures
do
is_expected
.
to
have_subject
(
/Alert:/
)
is_expected
.
to
have_subject
(
/
#{
environment
.
name
}
/
)
title
=
"
#{
alert
.
title
}
#{
alert
.
computed_operator
}
#{
alert
.
threshold
}
"
is_expected
.
to
have_subject
(
/
#{
title
}
/
)
end
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
.
to
have_body_text
(
environment
.
name
)
is_expected
.
to
have_body_text
(
alert
.
full_query
)
is_expected
.
to
have_body_text
(
metrics_url
)
end
end
context
'without an alert'
do
let
(
:alert_params
)
{
{}
}
it_behaves_like
'no email'
end
context
'with an unknown alert'
do
let
(
:alert_params
)
do
{
'labels'
=>
{
'gitlab_alert_id'
=>
'unknown'
}
}
end
it_behaves_like
'no email'
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment