Commit c0c34cda authored by Douwe Maan's avatar Douwe Maan

Merge branch '1123-mirroring-notifies-when-hard-failed' into 'master'

Resolve "Repository mirroring should notify when failed"

Closes #1123

See merge request gitlab-org/gitlab-ee!4699
parents b7fa60d3 3f335fad
...@@ -38,5 +38,13 @@ module Emails ...@@ -38,5 +38,13 @@ module Emails
reply_to: @message.reply_to, reply_to: @message.reply_to,
subject: @message.subject) subject: @message.subject)
end end
def mirror_was_hard_failed_email(project_id, user_id)
@project = Project.find(project_id)
user = User.find(user_id)
mail(to: user.notification_email,
subject: subject('Repository mirroring paused'))
end
end end
end end
...@@ -20,6 +20,12 @@ module EE ...@@ -20,6 +20,12 @@ module EE
end end
end end
after_transition started: :failed do |project, _|
if project.mirror? && project.mirror_hard_failed?
::NotificationService.new.mirror_was_hard_failed(project)
end
end
after_transition [:scheduled, :started] => [:finished, :failed] do |project, _| after_transition [:scheduled, :started] => [:finished, :failed] do |project, _|
::Gitlab::Mirror.decrement_capacity(project.id) if project.mirror? ::Gitlab::Mirror.decrement_capacity(project.id) if project.mirror?
end end
......
...@@ -20,7 +20,19 @@ module EE ...@@ -20,7 +20,19 @@ module EE
return if note.author == support_bot return if note.author == support_bot
return unless issue.subscribed?(support_bot, issue.project) return unless issue.subscribed?(support_bot, issue.project)
Notify.service_desk_new_note_email(issue.id, note.id).deliver_later mailer.service_desk_new_note_email(issue.id, note.id).deliver_later
end
def mirror_was_hard_failed(project)
recipients = project.members.owners_and_masters
unless recipients.present?
recipients = project.group.members.owners_and_masters
end
recipients.each do |recipient|
mailer.mirror_was_hard_failed_email(project.id, recipient.user.id).deliver_later
end
end end
end end
end end
%p
Repository mirroring on #{@project.full_path} has been paused due to too many failures. The last failure was:
%pre
= @project.import_error
%p
To resume mirroring update your #{link_to("repository mirroring settings", project_settings_repository_path(@project))}.
Repository mirroring on <%= @project.full_path %> has been paused due to too many failures. The last failure was:
<%= @project.import_error %>
To resume mirroring update your repository settings at <%= project_settings_repository_url(@project) %>.
...@@ -62,10 +62,9 @@ class RepositoryUpdateMirrorWorker ...@@ -62,10 +62,9 @@ class RepositoryUpdateMirrorWorker
end end
def fail_mirror(project, message) def fail_mirror(project, message)
error_message = "Mirror update for #{project.full_path} failed with the following message: #{message}" project.mark_import_as_failed(message)
project.mark_import_as_failed(error_message)
Rails.logger.error(error_message) Rails.logger.error("Mirror update for #{project.full_path} failed with the following message: #{message}")
Gitlab::Metrics.add_event(:mirrors_failed, path: project.full_path) Gitlab::Metrics.add_event(:mirrors_failed, path: project.full_path)
end end
......
---
title: Repository mirroring notifies when hard failed
merge_request: 4699
author:
type: added
...@@ -102,6 +102,17 @@ describe Project do ...@@ -102,6 +102,17 @@ describe Project do
end end
end end
describe 'hard failing a mirror' do
it 'sends a notification' do
project = create(:project, :mirror, :import_started)
project.mirror_data.update_attributes(retry_count: Gitlab::Mirror::MAX_RETRY)
expect_any_instance_of(EE::NotificationService).to receive(:mirror_was_hard_failed).with(project)
project.import_fail
end
end
describe '#push_rule' do describe '#push_rule' do
let(:project) { create(:project, push_rule: create(:push_rule)) } let(:project) { create(:project, push_rule: create(:push_rule)) }
......
...@@ -119,4 +119,70 @@ describe EE::NotificationService, :mailer do ...@@ -119,4 +119,70 @@ describe EE::NotificationService, :mailer do
end end
end end
end end
describe 'mirror hard failed' do
let(:user) { create(:user) }
context 'when user is owner' do
it 'sends email' do
project = create(:project, :mirror, :import_hard_failed)
expect(Notify).to receive(:mirror_was_hard_failed_email).with(project.id, project.owner.id).and_call_original
subject.mirror_was_hard_failed(project)
end
end
context 'when user is master' do
it 'sends email' do
project = create(:project, :mirror, :import_hard_failed)
project.add_master(user)
expect(Notify).to receive(:mirror_was_hard_failed_email).with(project.id, project.owner.id).and_call_original
expect(Notify).to receive(:mirror_was_hard_failed_email).with(project.id, user.id).and_call_original
subject.mirror_was_hard_failed(project)
end
end
context 'when user is not owner nor master' do
it 'does not send email' do
project = create(:project, :mirror, :import_hard_failed)
project.add_developer(user)
expect(Notify).not_to receive(:mirror_was_hard_failed_email).with(project.id, user.id).and_call_original
expect(Notify).to receive(:mirror_was_hard_failed_email).with(project.id, project.creator.id).and_call_original
subject.mirror_was_hard_failed(project)
end
context 'when user is group owner' do
it 'sends email' do
group = create(:group, :public) do |group|
group.add_owner(user)
end
project = create(:project, :mirror, :import_hard_failed, namespace: group)
expect(Notify).to receive(:mirror_was_hard_failed_email).with(project.id, user.id).and_call_original
subject.mirror_was_hard_failed(project)
end
end
context 'when user is group master' do
it 'sends email' do
group = create(:group, :public) do |group|
group.add_master(user)
end
project = create(:project, :mirror, :import_hard_failed, namespace: group)
expect(Notify).to receive(:mirror_was_hard_failed_email).with(project.id, user.id).and_call_original
subject.mirror_was_hard_failed(project)
end
end
end
end
end end
...@@ -1419,6 +1419,21 @@ describe Notify do ...@@ -1419,6 +1419,21 @@ describe Notify do
end end
end end
describe 'mirror was hard failed' do
let(:project) { create(:project, :mirror, :import_hard_failed) }
subject { described_class.mirror_was_hard_failed_email(project.id, user.id) }
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 the correct subject and body' do
is_expected.to have_subject("#{project.name} | Repository mirroring paused")
is_expected.to have_html_escaped_body_text(project.full_path)
end
end
describe 'admin notification' do describe 'admin notification' do
let(:example_site_path) { root_path } let(:example_site_path) { root_path }
let(:user) { create(:user) } let(:user) { create(:user) }
......
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