Commit 105fc705 authored by James Fargher's avatar James Fargher

Merge branch '210527-move-prometheus_alerts_fired-from-ee-to-ce' into 'master'

Move NotificationService#prometheus_alerts_fired to CE

Closes #210527

See merge request gitlab-org/gitlab!27353
parents c6a8926c 08534cd6
......@@ -55,6 +55,19 @@ module Emails
reply_to: @message.reply_to,
subject: @message.subject)
end
def prometheus_alert_fired_email(project_id, user_id, alert_payload)
@project = ::Project.find(project_id)
user = ::User.find(user_id)
@alert = ::Gitlab::Alerting::Alert
.new(project: @project, payload: alert_payload)
.present
return unless @alert.valid?
subject_text = "Alert: #{@alert.full_title}"
mail(to: user.notification_email_for(@project.group), subject: subject(subject_text))
end
end
end
......
......@@ -523,6 +523,14 @@ class NotificationService
end
end
def prometheus_alerts_fired(project, alerts)
return if project.emails_disabled?
owners_and_maintainers_without_invites(project).to_a.product(alerts).each do |recipient, alert|
mailer.prometheus_alert_fired_email(project.id, recipient.user.id, alert).deliver_later
end
end
protected
def new_resource_email(target, method)
......@@ -618,6 +626,16 @@ class NotificationService
private
def owners_and_maintainers_without_invites(project)
recipients = project.members.active_without_invites_and_requests.owners_and_maintainers
if recipients.empty? && project.group
recipients = project.group.members.active_without_invites_and_requests.owners_and_maintainers
end
recipients
end
def project_maintainers_recipients(target, action:)
NotificationRecipients::BuildService.build_project_maintainers_recipients(target, action: action)
end
......
......@@ -19,19 +19,6 @@ module EE
mail(to: user.notification_email_for(@project.group),
subject: subject('Mirror user changed'))
end
def prometheus_alert_fired_email(project_id, user_id, alert_payload)
@project = ::Project.find(project_id)
user = ::User.find(user_id)
@alert = ::Gitlab::Alerting::Alert
.new(project: @project, payload: alert_payload)
.present
return unless @alert.valid?
subject_text = "Alert: #{@alert.full_title}"
mail(to: user.notification_email_for(@project.group), subject: subject(subject_text))
end
end
end
end
......@@ -61,26 +61,8 @@ module EE
mailer.project_mirror_user_changed_email(new_mirror_user.id, deleted_user_name, project.id).deliver_later
end
def prometheus_alerts_fired(project, alerts)
return if project.emails_disabled?
owners_and_maintainers_without_invites(project).to_a.product(alerts).each do |recipient, alert|
mailer.prometheus_alert_fired_email(project.id, recipient.user.id, alert).deliver_later
end
end
private
def owners_and_maintainers_without_invites(project)
recipients = project.members.active_without_invites_and_requests.owners_and_maintainers
if recipients.empty? && project.group
recipients = project.group.members.active_without_invites_and_requests.owners_and_maintainers
end
recipients
end
def send_new_review_notification(review)
recipients = ::NotificationRecipients::BuildService.build_new_review_recipients(review)
......
......@@ -377,41 +377,6 @@ describe EE::NotificationService, :mailer do
end
end
describe '#prometheus_alerts_fired' do
let!(:project) { create(:project) }
let!(:prometheus_alert) { create(:prometheus_alert, project: project) }
let!(:master) { create(:user) }
let!(:developer) { create(:user) }
before do
project.add_master(master)
end
it 'sends the email to owners and masters' do
expect(Notify).to receive(:prometheus_alert_fired_email).with(project.id, master.id, prometheus_alert).and_call_original
expect(Notify).to receive(:prometheus_alert_fired_email).with(project.id, project.owner.id, prometheus_alert).and_call_original
expect(Notify).not_to receive(:prometheus_alert_fired_email).with(project.id, developer.id, prometheus_alert)
subject.prometheus_alerts_fired(prometheus_alert.project, [prometheus_alert])
end
it_behaves_like 'project emails are disabled' do
before do
allow_next_instance_of(::Gitlab::Alerting::Alert) do |instance|
allow(instance).to receive(:valid?).and_return(true)
end
end
let(:alert_params) { { 'labels' => { 'gitlab_alert_id' => 'unknown' } } }
let(:notification_target) { prometheus_alert.project }
let(:notification_trigger) { subject.prometheus_alerts_fired(prometheus_alert.project, [alert_params]) }
around do |example|
perform_enqueued_jobs { example.run }
end
end
end
describe 'epics' do
let_it_be(:group) { create(:group, :private) }
let_it_be(:epic) { create(:epic, group: group) }
......
......@@ -3,7 +3,7 @@
require 'spec_helper'
require 'email_spec'
describe EE::Emails::Projects do
describe Emails::Projects do
include EmailSpec::Matchers
include_context 'gitlab email notification'
......
......@@ -2783,6 +2783,41 @@ describe NotificationService, :mailer do
end
end
describe '#prometheus_alerts_fired' do
let!(:project) { create(:project) }
let!(:prometheus_alert) { create(:prometheus_alert, project: project) }
let!(:master) { create(:user) }
let!(:developer) { create(:user) }
before do
project.add_master(master)
end
it 'sends the email to owners and masters' do
expect(Notify).to receive(:prometheus_alert_fired_email).with(project.id, master.id, prometheus_alert).and_call_original
expect(Notify).to receive(:prometheus_alert_fired_email).with(project.id, project.owner.id, prometheus_alert).and_call_original
expect(Notify).not_to receive(:prometheus_alert_fired_email).with(project.id, developer.id, prometheus_alert)
subject.prometheus_alerts_fired(prometheus_alert.project, [prometheus_alert])
end
it_behaves_like 'project emails are disabled' do
before do
allow_next_instance_of(::Gitlab::Alerting::Alert) do |instance|
allow(instance).to receive(:valid?).and_return(true)
end
end
let(:alert_params) { { 'labels' => { 'gitlab_alert_id' => 'unknown' } } }
let(:notification_target) { prometheus_alert.project }
let(:notification_trigger) { subject.prometheus_alerts_fired(prometheus_alert.project, [alert_params]) }
around do |example|
perform_enqueued_jobs { example.run }
end
end
end
def build_team(project)
@u_watcher = create_global_setting_for(create(:user), :watch)
@u_participating = create_global_setting_for(create(:user), :participating)
......
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